Skip to content

Commit e998d78

Browse files
committed
fix(disk): restore updating boot disk.
Signed-off-by: Marco Attia <[email protected]>
1 parent 64ed6a6 commit e998d78

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

proxmoxtf/resource/vm/disk/disk.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,9 @@ func Update(
582582
planDisks vms.CustomStorageDevices,
583583
currentDisks vms.CustomStorageDevices,
584584
updateBody *vms.UpdateRequestBody,
585-
) (bool, error) {
585+
) (bool, bool, error) {
586586
rebootRequired := false
587+
shutdownBeforeUpdate := false
587588

588589
if d.HasChange(MkDisk) {
589590
for iface, disk := range planDisks {
@@ -595,7 +596,7 @@ func Update(
595596
// only disks with defined file ID are custom image disks that need to be created via import.
596597
err := createCustomDisk(ctx, client, nodeName, vmID, iface, *disk)
597598
if err != nil {
598-
return false, fmt.Errorf("creating custom disk: %w", err)
599+
return false, false, fmt.Errorf("creating custom disk: %w", err)
599600
}
600601
} else {
601602
// otherwise this is a blank disk that can be added directly via update API
@@ -611,7 +612,7 @@ func Update(
611612
tmp = currentDisks[iface]
612613
default:
613614
// something went wrong
614-
return false, fmt.Errorf("missing device %s", iface)
615+
return false, false, fmt.Errorf("missing device %s", iface)
615616
}
616617

617618
if tmp == nil || disk == nil {
@@ -623,8 +624,13 @@ func Update(
623624
tmp.AIO = disk.AIO
624625
}
625626

626-
// Never send ImportFrom for existing disks - it triggers re-import which fails for boot disks
627-
// ImportFrom is only for initial disk creation, not updates
627+
if disk.ImportFrom != nil && *disk.ImportFrom != "" {
628+
rebootRequired = true
629+
shutdownBeforeUpdate = true
630+
tmp.DatastoreID = disk.DatastoreID
631+
tmp.ImportFrom = disk.ImportFrom
632+
tmp.Size = disk.Size
633+
}
628634

629635
tmp.Backup = disk.Backup
630636
tmp.BurstableReadSpeedMbps = disk.BurstableReadSpeedMbps
@@ -646,5 +652,5 @@ func Update(
646652
}
647653
}
648654

649-
return rebootRequired, nil
655+
return shutdownBeforeUpdate, rebootRequired, nil
650656
}

proxmoxtf/resource/vm/disk/disk_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func TestDiskUpdateSkipsUnchangedDisks(t *testing.T) {
337337
require.NoError(t, err)
338338

339339
// Call the Update function
340-
_, err = Update(ctx, client, nodeName, vmID, resourceData, planDisks, currentDisks, updateBody)
340+
_, _, err = Update(ctx, client, nodeName, vmID, resourceData, planDisks, currentDisks, updateBody)
341341
require.NoError(t, err)
342342

343343
// Check that only the changed disk (scsi1) is in the update body

proxmoxtf/resource/vm/vm.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5426,7 +5426,7 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
54265426
return diag.FromErr(err)
54275427
}
54285428

5429-
rr, err := disk.Update(ctx, client, nodeName, vmID, d, planDisks, allDiskInfo, updateBody)
5429+
stoppedBeforeUpdate, rr, err := disk.Update(ctx, client, nodeName, vmID, d, planDisks, allDiskInfo, updateBody)
54305430
if err != nil {
54315431
return diag.FromErr(err)
54325432
}
@@ -5461,7 +5461,6 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
54615461
}
54625462

54635463
// Prepare the new cloud-init configuration.
5464-
stoppedBeforeUpdate := false
54655464
cloudInitRebuildRequired := false
54665465

54675466
if d.HasChange(mkInitialization) {
@@ -5750,16 +5749,11 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
57505749
// Update the configuration now that everything has been prepared.
57515750
updateBody.Delete = del
57525751

5753-
e = vmAPI.UpdateVM(ctx, updateBody)
5754-
if e != nil {
5755-
return diag.FromErr(e)
5756-
}
5757-
57585752
// Determine if the state of the virtual machine state needs to be changed.
57595753
//nolint: nestif
57605754
if (d.HasChange(mkStarted) || stoppedBeforeUpdate) && !bool(template) {
57615755
started := d.Get(mkStarted).(bool)
5762-
if started {
5756+
if started && !stoppedBeforeUpdate {
57635757
if diags := vmStart(ctx, vmAPI, d); diags != nil {
57645758
return diags
57655759
}
@@ -5772,6 +5766,11 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
57725766
}
57735767
}
57745768

5769+
e = vmAPI.UpdateVM(ctx, updateBody)
5770+
if e != nil {
5771+
return diag.FromErr(e)
5772+
}
5773+
57755774
if cloudInitRebuildRequired {
57765775
if er := vmAPI.RebuildCloudInitDisk(ctx); er != nil {
57775776
return diag.FromErr(err)

0 commit comments

Comments
 (0)