use strict;
use warnings;
+use POSIX;
+
use PVE::Diskmanage;
use PVE::JSONSchema qw(get_standard_option);
-use PVE::API2::Storage::Config;
+use PVE::RESTHandler;
+use PVE::RPCEnvironment;
+use PVE::Systemd;
use PVE::Tools qw(run_command trim file_set_contents file_get_contents dir_glob_foreach lock_file);
-use PVE::RPCEnvironment;
-use PVE::RESTHandler;
+use PVE::API2::Storage::Config;
use base qw(PVE::RESTHandler);
properties => {
unitfile => {
type => 'string',
- description => 'The path of the mount unit'.,
+ description => 'The path of the mount unit.',
},
path => {
type => 'string',
dir_glob_foreach('/etc/systemd/system', '^mnt-pve-(.+)\.mount$', sub {
my ($filename, $storid) = @_;
+ $storid = PVE::Systemd::unescape_unit($storid);
my $unitfile = "/etc/systemd/system/$filename";
my $unit = $read_ini->($unitfile);
name => get_standard_option('pve-storage-id'),
device => {
type => 'string',
- description => 'The block device you want to create the thinpool on.',
+ description => 'The block device you want to create the filesystem on.',
},
add_storage => {
description => "Configure storage using the directory.",
my $type = $param->{filesystem} // 'ext4';
$dev = PVE::Diskmanage::verify_blockdev_path($dev);
- die "device $dev is already in use\n" if PVE::Diskmanage::disk_is_used($dev);
-
- my $cfg = PVE::Storage::config();
-
- if (my $scfg = PVE::Storage::storage_config($cfg, $name, 1)) {
- die "storage ID '$name' already defined\n";
- }
+ PVE::Diskmanage::assert_disk_unused($dev);
+ PVE::Storage::assert_sid_unused($name) if $param->{add_storage};
my $worker = sub {
my $path = "/mnt/pve/$name";
- my $mountunitname = "mnt-pve-$name.mount";
+ my $mountunitname = PVE::Systemd::escape_unit($path, 1) . ".mount";
my $mountunitpath = "/etc/systemd/system/$mountunitname";
- lock_file('/run/lock/pve-diskmanage.lck', undef, sub {
- # create partition
- my $cmd = [$SGDISK, '-n1', '-t1:8300', $dev];
- print "# ", join(' ', @$cmd), "\n";
- run_command($cmd);
+ PVE::Diskmanage::locked_disk_action(sub {
+ PVE::Diskmanage::assert_disk_unused($dev);
+
+ my $part = $dev;
- my $part = "${dev}1";
+ if (PVE::Diskmanage::is_partition($dev)) {
+ eval { PVE::Diskmanage::change_parttype($dev, '8300'); };
+ warn $@ if $@;
+ } else {
+ # create partition
+ my $cmd = [$SGDISK, '-n1', '-t1:8300', $dev];
+ print "# ", join(' ', @$cmd), "\n";
+ run_command($cmd);
+
+ my ($devname) = $dev =~ m|^/dev/(.*)$|;
+ $part = "/dev/";
+ dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.+/, sub {
+ my ($partition) = @_;
+ $part .= $partition;
+ });
+ }
# create filesystem
- $cmd = [$MKFS, '-t', $type, $part];
+ my $cmd = [$MKFS, '-t', $type, $part];
print "# ", join(' ', @$cmd), "\n";
run_command($cmd);
$write_ini->($ini, $mountunitpath);
+ # FIXME: Remove once we depend on systemd >= v249.
+ # Work around udev bug https://github.com/systemd/systemd/issues/18525 to ensure the
+ # udev database is updated and the $uuid_path symlink is actually created!
+ eval { run_command(['udevadm', 'trigger', $part]); };
+ warn $@ if $@;
+
run_command(['systemctl', 'daemon-reload']);
run_command(['systemctl', 'enable', $mountunitname]);
run_command(['systemctl', 'start', $mountunitname]);
my $storage_params = {
type => 'dir',
storage => $name,
- content => 'rootdir,images,iso,backup,vztmpl',
+ content => 'rootdir,images,iso,backup,vztmpl,snippets',
is_mountpoint => 1,
path => $path,
nodes => $node,
PVE::API2::Storage::Config->create($storage_params);
}
});
-
- die $@ if $@;
};
-
return $rpcenv->fork_worker('dircreate', $name, $user, $worker);
}});
+__PACKAGE__->register_method ({
+ name => 'delete',
+ path => '{name}',
+ method => 'DELETE',
+ proxyto => 'node',
+ protected => 1,
+ permissions => {
+ check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
+ },
+ description => "Unmounts the storage and removes the mount unit.",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ node => get_standard_option('pve-node'),
+ name => get_standard_option('pve-storage-id'),
+ },
+ },
+ returns => { type => 'string' },
+ code => sub {
+ my ($param) = @_;
+
+ my $rpcenv = PVE::RPCEnvironment::get();
+ my $user = $rpcenv->get_user();
+
+ my $name = $param->{name};
+
+ my $worker = sub {
+ my $path = "/mnt/pve/$name";
+ my $mountunitname = PVE::Systemd::escape_unit($path, 1) . ".mount";
+ my $mountunitpath = "/etc/systemd/system/$mountunitname";
+
+ PVE::Diskmanage::locked_disk_action(sub {
+ run_command(['systemctl', 'stop', $mountunitname]);
+ run_command(['systemctl', 'disable', $mountunitname]);
+
+ unlink $mountunitpath or $! == ENOENT or die "cannot remove $mountunitpath - $!\n";
+ });
+ };
+
+ return $rpcenv->fork_worker('dirremove', $name, $user, $worker);
+ }});
+
1;