5
0
mirror of git://git.proxmox.com/git/qemu-server.git synced 2025-01-11 05:17:57 +03:00

implement unique option for restore

Also try to convert old vlanX to new netX syntax.
This commit is contained in:
Dietmar Maurer 2011-10-18 09:14:05 +02:00
parent a6af7b3e35
commit 51586c3a4c
3 changed files with 47 additions and 9 deletions

View File

@ -89,6 +89,13 @@ __PACKAGE__->register_method({
optional => 1,
type => 'boolean',
description => "Allow to overwrite existing VM.",
requires => 'archive',
},
unique => {
optional => 1,
type => 'boolean',
description => "Assign a unique random ethernet address.",
requires => 'archive',
},
}),
},
@ -110,6 +117,10 @@ __PACKAGE__->register_method({
my $storage = extract_param($param, 'storage');
my $force = extract_param($param, 'force');
my $unique = extract_param($param, 'unique');
my $filename = PVE::QemuServer::config_file($vmid);
my $storecfg = PVE::Storage::config();
@ -130,6 +141,9 @@ __PACKAGE__->register_method({
}
PVE::QemuServer::add_random_macs($param);
} else {
my $keystr = join(' ', keys %$param);
raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
}
# fixme: archive eq '-' (read from stdin)
@ -138,7 +152,7 @@ __PACKAGE__->register_method({
if (-f $filename) {
die "unable to restore vm $vmid: config file already exists\n"
if !$param->{force};
if !$force;
die "unable to restore vm $vmid: vm is running\n"
if PVE::QemuServer::check_running($vmid);
@ -148,7 +162,9 @@ __PACKAGE__->register_method({
}
my $realcmd = sub {
PVE::QemuServer::restore_archive($archive, $vmid, { storage => $storage});
PVE::QemuServer::restore_archive($archive, $vmid, {
storage => $storage,
unique => $unique });
};
return $rpcenv->fork_worker('qmrestore', $vmid, $user, $realcmd);

View File

@ -3030,18 +3030,35 @@ sub restore_archive {
my $outfd = new IO::File ($tmpfn, "w") ||
die "unable to write config for VM $vmid\n";
my $netcount = 0;
while (defined (my $line = <$srcfd>)) {
next if $line =~ m/^\#vzdump\#/;
next if $line =~ m/^lock:/;
next if $line =~ m/^unused\d+:/;
if (($line =~ m/^((vlan)\d+):(.*)$/) && ($opts->{unique})) {
my ($id,$ethcfg) = ($1,$3);
$ethcfg =~ s/^\s+//;
my ($model, $mac) = split(/\=/,$ethcfg);
my $printvlan = PVE::QemuServer::print_vlan(PVE::QemuServer::parse_vlan($model));
print $outfd "$id: $printvlan\n";
} elsif ($line =~ m/^((ide|scsi|virtio)\d+):(.*)$/) {
if (($line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/)) {
# try to convert old 1.X settings
my ($id, $ind, $ethcfg) = ($1, $2, $3);
foreach my $devconfig (PVE::Tools::split_list($ethcfg)) {
my ($model, $macaddr) = split(/\=/, $devconfig);
$macaddr = PVE::Tools::random_ether_addr() if !$macaddr || $opts->{unique};
my $net = {
model => $model,
bridge => "vmbr$ind",
macaddr => $macaddr,
};
my $netstr = print_net($net);
print $outfd "net${netcount}: $netstr\n";
$netcount++;
}
} elsif (($line =~ m/^(net\d+):\s*(\S+)\s*$/) && ($opts->{unique})) {
my ($id, $netstr) = ($1, $2);
my $net = parse_net($netstr);
$net->{macaddr} = PVE::Tools::random_ether_addr() if $net->{macaddr};
$netstr = print_net($net);
print $outfd "$id: $netstr\n";
} elsif ($line =~ m/^((ide|scsi|virtio)\d+):\s*(\S+)\s*$/) {
my $virtdev = $1;
my $value = $2;
if ($line =~ m/backup=no/) {

View File

@ -50,6 +50,11 @@ __PACKAGE__->register_method({
type => 'boolean',
description => "Allow to overwrite existing VM.",
},
unique => {
optional => 1,
type => 'boolean',
description => "Assign a unique random ethernet address.",
},
},
},
returns => {