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
this also uses the new ignore_ro flag for mount_all() --- src/PVE/API2/LXC.pm | 19 +++++++-- src/PVE/LXC/Create.pm | 107 +++++++++++++++++++++----------------------------- 2 files changed, 60 insertions(+), 66 deletions(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index d2b03ea..a59152c 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -344,10 +344,23 @@ __PACKAGE__->register_method({ $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf); + PVE::LXC::Create::write_config_file($storage_cfg, $vmid, $conf, $restore); + eval { + my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1); + PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors); - PVE::LXC::Create::create_rootfs($storage_cfg, $vmid, $conf, - $archive, $password, $restore, - $ignore_unpack_errors, $ssh_keys); + 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; diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm index 560d5b6..60470b1 100644 --- a/src/PVE/LXC/Create.pm +++ b/src/PVE/LXC/Create.pm @@ -138,66 +138,57 @@ sub recover_config { return wantarray ? ($conf, $mp_param, $delayed_mp_param) : $conf; } -sub restore_and_configure { - my ($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error, $ssh_keys) = @_; +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); + + 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; + } - restore_archive($archive, $rootdir, $conf, $no_unpack_error); + } 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"; - if (!$restore) { 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); - 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); - - } else { - print "###########################################################\n"; - print "Backup archive does not contain any configuration\n"; - print "###########################################################\n"; - } + print "###########################################################\n"; + print "Backup archive does not contain any configuration\n"; + print "###########################################################\n"; } } -sub create_rootfs { - my ($storage_cfg, $vmid, $conf, $archive, $password, $restore, $no_unpack_error, $ssh_keys) = @_; +sub write_config_file { + my ($storage_cfg, $vmid, $conf, $restore) = @_; my $config_fn = PVE::LXC::Config->config_file($vmid); if (-f $config_fn) { @@ -205,21 +196,11 @@ sub create_rootfs { my $old_conf = PVE::LXC::Config->load_config($vmid); - # destroy old container volume + # 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); - 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; -- 2.1.4 _______________________________________________ pve-devel mailing list pve-devel@pve.proxmox.com http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel