my ($filename, $conf) = @_;
delete $conf->{snapstate}; # just to be sure
- my $volidlist = get_vm_volumes($conf);
+ my $volidlist = PVE::LXC::Config->get_vm_volumes($conf);
my $used_volids = {};
foreach my $vid (@$volidlist) {
$used_volids->{$vid} = 1;
return $list;
}
-sub classify_mountpoint {
- my ($vol) = @_;
- if ($vol =~ m!^/!) {
- return 'device' if $vol =~ m!^/dev/!;
- return 'bind';
- }
- return 'volume';
-}
-
my $parse_ct_mountpoint_full = sub {
my ($desc, $data, $noerr) = @_;
$res->{size} = $size;
}
- $res->{type} = classify_mountpoint($res->{volume});
+ $res->{type} = PVE::LXC::Config->classify_mountpoint($res->{volume});
return $res;
};
$raw .= "lxc.id_map = g 0 100000 65536\n";
}
- if (!has_dev_console($conf)) {
+ if (!PVE::LXC::Config->has_dev_console($conf)) {
$raw .= "lxc.console = none\n";
$raw .= "lxc.cgroup.devices.deny = c 5:1 rwm\n";
}
return join(' ', @list);
}
-sub is_volume_in_use {
- my ($config, $volid, $include_snapshots) = @_;
- my $used = 0;
-
- foreach_mountpoint($config, sub {
- my ($ms, $mountpoint) = @_;
- return if $used;
- if ($mountpoint->{type} eq 'volume' && $mountpoint->{volume} eq $volid) {
- $used = 1;
- }
- });
-
- my $snapshots = $config->{snapshots};
- if ($include_snapshots && $snapshots) {
- foreach my $snap (keys %$snapshots) {
- $used ||= is_volume_in_use($snapshots->{$snap}, $volid);
- }
- }
-
- return $used;
-}
-
-sub add_unused_volume {
- my ($config, $volid) = @_;
-
- my $key;
- for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
- my $test = "unused$ind";
- if (my $vid = $config->{$test}) {
- return if $vid eq $volid; # do not add duplicates
- } else {
- $key = $test;
- }
- }
-
- die "Too many unused volumes - please delete them first.\n" if !$key;
-
- $config->{$key} = $volid;
-
- return $key;
-}
-
sub update_pct_config {
my ($vmid, $conf, $running, $param, $delete) = @_;
my $mp = parse_ct_mountpoint($conf->{$opt});
delete $conf->{$opt};
if ($mp->{type} eq 'volume') {
- add_unused_volume($conf, $mp->{volume});
+ PVE::LXC::Config->add_unused_volume($conf, $mp->{volume});
}
} elsif ($opt eq 'unprivileged') {
die "unable to delete read-only option: '$opt'\n";
if (defined($old)) {
my $mp = parse_ct_mountpoint($old);
if ($mp->{type} eq 'volume') {
- add_unused_volume($conf, $mp->{volume});
+ PVE::LXC::Config->add_unused_volume($conf, $mp->{volume});
}
}
$new_disks = 1;
if (defined($old)) {
my $mp = parse_ct_rootfs($old);
if ($mp->{type} eq 'volume') {
- add_unused_volume($conf, $mp->{volume});
+ PVE::LXC::Config->add_unused_volume($conf, $mp->{volume});
}
}
my $mp = parse_ct_rootfs($value);
foreach my $volume (@deleted_volumes) {
next if $used_volids->{$volume}; # could have been re-added, too
# also check for references in snapshots
- next if is_volume_in_use($conf, $volume, 1);
+ next if PVE::LXC::Config->is_volume_in_use($conf, $volume, 1);
delete_mountpoint_volume($storage_cfg, $vmid, $volume);
}
}
}
}
-sub has_dev_console {
- my ($conf) = @_;
-
- return !(defined($conf->{console}) && !$conf->{console});
-}
-
sub get_tty_count {
my ($conf) = @_;
sub delete_mountpoint_volume {
my ($storage_cfg, $vmid, $volume) = @_;
- return if classify_mountpoint($volume) ne 'volume';
+ return if PVE::LXC::Config->classify_mountpoint($volume) ne 'volume';
my ($vtype, $name, $owner) = PVE::Storage::parse_volname($storage_cfg, $volume);
PVE::Storage::vdisk_free($storage_cfg, $volume) if $vmid == $owner;
sub destroy_lxc_container {
my ($storage_cfg, $vmid, $conf) = @_;
- foreach_mountpoint($conf, sub {
+ PVE::LXC::Config->foreach_mountpoint($conf, sub {
my ($ms, $mountpoint) = @_;
delete_mountpoint_volume($storage_cfg, $vmid, $mountpoint->{volume});
});
eval {
if (!$keepActive) {
- my $vollist = get_vm_volumes($conf);
+ my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
PVE::Storage::deactivate_volumes($storage_cfg, $vollist);
}
};
PVE::LXC::Config->write_config($vmid, $conf);
}
-sub mountpoint_names {
- my ($reverse) = @_;
-
- my @names = ('rootfs');
-
- for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
- push @names, "mp$i";
- }
-
- return $reverse ? reverse @names : @names;
-}
-
-
-sub foreach_mountpoint_full {
- my ($conf, $reverse, $func) = @_;
-
- foreach my $key (mountpoint_names($reverse)) {
- my $value = $conf->{$key};
- next if !defined($value);
- my $mountpoint = $key eq 'rootfs' ? parse_ct_rootfs($value, 1) : parse_ct_mountpoint($value, 1);
- next if !defined($mountpoint);
-
- &$func($key, $mountpoint);
- }
-}
-
-sub foreach_mountpoint {
- my ($conf, $func) = @_;
-
- foreach_mountpoint_full($conf, 0, $func);
-}
-
-sub foreach_mountpoint_reverse {
- my ($conf, $func) = @_;
-
- foreach_mountpoint_full($conf, 1, $func);
-}
-
sub check_ct_modify_config_perm {
my ($rpcenv, $authuser, $vmid, $pool, $newconf, $delete) = @_;
my ($vmid, $storage_cfg, $conf, $noerr) = @_;
my $rootdir = "/var/lib/lxc/$vmid/rootfs";
- my $volid_list = get_vm_volumes($conf);
+ my $volid_list = PVE::LXC::Config->get_vm_volumes($conf);
- foreach_mountpoint_reverse($conf, sub {
+ PVE::LXC::Config->foreach_mountpoint_reverse($conf, sub {
my ($ms, $mountpoint) = @_;
my $volid = $mountpoint->{volume};
my $rootdir = "/var/lib/lxc/$vmid/rootfs";
File::Path::make_path($rootdir);
- my $volid_list = get_vm_volumes($conf);
+ my $volid_list = PVE::LXC::Config->get_vm_volumes($conf);
PVE::Storage::activate_volumes($storage_cfg, $volid_list);
eval {
- foreach_mountpoint($conf, sub {
+ PVE::LXC::Config->foreach_mountpoint($conf, sub {
my ($ms, $mountpoint) = @_;
mountpoint_mount($mountpoint, $rootdir, $storage_cfg);
die "unsupported storage";
}
-sub get_vm_volumes {
- my ($conf, $excludes) = @_;
-
- my $vollist = [];
-
- foreach_mountpoint($conf, sub {
- my ($ms, $mountpoint) = @_;
-
- return if $excludes && $ms eq $excludes;
-
- my $volid = $mountpoint->{volume};
-
- return if !$volid || $mountpoint->{type} ne 'volume';
-
- my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
- return if !$sid;
-
- push @$vollist, $volid;
- });
-
- return $vollist;
-}
-
sub mkfs {
my ($dev, $rootuid, $rootgid) = @_;
my (undef, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
my $chown_vollist = [];
- foreach_mountpoint($settings, sub {
+ PVE::LXC::Config->foreach_mountpoint($settings, sub {
my ($ms, $mountpoint) = @_;
my $volid = $mountpoint->{volume};
my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
my $err;
- PVE::LXC::foreach_mountpoint($conf, sub {
+ $class->foreach_mountpoint($conf, sub {
my ($ms, $mountpoint) = @_;
return if $err; # skip further test
sub __snapshot_foreach_volume {
my ($class, $conf, $func) = @_;
- PVE::LXC::foreach_mountpoint($conf, $func);
+ $class->foreach_mountpoint($conf, $func);
}
# END implemented abstract methods from PVE::AbstractConfig
+sub classify_mountpoint {
+ my ($class, $vol) = @_;
+ if ($vol =~ m!^/!) {
+ return 'device' if $vol =~ m!^/dev/!;
+ return 'bind';
+ }
+ return 'volume';
+}
+
+sub is_volume_in_use {
+ my ($class, $config, $volid, $include_snapshots) = @_;
+ my $used = 0;
+
+ $class->foreach_mountpoint($config, sub {
+ my ($ms, $mountpoint) = @_;
+ return if $used;
+ $used = $mountpoint->{type} eq 'volume' && $mountpoint->{volume} eq $volid;
+ });
+
+ my $snapshots = $config->{snapshots};
+ if ($include_snapshots && $snapshots) {
+ foreach my $snap (keys %$snapshots) {
+ $used ||= $class->is_volume_in_use($snapshots->{$snap}, $volid);
+ }
+ }
+
+ return $used;
+}
+
+sub has_dev_console {
+ my ($class, $conf) = @_;
+
+ return !(defined($conf->{console}) && !$conf->{console});
+}
+
+sub mountpoint_names {
+ my ($class, $reverse) = @_;
+
+ my @names = ('rootfs');
+
+ for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
+ push @names, "mp$i";
+ }
+
+ return $reverse ? reverse @names : @names;
+}
+
+sub foreach_mountpoint_full {
+ my ($class, $conf, $reverse, $func) = @_;
+
+ foreach my $key ($class->mountpoint_names($reverse)) {
+ my $value = $conf->{$key};
+ next if !defined($value);
+ my $mountpoint = $key eq 'rootfs' ? PVE::LXC::parse_ct_rootfs($value, 1) : PVE::LXC::parse_ct_mountpoint($value, 1);
+ next if !defined($mountpoint);
+
+ &$func($key, $mountpoint);
+ }
+}
+
+sub foreach_mountpoint {
+ my ($class, $conf, $func) = @_;
+
+ $class->foreach_mountpoint_full($conf, 0, $func);
+}
+
+sub foreach_mountpoint_reverse {
+ my ($class, $conf, $func) = @_;
+
+ $class->foreach_mountpoint_full($conf, 1, $func);
+}
+
+sub get_vm_volumes {
+ my ($class, $conf, $excludes) = @_;
+
+ my $vollist = [];
+
+ $class->foreach_mountpoint($conf, sub {
+ my ($ms, $mountpoint) = @_;
+
+ return if $excludes && $ms eq $excludes;
+
+ my $volid = $mountpoint->{volume};
+ return if !$volid || $mountpoint->{type} ne 'volume';
+
+ my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+ return if !$sid;
+
+ push @$vollist, $volid;
+ });
+
+ return $vollist;
+}
+
return 1;