]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC.pm
bwlimit: add parameter to rsync in copy_volume
[pve-container.git] / src / PVE / LXC.pm
index 0b57ae9f32ccf275e64cd85621dc02f62bce5f3c..f1aa9d7ea477b1ce45a0020010f39bc710f923d3 100644 (file)
@@ -26,9 +26,12 @@ use PVE::AccessControl;
 use PVE::ProcFSTools;
 use PVE::Syscall;
 use PVE::LXC::Config;
+use PVE::GuestHelpers;
 
 use Time::HiRes qw (gettimeofday);
 
+my $LXC_CONFIG_PATH = '/usr/share/lxc/config';
+
 my $nodename = PVE::INotify::nodename();
 
 my $cpuinfo= PVE::ProcFSTools::read_cpuinfo();
@@ -52,7 +55,8 @@ sub config_list {
 sub destroy_config {
     my ($vmid) = @_;
 
-    unlink PVE::LXC::Config->config_file($vmid, $nodename);
+    my $config_fn = PVE::LXC::Config->config_file($vmid, $nodename);
+    unlink $config_fn or die "failed to remove config file: $!\n";
 }
 
 # container status helpers
@@ -158,6 +162,11 @@ our $vmstatus_return_properties = {
        type => 'number',
        optional => 1,
     },
+    lock => {
+       description => "The current config lock, if any.",
+       type => 'string',
+       optional => 1,
+    }
 };
 
 sub vmstatus {
@@ -182,7 +191,7 @@ sub vmstatus {
        eval { $d->{pid} = find_lxc_pid($vmid) if defined($active_hash->{$vmid}); };
        warn $@ if $@; # ignore errors (consider them stopped)
 
-       $d->{status} = $d->{pid} ? 'running' : 'stopped';
+       $d->{status} = $active_hash->{$vmid} ? 'running' : 'stopped';
 
        my $cfspath = PVE::LXC::Config->cfs_config_path($vmid);
        my $conf = PVE::Cluster::cfs_read_file($cfspath) || {};
@@ -227,6 +236,7 @@ sub vmstatus {
        $d->{diskwrite} = 0;
 
        $d->{template} = PVE::LXC::Config->is_template($conf);
+       $d->{lock} = $conf->{lock} if $conf->{lock};
     }
 
     foreach my $vmid (keys %$list) {
@@ -252,7 +262,7 @@ sub vmstatus {
        }
 
        if (-d '/sys/fs/cgroup/blkio') {
-           my $blkio_bytes = read_cgroup_value('blkio', $vmid, $unpriv, 'blkio.throttle.io_service_bytes', 1);
+           my $blkio_bytes = read_cgroup_value('blkio', $vmid, 0, 'blkio.throttle.io_service_bytes', 1); # don't check if unpriv
            my @bytes = split(/\n/, $blkio_bytes);
            foreach my $byte (@bytes) {
                if (my ($key, $value) = $byte =~ /(Read|Write)\s+(\d+)/) {
@@ -413,6 +423,99 @@ sub get_cgroup_subsystems {
        return wantarray ? ($v1, $v2) : $v1;
 }
 
+# Currently we do not need to create seccomp profile 'files' as the only
+# choice our configuration actually allows is "with or without keyctl()",
+# so we distinguish between using lxc's "default" seccomp profile and our
+# added pve-userns.seccomp file.
+#
+# This returns a configuration line added to the raw lxc config.
+sub make_seccomp_config {
+    my ($conf, $unprivileged, $features) = @_;
+    # User-configured profile has precedence, note that the user's entry would
+    # be written 'after' this line anyway...
+    if (PVE::LXC::Config->has_lxc_entry($conf, 'lxc.seccomp.profile')) {
+       # Warn the user if this conflicts with a feature:
+       if ($features->{keyctl}) {
+           warn "explicitly configured lxc.seccomp.profile overrides the following settings: features:keyctl\n";
+       }
+       return '';
+    }
+
+    # Privileged containers keep using the default (which is already part of
+    # the files included via lxc.include, so we don't need to write it out,
+    # that way it stays admin-configurable via /usr/share/lxc/config/... as
+    # well)
+    return '' if !$unprivileged;
+
+    # Unprivileged containers will get keyctl() disabled by default as a
+    # workaround for systemd-networkd behavior. But we have an option to
+    # explicitly enable it:
+    return '' if $features->{keyctl};
+
+    # Finally we're in an unprivileged container without `keyctl` set
+    # explicitly. We have a file prepared for this:
+    return "lxc.seccomp.profile = $LXC_CONFIG_PATH/pve-userns.seccomp\n";
+}
+
+# Since lxc-3.0.2 we can have lxc generate a profile for the container
+# automatically. The default should be equivalent to the old
+# `lxc-container-default-cgns` profile.
+#
+# Additionally this also added `lxc.apparmor.raw` which can be used to inject
+# additional lines into the profile. We can use that to allow mounting specific
+# file systems.
+sub make_apparmor_config {
+    my ($conf, $unprivileged, $features) = @_;
+
+    # user-configured profile has precedence, but first we go through our own
+    # code to figure out whether we should warn the user:
+
+    my $raw = "lxc.apparmor.profile = generated\n";
+    my @profile_uses;
+
+    if ($features->{fuse}) {
+       # For the informational warning:
+       push @profile_uses, 'features:fuse';
+    }
+
+    # There's lxc.apparmor.allow_nesting now, which will add the necessary
+    # apparmor lines, create an apparmor namespace for the container, but also
+    # adds proc and sysfs mounts to /dev/.lxc/{proc,sys}. These do not have
+    # lxcfs mounted over them, because that would prevent the container from
+    # mounting new instances of them for nested containers.
+    if ($features->{nesting}) {
+       push @profile_uses, 'features:nesting';
+       $raw .= "lxc.apparmor.allow_nesting = 1\n"
+    } else {
+       # In the default profile in /etc/apparmor.d we patch this in because
+       # otherwise a container can for example run `chown` on /sys, breaking
+       # access to it for non-CAP_DAC_OVERRIDE tools on the host:
+       $raw .= "lxc.apparmor.raw = deny mount -> /proc/,\n";
+       $raw .= "lxc.apparmor.raw = deny mount -> /sys/,\n";
+       # Preferably we could use the 'remount' flag but this does not sit well
+       # with apparmor_parser currently:
+       #  mount options=(rw, nosuid, nodev, noexec, remount) -> /sys/,
+    }
+
+    if (my $mount = $features->{mount}) {
+       push @profile_uses, 'features:mount';
+       foreach my $fs (PVE::Tools::split_list($mount)) {
+           $raw .= "lxc.apparmor.raw = mount fstype=$fs,\n";
+       }
+    }
+
+    # More to come?
+
+    if (PVE::LXC::Config->has_lxc_entry($conf, 'lxc.apparmor.profile')) {
+       if (length(my $used = join(', ', @profile_uses))) {
+           warn "explicitly configured lxc.apparmor.profile overrides the following settings: $used\n";
+       }
+       return '';
+    }
+
+    return $raw;
+}
+
 sub update_lxc_config {
     my ($vmid, $conf) = @_;
 
@@ -430,8 +533,8 @@ sub update_lxc_config {
     die "missing 'arch' - internal error" if !$conf->{arch};
     $raw .= "lxc.arch = $conf->{arch}\n";
 
-    my $unprivileged = $conf->{unprivileged};
-    my $custom_idmap = grep { $_->[0] eq 'lxc.idmap' } @{$conf->{lxc}};
+    my $custom_idmap = PVE::LXC::Config->has_lxc_entry($conf, 'lxc.idmap');
+    my $unprivileged = $conf->{unprivileged} || $custom_idmap;
 
     my $ostype = $conf->{ostype} || die "missing 'ostype' - internal error";
 
@@ -439,11 +542,19 @@ sub update_lxc_config {
     my $inc = "$cfgpath/$ostype.common.conf";
     $inc ="$cfgpath/common.conf" if !-f $inc;
     $raw .= "lxc.include = $inc\n";
-    if ($unprivileged || $custom_idmap) {
+    if ($unprivileged) {
        $inc = "$cfgpath/$ostype.userns.conf";
        $inc = "$cfgpath/userns.conf" if !-f $inc;
        $raw .= "lxc.include = $inc\n";
-       $raw .= "lxc.seccomp.profile = $cfgpath/pve-userns.seccomp\n";
+    }
+
+    my $features = PVE::LXC::Config->parse_features($conf->{features});
+
+    $raw .= make_seccomp_config($conf, $unprivileged, $features);
+    $raw .= make_apparmor_config($conf, $unprivileged, $features);
+    if ($features->{fuse}) {
+       $raw .= "lxc.apparmor.raw = mount fstype=fuse,\n";
+       $raw .= "lxc.mount.entry = /dev/fuse dev/fuse none bind,create=file 0 0\n";
     }
 
     # WARNING: DO NOT REMOVE this without making sure that loop device nodes
@@ -570,17 +681,20 @@ sub verify_searchdomain_list {
 }
 
 sub get_console_command {
-    my ($vmid, $conf, $noescapechar) = @_;
+    my ($vmid, $conf, $escapechar) = @_;
+
+    # '-1' as $escapechar disables keyboard escape sequence
+    # any other passed char (a-z) will result in <Ctrl+$escapechar q>
 
     my $cmode = PVE::LXC::Config->get_cmode($conf);
 
     my $cmd = [];
     if ($cmode eq 'console') {
        push @$cmd, 'lxc-console', '-n',  $vmid, '-t', 0;
-       push @$cmd, '-e', -1 if $noescapechar;
+       push @$cmd, '-e', $escapechar if $escapechar;
     } elsif ($cmode eq 'tty') {
        push @$cmd, 'lxc-console', '-n',  $vmid;
-       push @$cmd, '-e', -1 if $noescapechar;
+       push @$cmd, '-e', $escapechar if $escapechar;
     } elsif ($cmode eq 'shell') {
        push @$cmd, 'lxc-attach', '--clear-env', '-n', $vmid;
     } else {
@@ -1001,6 +1115,12 @@ sub check_ct_modify_config_perm {
        } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
                 $opt eq 'searchdomain' || $opt eq 'hostname') {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
+       } elsif ($opt eq 'features') {
+           # For now this is restricted to root@pam
+           raise_perm_exc("changing feature flags is only allowed for root\@pam");
+       } elsif ($opt eq 'hookscript') {
+           # For now this is restricted to root@pam
+           raise_perm_exc("changing the hookscript is only allowed for root\@pam");
        } else {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
        }
@@ -1100,7 +1220,7 @@ sub query_loopdev {
 # The loop device is always detached afterwards (or set to autoclear).
 # Returns the loop device.
 sub run_with_loopdev {
-    my ($func, $file) = @_;
+    my ($func, $file, $readonly) = @_;
     my $device = query_loopdev($file);
     # Try to reuse an existing device
     if ($device) {
@@ -1116,7 +1236,14 @@ sub run_with_loopdev {
            $device = $1;
        }
     };
-    PVE::Tools::run_command(['losetup', '--show', '-f', $file], outfunc => $parser);
+    my $losetup_cmd = [
+       'losetup',
+       '--show',
+       '-f',
+       $file,
+    ];
+    push @$losetup_cmd, '-r' if $readonly;
+    PVE::Tools::run_command($losetup_cmd, outfunc => $parser);
     die "failed to setup loop device for $file\n" if !$device;
     eval { &$func($device); };
     my $err = $@;
@@ -1302,12 +1429,9 @@ sub mountpoint_mount {
 
        my $scfg = PVE::Storage::storage_config($storage_cfg, $storage);
 
-       # early sanity checks:
-       # we otherwise call realpath on the rbd url
-       die "containers on rbd storage without krbd are not supported\n"
-           if $scfg->{type} eq 'rbd' && !$scfg->{krbd};
+       my $path = PVE::Storage::map_volume($storage_cfg, $volid, $snapname);
 
-       my $path = PVE::Storage::path($storage_cfg, $volid, $snapname);
+       $path = PVE::Storage::path($storage_cfg, $volid, $snapname) if !defined($path);
 
        my ($vtype, undef, undef, undef, undef, $isBase, $format) =
            PVE::Storage::parse_volname($storage_cfg, $volid);
@@ -1362,7 +1486,7 @@ sub mountpoint_mount {
            };
            my $use_loopdev = 0;
            if ($scfg->{path}) {
-               $mounted_dev = run_with_loopdev($domount, $path);
+               $mounted_dev = run_with_loopdev($domount, $path, $readonly);
                $use_loopdev = 1;
            } elsif ($scfg->{type} eq 'drbd' || $scfg->{type} eq 'lvm' ||
                     $scfg->{type} eq 'rbd' || $scfg->{type} eq 'lvmthin') {
@@ -1414,7 +1538,9 @@ sub format_disk {
 
     PVE::Storage::activate_volumes($storage_cfg, [$volid]);
 
-    my $path = PVE::Storage::path($storage_cfg, $volid);
+    my $path = PVE::Storage::map_volume($storage_cfg, $volid);
+
+    $path = PVE::Storage::path($storage_cfg, $volid) if !defined($path);
 
     my ($vtype, undef, undef, undef, undef, $isBase, $format) =
        PVE::Storage::parse_volname($storage_cfg, $volid);
@@ -1467,7 +1593,6 @@ sub alloc_disk {
 
        } elsif ($scfg->{type} eq 'rbd') {
 
-           die "krbd option must be enabled on storage type '$scfg->{type}'\n" if !$scfg->{krbd};
            $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb);
            $do_format = 1;
        } else {
@@ -1538,6 +1663,150 @@ sub create_disks {
     return $vollist;
 }
 
+sub update_disksize {
+    my ($vmid, $conf, $all_volumes) = @_;
+
+    my $changes;
+    my $prefix = "CT $vmid:";
+
+    my $update_mp = sub {
+       my ($key, $mp, @param) = @_;
+       my $size = $all_volumes->{$mp->{volume}}->{size} // 0;
+
+       if (!defined($mp->{size}) || $size != $mp->{size}) {
+           $changes = 1;
+           print "$prefix updated volume size of '$mp->{volume}' in config.\n";
+           $mp->{size} = $size;
+           my $nomp = 1 if ($key eq 'rootfs');
+           $conf->{$key} = PVE::LXC::Config->print_ct_mountpoint($mp, $nomp);
+       }
+    };
+
+    PVE::LXC::Config->foreach_mountpoint($conf, $update_mp);
+
+    return $changes;
+}
+
+sub update_unused {
+    my ($vmid, $conf, $all_volumes) = @_;
+
+    my $changes;
+    my $prefix = "CT $vmid:";
+
+    # Note: it is allowed to define multiple storage entries with the same path
+    # (alias), so we need to check both 'volid' and real 'path' (two different
+    # volid can point to the same path).
+
+    # used and unused disks
+    my $refpath = {};
+    my $orphans = {};
+
+    foreach my $opt (keys %$conf) {
+       next if ($opt !~ m/^unused\d+$/);
+       my $vol = $all_volumes->{$conf->{$opt}};
+       $refpath->{$vol->{path}} = $vol->{volid};
+    }
+
+    foreach my $key (keys %$all_volumes) {
+       my $vol = $all_volumes->{$key};
+       my $in_use = PVE::LXC::Config->is_volume_in_use($conf, $vol->{volid});
+       my $path = $vol->{path};
+
+       if ($in_use) {
+           $refpath->{$path} = $key;
+           delete $orphans->{$path};
+       } else {
+           if ((!$orphans->{$path}) && (!$refpath->{$path})) {
+               $orphans->{$path} = $key;
+           }
+       }
+    }
+
+    for my $key (keys %$orphans) {
+       my $disk = $orphans->{$key};
+       my $unused = PVE::LXC::Config->add_unused_volume($conf, $disk);
+
+       if ($unused) {
+           $changes = 1;
+           print "$prefix add unreferenced volume '$disk' as '$unused' to config.\n";
+       }
+    }
+
+    return $changes;
+}
+
+sub scan_volids {
+    my ($cfg, $vmid) = @_;
+
+    my $info = PVE::Storage::vdisk_list($cfg, undef, $vmid);
+
+    my $all_volumes = {};
+    foreach my $storeid (keys %$info) {
+       foreach my $item (@{$info->{$storeid}}) {
+           my $volid = $item->{volid};
+           next if !($volid && $item->{size});
+           $item->{path} = PVE::Storage::path($cfg, $volid);
+           $all_volumes->{$volid} = $item;
+       }
+    }
+
+    return $all_volumes;
+}
+
+sub rescan {
+    my ($vmid, $nolock, $dryrun) = @_;
+
+    my $cfg = PVE::Storage::config();
+
+    # FIXME: Remove once our RBD plugin can handle CT and VM on a single storage
+    # see: https://pve.proxmox.com/pipermail/pve-devel/2018-July/032900.html
+    foreach my $stor (keys %{$cfg->{ids}}) {
+       delete($cfg->{ids}->{$stor}) if !$cfg->{ids}->{$stor}->{content}->{rootdir};
+    }
+
+    print "rescan volumes...\n";
+    my $all_volumes = scan_volids($cfg, $vmid);
+
+    my $updatefn =  sub {
+       my ($vmid) = @_;
+
+       my $changes;
+       my $conf = PVE::LXC::Config->load_config($vmid);
+
+       PVE::LXC::Config->check_lock($conf);
+
+       my $vm_volids = {};
+       foreach my $volid (keys %$all_volumes) {
+           my $info = $all_volumes->{$volid};
+           $vm_volids->{$volid} = $info if $info->{vmid} == $vmid;
+       }
+
+       my $upu = update_unused($vmid, $conf, $vm_volids);
+       my $upd = update_disksize($vmid, $conf, $vm_volids);
+       $changes = $upu || $upd;
+
+       PVE::LXC::Config->write_config($vmid, $conf) if $changes && !$dryrun;
+    };
+
+    if (defined($vmid)) {
+       if ($nolock) {
+           &$updatefn($vmid);
+       } else {
+           PVE::LXC::Config->lock_config($vmid, $updatefn, $vmid);
+       }
+    } else {
+       my $vmlist = config_list();
+       foreach my $vmid (keys %$vmlist) {
+           if ($nolock) {
+               &$updatefn($vmid);
+           } else {
+               PVE::LXC::Config->lock_config($vmid, $updatefn, $vmid);
+           }
+       }
+    }
+}
+
+
 # bash completion helper
 
 sub complete_os_templates {
@@ -1654,11 +1923,13 @@ sub vm_start {
 
     my $cmd = ['systemctl', 'start', "pve-container\@$vmid"];
 
+    PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start', 1);
     eval { PVE::Tools::run_command($cmd); };
     if (my $err = $@) {
        unlink $skiplock_flag_fn;
        die $err;
     }
+    PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-start');
 
     return;
 }
@@ -1681,6 +1952,9 @@ sub vm_stop {
        die "failed to open container ${vmid}'s command socket: $!\n";
     }
 
+    my $conf = PVE::LXC::Config->load_config($vmid);
+    PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-stop');
+
     # Stop the container:
 
     my $cmd = ['lxc-stop', '-n', $vmid];
@@ -1723,12 +1997,12 @@ sub run_unshared {
 }
 
 my $copy_volume = sub {
-    my ($src_volid, $src, $dst_volid, $dest, $storage_cfg, $snapname) = @_;
+    my ($src_volid, $src, $dst_volid, $dest, $storage_cfg, $snapname, $bwlimit) = @_;
 
-    my $src_mp = { volume => $src_volid, mp => '/' };
+    my $src_mp = { volume => $src_volid, mp => '/', ro => 1 };
     $src_mp->{type} = PVE::LXC::Config->classify_mountpoint($src_volid);
 
-    my $dst_mp = { volume => $dst_volid, mp => '/' };
+    my $dst_mp = { volume => $dst_volid, mp => '/', ro => 0 };
     $dst_mp->{type} = PVE::LXC::Config->classify_mountpoint($dst_volid);
 
     my @mounted;
@@ -1741,9 +2015,11 @@ my $copy_volume = sub {
        mountpoint_mount($dst_mp, $dest, $storage_cfg);
        push @mounted, $dest;
 
+       $bwlimit //= 0;
+
        PVE::Tools::run_command(['/usr/bin/rsync', '--stats', '-X', '-A', '--numeric-ids',
                                 '-aH', '--whole-file', '--sparse', '--one-file-system',
-                                "$src/", $dest]);
+                                "--bwlimit=$bwlimit", "$src/", $dest]);
     };
     my $err = $@;
     foreach my $mount (reverse @mounted) {
@@ -1761,7 +2037,7 @@ my $copy_volume = sub {
 
 # Should not be called after unsharing the mount namespace!
 sub copy_volume {
-    my ($mp, $vmid, $storage, $storage_cfg, $conf, $snapname) = @_;
+    my ($mp, $vmid, $storage, $storage_cfg, $conf, $snapname, $bwlimit) = @_;
 
     die "cannot copy volumes of type $mp->{type}\n" if $mp->{type} ne 'volume';
     File::Path::make_path("/var/lib/lxc/$vmid");
@@ -1787,7 +2063,7 @@ sub copy_volume {
        }
 
        run_unshared(sub {
-           $copy_volume->($mp->{volume}, $src, $new_volid, $dest, $storage_cfg, $snapname);
+           $copy_volume->($mp->{volume}, $src, $new_volid, $dest, $storage_cfg, $snapname, $bwlimit);
        });
     };
     if (my $err = $@) {