]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Config.pm
cifs plugin cleanups
[pve-storage.git] / PVE / API2 / Storage / Config.pm
index e5c57ee11c760a3fab89b124b0cc6cbbc880e71e..7856f45bd3886223998ae54d0f9a14b1547a56a4 100755 (executable)
@@ -36,6 +36,22 @@ my $api_storage_config = sub {
     return $scfg;
 };
 
+my $cifs_cred_file_name = sub {
+    my ($storeid) = @_;
+
+    return "/etc/pve/priv/${storeid}.cred";
+};
+
+my $set_cifs_credentials = sub {
+    my ($password, $storeid) = @_;
+
+    my $cred_file = $cifs_cred_file_name->($storeid);
+
+    PVE::Tools::file_set_contents($cred_file, "password=$password\n");
+
+    return $cred_file;
+};
+
 __PACKAGE__->register_method ({
     name => 'index', 
     path => '',
@@ -70,7 +86,7 @@ __PACKAGE__->register_method ({
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
 
-       my $cfg = cfs_read_file("storage.cfg");
+       my $cfg = PVE::Storage::config();
 
        my @sids = PVE::Storage::storage_ids($cfg);
 
@@ -105,7 +121,7 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $cfg = cfs_read_file("storage.cfg");
+       my $cfg = PVE::Storage::config();
 
        return &$api_storage_config($cfg, $param->{storage});
     }});
@@ -127,6 +143,13 @@ __PACKAGE__->register_method ({
        my $type = extract_param($param, 'type');
        my $storeid = extract_param($param, 'storage');
 
+       # revent an empty nodelist.
+       # fix me in section config create never need an empty entity.
+       delete $param->{nodes} if !$param->{nodes};
+
+       my $password = extract_param($param, 'password')
+           if $type eq 'cifs' && $param->{username};
+
        if ($param->{portal}) {
            $param->{portal} = PVE::Storage::resolv_portal($param->{portal});
        }
@@ -137,7 +160,7 @@ __PACKAGE__->register_method ({
         PVE::Storage::lock_storage_config(
            sub {
 
-               my $cfg = cfs_read_file('storage.cfg');
+               my $cfg = PVE::Storage::config();
 
                if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
                    die "storage ID '$storeid' already defined\n";
@@ -162,15 +185,40 @@ __PACKAGE__->register_method ({
                    PVE::Storage::activate_storage($cfg, $baseid);
 
                    PVE::Storage::LVMPlugin::lvm_create_volume_group($path, $opts->{vgname}, $opts->{shared});
+               } elsif ($type eq 'rbd' && !defined($opts->{monhost})) {
+                   my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
+                   my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
+
+                   die "ceph authx keyring file for storage '$storeid' already exists!\n"
+                       if -e $ceph_storage_keyring;
+
+                   eval {
+                       mkdir '/etc/pve/priv/ceph';
+                       PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
+                   };
+                   if (my $err = $@) {
+                       unlink $ceph_storage_keyring;
+                       die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
+                   }
                }
-
-               # try to activate if enabled on local node,
-               # we only do this to detect errors/problems sooner
-               if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
-                   PVE::Storage::activate_storage($cfg, $storeid);
+               # create a password file in /etc/pve/priv,
+               # this file is used as a cert_file at mount time.
+               my $cred_file = &$set_cifs_credentials($password, $storeid)
+                   if defined($password);
+
+               eval {
+                   # try to activate if enabled on local node,
+                   # we only do this to detect errors/problems sooner
+                   if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
+                       PVE::Storage::activate_storage($cfg, $storeid);
+                   }
+               };
+               if(my $err = $@) {
+                   unlink $cred_file if defined($cred_file);
+                   die $err;
                }
 
-               cfs_write_file('storage.cfg', $cfg);
+               PVE::Storage::write_config($cfg);
            
            }, "create storage failed");
 
@@ -197,7 +245,7 @@ __PACKAGE__->register_method ({
         PVE::Storage::lock_storage_config(
         sub {
 
-           my $cfg = cfs_read_file('storage.cfg');
+           my $cfg = PVE::Storage::config();
 
            PVE::SectionConfig::assert_if_modified($cfg, $digest);
 
@@ -210,7 +258,7 @@ __PACKAGE__->register_method ({
                $scfg->{$k} = $opts->{$k};
            }
 
-           cfs_write_file('storage.cfg', $cfg);
+           PVE::Storage::write_config($cfg);
 
            }, "update storage failed");
 
@@ -229,7 +277,9 @@ __PACKAGE__->register_method ({
     parameters => {
        additionalProperties => 0,
        properties => { 
-           storage => get_standard_option('pve-storage-id'),
+           storage => get_standard_option('pve-storage-id', {
+                completion => \&PVE::Storage::complete_storage,
+            }),
        },
     },
     returns => { type => 'null' },
@@ -241,17 +291,28 @@ __PACKAGE__->register_method ({
         PVE::Storage::lock_storage_config(
            sub {
 
-               my $cfg = cfs_read_file('storage.cfg');
+               my $cfg = PVE::Storage::config();
 
-               die "storage '$storeid' does not exist\n"
-                   if !($cfg->{ids}->{$storeid});
+               my $scfg = PVE::Storage::storage_config($cfg, $storeid);
 
                die "can't remove storage - storage is used as base of another storage\n"
                    if PVE::Storage::storage_is_used($cfg, $storeid);
 
+               if ($scfg->{type} eq 'cifs')  {
+                   my $cred_file = $cifs_cred_file_name->($storeid);
+                   if (-f $cred_file) {
+                       unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
+                   }
+               } elsif ($scfg->{type} eq 'rbd' && !defined($scfg->{monhost})) {
+                   my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
+                   if (-f $ceph_storage_keyring) {
+                       unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
+                   }
+               }
+
                delete $cfg->{ids}->{$storeid};
 
-               cfs_write_file('storage.cfg', $cfg);
+               PVE::Storage::write_config($cfg);
 
            }, "delete storage failed");