]> git.proxmox.com Git - pve-container.git/commitdiff
split up create_rootfs and restore_and_configure
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 3 Jun 2016 07:56:16 +0000 (09:56 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 3 Jun 2016 08:04:59 +0000 (10:04 +0200)
these were only used once and their method signatures were
already quite long, so split up into
- delete old existing container and write new config
- mount
- restore archive / extract template
- restore configuration / setup new container
- unmount

src/PVE/API2/LXC.pm
src/PVE/LXC/Create.pm

index 7ed778b3af99160484f0ce55b7669568654d814d..be765d89647f2a88208e56972de9e4795d4610c9 100644 (file)
@@ -355,10 +355,32 @@ __PACKAGE__->register_method({
 
                $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
 
+               my $config_fn = PVE::LXC::Config->config_file($vmid);
+               if (-f $config_fn) {
+                   die "container exists" if !$restore; # just to be sure
+                       my $old_conf = PVE::LXC::Config->load_config($vmid);
 
-               PVE::LXC::Create::create_rootfs($storage_cfg, $vmid, $conf,
-                                               $archive, $password, $restore,
-                                               $ignore_unpack_errors, $ssh_keys);
+                   # destroy old container volumes
+                   PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
+               }
+               PVE::LXC::Config->write_config($vmid, $conf);
+
+               eval {
+                   my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
+                   PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors);
+
+                   if ($restore) {
+                       PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf);
+                   } else {
+                       my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
+                       PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
+                       $lxc_setup->post_create_hook($password, $ssh_keys);
+                   }
+               };
+               my $err = $@;
+               PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
+               PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf));
+               die $err if $err;
                # set some defaults
                $conf->{hostname} ||= "CT$vmid";
                $conf->{memory} ||= 512;
index 123ca815f5c0e8e6c21e55b5270e1db31939506c..222551ddc07875fef5524873ca257263ae1493cf 100644 (file)
@@ -133,88 +133,53 @@ sub recover_config {
     return wantarray ? ($conf, $mp_param) : $conf;
 }
 
-sub restore_and_configure {
-    my ($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error, $ssh_keys) = @_;
-
-    restore_archive($archive, $rootdir, $conf, $no_unpack_error);
-
-    if (!$restore) {
-       my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
-
-       PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
-       $lxc_setup->post_create_hook($password, $ssh_keys);
-    } else {
-       # restore: try to extract configuration from archive
-
-       my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
-       my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
-       my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
-       if (-f $pct_cfg_fn) {
-           my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
-           my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
-
-           foreach my $key (keys %$oldconf) {
-               next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
-               next if $key =~ /^mp\d+$/; # don't recover mountpoints
-               next if $key =~ /^unused\d+$/; # don't recover unused disks
-               $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
-           }
-           unlink($pct_cfg_fn);
-
-           if (-f $pct_fwcfg_fn) {
-               my $pve_firewall_dir = '/etc/pve/firewall';
-               mkdir $pve_firewall_dir; # make sure the directory exists
-               PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
-               unlink $pct_fwcfg_fn;
-           }
-
-       } elsif (-f $ovz_cfg_fn) {
-           print "###########################################################\n";
-           print "Converting OpenVZ configuration to LXC.\n";
-           print "Please check the configuration and reconfigure the network.\n";
-           print "###########################################################\n";
-
-           my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
-           $conf->{ostype} = $lxc_setup->{conf}->{ostype};
-           my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
-           my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
-           foreach my $key (keys %$oldconf) {
-               $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
-           }
-           unlink($ovz_cfg_fn);
+sub restore_configuration {
+    my ($vmid, $rootdir, $conf) = @_;
+
+    # restore: try to extract configuration from archive
+
+    my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
+    my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
+    my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
+    if (-f $pct_cfg_fn) {
+       my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
+       my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
+
+       foreach my $key (keys %$oldconf) {
+           next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
+           next if $key =~ /^mp\d+$/; # don't recover mountpoints
+           next if $key =~ /^unused\d+$/; # don't recover unused disks
+           $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
+       }
+       unlink($pct_cfg_fn);
 
-       } else {
-           print "###########################################################\n";
-           print "Backup archive does not contain any configuration\n";
-           print "###########################################################\n";
+       if (-f $pct_fwcfg_fn) {
+           my $pve_firewall_dir = '/etc/pve/firewall';
+           mkdir $pve_firewall_dir; # make sure the directory exists
+           PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
+           unlink $pct_fwcfg_fn;
        }
-    }
-}
 
-sub create_rootfs {
-    my ($storage_cfg, $vmid, $conf, $archive, $password, $restore, $no_unpack_error, $ssh_keys) = @_;
+    } elsif (-f $ovz_cfg_fn) {
+       print "###########################################################\n";
+       print "Converting OpenVZ configuration to LXC.\n";
+       print "Please check the configuration and reconfigure the network.\n";
+       print "###########################################################\n";
 
-    my $config_fn = PVE::LXC::Config->config_file($vmid);
-    if (-f $config_fn) {
-       die "container exists" if !$restore; # just to be sure
+       my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
+       $conf->{ostype} = $lxc_setup->{conf}->{ostype};
+       my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
+       my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
+       foreach my $key (keys %$oldconf) {
+           $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
+       }
+       unlink($ovz_cfg_fn);
 
-       my $old_conf = PVE::LXC::Config->load_config($vmid);
-       
-       # destroy old container volume
-       PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
+    } else {
+       print "###########################################################\n";
+       print "Backup archive does not contain any configuration\n";
+       print "###########################################################\n";
     }
-
-    PVE::LXC::Config->write_config($vmid, $conf);
-
-    eval {
-       my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
-        restore_and_configure($vmid, $archive, $rootdir, $conf, $password,
-                             $restore, $no_unpack_error, $ssh_keys);
-    };
-    my $err = $@;
-    PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
-    PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf));
-    die $err if $err;
 }
 
 1;