From 2a0777fe85f7ce2f8b321a1fe8a6fb7a52094fd6 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Tue, 27 Jun 2023 14:33:34 +0200 Subject: [PATCH] lxd/storage/drivers: Drop FS suffix on VM image block volumes Signed-off-by: Thomas Hipp --- lxd/storage/drivers/driver_btrfs.go | 7 ++-- lxd/storage/drivers/driver_ceph.go | 7 ++-- lxd/storage/drivers/driver_cephfs.go | 7 ++-- lxd/storage/drivers/driver_cephobject.go | 7 ++-- lxd/storage/drivers/driver_dir.go | 7 ++-- lxd/storage/drivers/driver_lvm.go | 7 ++-- lxd/storage/drivers/driver_zfs.go | 43 ++++++++++++++++++++++-- 7 files changed, 64 insertions(+), 21 deletions(-) diff --git a/lxd/storage/drivers/driver_btrfs.go b/lxd/storage/drivers/driver_btrfs.go index 1b4ce828949e..c496a5303dbe 100644 --- a/lxd/storage/drivers/driver_btrfs.go +++ b/lxd/storage/drivers/driver_btrfs.go @@ -32,9 +32,10 @@ type btrfs struct { func (d *btrfs) load() error { // Register the patches. d.patches = map[string]func() error{ - "storage_lvm_skipactivation": nil, - "storage_missing_snapshot_records": nil, - "storage_delete_old_snapshot_records": nil, + "storage_lvm_skipactivation": nil, + "storage_missing_snapshot_records": nil, + "storage_delete_old_snapshot_records": nil, + "storage_zfs_drop_block_volume_filesystem_extension": nil, } // Done if previously loaded. diff --git a/lxd/storage/drivers/driver_ceph.go b/lxd/storage/drivers/driver_ceph.go index 6958998f0174..d7efd4bc5fb4 100644 --- a/lxd/storage/drivers/driver_ceph.go +++ b/lxd/storage/drivers/driver_ceph.go @@ -29,9 +29,10 @@ type ceph struct { func (d *ceph) load() error { // Register the patches. d.patches = map[string]func() error{ - "storage_lvm_skipactivation": nil, - "storage_missing_snapshot_records": nil, - "storage_delete_old_snapshot_records": nil, + "storage_lvm_skipactivation": nil, + "storage_missing_snapshot_records": nil, + "storage_delete_old_snapshot_records": nil, + "storage_zfs_drop_block_volume_filesystem_extension": nil, } // Done if previously loaded. diff --git a/lxd/storage/drivers/driver_cephfs.go b/lxd/storage/drivers/driver_cephfs.go index d775e4bf5d5b..9e3df4a1dc75 100644 --- a/lxd/storage/drivers/driver_cephfs.go +++ b/lxd/storage/drivers/driver_cephfs.go @@ -26,9 +26,10 @@ type cephfs struct { func (d *cephfs) load() error { // Register the patches. d.patches = map[string]func() error{ - "storage_lvm_skipactivation": nil, - "storage_missing_snapshot_records": nil, - "storage_delete_old_snapshot_records": nil, + "storage_lvm_skipactivation": nil, + "storage_missing_snapshot_records": nil, + "storage_delete_old_snapshot_records": nil, + "storage_zfs_drop_block_volume_filesystem_extension": nil, } // Done if previously loaded. diff --git a/lxd/storage/drivers/driver_cephobject.go b/lxd/storage/drivers/driver_cephobject.go index 72ccf22b8bce..387e9d532fb7 100644 --- a/lxd/storage/drivers/driver_cephobject.go +++ b/lxd/storage/drivers/driver_cephobject.go @@ -28,9 +28,10 @@ type cephobject struct { func (d *cephobject) load() error { // Register the patches. d.patches = map[string]func() error{ - "storage_lvm_skipactivation": nil, - "storage_missing_snapshot_records": nil, - "storage_delete_old_snapshot_records": nil, + "storage_lvm_skipactivation": nil, + "storage_missing_snapshot_records": nil, + "storage_delete_old_snapshot_records": nil, + "storage_zfs_drop_block_volume_filesystem_extension": nil, } // Done if previously loaded. diff --git a/lxd/storage/drivers/driver_dir.go b/lxd/storage/drivers/driver_dir.go index 01e15c3fa4b3..e568e2764b39 100644 --- a/lxd/storage/drivers/driver_dir.go +++ b/lxd/storage/drivers/driver_dir.go @@ -20,9 +20,10 @@ type dir struct { func (d *dir) load() error { // Register the patches. d.patches = map[string]func() error{ - "storage_lvm_skipactivation": nil, - "storage_missing_snapshot_records": nil, - "storage_delete_old_snapshot_records": nil, + "storage_lvm_skipactivation": nil, + "storage_missing_snapshot_records": nil, + "storage_delete_old_snapshot_records": nil, + "storage_zfs_drop_block_volume_filesystem_extension": nil, } return nil diff --git a/lxd/storage/drivers/driver_lvm.go b/lxd/storage/drivers/driver_lvm.go index 9d13fda02680..44e86592c74f 100644 --- a/lxd/storage/drivers/driver_lvm.go +++ b/lxd/storage/drivers/driver_lvm.go @@ -31,9 +31,10 @@ type lvm struct { func (d *lvm) load() error { // Register the patches. d.patches = map[string]func() error{ - "storage_lvm_skipactivation": d.patchStorageSkipActivation, - "storage_missing_snapshot_records": nil, - "storage_delete_old_snapshot_records": nil, + "storage_lvm_skipactivation": d.patchStorageSkipActivation, + "storage_missing_snapshot_records": nil, + "storage_delete_old_snapshot_records": nil, + "storage_zfs_drop_block_volume_filesystem_extension": nil, } // Done if previously loaded. diff --git a/lxd/storage/drivers/driver_zfs.go b/lxd/storage/drivers/driver_zfs.go index 713eedfafae7..027daadeba64 100644 --- a/lxd/storage/drivers/driver_zfs.go +++ b/lxd/storage/drivers/driver_zfs.go @@ -44,9 +44,10 @@ type zfs struct { func (d *zfs) load() error { // Register the patches. d.patches = map[string]func() error{ - "storage_lvm_skipactivation": nil, - "storage_missing_snapshot_records": nil, - "storage_delete_old_snapshot_records": nil, + "storage_lvm_skipactivation": nil, + "storage_missing_snapshot_records": nil, + "storage_delete_old_snapshot_records": nil, + "storage_zfs_drop_block_volume_filesystem_extension": d.patchDropBlockVolumeFilesystemExtension, } // Done if previously loaded. @@ -664,3 +665,39 @@ func (d *zfs) MigrationTypes(contentType ContentType, refresh bool, copySnapshot }, } } + +// patchDropBlockVolumeFilesystemExtension removes the filesystem extension (e.g _ext4) from VM image block volumes. +func (d *zfs) patchDropBlockVolumeFilesystemExtension() error { + poolName, ok := d.config["zfs.pool_name"] + if !ok { + poolName = d.name + } + + out, err := shared.RunCommand("zfs", "list", "-H", "-r", "-o", "name", "-t", "volume", fmt.Sprintf("%s/images", poolName)) + if err != nil { + return fmt.Errorf("Failed listing images: %w", err) + } + + for _, volume := range strings.Split(out, "\n") { + fields := strings.SplitN(volume, "/", 3) + + if len(fields) != 3 { + continue + } + + // Ignore non-block images, and images without filesystem extension + if !strings.HasSuffix(fields[2], ".block") || !strings.Contains(fields[2], "_") { + continue + } + + // Rename zfs dataset. Snapshots will automatically be renamed. + newName := fmt.Sprintf("%s/images/%s.block", poolName, strings.Split(fields[2], "_")[0]) + + _, err = shared.RunCommand("zfs", "rename", volume, newName) + if err != nil { + return fmt.Errorf("Failed renaming zfs dataset: %w", err) + } + } + + return nil +}