use PVE::INotify;
use PVE::JSONSchema qw(get_standard_option);
use PVE::Tools qw(
- dir_glob_foreach file_get_contents file_set_contents lock_file
- lock_file_full 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::GuestHelpers qw(safe_string_ne safe_num_ne safe_boolean_ne);
use PVE::LXC::Tools;
use PVE::LXC::CGroup;
+use PVE::LXC::Monitor;
+use PVE::CGroup;
use Time::HiRes qw (gettimeofday);
my $have_sdn;
$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}) {
die "unable to parse ipv4 address/mask\n";
}
-# Deprecated. Use `PVE::LXC::CGroup::get_cgroup_controllers()` instead.
+# Deprecated. Use `PVE::CGroup::get_cgroup_controllers()` instead.
sub get_cgroup_subsystems {
- PVE::LXC::CGroup::get_v1_controllers();
+ PVE::CGroup::get_v1_controllers();
}
# With seccomp trap to userspace we now have the ability to optionally forward
}
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 $@;
}
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 {
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);
return [];
}
+my sub print_ct_stderr_log {
+ my ($vmid) = @_;
+ my $log = eval { file_get_contents("/run/pve/ct-$vmid.stderr") };
+ return if !$log;
+
+ while ($log =~ /^\h*(lxc-start:?\s+$vmid:?\s*\S+\s*)?(.*?)\h*$/gm) {
+ my $line = $2;
+ print STDERR "$line\n";
+ }
+}
+
+my sub monitor_state_change($$) {
+ my ($monitor_socket, $vmid) = @_;
+ die "no monitor socket\n" if !defined($monitor_socket);
+
+ while (1) {
+ my ($type, $name, $value) = PVE::LXC::Monitor::read_lxc_message($monitor_socket);
+
+ die "monitor socket: got EOF\n" if !defined($type);
+
+ next if $name ne "$vmid" || $type ne 'STATE';
+
+ if ($value eq PVE::LXC::Monitor::STATE_STARTING) {
+ alarm(0); # don't timeout after seeing the starting state
+ } elsif ($value eq PVE::LXC::Monitor::STATE_ABORTING ||
+ $value eq PVE::LXC::Monitor::STATE_STOPPING ||
+ $value eq PVE::LXC::Monitor::STATE_STOPPED) {
+ return 0;
+ } elsif ($value eq PVE::LXC::Monitor::STATE_RUNNING) {
+ return 1;
+ } else {
+ warn "unexpected message from monitor socket - " .
+ "type: '$type' - value: '$value'\n";
+ }
+ }
+}
+my sub monitor_start($$) {
+ my ($monitor_socket, $vmid) = @_;
+
+ my $success = eval {
+ PVE::Tools::run_with_timeout(10, \&monitor_state_change, $monitor_socket, $vmid)
+ };
+ if (my $err = $@) {
+ warn "problem with monitor socket, but continuing anyway: $err\n";
+ } elsif (!$success) {
+ print_ct_stderr_log($vmid);
+ die "startup for container '$vmid' failed\n";
+ }
+}
+
sub vm_start {
- my ($vmid, $conf, $skiplock) = @_;
+ my ($vmid, $conf, $skiplock, $debug) = @_;
# apply pending changes while starting
if (scalar(keys %{$conf->{pending}})) {
my $storecfg = PVE::Storage::config();
PVE::LXC::Config->vmconfig_apply_pending($vmid, $conf, $storecfg);
+ PVE::LXC::Config->write_config($vmid, $conf);
$conf = PVE::LXC::Config->load_config($vmid); # update/reload
}
close($fh);
}
- my $cmd = ['systemctl', 'start', "pve-container\@$vmid"];
+ my $storage_cfg = PVE::Storage::config();
+ my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
+
+ PVE::Storage::activate_volumes($storage_cfg, $vollist);
+
+ my $monitor_socket = eval { PVE::LXC::Monitor::get_monitor_socket() };
+ warn $@ if $@;
+
+ unlink "/run/pve/ct-$vmid.stderr"; # systemd does not truncate log files
+
+ my $is_debug = $debug || (!defined($debug) && $conf->{debug});
+ my $base_unit = $is_debug ? 'pve-container-debug' : 'pve-container';
+
+ my $cmd = ['systemctl', 'start', "$base_unit\@$vmid"];
PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start', 1);
- eval { PVE::Tools::run_command($cmd); };
+ eval {
+ run_command($cmd);
+
+ monitor_start($monitor_socket, $vmid) if defined($monitor_socket);
+
+ # if debug is requested, print the log it also when the start succeeded
+ print_ct_stderr_log($vmid) if $is_debug;
+ };
if (my $err = $@) {
unlink $skiplock_flag_fn;
die $err;
}
}
- 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.*)?$/) {
return $version->@*;
}
+sub freeze($) {
+ my ($vmid) = @_;
+ 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) = @_;
+ if (PVE::CGroup::cgroup_mode() == 2) {
+ PVE::LXC::Command::unfreeze($vmid, 30);
+ } else {
+ PVE::LXC::CGroup->new($vmid)->freeze_thaw(0);
+ }
+}
+
1;