]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/LXC.pm
add PVE::LXC::freeze_thaw
[pve-container.git] / src / PVE / LXC.pm
index b4ffc9bdc5508c3f058db6330b2bf0971b7c09c3..86a96a902d12e1fa255b51eec34daaabcdc72fdb 100644 (file)
@@ -20,8 +20,8 @@ use PVE::SafeSyslog;
 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
+    dir_glob_foreach file_get_contents file_set_contents
+    AT_FDCWD O_PATH $IPV4RE $IPV6RE
 );
 use PVE::CpuSet;
 use PVE::Network;
@@ -206,7 +206,7 @@ sub vmstatus {
            $d->{disk} = 0;
            # use 4GB by default ??
            if (my $rootfs = $conf->{rootfs}) {
-               my $rootinfo = PVE::LXC::Config->parse_ct_rootfs($rootfs);
+               my $rootinfo = PVE::LXC::Config->parse_volume('rootfs', $rootfs);
                $d->{maxdisk} = $rootinfo->{size} || (4*1024*1024*1024);
            } else {
                $d->{maxdisk} = 4*1024*1024*1024;
@@ -401,19 +401,9 @@ sub parse_ipv4_cidr {
     die "unable to parse ipv4 address/mask\n";
 }
 
+# Deprecated. Use `PVE::LXC::CGroup::get_cgroup_controllers()` instead.
 sub get_cgroup_subsystems {
-       my $v1 = {};
-       my $v2 = 0;
-       my $data = PVE::Tools::file_get_contents('/proc/self/cgroup');
-       while ($data =~ /^\d+:([^:\n]*):.*$/gm) {
-               my $type = $1;
-               if (length($type)) {
-                       $v1->{$_} = 1 foreach split(/,/, $type);
-               } else {
-                       $v2 = 1;
-               }
-       }
-       return wantarray ? ($v1, $v2) : $v1;
+    PVE::LXC::CGroup::get_v1_controllers();
 }
 
 # With seccomp trap to userspace we now have the ability to optionally forward
@@ -687,7 +677,7 @@ sub update_lxc_config {
     die "missing 'rootfs' configuration\n"
        if !defined($conf->{rootfs});
 
-    my $mountpoint = PVE::LXC::Config->parse_ct_rootfs($conf->{rootfs});
+    my $mountpoint = PVE::LXC::Config->parse_volume('rootfs', $conf->{rootfs});
 
     $raw .= "lxc.rootfs.path = $dir/rootfs\n";
 
@@ -711,13 +701,13 @@ sub update_lxc_config {
     if (my $lxcconf = $conf->{lxc}) {
        foreach my $entry (@$lxcconf) {
            my ($k, $v) = @$entry;
-           $had_cpuset = 1 if $k eq 'lxc.cgroup.cpuset.cpus';
+           $had_cpuset = 1 if $k eq 'lxc.cgroup.cpuset.cpus' || $k eq 'lxc.cgroup2.cpuset.cpus';
            $raw .= "$k = $v\n";
        }
     }
 
     my $cpuset;
-    my $cpuset_cgroup = eval { PVE::LXC::CGroup::cpuset_controller_path() };
+    my ($cpuset_cgroup, $cpuset_version) = eval { PVE::LXC::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);
@@ -730,7 +720,8 @@ sub update_lxc_config {
            $cpuset->delete($members[$randidx]);
            splice(@members, $randidx, 1); # keep track of the changes
        }
-       $raw .= "lxc.cgroup.cpuset.cpus = ".$cpuset->short_string()."\n";
+       my $ver = $cpuset_version == 1 ? '' : '2';
+       $raw .= "lxc.cgroup$ver.cpuset.cpus = ".$cpuset->short_string()."\n";
     }
 
     File::Path::mkpath("$dir/rootfs");
@@ -832,7 +823,7 @@ sub delete_mountpoint_volume {
 sub destroy_lxc_container {
     my ($storage_cfg, $vmid, $conf, $replacement_conf) = @_;
 
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume_full($conf, {include_unused => 1}, sub {
        my ($ms, $mountpoint) = @_;
        delete_mountpoint_volume($storage_cfg, $vmid, $mountpoint->{volume});
     });
@@ -1163,7 +1154,7 @@ sub template_create {
 
     my $storecfg = PVE::Storage::config();
 
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
        my ($ms, $mountpoint) = @_;
 
        my $volid = $mountpoint->{volume};
@@ -1172,7 +1163,7 @@ sub template_create {
            if !PVE::Storage::volume_has_feature($storecfg, 'template', $volid);
     });
 
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
+    PVE::LXC::Config->foreach_volume($conf, sub {
        my ($ms, $mountpoint) = @_;
 
        my $volid = $mountpoint->{volume};
@@ -1199,8 +1190,7 @@ sub check_ct_modify_config_perm {
        } elsif ($opt eq 'rootfs' || $opt =~ /^mp\d+$/) {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
            return if $delete;
-           my $data = $opt eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($newconf->{$opt})
-                                       : PVE::LXC::Config->parse_ct_mountpoint($newconf->{$opt});
+           my $data = PVE::LXC::Config->parse_volume($opt, $newconf->{$opt});
            raise_perm_exc("mount point type $data->{type} is only allowed for root\@pam")
                if $data->{type} ne 'volume';
        } elsif ($opt eq 'memory' || $opt eq 'swap') {
@@ -1237,7 +1227,7 @@ sub umount_all {
 
     my $res = 1;
 
-    PVE::LXC::Config->foreach_mountpoint_reverse($conf, sub {
+    PVE::LXC::Config->foreach_volume_full($conf, {'reverse' => 1}, sub {
        my ($ms, $mountpoint) = @_;
 
        my $volid = $mountpoint->{volume};
@@ -1278,7 +1268,7 @@ sub mount_all {
     my (undef, $rootuid, $rootgid) = parse_id_maps($conf);
 
     eval {
-       PVE::LXC::Config->foreach_mountpoint($conf, sub {
+       PVE::LXC::Config->foreach_volume($conf, sub {
            my ($ms, $mountpoint) = @_;
 
            $mountpoint->{ro} = 0 if $ignore_ro;
@@ -1885,7 +1875,7 @@ sub create_disks {
        my (undef, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
        my $chown_vollist = [];
 
-       PVE::LXC::Config->foreach_mountpoint($settings, sub {
+       PVE::LXC::Config->foreach_volume($settings, sub {
            my ($ms, $mountpoint) = @_;
 
            my $volid = $mountpoint->{volume};
@@ -1949,7 +1939,7 @@ sub update_disksize {
        }
     };
 
-    PVE::LXC::Config->foreach_mountpoint($conf, $update_mp);
+    PVE::LXC::Config->foreach_volume($conf, $update_mp);
 
     return $changes;
 }
@@ -2183,6 +2173,7 @@ sub vm_start {
     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
     }
 
@@ -2385,4 +2376,12 @@ sub get_lxc_version() {
     return $version->@*;
 }
 
+sub freeze_thaw($$) {
+    my ($vmid, $freeze) = @_;
+
+    my $cgroup = PVE::LXC::CGroup->new($vmid);
+
+    $cgroup->freeze_thaw($freeze);
+}
+
 1;