use PVE::INotify;
use PVE::JSONSchema qw(get_standard_option);
use PVE::Tools qw(
- dir_glob_foreach file_get_contents file_set_contents
- AT_FDCWD O_PATH $IPV4RE $IPV6RE
+ run_command
+ dir_glob_foreach
+ file_get_contents
+ file_set_contents
+ AT_FDCWD
+ O_PATH
+ $IPV4RE
+ $IPV6RE
);
use PVE::CpuSet;
use PVE::Network;
use PVE::LXC::Tools;
use PVE::LXC::CGroup;
use PVE::LXC::Monitor;
+use PVE::CGroup;
use Time::HiRes qw (gettimeofday);
my $have_sdn;
sub vmstatus {
my ($opt_vmid) = @_;
- my $list = $opt_vmid ? { $opt_vmid => { type => 'lxc', vmid => $opt_vmid }} : config_list();
+ my $list = $opt_vmid ? { $opt_vmid => { type => 'lxc', vmid => int($opt_vmid) }} : config_list();
my $active_hash = list_active_containers();
foreach my $vmid (keys %$list) {
my $d = $list->{$vmid};
- eval { $d->{pid} = find_lxc_pid($vmid) if defined($active_hash->{$vmid}); };
+ eval { $d->{pid} = int(find_lxc_pid($vmid)) if defined($active_hash->{$vmid}); };
warn $@ if $@; # ignore errors (consider them stopped)
$d->{status} = $active_hash->{$vmid} ? 'running' : 'stopped';
$d->{cpus} = $conf->{cores} || $conf->{cpulimit};
$d->{cpus} = $cpucount if !$d->{cpus};
- $d->{lock} = $conf->{lock} || '';
$d->{tags} = $conf->{tags} if defined($conf->{tags});
if ($d->{pid}) {
my $res = get_container_disk_usage($vmid, $d->{pid});
- $d->{disk} = $res->{used};
- $d->{maxdisk} = $res->{total};
+ $d->{disk} = int($res->{used});
+ $d->{maxdisk} = int($res->{total});
} else {
$d->{disk} = 0;
# use 4GB by default ??
$d->{diskread} = 0;
$d->{diskwrite} = 0;
- $d->{template} = PVE::LXC::Config->is_template($conf);
+ $d->{template} = 1 if PVE::LXC::Config->is_template($conf);
$d->{lock} = $conf->{lock} if $conf->{lock};
}
my $cgroups = PVE::LXC::CGroup->new($vmid);
if (defined(my $mem = $cgroups->get_memory_stat())) {
- $d->{mem} = $mem->{mem};
- $d->{swap} = $mem->{swap};
+ $d->{mem} = int($mem->{mem});
+ $d->{swap} = int($mem->{swap});
} else {
$d->{mem} = 0;
$d->{swap} = 0;
}
if (defined(my $blkio = $cgroups->get_io_stats())) {
- $d->{diskread} = $blkio->{diskread};
- $d->{diskwrite} = $blkio->{diskwrite};
+ $d->{diskread} = int($blkio->{diskread});
+ $d->{diskwrite} = int($blkio->{diskwrite});
} else {
$d->{diskread} = 0;
$d->{diskwrite} = 0;
die "unable to parse ipv4 address/mask\n";
}
-# Deprecated. Use `PVE::LXC::CGroup::get_cgroup_controllers()` instead.
-sub get_cgroup_subsystems {
- PVE::LXC::CGroup::get_v1_controllers();
-}
-
# With seccomp trap to userspace we now have the ability to optionally forward
# certain syscalls to the "host" to handle (via our pve-lxc-syscalld daemon).
#
# files while the container is running!
$raw .= "lxc.monitor.unshare = 1\n";
- my $cgv1 = get_cgroup_subsystems();
+ my ($cgv1, $cgv2) = PVE::CGroup::get_cgroup_controllers();
# Should we read them from /etc/subuid?
if ($unprivileged && !$custom_idmap) {
if (!PVE::LXC::Config->has_dev_console($conf)) {
$raw .= "lxc.console.path = none\n";
- $raw .= "lxc.cgroup.devices.deny = c 5:1 rwm\n" if $cgv1->{devices};
+ if ($cgv1->{devices}) {
+ $raw .= "lxc.cgroup.devices.deny = c 5:1 rwm\n";
+ } elsif (defined($cgv2)) {
+ $raw .= "lxc.cgroup2.devices.deny = c 5:1 rwm\n";
+ }
}
my $ttycount = PVE::LXC::Config->get_tty_count($conf);
my $lxcswap = int(($memory + $swap)*1024*1024);
$raw .= "lxc.cgroup.memory.memsw.limit_in_bytes = $lxcswap\n";
+ } elsif ($cgv2->{memory}) {
+ my $memory = $conf->{memory} || 512;
+ my $swap = $conf->{swap} // 0;
+
+ my $lxcmem = int($memory*1024*1024);
+ $raw .= "lxc.cgroup2.memory.max = $lxcmem\n";
+
+ my $lxcswap = int($swap*1024*1024);
+ $raw .= "lxc.cgroup2.memory.swap.max = $lxcswap\n";
}
if ($cgv1->{cpu}) {
my $shares = $conf->{cpuunits} || 1024;
$raw .= "lxc.cgroup.cpu.shares = $shares\n";
+ } elsif ($cgv2->{cpu}) {
+ # See PVE::CGroup
+ if (my $cpulimit = $conf->{cpulimit}) {
+ my $value = int(100000*$cpulimit);
+ $raw .= "lxc.cgroup2.cpu.max = $value 100000\n";
+ }
+
+ if (defined(my $shares = $conf->{cpuunits})) {
+ die "cpu weight (shares) must be in range [1, 10000]\n"
+ if $shares < 1 || $shares > 10000;
+ $raw .= "lxc.cgroup2.cpu.weight = $shares\n";
+ }
}
die "missing 'rootfs' configuration\n"
}
my $cpuset;
- my ($cpuset_cgroup, $cpuset_version) = eval { PVE::LXC::CGroup::cpuset_controller_path() };
+ my ($cpuset_cgroup, $cpuset_version) = eval { PVE::CGroup::cpuset_controller_path() };
if (defined($cpuset_cgroup)) {
$cpuset = eval { PVE::CpuSet->new_from_path("$cpuset_cgroup/lxc", 1) }
|| PVE::CpuSet->new_from_path($cpuset_cgroup, 1);
my @list = ();
foreach my $server (PVE::Tools::split_list($nameserver_list)) {
- PVE::JSONSchema::pve_verify_ip($server);
+ PVE::LXC::Config::verify_ip_with_ll_iface($server);
push @list, $server;
}
}
sub destroy_lxc_container {
- my ($storage_cfg, $vmid, $conf, $replacement_conf) = @_;
+ my ($storage_cfg, $vmid, $conf, $replacement_conf, $purge_unreferenced) = @_;
PVE::LXC::Config->foreach_volume_full($conf, {include_unused => 1}, sub {
my ($ms, $mountpoint) = @_;
delete_mountpoint_volume($storage_cfg, $vmid, $mountpoint->{volume});
});
+ if ($purge_unreferenced) { # also remove unreferenced disk
+ my $vmdisks = PVE::Storage::vdisk_list($storage_cfg, undef, $vmid, undef, 'rootdir');
+ PVE::Storage::foreach_volid($vmdisks, sub {
+ my ($volid, $sid, $volname, $d) = @_;
+ eval { PVE::Storage::vdisk_free($storage_cfg, $volid) };
+ warn $@ if $@;
+ });
+ }
+
rmdir "/var/lib/lxc/$vmid/rootfs";
unlink "/var/lib/lxc/$vmid/config";
rmdir "/var/lib/lxc/$vmid";
my $mountdata = do { local $/ = undef; <$socket> };
close $socket;
+ my %nosyncfs = (
+ cgroup => 1,
+ cgroup2 => 1,
+ devtmpfs => 1,
+ devpts => 1,
+ 'fuse.lxcfs' => 1,
+ fusectl => 1,
+ mqueue => 1,
+ proc => 1,
+ sysfs => 1,
+ tmpfs => 1,
+ );
+
# Now sync all mountpoints...
my $mounts = PVE::ProcFSTools::parse_mounts($mountdata);
foreach my $mp (@$mounts) {
my ($what, $dir, $fs) = @$mp;
- next if $fs eq 'fuse.lxcfs';
+ next if $nosyncfs{$fs};
eval { PVE::Tools::sync_mountpoint($dir); };
warn $@ if $@;
}
}
};
my $use_loopdev = 0;
- if ($scfg->{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') {
- $mounted_dev = $path;
- &$domount($path);
+ if ($scfg->{content}->{rootdir}) {
+ if ($scfg->{path}) {
+ $mounted_dev = run_with_loopdev($domount, $path, $readonly);
+ $use_loopdev = 1;
+ } else {
+ $mounted_dev = $path;
+ &$domount($path);
+ }
} else {
- die "unsupported storage type '$scfg->{type}'\n";
+ die "storage '$storage' does not support containers\n";
}
return wantarray ? ($path, $use_loopdev, $mounted_dev) : $path;
} else {
sub mkfs {
my ($dev, $rootuid, $rootgid) = @_;
- PVE::Tools::run_command(['mkfs.ext4', '-O', 'mmp',
- '-E', "root_owner=$rootuid:$rootgid",
- $dev]);
+ run_command(
+ [
+ 'mkfs.ext4',
+ '-O',
+ 'mmp',
+ '-E',
+ "root_owner=$rootuid:$rootgid",
+ $dev,
+ ],
+ outfunc => sub {
+ my $line = shift;
+ # a hack to print only the relevant stuff, i.e., the one which could help on repair
+ if ($line =~ /^(Creating filesystem|Filesystem UUID|Superblock backups|\s+\d+, \d)/) {
+ print "$line\n";
+ }
+ },
+ errfunc => sub {
+ my $line = shift;
+ print STDERR "$line\n" if $line && $line !~ /^mke2fs \d\.\d/;
+ }
+ );
}
sub format_disk {
eval {
my $do_format = 0;
- if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs' || $scfg->{type} eq 'cifs' ) {
+ if ($scfg->{content}->{rootdir} && $scfg->{path}) {
if ($size_kb > 0) {
$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw',
undef, $size_kb);
$needs_chown = 1;
}
} elsif ($scfg->{type} eq 'zfspool') {
-
$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol',
undef, $size_kb);
$needs_chown = 1;
- } elsif ($scfg->{type} eq 'drbd' || $scfg->{type} eq 'lvm' || $scfg->{type} eq 'lvmthin') {
-
- $volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb);
- $do_format = 1;
-
- } elsif ($scfg->{type} eq 'rbd') {
-
+ } elsif ($scfg->{content}->{rootdir}) {
$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size_kb);
$do_format = 1;
} else {
- die "unable to create containers on storage type '$scfg->{type}'\n";
+ die "storage '$storage' does not support containers\n";
}
format_disk($storecfg, $volid, $rootuid, $rootgid) if $do_format;
};
sub scan_volids {
my ($cfg, $vmid) = @_;
- my $info = PVE::Storage::vdisk_list($cfg, undef, $vmid);
+ my $info = PVE::Storage::vdisk_list($cfg, undef, $vmid, undef, 'rootdir');
my $all_volumes = {};
foreach my $storeid (keys %$info) {
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);
PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start', 1);
eval {
- PVE::Tools::run_command($cmd);
+ run_command($cmd);
monitor_start($monitor_socket, $vmid) if defined($monitor_socket);
}
}
- eval { PVE::Tools::run_command($cmd, timeout => $shutdown_timeout) };
+ eval { run_command($cmd, timeout => $shutdown_timeout) };
if (my $err = $@) {
warn $@ if $@;
}
# Unshare the mount namespace
die "failed to unshare mount namespace: $!\n"
if !PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
- PVE::Tools::run_command(['mount', '--make-rslave', '/']);
+ run_command(['mount', '--make-rslave', '/']);
return $code->();
});
}
$bwlimit //= 0;
- PVE::Tools::run_command(['/usr/bin/rsync', '--stats', '-X', '-A', '--numeric-ids',
- '-aH', '--whole-file', '--sparse', '--one-file-system',
- "--bwlimit=$bwlimit", "$src/", $dest]);
+ run_command([
+ 'rsync',
+ '--stats',
+ '-X',
+ '-A',
+ '--numeric-ids',
+ '-aH',
+ '--whole-file',
+ '--sparse',
+ '--one-file-system',
+ "--bwlimit=$bwlimit",
+ "$src/",
+ $dest
+ ]);
};
my $err = $@;
while ((system {"fuser"} "fuser", "-s", $dest) == 0) {sleep 1};
foreach my $mount (reverse @mounted) {
- eval { PVE::Tools::run_command(['/bin/umount', $mount], errfunc => sub{})};
+ eval { run_command(['/bin/umount', $mount], errfunc => sub{})};
warn "Can't umount $mount\n" if $@;
}
sub get_lxc_version() {
my $version;
- PVE::Tools::run_command([qw(lxc-start --version)], outfunc => sub {
+ run_command([qw(lxc-start --version)], outfunc => sub {
my ($line) = @_;
# We only parse out major & minor version numbers.
if ($line =~ /^(\d+)\.(\d+)(?:\D.*)?$/) {
sub freeze($) {
my ($vmid) = @_;
- PVE::LXC::CGroup->new($vmid)->freeze_thaw(1);
+ if (PVE::CGroup::cgroup_mode() == 2) {
+ PVE::LXC::Command::freeze($vmid, 30);
+ } else {
+ PVE::LXC::CGroup->new($vmid)->freeze_thaw(1);
+ }
}
sub thaw($) {
my ($vmid) = @_;
- PVE::LXC::CGroup->new($vmid)->freeze_thaw(0);
+ if (PVE::CGroup::cgroup_mode() == 2) {
+ PVE::LXC::Command::unfreeze($vmid, 30);
+ } else {
+ PVE::LXC::CGroup->new($vmid)->freeze_thaw(0);
+ }
}
1;