]> git.proxmox.com Git - qemu-server.git/commitdiff
implement unique option for restore
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 18 Oct 2011 07:14:05 +0000 (09:14 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 18 Oct 2011 07:14:05 +0000 (09:14 +0200)
Also try to convert old vlanX to new netX syntax.

PVE/API2/Qemu.pm
PVE/QemuServer.pm
qmrestore

index eaaa7533c7dcd16f01c324226160f5451070cb27..3d6304e115298d4bdd18d805ea7ea577e27a513d 100644 (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);
index 8afdc38972a976b07d0eaaaf1cfcc3fdf669c5bf..2377db507e5cf2faaaca3e94b5873094dab6a3e0 100644 (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/) {
index f256b39fa109f752ea55c27e19bd703650123541..f389a24e5f9079abd3342683d72c09ee71b3145e 100755 (executable)
--- a/qmrestore
+++ b/qmrestore
@@ -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 => {