Skip to content

Commit 7dcc654

Browse files
committed
Merge pull request apache#1121 from shapeblue/4.6-cloudstack-9083
[4.6] CLOUDSTACK-9083: Add disk serial to kvm virt xmlAdds disk serial ids based on volume uuids to the virt xml. This may be useful for appliances/software that needs some serial ids on the VM disks. This does not impact existing/running VMs, the vm virt xmls will be updates for running VMs the next time they are stopped/started. For testing, disk serial (of debian based systemvm) in the virt xml matched that in /sys/devices/pci0000:00:0000:00:07.0/virtio4/block/vda/serial. We currently don't support scsi-blcok devices for which serial is not supported, for this we've added a DeviceType (LUN) which may be used in future and a check to not add the serial to the xml if disk type is LUN. Refer: https://libvirt.org/formatdomain.html#elementsDisks * pr/1121: CLOUDSTACK-9083: Add disk serial to kvm virt xml Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
2 parents 17eb420 + 12c395b commit 7dcc654

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,7 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
21402140

21412141
if (data instanceof VolumeObjectTO) {
21422142
final VolumeObjectTO volumeObjectTO = (VolumeObjectTO)data;
2143+
disk.setSerial(diskUuidToSerial(volumeObjectTO.getUuid()));
21432144
if (volumeObjectTO.getBytesReadRate() != null && volumeObjectTO.getBytesReadRate() > 0) {
21442145
disk.setBytesReadRate(volumeObjectTO.getBytesReadRate());
21452146
}
@@ -2419,6 +2420,11 @@ public StartupCommand[] initialize() {
24192420
}
24202421
}
24212422

2423+
public String diskUuidToSerial(String uuid) {
2424+
String uuidWithoutHyphen = uuid.replace("-","");
2425+
return uuidWithoutHyphen.substring(0, Math.min(uuidWithoutHyphen.length(), 20));
2426+
}
2427+
24222428
private String getIqn() {
24232429
try {
24242430
final String textToFind = "InitiatorName=";

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public List<InterfaceDef> getInterfaces() {
419419

420420
public static class DiskDef {
421421
public enum DeviceType {
422-
FLOPPY("floppy"), DISK("disk"), CDROM("cdrom");
422+
FLOPPY("floppy"), DISK("disk"), CDROM("cdrom"), LUN("lun");
423423
String _type;
424424

425425
DeviceType(String type) {
@@ -524,6 +524,7 @@ public String toString() {
524524
private Long _iopsReadRate;
525525
private Long _iopsWriteRate;
526526
private DiskCacheMode _diskCacheMode;
527+
private String _serial;
527528
private boolean qemuDriver = true;
528529

529530
public void setDeviceType(DeviceType deviceType) {
@@ -708,6 +709,10 @@ public void setQemuDriver(boolean qemuDriver){
708709
this.qemuDriver = qemuDriver;
709710
}
710711

712+
public void setSerial(String serial) {
713+
this._serial = serial;
714+
}
715+
711716
@Override
712717
public String toString() {
713718
StringBuilder diskBuilder = new StringBuilder();
@@ -761,6 +766,10 @@ public String toString() {
761766
}
762767
diskBuilder.append("/>\n");
763768

769+
if (_serial != null && !_serial.isEmpty() && _deviceType != DeviceType.LUN) {
770+
diskBuilder.append("<serial>" + _serial + "</serial>");
771+
}
772+
764773
if ((_deviceType != DeviceType.CDROM) &&
765774
(s_libvirtVersion >= 9008) &&
766775
(s_qemuVersion >= 1001000) &&

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ protected synchronized String attachOrDetachDevice(final Connect conn, final boo
950950
return null;
951951
}
952952

953-
protected synchronized String attachOrDetachDisk(final Connect conn, final boolean attach, final String vmName, final KVMPhysicalDisk attachingDisk, final int devId) throws LibvirtException,
953+
protected synchronized String attachOrDetachDisk(final Connect conn, final boolean attach, final String vmName, final KVMPhysicalDisk attachingDisk, final int devId, final String serial) throws LibvirtException,
954954
InternalErrorException {
955955
List<DiskDef> disks = null;
956956
Domain dm = null;
@@ -986,6 +986,7 @@ protected synchronized String attachOrDetachDisk(final Connect conn, final boole
986986
}
987987
} else {
988988
diskdef = new DiskDef();
989+
diskdef.setSerial(serial);
989990
if (attachingPool.getType() == StoragePoolType.RBD) {
990991
if(resource.getHypervisorType() == Hypervisor.HypervisorType.LXC){
991992
// For LXC, map image to host and then attach to Vm
@@ -1028,14 +1029,15 @@ public Answer attachVolume(final AttachCommand cmd) {
10281029
final VolumeObjectTO vol = (VolumeObjectTO)disk.getData();
10291030
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore();
10301031
final String vmName = cmd.getVmName();
1032+
final String serial = resource.diskUuidToSerial(vol.getUuid());
10311033
try {
10321034
final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
10331035

10341036
storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails());
10351037

10361038
final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
10371039

1038-
attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue());
1040+
attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue(), serial);
10391041

10401042
return new AttachAnswer(disk);
10411043
} catch (final LibvirtException e) {
@@ -1054,12 +1056,13 @@ public Answer dettachVolume(final DettachCommand cmd) {
10541056
final VolumeObjectTO vol = (VolumeObjectTO)disk.getData();
10551057
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore();
10561058
final String vmName = cmd.getVmName();
1059+
final String serial = resource.diskUuidToSerial(vol.getUuid());
10571060
try {
10581061
final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
10591062

10601063
final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
10611064

1062-
attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue());
1065+
attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue(), serial);
10631066

10641067
storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
10651068

plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,14 @@ public void testGetNicStats() {
389389
assertNotNull(stats);
390390
}
391391

392+
@Test
393+
public void diskUuidToSerialTest() {
394+
String uuid = "38400000-8cf0-11bd-b24e-10b96e4ef00d";
395+
String expected = "384000008cf011bdb24e";
396+
LibvirtComputingResource lcr = new LibvirtComputingResource();
397+
Assert.assertEquals(expected, lcr.diskUuidToSerial(uuid));
398+
}
399+
392400
@Test
393401
public void testUUID() {
394402
String uuid = "1";

0 commit comments

Comments
 (0)