]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC.pm
Use run_command instead of system to stop CT
[pve-container.git] / src / PVE / LXC.pm
index b393d101090ecb75f16ac09dd6cf686d191075c6..4ba91257958858ca5f5ce83968c40333b44b0fd7 100644 (file)
@@ -12,6 +12,7 @@ use Cwd qw();
 use Fcntl qw(O_RDONLY);
 
 use PVE::Cluster qw(cfs_register_file cfs_read_file);
+use PVE::Exception qw(raise_perm_exc);
 use PVE::Storage;
 use PVE::SafeSyslog;
 use PVE::INotify;
@@ -119,8 +120,8 @@ my $confdesc = {
     ostype => {
        optional => 1,
        type => 'string',
-       enum => ['debian', 'ubuntu', 'centos', 'fedora', 'opensuse', 'archlinux', 'alpine'],
-       description => "OS type. Corresponds to lxc setup scripts in /usr/share/lxc/config/<ostype>.common.conf.",
+       enum => ['debian', 'ubuntu', 'centos', 'fedora', 'opensuse', 'archlinux', 'alpine', 'unmanaged'],
+       description => "OS type. This is used to setup configuration inside the container, and corresponds to lxc setup scripts in /usr/share/lxc/config/<ostype>.common.conf. Value 'unmanaged' can be used to skip and OS specific setup.",
     },
     console => {
        optional => 1,
@@ -1082,7 +1083,7 @@ sub update_lxc_config {
     my $custom_idmap = grep { $_->[0] eq 'lxc.id_map' } @{$conf->{lxc}};
 
     my $ostype = $conf->{ostype} || die "missing 'ostype' - internal error";
-    if ($ostype =~ /^(?:debian | ubuntu | centos | fedora | opensuse | archlinux | alpine)$/x) {
+    if ($ostype =~ /^(?:debian | ubuntu | centos | fedora | opensuse | archlinux | alpine | unmanaged)$/x) {
        my $inc ="/usr/share/lxc/config/$ostype.common.conf";
        $inc ="/usr/share/lxc/config/common.conf" if !-f $inc;
        $raw .= "lxc.include = $inc\n";
@@ -1331,6 +1332,8 @@ sub update_pct_config {
        write_config($vmid, $conf) if $running;
     }
 
+    my $used_volids = {};
+
     foreach my $opt (keys %$param) {
        my $value = $param->{$opt};
        if ($opt eq 'hostname') {
@@ -1380,6 +1383,8 @@ sub update_pct_config {
                }
            }
            $new_disks = 1;
+           my $mp = parse_ct_mountpoint($value);
+           $used_volids->{$mp->{volume}} = 1;
         } elsif ($opt eq 'rootfs') {
            next if $hotplug_error->($opt);
            check_protection($conf, "can't update CT $vmid drive '$opt'");
@@ -1391,17 +1396,35 @@ sub update_pct_config {
                    add_unused_volume($conf, $mp->{volume});
                }
            }
+           my $mp = parse_ct_rootfs($value);
+           $used_volids->{$mp->{volume}} = 1;
        } elsif ($opt eq 'unprivileged') {
            die "unable to modify read-only option: '$opt'\n";
+       } elsif ($opt eq 'ostype') {
+           next if $hotplug_error->($opt);
+           $conf->{$opt} = $value;
        } else {
            die "implement me: $opt";
        }
        write_config($vmid, $conf) if $running;
     }
 
+    # Cleanup config:
+
+    # Remove unused disks after re-adding
+    foreach my $key (keys %$conf) {
+       next if $key !~ /^unused\d+/;
+       my $volid = $conf->{$key};
+       if ($used_volids->{$volid}) {
+           delete $conf->{$key};
+       }
+    }
+
+    # Apply deletions and creations of new volumes
     if (@deleted_volumes) {
        my $storage_cfg = PVE::Storage::config();
        foreach my $volume (@deleted_volumes) {
+           next if $used_volids->{$volume}; # could have been re-added, too
            delete_mountpoint_volume($storage_cfg, $vmid, $volume);
        }
     }
@@ -1985,36 +2008,11 @@ sub snapshot_create {
 sub snapshot_delete {
     my ($vmid, $snapname, $force, $drivehash) = @_;
 
-    my $snap;
-
-    my $conf;
-
-    my $updatefn =  sub {
-
-       $conf = load_config($vmid);
-
-       die "you can't delete a snapshot if vm is a template\n"
-           if is_template($conf);
-
-       $snap = $conf->{snapshots}->{$snapname};
-
-       if (!$drivehash) {
-           check_lock($conf);
-       }
-
-       die "snapshot '$snapname' does not exist\n" if !defined($snap);
-
-       $snap->{snapstate} = 'delete';
-
-       write_config($vmid, $conf);
-    };
+    my $prepare = 1;
 
-    lock_config($vmid, $updatefn);
-
-    my $storecfg = PVE::Storage::config();
+    my $snap;
 
     my $unlink_parent = sub {
-
        my ($confref, $new_parent) = @_;
 
        if ($confref->{parent} && $confref->{parent} eq $snapname) {
@@ -2026,58 +2024,99 @@ sub snapshot_delete {
        }
     };
 
-    my $del_snap =  sub {
+    my $updatefn =  sub {
+       my ($remove_drive) = @_;
 
-       $conf = load_config($vmid);
+       my $conf = load_config($vmid);
 
-       if ($drivehash) {
-           delete $conf->{lock};
-       } else {
+       if (!$drivehash) {
            check_lock($conf);
+           die "you can't delete a snapshot if vm is a template\n"
+               if is_template($conf);
        }
 
-       my $parent = $conf->{snapshots}->{$snapname}->{parent};
-       foreach my $snapkey (keys %{$conf->{snapshots}}) {
-           &$unlink_parent($conf->{snapshots}->{$snapkey}, $parent);
+       $snap = $conf->{snapshots}->{$snapname};
+
+       die "snapshot '$snapname' does not exist\n" if !defined($snap);
+
+       # remove parent refs
+       if (!$prepare) {
+           &$unlink_parent($conf, $snap->{parent});
+           foreach my $sn (keys %{$conf->{snapshots}}) {
+               next if $sn eq $snapname;
+               &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
+           }
        }
 
-       &$unlink_parent($conf, $parent);
+       if ($remove_drive) {
+           if ($remove_drive eq 'vmstate') {
+               die "implement me - saving vmstate\n";
+           } else {
+               die "implement me - remove drive\n";
+           }
+       }
 
-       delete $conf->{snapshots}->{$snapname};
+       if ($prepare) {
+           $snap->{snapstate} = 'delete';
+       } else {
+           delete $conf->{snapshots}->{$snapname};
+           delete $conf->{lock} if $drivehash;
+       }
 
        write_config($vmid, $conf);
     };
 
-    my $rootfs = $conf->{snapshots}->{$snapname}->{rootfs};
-    my $rootinfo = parse_ct_rootfs($rootfs);
-    my $volid = $rootinfo->{volume};
+    lock_config($vmid, $updatefn);
+
+    # now remove vmstate file
+    # never set for LXC!
+    my $storecfg = PVE::Storage::config();
+
+    if ($snap->{vmstate}) {
+       die "implement me - saving vmstate\n";
+    };
 
+    # now remove all volume snapshots
+    # only rootfs for now!
     eval {
+       my $rootfs = $snap->{rootfs};
+       my $rootinfo = parse_ct_rootfs($rootfs);
+       my $volid = $rootinfo->{volume};
        PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snapname);
     };
-    my $err = $@;
-
-    if(!$err || ($err && $force)) {
-       lock_config($vmid, $del_snap);
-       if ($err) {
-           die "Can't delete snapshot: $vmid $snapname $err\n";
-       }
+    if (my $err = $@) {
+       die $err if !$force;
+       warn $err;
     }
+
+    # now cleanup config
+    $prepare = 0;
+    lock_config($vmid, $updatefn);
 }
 
 sub snapshot_rollback {
     my ($vmid, $snapname) = @_;
 
+    my $prepare = 1;
+
     my $storecfg = PVE::Storage::config();
 
     my $conf = load_config($vmid);
 
-    die "you can't rollback if vm is a template\n" if is_template($conf);
+    my $get_snapshot_config = sub {
+
+       die "you can't rollback if vm is a template\n" if is_template($conf);
+
+       my $res = $conf->{snapshots}->{$snapname};
+
+       die "snapshot '$snapname' does not exist\n" if !defined($res);
 
-    my $snap = $conf->{snapshots}->{$snapname};
+       return $res;
+    };
 
-    die "snapshot '$snapname' does not exist\n" if !defined($snap);
+    my $snap = &$get_snapshot_config();
 
+    # only for rootfs for now!
     my $rootfs = $snap->{rootfs};
     my $rootinfo = parse_ct_rootfs($rootfs);
     my $volid = $rootinfo->{volume};
@@ -2086,42 +2125,51 @@ sub snapshot_rollback {
 
     my $updatefn = sub {
 
-       die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n" 
-           if $snap->{snapstate};
+       $conf = load_config($vmid);
 
-       check_lock($conf);
+       $snap = &$get_snapshot_config();
 
-       system("lxc-stop -n $vmid --kill") if check_running($vmid);
+       die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
+           if $snap->{snapstate};
+
+       if ($prepare) {
+           check_lock($conf);
+           PVE::Tools::run_command(['/usr/bin/lxc-stop', '-n', $vmid, '--kill'])
+               if check_running($vmid);
+       }
 
        die "unable to rollback vm $vmid: vm is running\n"
            if check_running($vmid);
 
-       $conf->{lock} = 'rollback';
+       if ($prepare) {
+           $conf->{lock} = 'rollback';
+       } else {
+           die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
+           delete $conf->{lock};
+       }
 
        my $forcemachine;
 
-       # copy snapshot config to current config
-
-       my $tmp_conf = $conf;
-       &$snapshot_copy_config($tmp_conf->{snapshots}->{$snapname}, $conf);
-       $conf->{snapshots} = $tmp_conf->{snapshots};
-       delete $conf->{snaptime};
-       delete $conf->{snapname};
-       $conf->{parent} = $snapname;
+       if (!$prepare) {
+           # copy snapshot config to current config
+           $conf = &$snapshot_apply_config($conf, $snap);
+           $conf->{parent} = $snapname;
+       }
 
        write_config($vmid, $conf);
-    };
 
-    my $unlockfn = sub {
-       delete $conf->{lock};
-       write_config($vmid, $conf);
+       if (!$prepare && $snap->{vmstate}) {
+           die "implement me - save vmstate\n";
+       }
     };
 
     lock_config($vmid, $updatefn);
 
+    # only rootfs for now!
     PVE::Storage::volume_snapshot_rollback($storecfg, $volid, $snapname);
 
-    lock_config($vmid, $unlockfn);
+    $prepare = 0;
+    lock_config($vmid, $updatefn);
 }
 
 sub template_create {
@@ -2189,16 +2237,20 @@ sub foreach_mountpoint_reverse {
 }
 
 sub check_ct_modify_config_perm {
-    my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
-
-    return 1 if $authuser ne 'root@pam';
+    my ($rpcenv, $authuser, $vmid, $pool, $newconf, $delete) = @_;
 
-    foreach my $opt (@$key_list) {
+    return 1 if $authuser eq 'root@pam';
 
+    my $check = sub {
+       my ($opt, $delete) = @_;
        if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
        } elsif ($opt eq 'rootfs' || $opt =~ /^mp\d+$/) {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
+           return if $delete;
+           my $data = $opt eq 'rootfs' ? parse_ct_rootfs($newconf->{$opt})
+                                       : parse_ct_mountpoint($newconf->{$opt});
+           raise_perm_exc("mountpoint type $data->{type}") if $data->{type} ne 'volume';
        } elsif ($opt eq 'memory' || $opt eq 'swap') {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
        } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
@@ -2207,6 +2259,13 @@ sub check_ct_modify_config_perm {
        } else {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
        }
+    };
+
+    foreach my $opt (keys %$newconf) {
+       &$check($opt, 0);
+    }
+    foreach my $opt (@$delete) {
+       &$check($opt, 1);
     }
 
     return 1;