[pve-devel] [PATCH][pve-manager] Changes set_userpasswd method

Игорь Шестаков shine at selectel.ru
Fri Oct 25 15:37:19 CEST 2013


Done

diff --git a/PVE/API2/OpenVZ.pm b/PVE/API2/OpenVZ.pm
index 06fe60b..eeba1a2 100644
--- a/PVE/API2/OpenVZ.pm
+++ b/PVE/API2/OpenVZ.pm
@@ -413,7 +413,7 @@ __PACKAGE__->register_method({

  # hack: vzctl '--userpasswd' starts the CT, but we want
  # to avoid that for create
- PVE::OpenVZ::set_rootpasswd($private, $password)
+ PVE::OpenVZ::set_rootpasswd($private, $password, $vmid)
     if defined($password);
     }



diff --git a/PVE/OpenVZ.pm b/PVE/OpenVZ.pm
index a16d4fc..ea0717f 100644
--- a/PVE/OpenVZ.pm
+++ b/PVE/OpenVZ.pm
@@ -1243,24 +1243,55 @@ sub replacepw {
     }
 }

+sub ploop_mount {
+    my ($rootdisk, $vmid) = @_;
+ my $fstype = "ext4";
+ my $tmpdir = "/tmp/$vmid";
+    if (-f $rootdisk) {
+        #create tmp dir
+        mkdir $tmpdir;
+ #mount image to tmpdir
+        my $cmd = ['/usr/sbin/ploop', 'mount', '-t', $fstype, '-m',
$tmpdir, $rootdisk];
+        eval { PVE::Tools::run_command($cmd); };
+        my $err = $?;
+        die "cannot mount $rootdisk" if ($err ne 0);
+        return $tmpdir;
+    }
+}
+
+sub ploop_umount {
+        my $ploopdisk = shift;
+        my $cmd = ['/usr/sbin/ploop', 'umount', $ploopdisk];
+        eval { PVE::Tools::run_command($cmd); };
+        my $err = $?;
+        die "cannot umount $ploopdisk" if ($err ne 0);
+}
+
 sub set_rootpasswd {
-    my ($privatedir, $opt_rootpasswd) = @_;
+    my ($privatedir, $opt_rootpasswd, $vmid) = @_;
+
+    my $rootdisk = "$privatedir/root.hdd/root.hdd";
+    my ($pwfile, $shadow, $tmpdir);

-    my $pwfile = "$privatedir/etc/passwd";
+    $tmpdir = ploop_mount($rootdisk, $vmid) if (-f $rootdisk);
+    $privatedir = $tmpdir if defined($tmpdir);
+
+    $pwfile = "$privatedir/etc/passwd";

     return if ! -f $pwfile;

-    my $shadow = "$privatedir/etc/shadow";
+    $shadow = "$privatedir/etc/shadow";

     if ($opt_rootpasswd !~ m/^\$/) {
- my $time = substr (Digest::SHA::sha1_base64 (time), 0, 8);
- $opt_rootpasswd = crypt(encode("utf8", $opt_rootpasswd), "\$1\$$time\$");
+                my $time = substr (Digest::SHA::sha1_base64 (time), 0, 8);
+                $opt_rootpasswd = crypt(encode("utf8", $opt_rootpasswd),
"\$1\$$time\$");
     };

     if (-f $shadow) {
- replacepw ($shadow, $opt_rootpasswd);
- replacepw ($pwfile, 'x');
+                replacepw ($shadow, $opt_rootpasswd);
+                replacepw ($pwfile, 'x');
     } else {
- replacepw ($pwfile, $opt_rootpasswd);
+                replacepw ($pwfile, $opt_rootpasswd);
     }
-}
+    ploop_umount($rootdisk) if (-f $rootdisk);
+}
\ No newline at end of file



On Fri, Oct 25, 2013 at 11:44 AM, Dietmar Maurer <dietmar at proxmox.com>wrote:

> > Previous solution work only with simfs.
> > If we use ploop, we need to mount VM image and then change password.
> >
> > Why we can't solve this with simple, built-in solution?
>
> Because that made many troubles. Starting a container at create stage
> is really unexpected and not what we want to do (for example many prebuild
> appliances do special things a first boot).
>
> But maybe you can find another solution, i.e. mount ploop first?
>
>


-- 
С уважением, Шестаков Игорь
Сеть дата-центров "Селектел"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.proxmox.com/pipermail/pve-devel/attachments/20131025/80f58f9c/attachment.htm>


More information about the pve-devel mailing list