]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/CLI/pct.pm
push/pull permissions: use octal by default
[pve-container.git] / src / PVE / CLI / pct.pm
index befe1971d32fcb8b667bef7a07798317c33c2f73..fb2ec709fde4ed7d8207ff76255ffb1f7bf3cb43 100755 (executable)
@@ -6,6 +6,7 @@ use warnings;
 use POSIX;
 use Fcntl;
 use File::Copy 'copy';
+use Term::ReadLine;
 
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
@@ -31,6 +32,26 @@ my $upid_exit = sub {
     exit($status eq 'OK' ? 0 : -1);
 };
 
+sub read_password {
+    my $term = new Term::ReadLine ('pct');
+    my $attribs = $term->Attribs;
+    $attribs->{redisplay_function} = $attribs->{shadow_redisplay};
+    my $input = $term->readline('Enter password: ');
+    my $conf = $term->readline('Retype password: ');
+    die "Passwords do not match.\n" if ($input ne $conf);
+    return $input;
+}
+
+sub string_param_file_mapping {
+    my ($name) = @_;
+
+    my $mapping = {
+       'create_vm' => ['ssh-public-keys'],
+    };
+
+    return defined($mapping->{$name}) ? $mapping->{$name} : [];
+}
+
 __PACKAGE__->register_method ({
     name => 'unlock',
     path => 'unlock',
@@ -48,11 +69,7 @@ __PACKAGE__->register_method ({
 
        my $vmid = $param->{vmid};
 
-       PVE::LXC::lock_config($vmid, sub {
-           my $conf = PVE::LXC::load_config($vmid);
-           delete $conf->{lock};
-           PVE::LXC::write_config($vmid, $conf);
-       });
+       PVE::LXC::Config->remove_lock($vmid);
 
        return undef;
     }});
@@ -74,7 +91,7 @@ __PACKAGE__->register_method ({
        my ($param) = @_;
 
        # test if container exists on this node
-       my $conf = PVE::LXC::load_config($param->{vmid});
+       my $conf = PVE::LXC::Config->load_config($param->{vmid});
 
        my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf);
        exec(@$cmd);
@@ -99,7 +116,7 @@ __PACKAGE__->register_method ({
        my $vmid = $param->{vmid};
 
        # test if container exists on this node
-       PVE::LXC::load_config($vmid);
+       PVE::LXC::Config->load_config($vmid);
 
        die "Error: container '$vmid' not running!\n" if !PVE::LXC::check_running($vmid);
 
@@ -124,7 +141,7 @@ __PACKAGE__->register_method ({
        my ($param) = @_;
 
        # test if container exists on this node
-       PVE::LXC::load_config($param->{vmid});
+       PVE::LXC::Config->load_config($param->{vmid});
 
        if (!@{$param->{'extra-args'}}) {
            die "missing command";
@@ -151,7 +168,7 @@ __PACKAGE__->register_method ({
                optional => 1,
                type => 'string',
                description => "A volume on which to run the filesystem check",
-               enum => [PVE::LXC::mountpoint_names()],
+               enum => [PVE::LXC::Config->mountpoint_names()],
            },
        },
     },
@@ -168,13 +185,13 @@ __PACKAGE__->register_method ({
        # critical path: all of this will be done while the container is locked
        my $do_fsck = sub {
 
-           my $conf = PVE::LXC::load_config($vmid);
+           my $conf = PVE::LXC::Config->load_config($vmid);
            my $storage_cfg = PVE::Storage::config();
 
            defined($conf->{$device}) || die "cannot run command on unexisting mountpoint $device\n";
 
-           my $mount_point = $device eq 'rootfs' ? PVE::LXC::parse_ct_rootfs($conf->{$device}) :
-               PVE::LXC::parse_ct_mountpoint($conf->{$device});
+           my $mount_point = $device eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$device}) :
+               PVE::LXC::Config->parse_ct_mountpoint($conf->{$device});
 
            my $volid = $mount_point->{volume};
 
@@ -207,7 +224,62 @@ __PACKAGE__->register_method ({
            PVE::Tools::run_command($command);
        };
 
-       PVE::LXC::lock_config($vmid, $do_fsck);
+       PVE::LXC::Config->lock_config($vmid, $do_fsck);
+       return undef;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'mount',
+    path => 'mount',
+    method => 'POST',
+    description => "Mount the container's filesystem on the host. " .
+                  "This will hold a lock on the container and is meant for emergency maintenance only " .
+                  "as it will prevent further operations on the container other than start and stop.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $vmid = extract_param($param, 'vmid');
+       my $storecfg = PVE::Storage::config();
+       PVE::LXC::Config->lock_config($vmid, sub {
+           my $conf = PVE::LXC::Config->set_lock($vmid, 'mounted');
+           PVE::LXC::mount_all($vmid, $storecfg, $conf);
+       });
+       return undef;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'unmount',
+    path => 'unmount',
+    method => 'POST',
+    description => "Unmount the container's filesystem.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $vmid = extract_param($param, 'vmid');
+       my $storecfg = PVE::Storage::config();
+       PVE::LXC::Config->lock_config($vmid, sub {
+           my $conf = PVE::LXC::Config->load_config($vmid);
+           PVE::LXC::umount_all($vmid, $storecfg, $conf, 0);
+           PVE::LXC::Config->remove_lock($vmid, 'mounted');
+       });
        return undef;
     }});
 
@@ -297,7 +369,7 @@ __PACKAGE__->register_method({
            },
            perms => {
                type => 'string',
-               description => 'File permissions to use.',
+               description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
                optional => 1,
            },
        },
@@ -316,6 +388,7 @@ __PACKAGE__->register_method({
        my $dest = extract_param($param, 'destination');
 
        my $perms = extract_param($param, 'perms');
+       $perms = '0'.$perms if defined($perms) && $perms !~m/^0/;
        my $user = extract_param($param, 'user');
        my $group = extract_param($param, 'group');
 
@@ -346,7 +419,7 @@ __PACKAGE__->register_method({
            return $rpcenv->fork_worker('pull_file', $vmid, undef, $realcmd);
        };
 
-       return PVE::LXC::lock_config($vmid, $code);
+       return PVE::LXC::Config->lock_config($vmid, $code);
     }});
 
 __PACKAGE__->register_method({
@@ -378,7 +451,7 @@ __PACKAGE__->register_method({
            },
            perms => {
                type => 'string',
-               description => 'File permissions to use.',
+               description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
                optional => 1,
            },
        },
@@ -397,6 +470,7 @@ __PACKAGE__->register_method({
        my $dest = extract_param($param, 'destination');
 
        my $perms = extract_param($param, 'perms');
+       $perms = '0'.$perms if defined($perms) && $perms !~m/^0/;
        my $user = extract_param($param, 'user');
        my $group = extract_param($param, 'group');
 
@@ -404,7 +478,7 @@ __PACKAGE__->register_method({
            my $running = PVE::LXC::check_running($vmid);
            die "can only push files to a running VM" if !$running;
 
-           my $conf = PVE::LXC::load_config($vmid);
+           my $conf = PVE::LXC::Config->load_config($vmid);
            my $unprivileged = $conf->{unprivileged};
 
            my $realcmd = sub {
@@ -448,17 +522,17 @@ __PACKAGE__->register_method({
            return $rpcenv->fork_worker('push_file', $vmid, undef, $realcmd);
        };
 
-       return PVE::LXC::lock_config($vmid, $code);
+       return PVE::LXC::Config->lock_config($vmid, $code);
     }});
 
 our $cmddef = {
     list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
        my $res = shift;
        return if !scalar(@$res);
-       my $format = "%-10s %-10s %-20s\n";
-       printf($format, 'VMID', 'Status', 'Name');
+       my $format = "%-10s %-10s %-12s %-20s\n";
+       printf($format, 'VMID', 'Status', 'Lock', 'Name');
        foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
-           printf($format, $d->{vmid}, $d->{status}, $d->{name});
+           printf($format, $d->{vmid}, $d->{status}, $d->{lock}, $d->{name});
        }
     }],
     config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'], 
@@ -495,6 +569,8 @@ our $cmddef = {
     exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']],
     fsck => [ __PACKAGE__, 'fsck', ['vmid']],
 
+    mount => [ __PACKAGE__, 'mount', ['vmid']],
+    unmount => [ __PACKAGE__, 'unmount', ['vmid']],
     push => [ __PACKAGE__, 'push', ['vmid', 'file', 'destination']],
     pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']],