Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retries when removing device mapper target #1200

Merged
merged 7 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions internal/guest/storage/devicemapper/devicemapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"os"
"path"
"syscall"
"time"
"unsafe"

"golang.org/x/sys/unix"
Expand All @@ -19,6 +21,11 @@ const (
CreateReadOnly CreateFlags = 1 << iota
)

var (
removeDeviceWrapper = removeDevice
openMapperWrapper = openMapper
)

const (
_IOC_WRITE = 1
_IOC_READ = 2
Expand Down Expand Up @@ -223,7 +230,7 @@ func makeTableIoctl(name string, targets []Target) *dmIoctl {
// CreateDevice creates a device-mapper device with the given target spec. It returns
// the path of the new device node.
func CreateDevice(name string, flags CreateFlags, targets []Target) (_ string, err error) {
f, err := openMapper()
f, err := openMapperWrapper()
if err != nil {
return "", err
}
Expand All @@ -238,7 +245,7 @@ func CreateDevice(name string, flags CreateFlags, targets []Target) (_ string, e
}
defer func() {
if err != nil {
removeDevice(f, name)
removeDeviceWrapper(f, name)
}
}()

Expand Down Expand Up @@ -269,14 +276,27 @@ func CreateDevice(name string, flags CreateFlags, targets []Target) (_ string, e
}

// RemoveDevice removes a device-mapper device and its associated device node.
func RemoveDevice(name string) error {
f, err := openMapper()
if err != nil {
return err
func RemoveDevice(name string) (err error) {
rm := func() error {
f, err := openMapperWrapper()
if err != nil {
return err
}
defer f.Close()
os.Remove(path.Join("/dev/mapper", name))
dcantah marked this conversation as resolved.
Show resolved Hide resolved
return removeDeviceWrapper(f, name)
}
defer f.Close()
os.Remove(path.Join("/dev/mapper", name))
return removeDevice(f, name)

// This is workaround for "device or resource busy" error, which occasionally happens after the device mapper
// target has been unmounted.
for i := 0; i < 10; i++ {
if err = rm(); err != nil && err == syscall.EBUSY {
anmaxvl marked this conversation as resolved.
Show resolved Hide resolved
time.Sleep(10 * time.Millisecond)
continue
}
break
}
return
}

func removeDevice(f *os.File, name string) error {
Expand Down
49 changes: 49 additions & 0 deletions internal/guest/storage/devicemapper/devicemapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package devicemapper

import (
"flag"
"io/ioutil"
"os"
"syscall"
"testing"
"unsafe"

Expand All @@ -15,6 +17,11 @@ var (
integration = flag.Bool("integration", false, "run integration tests")
)

func clearTestDependencies() {
removeDeviceWrapper = removeDevice
openMapperWrapper = openMapper
}

func TestMain(m *testing.M) {
flag.Parse()
m.Run()
Expand Down Expand Up @@ -75,6 +82,8 @@ func createDevice(name string, flags CreateFlags, targets []Target) (*device, er
}

func TestCreateError(t *testing.T) {
clearTestDependencies()

if !*integration {
t.Skip()
}
Expand All @@ -94,6 +103,8 @@ func TestCreateError(t *testing.T) {
}

func TestReadOnlyError(t *testing.T) {
clearTestDependencies()

if !*integration {
t.Skip()
}
Expand All @@ -113,6 +124,8 @@ func TestReadOnlyError(t *testing.T) {
}

func TestLinearError(t *testing.T) {
clearTestDependencies()

if !*integration {
t.Skip()
}
Expand Down Expand Up @@ -140,3 +153,39 @@ func TestLinearError(t *testing.T) {
t.Fatal(err)
}
}

func TestRemoveDeviceRetries(t *testing.T) {
clearTestDependencies()

rmDeviceCalled := false
retryCalled := false
// Overrides openMapper to return temp file handle
openMapperWrapper = func() (*os.File, error) {
tmpFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
return tmpFile, nil
}
removeDeviceWrapper = func(_ *os.File, name string) error {
if !rmDeviceCalled {
rmDeviceCalled = true
return syscall.EBUSY
}
if !retryCalled {
retryCalled = true
return nil
}
return nil
}

if err := RemoveDevice("test"); err != nil {
t.Fatalf("expected no error, got: %s", err)
}
if !rmDeviceCalled {
t.Fatalf("expected removeDevice to be called at least once")
}
if !retryCalled {
t.Fatalf("expected removeDevice to be retried after initial failure")
}
}
6 changes: 4 additions & 2 deletions internal/guest/storage/pmem/pmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ func Unmount(ctx context.Context, devNumber uint32, target string, mappingInfo *
if verityInfo != nil {
dmVerityName := fmt.Sprintf(verityDeviceFmt, devNumber, verityInfo.RootDigest)
if err := dm.RemoveDevice(dmVerityName); err != nil {
return errors.Wrapf(err, "failed to remove dm verity target: %s", dmVerityName)
// The target is already unmounted at this point, ignore potential errors
log.G(ctx).WithError(err).Debugf("failed to remove dm verity target: %s", dmVerityName)
}
}

if mappingInfo != nil {
dmLinearName := fmt.Sprintf(linearDeviceFmt, devNumber, mappingInfo.DeviceOffsetInBytes, mappingInfo.DeviceSizeInBytes)
if err := dm.RemoveDevice(dmLinearName); err != nil {
return errors.Wrapf(err, "failed to remove dm linear target: %s", dmLinearName)
// The target is already unmounted at this point, ignore potential errors
log.G(ctx).WithError(err).Debugf("failed to remove dm linear target: %s", dmLinearName)
}
}

Expand Down