[pve-devel] [PATCH container 2/3] split up create_rootfs and restore_and_configure
Fabian Grünbichler
f.gruenbichler at proxmox.com
Fri Jun 3 09:56:16 CEST 2016
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
---
Changes to previous version:
- destroy old container volumes and configuration also moved to restore API
src/PVE/API2/LXC.pm | 28 ++++++++++--
src/PVE/LXC/Create.pm | 117 ++++++++++++++++++--------------------------------
2 files changed, 66 insertions(+), 79 deletions(-)
diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 7ed778b..be765d8 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -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;
diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
index 123ca81..222551d 100644
--- a/src/PVE/LXC/Create.pm
+++ b/src/PVE/LXC/Create.pm
@@ -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;
--
2.1.4
More information about the pve-devel
mailing list