]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Disks/Directory.pm
api: disks: add DELETE endpoint for directory, lvm, lvmthin, zfs
[pve-storage.git] / PVE / API2 / Disks / Directory.pm
index f554d6c9a2e0ef5130f8a0765e0a751302d27052..36cebbc9d2a271da62460c15c530ea361a6e797d 100644 (file)
@@ -3,13 +3,16 @@ package PVE::API2::Disks::Directory;
 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);
 
@@ -110,7 +113,7 @@ __PACKAGE__->register_method ({
            properties => {
                unitfile => {
                    type => 'string',
-                   description => 'The path of the mount unit'.,
+                   description => 'The path of the mount unit.',
                },
                path => {
                    type => 'string',
@@ -138,6 +141,7 @@ __PACKAGE__->register_method ({
 
        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);
@@ -171,7 +175,7 @@ __PACKAGE__->register_method ({
            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.",
@@ -201,29 +205,38 @@ __PACKAGE__->register_method ({
        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);
 
@@ -262,6 +275,12 @@ __PACKAGE__->register_method ({
 
                $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]);
@@ -270,7 +289,7 @@ __PACKAGE__->register_method ({
                    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,
@@ -279,12 +298,51 @@ __PACKAGE__->register_method ({
                    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;