fix #1908: add vmgenid config/device

this adds a VM Generation ID device uses by Windows (Server) to determine
some specific actions that may have happened with the vm
such as rollback, restore, etc.

see:

https://docs.microsoft.com/en-us/windows/desktop/hyperv_v2/virtual-machine-generation-identifier

for details on how it works and when it should change

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2018-09-19 11:35:11 +02:00 committed by Thomas Lamprecht
parent dd84e5ec14
commit 6ee499fff8
4 changed files with 43 additions and 6 deletions

View File

@ -599,6 +599,10 @@ __PACKAGE__->register_method({
$conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid(); $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
} }
if (!defined($conf->{vmgenid}) || $conf->{vmgenid} eq '1') {
$conf->{vmgenid} = PVE::QemuServer::generate_uuid();
}
PVE::QemuConfig->write_config($vmid, $conf); PVE::QemuConfig->write_config($vmid, $conf);
}; };
@ -1091,6 +1095,10 @@ my $update_vm_api = sub {
# add macaddr # add macaddr
my $net = PVE::QemuServer::parse_net($param->{$opt}); my $net = PVE::QemuServer::parse_net($param->{$opt});
$param->{$opt} = PVE::QemuServer::print_net($net); $param->{$opt} = PVE::QemuServer::print_net($net);
} elsif ($opt eq 'vmgenid') {
if ($param->{$opt} eq '1') {
$param->{$opt} = PVE::QemuServer::generate_uuid();
}
} }
} }
@ -2725,13 +2733,15 @@ __PACKAGE__->register_method({
} }
# auto generate a new uuid # auto generate a new uuid
my ($uuid, $uuid_str);
UUID::generate($uuid);
UUID::unparse($uuid, $uuid_str);
my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || ''); my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || '');
$smbios1->{uuid} = $uuid_str; $smbios1->{uuid} = PVE::QemuServer::generate_uuid();
$newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1); $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1);
# auto generate a new vmgenid if the option was set
if ($newconf->{vmgenid}) {
$newconf->{vmgenid} = PVE::QemuServer::generate_uuid();
}
delete $newconf->{template}; delete $newconf->{template};
if ($param->{name}) { if ($param->{name}) {

View File

@ -621,6 +621,7 @@ __PACKAGE__->register_method ({
eval { eval {
# order matters, as do_import() will load_config() internally # order matters, as do_import() will load_config() internally
$conf->{vmgenid} = PVE::QemuServer::generate_uuid();
$conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid(); $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
PVE::QemuConfig->write_config($vmid, $conf); PVE::QemuConfig->write_config($vmid, $conf);

View File

@ -300,6 +300,10 @@ sub __snapshot_rollback_hook {
# in the original config. # in the original config.
delete $conf->{machine} if $snap->{vmstate} && !defined($data->{oldmachine}); delete $conf->{machine} if $snap->{vmstate} && !defined($data->{oldmachine});
} }
if ($conf->{vmgenid}) {
$conf->{vmgenid} = PVE::QemuServer::generate_uuid();
}
} }
return; return;

View File

@ -559,6 +559,13 @@ EODESCR
description => "Select BIOS implementation.", description => "Select BIOS implementation.",
default => 'seabios', default => 'seabios',
}, },
vmgenid => {
type => 'string',
pattern => '(?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01])',
format_description => 'UUID',
description => "Set VM Generation ID UUID. Use special value 1 to autogenerate one (API only). Use special value 0 to disable explicitly.",
optional => 1,
},
}; };
my $confdesc_cloudinit = { my $confdesc_cloudinit = {
@ -3191,6 +3198,10 @@ sub config_to_command {
push @$cmd, '-smbios', "type=1,$conf->{smbios1}"; push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
} }
if ($conf->{vmgenid}) {
push @$devices, '-device', 'vmgenid,guid='.$conf->{vmgenid};
}
if ($conf->{bios} && $conf->{bios} eq 'ovmf') { if ($conf->{bios} && $conf->{bios} eq 'ovmf') {
die "uefi base image not found\n" if ! -f $OVMF_CODE; die "uefi base image not found\n" if ! -f $OVMF_CODE;
@ -5554,6 +5565,13 @@ sub restore_update_config_line {
} else { } else {
print $outfd $line; print $outfd $line;
} }
} elsif (($line =~ m/^(vmgenid: )(.*)/)) {
# always generate a new vmgenid
my $vmgenid = $2;
if ($vmgenid ne '0') {
$vmgenid = generate_uuid();
}
print $outfd $1.$vmgenid."\n";
} elsif (($line =~ m/^(smbios1: )(.*)/) && $unique) { } elsif (($line =~ m/^(smbios1: )(.*)/) && $unique) {
my ($uuid, $uuid_str); my ($uuid, $uuid_str);
UUID::generate($uuid); UUID::generate($uuid);
@ -6756,11 +6774,15 @@ sub resolve_first_disk {
return $firstdisk; return $firstdisk;
} }
sub generate_smbios1_uuid { sub generate_uuid {
my ($uuid, $uuid_str); my ($uuid, $uuid_str);
UUID::generate($uuid); UUID::generate($uuid);
UUID::unparse($uuid, $uuid_str); UUID::unparse($uuid, $uuid_str);
return "uuid=$uuid_str"; return $uuid_str;
}
sub generate_smbios1_uuid {
return "uuid=".generate_uuid();
} }
# bash completion helper # bash completion helper