[libvirt] [PATCH 2/3] virsh: add set-user-password command

Ján Tomko jtomko at redhat.com
Mon May 18 12:43:49 UTC 2015


Expose the virDomainSetUserPassword API in virsh:
virsh set-user-password dom 123456 user

For root, the username can be omitted:
virsh set-user-password dom 123456
---
 tools/virsh-domain.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod      | 11 ++++++++
 2 files changed, 87 insertions(+)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 20f8c75..a654623 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -5381,6 +5381,76 @@ cmdScreenshot(vshControl *ctl, const vshCmd *cmd)
 }
 
 /*
+ * "set-user-password" command
+ */
+static const vshCmdInfo info_set_user_password[] = {
+    {.name = "help",
+     .data = N_("set the user password inside the domain")
+    },
+    {.name = "desc",
+     .data = N_("changes the password of the specified user inside the domain")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_set_user_password[] = {
+    {.name = "domain",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("domain name, id or uuid")
+    },
+    {.name = "password",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("the new password")
+    },
+    {.name = "user",
+     .type = VSH_OT_STRING,
+     .help = N_("the username")
+    },
+    {.name = "crypted",
+     .type = VSH_OT_BOOL,
+     .help = N_("the password is already crypted")
+    },
+    {.name = NULL}
+};
+
+static bool
+cmdSetUserPassword(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    const char *name;
+    const char *password = NULL;
+    const char *user = NULL;
+    unsigned int flags = 0;
+    bool ret = false;
+
+    if (vshCommandOptBool(cmd, "crypted"))
+        flags = VIR_DOMAIN_PASSWORD_CRYPTED;
+
+    if (vshCommandOptStringReq(ctl, cmd, "user", &user) < 0)
+        return false;
+
+    if (vshCommandOptStringReq(ctl, cmd, "password", &password) < 0)
+        return false;
+
+    if (!user)
+        user = "root";
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, &name)))
+        return false;
+
+    if (virDomainSetUserPassword(dom, user, password, flags) < 0)
+        goto cleanup;
+
+    vshPrint(ctl, _("Password set successfully for %s in %s"), user, name);
+    ret = true;
+
+ cleanup:
+    virDomainFree(dom);
+    return ret;
+}
+/*
  * "resume" command
  */
 static const vshCmdInfo info_resume[] = {
@@ -13159,6 +13229,12 @@ const vshCmdDef domManagementCmds[] = {
      .info = info_screenshot,
      .flags = 0
     },
+    {.name = "set-user-password",
+     .handler = cmdSetUserPassword,
+     .opts = opts_set_user_password,
+     .info = info_set_user_password,
+     .flags = 0
+    },
     {.name = "setmaxmem",
      .handler = cmdSetmaxmem,
      .opts = opts_setmaxmem,
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 1bb655b..9bd52f2 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2015,6 +2015,17 @@ the value from the host, use the B<virsh memtune> command. In order to view
 the current memory in use and the maximum value allowed to set memory, use
 the B<virsh dominfo> command.
 
+=item B<set-user-password> I<domain> I<password> [I<user>] [I<--crypted>]
+
+Set the password for the I<user> account in the guest domain.
+If the user is omitted, root is assumed by virsh.
+
+If I<--crypted> is specified, the password is assumed to be crypted
+already by crypt(3).
+
+For QEMU/KVM, this requires the guest agent to be configured
+and running.
+
 =item B<setmaxmem> I<domain> B<size> [[I<--config>] [I<--live>] |
 [I<--current>]]
 
-- 
2.0.5




More information about the libvir-list mailing list