]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC/Create.pm
supress warning when we try to delete a non-existent option
[pve-container.git] / src / PVE / LXC / Create.pm
index ebd81cc344aa5d1a2aaba1a4d94bdde579dd1740..11bc00d9e9d6864dda190b0f5f9e5bc1066ae1f8 100644 (file)
@@ -10,6 +10,7 @@ use PVE::Storage;
 use PVE::LXC;
 use PVE::LXC::Setup;
 use PVE::VZDump::ConvertOVZ;
+use PVE::Tools;
 
 sub next_free_nbd_dev {
     
@@ -23,32 +24,37 @@ sub next_free_nbd_dev {
 }
 
 sub restore_archive {
-    my ($archive, $rootdir, $conf) = @_;
+    my ($archive, $rootdir, $conf, $no_unpack_error) = @_;
 
-    my $userns_cmd = [];
+    my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
+    my $userns_cmd = PVE::LXC::userns_command($id_map);
 
-#    we always use the same mapping: 'b:0:100000:65536'
-#    if ($conf->{'lxc.id_map'}) {
-#      $userns_cmd = ['lxc-usernsexec', '-m', 'b:0:100000:65536', '--'];
-#      PVE::Tools::run_command(['chown', '-R', '100000:100000', $rootdir]);
-#    }
-
-    my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--numeric-owner', '--totals',
-           '--sparse', '-C', $rootdir];
+    my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
+               @$PVE::LXC::COMMON_TAR_FLAGS,
+               '-C', $rootdir];
 
+    # skip-old-files doesn't have anything to do with time (old/new), but is
+    # simply -k (annoyingly also called --keep-old-files) without the 'treat
+    # existing files as errors' part... iow. it's bsdtar's interpretation of -k
+    # *sigh*, gnu...
+    push @$cmd, '--skip-old-files';
     push @$cmd, '--anchored';
     push @$cmd, '--exclude' , './dev/*';
 
     if ($archive eq '-') {
        print "extracting archive from STDIN\n";
-       PVE::Tools::run_command($cmd, input => "<&STDIN");
+       eval { PVE::Tools::run_command($cmd, input => "<&STDIN"); };
     } else {
        print "extracting archive '$archive'\n";
-       PVE::Tools::run_command($cmd);
+       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/usr/bin/file"];
+    $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/bin/sh"];
     PVE::Tools::run_command($cmd, outfunc => sub {
        shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
        my $arch_str = $1;
@@ -58,186 +64,100 @@ sub restore_archive {
            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_mountpoint($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) = @_;
-
-    restore_archive($archive, $rootdir, $conf);
-
-    if (!$restore) {
-       my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
-
-       PVE::LXC::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 $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';
-               $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
-           }
-           
-       } 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});
+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;
            }
-
-       } 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) = @_;
-
-    my $config_fn = PVE::LXC::config_file($vmid);
-    if (-f $config_fn) {
-       die "container exists" if !$restore; # just to be sure
-
-       my $old_conf = PVE::LXC::load_config($vmid);
-       
-       # destroy old container volume
-       PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
+       unlink($pct_cfg_fn);
 
-       # do not copy all settings to restored container
-       foreach my $opt (qw(rootfs digest snapshots arch ostype)) {
-           delete $old_conf->{$opt};
+       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;
        }
-       foreach my $opt (keys %$old_conf) {
-           delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
-       }
-
-       PVE::LXC::update_pct_config($vmid, $conf, 0, $old_conf);
-
-       PVE::LXC::create_config($vmid, $conf);
-
-    } else {
-       
-       PVE::LXC::create_config($vmid, $conf);
-    }
 
-    my $mountpoint = PVE::LXC::parse_ct_mountpoint($conf->{rootfs});
-    $mountpoint->{mp} = '/';
+    } 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 $volid = $mountpoint->{volume};
-
-    my $image_path = PVE::Storage::path($storage_cfg, $volid);
-    my $mountpoint_path = "/var/lib/lxc/$vmid/rootfs";
-
-    eval {
-        PVE::Storage::activate_volumes($storage_cfg, [$volid]);
-        my $loopdevs = PVE::LXC::attach_loops($storage_cfg, [$volid]);
-       if (!-d $image_path) {
-           my $cmd = ['mkfs.ext4', $image_path];
-           PVE::Tools::run_command($cmd);
+       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::mountpoint_mount($mountpoint, $mountpoint_path, $storage_cfg, $loopdevs);
-
-        restore_and_configure($vmid, $archive, $mountpoint_path, $conf, $password, $restore);
-    };
-    if (my $err = $@) {
-       eval {
-            PVE::LXC::dettach_loops($storage_cfg, [$volid]);
-            PVE::Storage::deactivate_volumes($storage_cfg, [$volid]);
-       };
-       warn $@ if $@;
-        die $err;
+    } else {
+       print "###########################################################\n";
+       print "Backup archive does not contain any configuration\n";
+       print "###########################################################\n";
     }
-
-    PVE::Tools::run_command(['umount', '-l', '-d', $mountpoint_path]);
-    PVE::LXC::dettach_loops($storage_cfg, [$volid]);
-    PVE::Storage::deactivate_volumes($storage_cfg, [$volid]);
-
 }
 
 1;