Skip to content

Commit 99cc4cb

Browse files
spacegospodtenthirtyam
authored andcommitted
support attaching existing sparse vmdk when creating VM
Signed-off-by: Stoyan Zhelyazkov <[email protected]>
1 parent dea5add commit 99cc4cb

File tree

1 file changed

+63
-29
lines changed

1 file changed

+63
-29
lines changed

vsphere/internal/virtualdevice/virtual_machine_disk_subresource.go

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,48 @@ func (r *DiskSubresource) Read(l object.VirtualDeviceList) error {
14361436
attach = r.Get("attach").(bool)
14371437
}
14381438
// Save disk backing settings
1439-
b, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo)
1440-
if !ok {
1439+
if b, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok {
1440+
if err := r.setFlatBackingProperties(b, disk, attach); err != nil {
1441+
return err
1442+
}
1443+
} else if b, ok := disk.Backing.(*types.VirtualDiskSparseVer2BackingInfo); ok {
1444+
if err := r.setSparseBackingProperties(b, disk, attach); err != nil {
1445+
return err
1446+
}
1447+
} else {
14411448
return fmt.Errorf("disk backing at %s is of an unsupported type (type %T)", r.Get("device_address").(string), disk.Backing)
14421449
}
1450+
1451+
if allocation := disk.StorageIOAllocation; allocation != nil {
1452+
r.Set("io_limit", allocation.Limit)
1453+
r.Set("io_reservation", allocation.Reservation)
1454+
if shares := allocation.Shares; shares != nil {
1455+
r.Set("io_share_level", string(shares.Level))
1456+
r.Set("io_share_count", shares.Shares)
1457+
}
1458+
}
1459+
1460+
if spbm.IsSupported(r.client) {
1461+
// Set storage policy if the VM exists.
1462+
vmUUID := r.rdd.Id()
1463+
if vmUUID != "" {
1464+
result, err := virtualmachine.MOIDForUUID(r.client, vmUUID)
1465+
if err != nil {
1466+
return err
1467+
}
1468+
polID, err := spbm.PolicyIDByVirtualDisk(r.client, result.MOID, r.Get("key").(int))
1469+
if err != nil {
1470+
return err
1471+
}
1472+
r.Set("storage_policy_id", polID)
1473+
}
1474+
}
1475+
1476+
log.Printf("[DEBUG] %s: Read finished (key and device address may have changed)", r)
1477+
return nil
1478+
}
1479+
1480+
func (r *DiskSubresource) setFlatBackingProperties(b *types.VirtualDiskFlatVer2BackingInfo, disk *types.VirtualDisk, attach bool) error {
14431481
r.Set("uuid", b.Uuid)
14441482
r.Set("disk_mode", b.DiskMode)
14451483
r.Set("write_through", b.WriteThrough)
@@ -1469,29 +1507,24 @@ func (r *DiskSubresource) Read(l object.VirtualDeviceList) error {
14691507
r.Set("size", diskCapacityInGiB(disk))
14701508
}
14711509

1472-
if allocation := disk.StorageIOAllocation; allocation != nil {
1473-
r.Set("io_limit", allocation.Limit)
1474-
r.Set("io_reservation", allocation.Reservation)
1475-
if shares := allocation.Shares; shares != nil {
1476-
r.Set("io_share_level", string(shares.Level))
1477-
r.Set("io_share_count", shares.Shares)
1478-
}
1479-
}
1510+
return nil
1511+
}
14801512

1481-
if spbm.IsSupported(r.client) {
1482-
// Set storage policy if the VM exists.
1483-
vmUUID := r.rdd.Id()
1484-
if vmUUID != "" {
1485-
result, err := virtualmachine.MOIDForUUID(r.client, vmUUID)
1486-
if err != nil {
1487-
return err
1488-
}
1489-
polID, err := spbm.PolicyIDByVirtualDisk(r.client, result.MOID, r.Get("key").(int))
1490-
if err != nil {
1491-
return err
1492-
}
1493-
r.Set("storage_policy_id", polID)
1513+
func (r *DiskSubresource) setSparseBackingProperties(b *types.VirtualDiskSparseVer2BackingInfo, disk *types.VirtualDisk, attach bool) error {
1514+
r.Set("uuid", b.Uuid)
1515+
r.Set("disk_mode", b.DiskMode)
1516+
r.Set("write_through", b.WriteThrough)
1517+
1518+
r.Set("datastore_id", b.Datastore.Value)
1519+
1520+
// Disk settings
1521+
if !attach {
1522+
dp := &object.DatastorePath{}
1523+
if ok := dp.FromString(b.FileName); !ok {
1524+
return fmt.Errorf("could not parse path from filename: %s", b.FileName)
14941525
}
1526+
r.Set("path", dp.Path)
1527+
r.Set("size", diskCapacityInGiB(disk))
14951528
}
14961529

14971530
log.Printf("[DEBUG] %s: Read finished (key and device address may have changed)", r)
@@ -2364,14 +2397,15 @@ func diskUUIDMatch(device types.BaseVirtualDevice, uuid string) bool {
23642397
if !ok {
23652398
return false
23662399
}
2367-
backing, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo)
2368-
if !ok {
2369-
return false
2400+
2401+
if backing, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok {
2402+
return backing.Uuid == uuid
23702403
}
2371-
if backing.Uuid != uuid {
2372-
return false
2404+
if backing, ok := disk.Backing.(*types.VirtualDiskSparseVer2BackingInfo); ok {
2405+
return backing.Uuid == uuid
23732406
}
2374-
return true
2407+
2408+
return false
23752409
}
23762410

23772411
// diskCapacityInGiB reports the supplied disk's capacity, by first checking

0 commit comments

Comments
 (0)