Discard blocks when removing a thin device

dmsetup does not discard blocks when removing a thin device. The unused blocks
are reused by the thin-pool, but will remain allocated in the underlying
device indefinitely. For loop device backed thin-pools, this results in
"lost" disk space in the underlying file system as the blocks remain allocated
in the loop device's backing file.

This change adds an option, discard_blocks, to the devmapper snapshotter which
causes the snapshotter to issue blkdiscard ioctls on the thin device before
removal. With this option enabled, loop device setups will see disk space
return to the underlying filesystem immediately on exiting a container.

Fixes #5691

Signed-off-by: Kern Walster <walster@amazon.com>
This commit is contained in:
Kern Walster
2021-07-19 16:37:46 +00:00
parent b88bf1eca7
commit f1d79d33b8
7 changed files with 114 additions and 4 deletions

View File

@@ -16,6 +16,8 @@
limitations under the License.
*/
// Copyright 2012-2017 Docker, Inc.
package dmsetup
import (
@@ -26,6 +28,7 @@ import (
"strconv"
"strings"
blkdiscard "github.com/containerd/containerd/snapshots/devmapper/blkdiscard"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
@@ -37,6 +40,9 @@ const (
SectorSize = 512
)
// ErrInUse represents an error mutating a device because it is in use elsewhere
var ErrInUse = errors.New("device is in use")
// DeviceInfo represents device info returned by "dmsetup info".
// dmsetup(8) provides more information on each of these fields.
type DeviceInfo struct {
@@ -345,6 +351,24 @@ func BlockDeviceSize(path string) (int64, error) {
return size, nil
}
// DiscardBlocks discards all blocks for the given thin device
// ported from https://github.com/moby/moby/blob/7b9275c0da707b030e62c96b679a976f31f929d3/pkg/devicemapper/devmapper.go#L416
func DiscardBlocks(deviceName string) error {
inUse, err := isInUse(deviceName)
if err != nil {
return err
}
if inUse {
return ErrInUse
}
path := GetFullDevicePath(deviceName)
_, err = blkdiscard.BlkDiscard(path)
if err != nil {
return err
}
return nil
}
func dmsetup(args ...string) (string, error) {
data, err := exec.Command("dmsetup", args...).CombinedOutput()
output := string(data)
@@ -406,3 +430,14 @@ func parseDmsetupError(output string) string {
str = strings.ToLower(str)
return str
}
func isInUse(deviceName string) (bool, error) {
info, err := Info(deviceName)
if err != nil {
return true, err
}
if len(info) != 1 {
return true, errors.New("could not get device info")
}
return info[0].OpenCount != 0, nil
}

View File

@@ -83,6 +83,7 @@ func TestDMSetup(t *testing.T) {
t.Run("ActivateDevice", testActivateDevice)
t.Run("DeviceStatus", testDeviceStatus)
t.Run("SuspendResumeDevice", testSuspendResumeDevice)
t.Run("DiscardBlocks", testDiscardBlocks)
t.Run("RemoveDevice", testRemoveDevice)
t.Run("RemovePool", func(t *testing.T) {
@@ -169,6 +170,11 @@ func testSuspendResumeDevice(t *testing.T) {
assert.NilError(t, err)
}
func testDiscardBlocks(t *testing.T) {
err := DiscardBlocks(testDeviceName)
assert.NilError(t, err, "failed to discard blocks")
}
func testRemoveDevice(t *testing.T) {
err := RemoveDevice(testPoolName)
assert.Assert(t, err == unix.EBUSY, "removing thin-pool with dependencies shouldn't be allowed")