eval { PVE::Tools::run_command($cmd); };
}
die $@ if $@ && !$no_unpack_error;
-
+
+ # if arch is set, we do not try to autodetect it
+ return if defined($conf->{arch});
+
# determine file type of /usr/bin/file itself to get guests' architecture
$cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/bin/sh"];
PVE::Tools::run_command($cmd, outfunc => sub {
print "Detected container architecture: $conf->{'arch'}\n";
} else {
print "CT architecture detection failed, falling back to amd64.\n" .
- "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
+ "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}.conf " .
"to set another architecture.\n";
}
});
}
-sub tar_archive_search_conf {
- my ($archive) = @_;
-
- die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
-
- my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
- die "unable to open file '$archive'\n";
-
- my $file;
- while (defined($file = <$fh>)) {
- if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
- $file = $1; # untaint
- last;
- }
- }
-
- kill 15, $pid;
- waitpid $pid, 0;
- close $fh;
-
- die "ERROR: archive contains no configuration file\n" if !$file;
- chomp $file;
-
- return $file;
-}
-
sub recover_config {
my ($archive) = @_;
- my $conf_file = tar_archive_search_conf($archive);
-
- my $raw = '';
- my $out = sub {
- my $output = shift;
- $raw .= "$output\n";
- };
-
- PVE::Tools::run_command(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc => $out);
-
+ my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
my $conf;
- my $disksize;
+ my $mp_param = {};
if ($conf_file =~ m/pct\.conf/) {
- $conf = PVE::LXC::parse_pct_config("/lxc/0.conf" , $raw);
+ $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
delete $conf->{snapshots};
delete $conf->{template}; # restored CT is never a template
-
- if (defined($conf->{rootfs})) {
- my $rootinfo = PVE::LXC::parse_ct_rootfs($conf->{rootfs});
- $disksize = $rootinfo->{size} if defined($rootinfo->{size});
- }
-
+
+ PVE::LXC::Config->foreach_mountpoint($conf, sub {
+ my ($ms, $mountpoint) = @_;
+ $mp_param->{$ms} = $conf->{$ms};
+ });
+
} elsif ($conf_file =~ m/vps\.conf/) {
-
- ($conf, $disksize) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
-
+
+ ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
+
} else {
die "internal error";
}
- return wantarray ? ($conf, $disksize) : $conf;
+ return wantarray ? ($conf, $mp_param) : $conf;
}
-sub restore_and_configure {
- my ($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error) = @_;
-
- 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);
- } 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::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';
- next if $key =~ /^mp\d+$/; # don't recover mountpoints
- $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
+sub restore_configuration {
+ my ($vmid, $rootdir, $conf, $restricted) = @_;
+
+ # 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 'parent';
+ next if $key =~ /^mp\d+$/; # don't recover mountpoints
+ next if $key =~ /^unused\d+$/; # don't recover unused disks
+ if ($restricted && $key eq 'lxc') {
+ warn "skipping custom lxc options, restore manually as root:\n";
+ warn "--------------------------------\n";
+ my $lxc_list = $oldconf->{'lxc'};
+ foreach my $lxc_opt (@$lxc_list) {
+ warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
+ }
+ warn "--------------------------------\n";
+ next;
}
- 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";
+ $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
}
- }
-}
-
-sub create_rootfs {
- my ($storage_cfg, $vmid, $conf, $archive, $password, $restore, $no_unpack_error) = @_;
-
- my $config_fn = PVE::LXC::Config->config_file($vmid);
- if (-f $config_fn) {
- die "container exists" if !$restore; # just to be sure
+ unlink($pct_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);
-
- # do not copy all settings to restored container
- foreach my $opt (qw(rootfs digest snapshots arch ostype unprivileged parent)) {
- delete $old_conf->{$opt};
- }
- foreach my $opt (keys %$old_conf) {
- delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
+ 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;
}
- PVE::LXC::update_pct_config($vmid, $conf, 0, $old_conf);
+ } 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";
- PVE::LXC::Config->write_config($vmid, $conf);
+ 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 {
-
- PVE::LXC::Config->write_config($vmid, $conf);
+ print "###########################################################\n";
+ print "Backup archive does not contain any configuration\n";
+ print "###########################################################\n";
}
-
- eval {
- my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf);
- restore_and_configure($vmid, $archive, $rootdir, $conf, $password, $restore, $no_unpack_error);
- };
- my $err = $@;
- PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
- PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::get_vm_volumes($conf));
- die $err if $err;
}
1;