@@ -634,7 +634,8 @@ func (t *InternalIncusTarget) SetupVM(ctx context.Context, instDef migration.Ins
634634
635635 tgtClient := t .incusClient .UseTarget (instInfo .Location )
636636 defaultDiskDef := map [string ]string {
637- "type" : "disk" ,
637+ "type" : "disk" ,
638+ "dependent" : "true" ,
638639 }
639640
640641 // Create volumes for the remaining disks.
@@ -729,12 +730,30 @@ func (t *InternalIncusTarget) CleanupVM(ctx context.Context, name string, requir
729730 return fmt .Errorf ("Failed to wait for delete operation for instance %q: %w" , name , err )
730731 }
731732
732- tgtClient := t . incusClient . UseTarget ( instInfo . Location )
733+ volsByPool := map [ string ] map [ string ] bool {}
733734 for _ , dev := range instInfo .Devices {
734- if dev ["type" ] == "disk" && dev ["user.migration_source" ] != "" && dev ["source" ] != "" {
735- err := tgtClient .DeleteStoragePoolVolume (dev ["pool" ], "custom" , dev ["source" ])
736- if err != nil {
737- return fmt .Errorf ("Failed to delete instance %q storage volume %q on pool %q: %w" , name , dev ["source" ], dev ["pool" ], err )
735+ if dev ["type" ] == "disk" && dev ["user.migration_source" ] != "" && dev ["source" ] != "" && dev ["pool" ] != "" {
736+ if volsByPool [dev ["pool" ]] == nil {
737+ volsByPool [dev ["pool" ]] = map [string ]bool {}
738+ }
739+
740+ volsByPool [dev ["pool" ]][dev ["source" ]] = true
741+ }
742+ }
743+
744+ tgtClient := t .incusClient .UseTarget (instInfo .Location )
745+ for pool , volsByName := range volsByPool {
746+ volNames , err := tgtClient .GetStoragePoolVolumeNames (pool )
747+ if err != nil {
748+ return fmt .Errorf ("Failed to find %q volumes for instance %q: %w" , pool , name , err )
749+ }
750+
751+ for _ , vol := range volNames {
752+ if volsByName [vol ] {
753+ err := tgtClient .DeleteStoragePoolVolume (pool , "custom" , vol )
754+ if err != nil {
755+ return fmt .Errorf ("Failed to delete instance %q storage volume %q on pool %q: %w" , name , vol , pool , err )
756+ }
738757 }
739758 }
740759 }
0 commit comments