]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC.pm
Add device passthrough
[pve-container.git] / src / PVE / LXC.pm
index 0d0141dc483ee1ba430e410bc6fdbe1267bbfc9a..259fcb25b4c30514adfba71e4650164dee2fb526 100644 (file)
@@ -3,22 +3,29 @@ package PVE::LXC;
 use strict;
 use warnings;
 
-use POSIX qw(EINTR);
-
-use Socket;
-
+use Cwd qw();
+use Errno qw(ELOOP ENOENT ENOTDIR EROFS ECONNREFUSED EEXIST);
+use Fcntl qw(O_RDONLY O_WRONLY O_NOFOLLOW O_DIRECTORY :mode);
 use File::Path;
 use File::Spec;
-use Cwd qw();
-use Fcntl qw(O_RDONLY O_WRONLY O_NOFOLLOW O_DIRECTORY);
-use Errno qw(ELOOP ENOTDIR EROFS ECONNREFUSED ENOSYS EEXIST);
+use IO::Poll qw(POLLIN POLLHUP);
 use IO::Socket::UNIX;
+use POSIX qw(EINTR);
+use Socket;
+use Time::HiRes qw (gettimeofday);
 
+use PVE::AccessControl;
+use PVE::CGroup;
+use PVE::CpuSet;
 use PVE::Exception qw(raise_perm_exc);
-use PVE::Storage;
-use PVE::SafeSyslog;
+use PVE::GuestHelpers qw(check_vnet_access safe_string_ne safe_num_ne safe_boolean_ne);
 use PVE::INotify;
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::Network;
+use PVE::ProcFSTools;
+use PVE::RESTEnvironment;
+use PVE::SafeSyslog;
+use PVE::Storage;
 use PVE::Tools qw(
     run_command
     dir_glob_foreach
@@ -29,20 +36,13 @@ use PVE::Tools qw(
     $IPV4RE
     $IPV6RE
 );
-use PVE::RPCEnvironment;
-use PVE::CpuSet;
-use PVE::Network;
-use PVE::AccessControl;
-use PVE::ProcFSTools;
 use PVE::Syscall qw(:fsmount);
-use PVE::LXC::Config;
-use PVE::GuestHelpers qw(safe_string_ne safe_num_ne safe_boolean_ne);
-use PVE::LXC::Tools;
+
 use PVE::LXC::CGroup;
+use PVE::LXC::Config;
 use PVE::LXC::Monitor;
-use PVE::CGroup;
+use PVE::LXC::Tools;
 
-use Time::HiRes qw (gettimeofday);
 my $have_sdn;
 eval {
     require PVE::Network::SDN::Zones;
@@ -639,6 +639,29 @@ sub update_lxc_config {
        $raw .= "lxc.mount.auto = sys:mixed\n";
     }
 
+    PVE::LXC::Config->foreach_passthrough_device($conf, sub {
+       my ($key, $device) = @_;
+
+       die "Path is not defined for passthrough device $key"
+           unless (defined($device->{path}));
+
+       my $absolute_path = $device->{path};
+       my ($mode, $rdev) = (stat($absolute_path))[2, 6];
+
+       die "Device $absolute_path does not exist\n" if $! == ENOENT;
+
+       die "Error accessing device $absolute_path\n"
+           if (!defined($mode) || !defined($rdev));
+
+       die "$absolute_path is not a device\n"
+           if (!S_ISBLK($mode) && !S_ISCHR($mode));
+
+       my $major = PVE::Tools::dev_t_major($rdev);
+       my $minor = PVE::Tools::dev_t_minor($rdev);
+       my $device_type_char = S_ISBLK($mode) ? 'b' : 'c';
+       $raw .= "lxc.cgroup2.devices.allow = $device_type_char $major:$minor rw\n";
+    });
+
     # WARNING: DO NOT REMOVE this without making sure that loop device nodes
     # cannot be exposed to the container with r/w access (cgroup perms).
     # When this is enabled mounts will still remain in the monitor's namespace
@@ -668,7 +691,7 @@ sub update_lxc_config {
 
     # some init scripts expect a linux terminal (turnkey).
     $raw .= "lxc.environment = TERM=linux\n";
-    
+
     my $utsname = $conf->{hostname} || "CT$vmid";
     $raw .= "lxc.uts.name = $utsname\n";
 
@@ -685,8 +708,11 @@ sub update_lxc_config {
        my $memory = $conf->{memory} || 512;
        my $swap = $conf->{swap} // 0;
 
-       my $lxcmem = int($memory*1024*1024);
-       $raw .= "lxc.cgroup2.memory.max = $lxcmem\n";
+       # cgroup memory usage is limited by the hard 'max' limit (OOM-killer enforced) and the soft
+       # 'high' limit (cgroup processes get throttled and put under heavy reclaim pressure).
+       my ($lxc_mem_max, $lxc_mem_high) = PVE::LXC::Config::calculate_memory_constraints($memory);
+       $raw .= "lxc.cgroup2.memory.max = $lxc_mem_max\n";
+       $raw .= "lxc.cgroup2.memory.high = $lxc_mem_high\n";
 
        my $lxcswap = int($swap*1024*1024);
        $raw .= "lxc.cgroup2.memory.swap.max = $lxcswap\n";
@@ -729,7 +755,15 @@ sub update_lxc_config {
        $raw .= "lxc.net.$ind.veth.pair = veth${vmid}i${ind}\n";
        $raw .= "lxc.net.$ind.hwaddr = $d->{hwaddr}\n" if defined($d->{hwaddr});
        $raw .= "lxc.net.$ind.name = $d->{name}\n" if defined($d->{name});
-       $raw .= "lxc.net.$ind.mtu = $d->{mtu}\n" if defined($d->{mtu});
+
+       my $bridge_mtu = PVE::Network::read_bridge_mtu($d->{bridge});
+       my $mtu = $d->{mtu} || $bridge_mtu;
+
+       # Keep container from starting with invalid mtu configuration
+       die "$k: MTU size '$mtu' is bigger than bridge MTU '$bridge_mtu'\n"
+           if ($mtu > $bridge_mtu);
+
+       $raw .= "lxc.net.$ind.mtu = $mtu\n";
 
        # Starting with lxc 4.0, we do not patch lxc to execute our up-scripts.
        if ($lxc_major >= 4) {
@@ -909,6 +943,29 @@ sub vm_stop_cleanup {
     warn $@ if $@; # avoid errors - just warn
 }
 
+sub net_tap_plug : prototype($$) {
+    my ($iface, $net) = @_;
+
+    if (defined($net->{link_down})) {
+       PVE::Tools::run_command(['/sbin/ip', 'link', 'set', 'dev', $iface, 'down']);
+       # Don't add disconnected interfaces to the bridge, otherwise e.g. applying any network
+       # change (e.g. `ifreload -a`) could (re-)activate it unintentionally.
+       return;
+    }
+
+    my ($bridge, $tag, $firewall, $trunks, $rate, $hwaddr) =
+       $net->@{'bridge', 'tag', 'firewall', 'trunks', 'rate', 'hwaddr'};
+
+    if ($have_sdn) {
+       PVE::Network::SDN::Zones::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
+       PVE::Network::SDN::Zones::add_bridge_fdb($iface, $hwaddr, $bridge);
+    } else {
+       PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate, { mac => $hwaddr });
+    }
+
+    PVE::Tools::run_command(['/sbin/ip', 'link', 'set', 'dev', $iface, 'up']);
+}
+
 sub update_net {
     my ($vmid, $conf, $opt, $newnet, $netid, $rootdir) = @_;
 
@@ -935,7 +992,9 @@ sub update_net {
        } else {
            if (safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
                safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
-               safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
+               safe_num_ne($oldnet->{firewall}, $newnet->{firewall}) ||
+               safe_boolean_ne($oldnet->{link_down}, $newnet->{link_down})
+           ) {
 
                if ($oldnet->{bridge}) {
                    PVE::Network::tap_unplug($veth);
@@ -946,14 +1005,10 @@ sub update_net {
                    PVE::LXC::Config->write_config($vmid, $conf);
                }
 
-               if ($have_sdn) {
-                   PVE::Network::SDN::Zones::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
-               } else {
-                   PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
-               }
+               PVE::LXC::net_tap_plug($veth, $newnet);
 
                # This includes the rate:
-               foreach (qw(bridge tag firewall rate)) {
+               foreach (qw(bridge tag firewall rate link_down)) {
                    $oldnet->{$_} = $newnet->{$_} if $newnet->{$_};
                }
            } elsif (safe_string_ne($oldnet->{rate}, $newnet->{rate})) {
@@ -981,12 +1036,12 @@ sub hotplug_net {
 
     if ($have_sdn) {
        PVE::Network::SDN::Zones::veth_create($veth, $vethpeer, $newnet->{bridge}, $newnet->{hwaddr});
-       PVE::Network::SDN::Zones::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
     } else {
        PVE::Network::veth_create($veth, $vethpeer, $newnet->{bridge}, $newnet->{hwaddr});
-       PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
     }
 
+    PVE::LXC::net_tap_plug($veth, $newnet);
+
     # attach peer in container
     my $cmd = ['lxc-device', '-n', $vmid, 'add', $vethpeer, "$eth" ];
     PVE::Tools::run_command($cmd);
@@ -996,7 +1051,7 @@ sub hotplug_net {
     PVE::Tools::run_command($cmd);
 
     my $done = { type => 'veth' };
-    foreach (qw(bridge tag firewall hwaddr name)) {
+    foreach (qw(bridge tag firewall hwaddr name link_down)) {
        $done->{$_} = $newnet->{$_} if $newnet->{$_};
     }
     $conf->{$opt} = PVE::LXC::Config->print_lxc_network($done);
@@ -1004,6 +1059,32 @@ sub hotplug_net {
     PVE::LXC::Config->write_config($vmid, $conf);
 }
 
+sub get_interfaces {
+    my ($vmid) = @_;
+
+    my $pid = eval { find_lxc_pid($vmid); };
+    return if $@;
+
+    my $output;
+    # enters the network namespace of the container and executes 'ip a'
+    run_command(['nsenter', '-t', $pid, '--net', '--', 'ip', '--json', 'a'],
+       outfunc => sub { $output .= shift; });
+
+    my $config = JSON::decode_json($output);
+
+    my $res;
+    for my $interface ($config->@*) {
+       my $obj = { name => $interface->{ifname} };
+       for my $ip ($interface->{addr_info}->@*) {
+           $obj->{$ip->{family}} = $ip->{local} . "/" . $ip->{prefixlen};
+       }
+       $obj->{hwaddr} = $interface->{address};
+       push @$res, $obj
+    }
+
+    return $res;
+}
+
 sub update_ipconfig {
     my ($vmid, $conf, $opt, $eth, $newnet, $rootdir) = @_;
 
@@ -1282,8 +1363,13 @@ sub check_ct_modify_config_perm {
            }
        } 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' ||
-                $opt eq 'searchdomain' || $opt eq 'hostname') {
+       } elsif ($opt =~ m/^net\d+$/) {
+           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
+           check_bridge_access($rpcenv, $authuser, $oldconf->{$opt}) if $oldconf->{$opt};
+           check_bridge_access($rpcenv, $authuser, $newconf->{$opt}) if $newconf->{$opt};
+       } elsif ($opt =~ m/^dev\d+$/) {
+           raise_perm_exc("configuring device passthrough is only allowed for root\@pam");
+       } elsif ($opt eq 'nameserver' || $opt eq 'searchdomain' || $opt eq 'hostname') {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
        } elsif ($opt eq 'features') {
            raise_perm_exc("changing feature flags for privileged container is only allowed for root\@pam")
@@ -1332,6 +1418,10 @@ sub check_ct_modify_config_perm {
        } elsif ($opt eq 'hookscript') {
            # For now this is restricted to root@pam
            raise_perm_exc("changing the hookscript is only allowed for root\@pam");
+       } elsif ($opt eq 'tags') {
+           my $old = $oldconf->{$opt};
+           my $new = $delete ? '' : $newconf->{$opt};
+           PVE::GuestHelpers::assert_tag_permissions($vmid, $old, $new, $rpcenv, $authuser);
        } else {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
        }
@@ -1347,6 +1437,18 @@ sub check_ct_modify_config_perm {
     return 1;
 }
 
+sub check_bridge_access {
+    my ($rpcenv, $authuser, $raw) = @_;
+
+    return 1 if $authuser eq 'root@pam';
+
+    my $net = PVE::LXC::Config->parse_lxc_network($raw);
+    my ($bridge, $tag, $trunks) = $net->@{'bridge', 'tag', 'trunks'};
+    check_vnet_access($rpcenv, $authuser, $bridge, $tag, $trunks);
+
+    return 1;
+};
+
 sub umount_all {
     my ($vmid, $storage_cfg, $conf, $noerr) = @_;
 
@@ -1630,7 +1732,6 @@ sub mountpoint_stage {
        __mountpoint_mount($mountpoint, $stage_dir, $storage_cfg, $snapname, $rootuid, $rootgid, 1);
 
     if (!defined($path)) {
-       return undef if $! == ENOSYS;
        die "failed to mount subvolume: $!\n";
     }
 
@@ -1665,16 +1766,9 @@ sub mountpoint_insert_staged {
 
 # Use $stage_mount, $rootdir is treated as a temporary path to "stage" the file system. The user
 #   can then open a file descriptor to it which can be used with the `move_mount` syscall.
-#   Note that if the kernel does not support the new mount API, this will not perform any action
-#   and return `undef` with $! = ENOSYS.
 sub __mountpoint_mount {
     my ($mountpoint, $rootdir, $storage_cfg, $snapname, $rootuid, $rootgid, $stage_mount) = @_;
 
-    if (defined($stage_mount) && !PVE::LXC::Tools::can_use_new_mount_api()) {
-       $! = ENOSYS;
-       return undef;
-    }
-
     # When staging mount points we always mount to $rootdir directly (iow. as if `mp=/`).
     # This is required since __mount_prepare_rootdir() will return handles to the parent directory
     # which we use in __bindmount_verify()!
@@ -1684,14 +1778,14 @@ sub __mountpoint_mount {
     my $type = $mountpoint->{type};
     my $quota = !$snapname && !$mountpoint->{ro} && $mountpoint->{quota};
     my $mounted_dev;
-    
+
     return if !$volid || !$mount;
 
     $mount =~ s!/+!/!g;
 
     my $mount_path;
     my ($mpfd, $parentfd, $last_dir);
-    
+
     if (defined($rootdir)) {
        ($rootdir, $mount_path, $mpfd, $parentfd, $last_dir) =
            __mount_prepare_rootdir($rootdir, $mount, $rootuid, $rootgid);
@@ -1700,7 +1794,7 @@ sub __mountpoint_mount {
     if (defined($stage_mount)) {
        $mount_path = $rootdir;
     }
-    
+
     my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1);
 
     die "unknown snapshot path for '$volid'" if !$storage && defined($snapname);
@@ -1809,7 +1903,7 @@ sub __mountpoint_mount {
        warn "cannot enable quota control for bind mounts\n" if $quota;
        return wantarray ? ($volid, 0, undef) : $volid;
     }
-    
+
     die "unsupported storage";
 }
 
@@ -1882,7 +1976,7 @@ sub get_staging_mount_path($) {
     return $target;
 }
 
-# Mount /run/pve/mountpoints as tmpfs
+# Mount tmpfs for mount point staging and return the path.
 sub get_staging_tempfs() {
     # We choose a path in /var/lib/lxc/ here because the lxc-start apparmor profile restricts most
     # mounts to that.
@@ -2070,8 +2164,8 @@ sub update_disksize {
            $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);
+           my $no_mp = $key eq 'rootfs'; # rootfs is handled different from other mount points
+           $conf->{$key} = PVE::LXC::Config->print_ct_mountpoint($mp, $no_mp);
        }
     };
 
@@ -2264,8 +2358,9 @@ sub parse_id_maps {
     my $lxc = $conf->{lxc};
     foreach my $entry (@$lxc) {
        my ($key, $value) = @$entry;
-       # FIXME: remove the 'id_map' variant when lxc-3.0 arrives
-       next if $key ne 'lxc.idmap' && $key ne 'lxc.id_map';
+
+       next if $key ne 'lxc.idmap';
+
        if ($value =~ /^([ug])\s+(\d+)\s+(\d+)\s+(\d+)\s*$/) {
            my ($type, $ct, $host, $length) = ($1, $2, $3, $4);
            push @$id_map, [$type, $ct, $host, $length];
@@ -2288,6 +2383,69 @@ sub parse_id_maps {
     return ($id_map, $rootuid, $rootgid);
 }
 
+sub validate_id_maps {
+    my ($id_map) = @_;
+
+    # $mappings->{$type}->{$side} = [ { line => $line, start => $start, count => $count }, ... ]
+    #   $type: either "u" or "g"
+    #   $side: either "container" or "host"
+    #   $line: index of this mapping in @$id_map
+    #   $start, $count: interval of this mapping
+    my $mappings = { u => {}, g => {} };
+    for (my $i = 0; $i < scalar(@$id_map); $i++) {
+       my ($type, $ct_start, $host_start, $count) = $id_map->[$i]->@*;
+       my $sides = $mappings->{$type};
+       push $sides->{host}->@*, { line => $i, start => $host_start, count => $count };
+       push $sides->{container}->@*, { line => $i, start => $ct_start, count => $count };
+    }
+
+    # find the first conflict between two consecutive mappings when sorted by their start id
+    for my $type (qw(u g)) {
+       for my $side (qw(container host)) {
+           my @entries = sort { $a->{start} <=> $b->{start} } $mappings->{$type}->{$side}->@*;
+           for my $idx (1..scalar(@entries) - 1) {
+               my $previous = $entries[$idx - 1];
+               my $current = $entries[$idx];
+               if ($previous->{start} + $previous->{count} > $current->{start}) {
+                   my $conflict = $current->{start};
+                   my @previous_line = $id_map->[$previous->{line}]->@*;
+                   my @current_line = $id_map->[$current->{line}]->@*;
+                   die "invalid map entry '@current_line': $side ${type}id $conflict "
+                      ."is also mapped by entry '@previous_line'\n";
+               }
+           }
+       }
+    }
+}
+
+sub map_ct_id_to_host {
+    my ($id, $id_map, $id_type) = @_;
+
+    for my $mapping (@$id_map) {
+       my ($type, $ct, $host, $length) = @$mapping;
+
+       next if ($type ne $id_type);
+
+       if ($id >= $ct && $id < ($ct + $length)) {
+           return $host - $ct + $id;
+       }
+    }
+
+    return $id;
+}
+
+sub map_ct_uid_to_host {
+    my ($uid, $id_map) = @_;
+
+    return map_ct_id_to_host($uid, $id_map, 'u');
+}
+
+sub map_ct_gid_to_host {
+    my ($gid, $id_map) = @_;
+
+    return map_ct_id_to_host($gid, $id_map, 'g');
+}
+
 sub userns_command {
     my ($id_map) = @_;
     if (@$id_map) {
@@ -2312,13 +2470,8 @@ my sub print_ct_warn_log {
     my $log = eval { file_get_contents($log_fn) };
     return if !$log;
 
-    my $rpcenv = eval { PVE::RPCEnvironment::get() };
-
-    my $warn_fn = $rpcenv ? sub { $rpcenv->warn($_[0]) } : sub { print STDERR "WARN: $_[0]\n" };
-
     while ($log =~ /^\h*\s*(.*?)\h*$/gm) {
-       my $line = $1;
-       $warn_fn->($line);
+       PVE::RESTEnvironment::log_warn($1);
     }
     unlink $log_fn or warn "could not unlink '$log_fn' - $!\n";
 }
@@ -2375,6 +2528,12 @@ sub vm_start {
 
     update_lxc_config($vmid, $conf);
 
+    eval {
+       my ($id_map, undef, undef) = PVE::LXC::parse_id_maps($conf);
+       PVE::LXC::validate_id_maps($id_map);
+    };
+    warn "lxc.idmap: $@" if $@;
+
     my $skiplock_flag_fn = "/run/lxc/skiplock-$vmid";
 
     if ($skiplock) {
@@ -2459,13 +2618,22 @@ sub vm_stop {
     }
 
     eval { run_command($cmd, timeout => $shutdown_timeout) };
+
+    # Wait until the command socket is closed.
+    # In case the lxc-stop call failed, reading from the command socket may block forever,
+    # so poll with another timeout to avoid freezing the shutdown task.
     if (my $err = $@) {
-       warn $@ if $@;
-    }
+       warn $err if $err;
 
-    my $result = <$sock>;
+       my $poll = IO::Poll->new();
+       $poll->mask($sock => POLLIN | POLLHUP); # watch for input and EOF events
+       $poll->poll($shutdown_timeout); # IO::Poll timeout is in seconds
+       return if ($poll->events($sock) & POLLHUP);
+    } else {
+       my $result = <$sock>;
+       return if !defined $result; # monitor is gone and the ct has stopped.
+    }
 
-    return if !defined $result; # monitor is gone and the ct has stopped.
     die "container did not stop\n";
 }