Skip to content

Commit

Permalink
lxd/storage/drivers: Drop FS suffix on VM image block volumes
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Hipp <[email protected]>
  • Loading branch information
monstermunchkin committed Jun 27, 2023
1 parent d8937db commit 2a0777f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 21 deletions.
7 changes: 4 additions & 3 deletions lxd/storage/drivers/driver_btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions lxd/storage/drivers/driver_ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions lxd/storage/drivers/driver_cephfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions lxd/storage/drivers/driver_cephobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions lxd/storage/drivers/driver_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lxd/storage/drivers/driver_lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 40 additions & 3 deletions lxd/storage/drivers/driver_zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit 2a0777f

Please sign in to comment.