Add support for setting disk drive serial numbers

* docs/schemas/domain.rng: Add <serial> element to disks
* src/domain_conf.h, src/domain_conf.c: XML parsing and
  formatting for disk serial numbers
* src/qemu_conf.c: Set serial number when launching guests
* tests/qemuxml2argvdata/qemuxml2argv-disk-drive-shared.args,
  tests/qemuxml2argvdata/qemuxml2argv-disk-drive-shared.xml: Add
  serial number to XML test
This commit is contained in:
Daniel P. Berrange 2009-08-14 12:22:01 +01:00
parent d823a05aef
commit 85d15b5143
9 changed files with 54 additions and 2 deletions

View File

@ -347,6 +347,11 @@
<empty/> <empty/>
</element> </element>
</optional> </optional>
<optional>
<element name="serial">
<ref name="diskSerial"/>
</element>
</optional>
<optional> <optional>
<ref name="encryption"/> <ref name="encryption"/>
</optional> </optional>
@ -1169,6 +1174,11 @@
<param name="pattern">[A-Za-z0-9_\.\+\-&amp;:/]+</param> <param name="pattern">[A-Za-z0-9_\.\+\-&amp;:/]+</param>
</data> </data>
</define> </define>
<define name="diskSerial">
<data type="string">
<param name="pattern">[A-Za-z0-9_\.\+\-]+</param>
</data>
</define>
<define name="genericName"> <define name="genericName">
<data type="string"> <data type="string">
<param name="pattern">[a-zA-Z0-9_\+\-]+</param> <param name="pattern">[a-zA-Z0-9_\+\-]+</param>

View File

@ -284,6 +284,7 @@ void virDomainDiskDefFree(virDomainDiskDefPtr def)
if (!def) if (!def)
return; return;
VIR_FREE(def->serial);
VIR_FREE(def->src); VIR_FREE(def->src);
VIR_FREE(def->dst); VIR_FREE(def->dst);
VIR_FREE(def->driverName); VIR_FREE(def->driverName);
@ -653,6 +654,7 @@ virDomainDiskDefParseXML(virConnectPtr conn,
char *cachetag = NULL; char *cachetag = NULL;
char *devaddr = NULL; char *devaddr = NULL;
virStorageEncryptionPtr encryption = NULL; virStorageEncryptionPtr encryption = NULL;
char *serial = NULL;
if (VIR_ALLOC(def) < 0) { if (VIR_ALLOC(def) < 0) {
virReportOOMError(conn); virReportOOMError(conn);
@ -716,6 +718,9 @@ virDomainDiskDefParseXML(virConnectPtr conn,
cur); cur);
if (encryption == NULL) if (encryption == NULL)
goto error; goto error;
} else if ((serial == NULL) &&
(xmlStrEqual(cur->name, BAD_CAST "serial"))) {
serial = (char *)xmlNodeGetContent(cur);
} }
} }
cur = cur->next; cur = cur->next;
@ -836,6 +841,8 @@ virDomainDiskDefParseXML(virConnectPtr conn,
driverType = NULL; driverType = NULL;
def->encryption = encryption; def->encryption = encryption;
encryption = NULL; encryption = NULL;
def->serial = serial;
serial = NULL;
cleanup: cleanup:
VIR_FREE(bus); VIR_FREE(bus);
@ -847,6 +854,7 @@ cleanup:
VIR_FREE(driverName); VIR_FREE(driverName);
VIR_FREE(cachetag); VIR_FREE(cachetag);
VIR_FREE(devaddr); VIR_FREE(devaddr);
VIR_FREE(serial);
virStorageEncryptionFree(encryption); virStorageEncryptionFree(encryption);
return def; return def;
@ -3616,6 +3624,9 @@ virDomainDiskDefFormat(virConnectPtr conn,
virBufferAddLit(buf, " <readonly/>\n"); virBufferAddLit(buf, " <readonly/>\n");
if (def->shared) if (def->shared)
virBufferAddLit(buf, " <shareable/>\n"); virBufferAddLit(buf, " <shareable/>\n");
if (def->serial)
virBufferEscapeString(buf, " <serial>%s</serial>\n",
def->serial);
if (def->encryption != NULL && if (def->encryption != NULL &&
virStorageEncryptionFormat(conn, buf, def->encryption) < 0) virStorageEncryptionFormat(conn, buf, def->encryption) < 0)
return -1; return -1;

View File

@ -110,6 +110,7 @@ struct _virDomainDiskDef {
char *dst; char *dst;
char *driverName; char *driverName;
char *driverType; char *driverType;
char *serial;
int cachemode; int cachemode;
unsigned int readonly : 1; unsigned int readonly : 1;
unsigned int shared : 1; unsigned int shared : 1;

View File

@ -810,6 +810,8 @@ static unsigned int qemudComputeCmdFlags(const char *help,
flags |= QEMUD_CMD_FLAG_VGA; flags |= QEMUD_CMD_FLAG_VGA;
if (strstr(help, "boot=on")) if (strstr(help, "boot=on"))
flags |= QEMUD_CMD_FLAG_DRIVE_BOOT; flags |= QEMUD_CMD_FLAG_DRIVE_BOOT;
if (strstr(help, "serial=s"))
flags |= QEMUD_CMD_FLAG_DRIVE_SERIAL;
if (strstr(help, "-pcidevice")) if (strstr(help, "-pcidevice"))
flags |= QEMUD_CMD_FLAG_PCIDEVICE; flags |= QEMUD_CMD_FLAG_PCIDEVICE;
if (strstr(help, "-mem-path")) if (strstr(help, "-mem-path"))
@ -1405,6 +1407,23 @@ static int qemudBuildCommandLineChrDevStr(virDomainChrDefPtr dev,
return 0; return 0;
} }
#define QEMU_SERIAL_PARAM_ACCEPTED_CHARS \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
static int
qemuSafeSerialParamValue(virConnectPtr conn,
const char *value)
{
if (strspn(value, QEMU_SERIAL_PARAM_ACCEPTED_CHARS) != strlen (value)) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
_("driver serial '%s' contains unsafe characters"),
value);
return -1;
}
return 0;
}
/* /*
* Constructs a argv suitable for launching qemu with config defined * Constructs a argv suitable for launching qemu with config defined
* for a given virtual machine. * for a given virtual machine.
@ -1804,6 +1823,12 @@ int qemudBuildCommandLine(virConnectPtr conn,
if (disk->driverType && if (disk->driverType &&
qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_FORMAT) qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_FORMAT)
virBufferVSprintf(&opt, ",format=%s", disk->driverType); virBufferVSprintf(&opt, ",format=%s", disk->driverType);
if (disk->serial &&
(qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE_SERIAL)) {
if (qemuSafeSerialParamValue(conn, disk->serial) < 0)
goto error;
virBufferVSprintf(&opt, ",serial=%s", disk->serial);
}
if (disk->cachemode) { if (disk->cachemode) {
const char *mode = const char *mode =

View File

@ -68,6 +68,7 @@ enum qemud_cmd_flags {
QEMUD_CMD_FLAG_PCIDEVICE = (1 << 17), /* PCI device assignment only supported by qemu-kvm */ QEMUD_CMD_FLAG_PCIDEVICE = (1 << 17), /* PCI device assignment only supported by qemu-kvm */
QEMUD_CMD_FLAG_MEM_PATH = (1 << 18), /* mmap'ped guest backing supported */ QEMUD_CMD_FLAG_MEM_PATH = (1 << 18), /* mmap'ped guest backing supported */
QEMUD_CMD_FLAG_DRIVE_SERIAL = (1 << 19), /* -driver serial= available */
}; };
/* Main driver state */ /* Main driver state */

View File

@ -119,6 +119,7 @@ mymain(int argc, char **argv)
QEMUD_CMD_FLAG_MIGRATE_QEMU_EXEC | QEMUD_CMD_FLAG_MIGRATE_QEMU_EXEC |
QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_CACHE_V2 |
QEMUD_CMD_FLAG_DRIVE_FORMAT | QEMUD_CMD_FLAG_DRIVE_FORMAT |
QEMUD_CMD_FLAG_DRIVE_SERIAL |
QEMUD_CMD_FLAG_VGA | QEMUD_CMD_FLAG_VGA |
QEMUD_CMD_FLAG_0_10, QEMUD_CMD_FLAG_0_10,
10005, 0, 0); 10005, 0, 0);
@ -135,6 +136,7 @@ mymain(int argc, char **argv)
QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_CACHE_V2 |
QEMUD_CMD_FLAG_KVM | QEMUD_CMD_FLAG_KVM |
QEMUD_CMD_FLAG_DRIVE_FORMAT | QEMUD_CMD_FLAG_DRIVE_FORMAT |
QEMUD_CMD_FLAG_DRIVE_SERIAL |
QEMUD_CMD_FLAG_VGA | QEMUD_CMD_FLAG_VGA |
QEMUD_CMD_FLAG_0_10 | QEMUD_CMD_FLAG_0_10 |
QEMUD_CMD_FLAG_PCIDEVICE | QEMUD_CMD_FLAG_PCIDEVICE |
@ -153,6 +155,7 @@ mymain(int argc, char **argv)
QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_CACHE_V2 |
QEMUD_CMD_FLAG_KVM | QEMUD_CMD_FLAG_KVM |
QEMUD_CMD_FLAG_DRIVE_FORMAT | QEMUD_CMD_FLAG_DRIVE_FORMAT |
QEMUD_CMD_FLAG_DRIVE_SERIAL |
QEMUD_CMD_FLAG_VGA | QEMUD_CMD_FLAG_VGA |
QEMUD_CMD_FLAG_0_10 | QEMUD_CMD_FLAG_0_10 |
QEMUD_CMD_FLAG_PCIDEVICE, QEMUD_CMD_FLAG_PCIDEVICE,

View File

@ -1 +1 @@
LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -monitor unix:/tmp/test-monitor,server,nowait -no-acpi -boot c -drive file=/dev/HostVG/QEMUGuest1,if=ide,index=0,format=qcow2,cache=off -drive file=/dev/HostVG/QEMUGuest2,if=ide,media=cdrom,index=2,format=raw -net none -serial none -parallel none -usb LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -monitor unix:/tmp/test-monitor,server,nowait -no-acpi -boot c -drive file=/dev/HostVG/QEMUGuest1,if=ide,index=0,format=qcow2,serial=XYZXYZXYZYXXYZYZYXYZY,cache=off -drive file=/dev/HostVG/QEMUGuest2,if=ide,media=cdrom,index=2,format=raw -net none -serial none -parallel none -usb

View File

@ -19,6 +19,7 @@
<source dev='/dev/HostVG/QEMUGuest1'/> <source dev='/dev/HostVG/QEMUGuest1'/>
<target dev='hda' bus='ide'/> <target dev='hda' bus='ide'/>
<shareable/> <shareable/>
<serial>XYZXYZXYZYXXYZYZYXYZY</serial>
</disk> </disk>
<disk type='block' device='cdrom'> <disk type='block' device='cdrom'>
<driver name='qemu' type='raw'/> <driver name='qemu' type='raw'/>

View File

@ -209,7 +209,7 @@ mymain(int argc, char **argv)
DO_TEST("disk-drive-fmt-qcow", QEMUD_CMD_FLAG_DRIVE | DO_TEST("disk-drive-fmt-qcow", QEMUD_CMD_FLAG_DRIVE |
QEMUD_CMD_FLAG_DRIVE_BOOT | QEMUD_CMD_FLAG_DRIVE_FORMAT); QEMUD_CMD_FLAG_DRIVE_BOOT | QEMUD_CMD_FLAG_DRIVE_FORMAT);
DO_TEST("disk-drive-shared", QEMUD_CMD_FLAG_DRIVE | DO_TEST("disk-drive-shared", QEMUD_CMD_FLAG_DRIVE |
QEMUD_CMD_FLAG_DRIVE_FORMAT); QEMUD_CMD_FLAG_DRIVE_FORMAT | QEMUD_CMD_FLAG_DRIVE_SERIAL);
DO_TEST("disk-drive-cache-v1-wt", QEMUD_CMD_FLAG_DRIVE | DO_TEST("disk-drive-cache-v1-wt", QEMUD_CMD_FLAG_DRIVE |
QEMUD_CMD_FLAG_DRIVE_FORMAT); QEMUD_CMD_FLAG_DRIVE_FORMAT);
DO_TEST("disk-drive-cache-v1-wb", QEMUD_CMD_FLAG_DRIVE | DO_TEST("disk-drive-cache-v1-wb", QEMUD_CMD_FLAG_DRIVE |