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);
file_set_contents($filename, $content);
};
-sub systemd_escape {
- my ($val) = @_;
-
- # NOTE: this is not complete, but enough for our needs. normally all
- # characters which are not alpha-numerical, '.' or '_' would need escaping
- $val =~ s/\-/\\x2d/g;
-
- return $val;
-}
-
-sub systemd_unescape {
- my ($val) = @_;
-
- $val =~ s/\\x([a-fA-F0-9]{2})/chr(hex($1))/eg;
-
- return $val;
-}
-
__PACKAGE__->register_method ({
name => 'index',
path => '',
dir_glob_foreach('/etc/systemd/system', '^mnt-pve-(.+)\.mount$', sub {
my ($filename, $storid) = @_;
- $storid = systemd_unescape($storid);
+ $storid = PVE::Systemd::unescape_unit($storid);
my $unitfile = "/etc/systemd/system/$filename";
my $unit = $read_ini->($unitfile);
my $worker = sub {
my $path = "/mnt/pve/$name";
- my $mountunitname = "mnt-pve-".systemd_escape($name).".mount";
+ my $mountunitname = PVE::Systemd::escape_unit($path, 1) . ".mount";
my $mountunitpath = "/etc/systemd/system/$mountunitname";
PVE::Diskmanage::locked_disk_action(sub {
- # create partition
- my $cmd = [$SGDISK, '-n1', '-t1:8300', $dev];
- print "# ", join(' ', @$cmd), "\n";
- run_command($cmd);
+ PVE::Diskmanage::assert_disk_unused($dev);
- my ($devname) = $dev =~ m|^/dev/(.*)$|;
- my $part = "/dev/";
- dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.+/, sub {
- my ($partition) = @_;
- $part .= $partition;
- });
+ my $part = $dev;
+
+ 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]);
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;