[virt-tools-list] [virt-manager PATCH] cloudinit: Add ssh-key=/path/to/key cli option

Cole Robinson crobinso at redhat.com
Sun Jul 28 18:38:45 UTC 2019


On 7/25/19 5:07 AM, athina.plaskasoviti at gmail.com wrote:
> From: AthinaPl <athina.plaskasoviti at gmail.com>
> 
> Login to VM with:
> ssh root at vm_ip_address
> 
> Signed-off-by: AthinaPl <athina.plaskasoviti at gmail.com>
> ---
>  tests/cli-test-xml/ssh-key.txt |  1 +
>  tests/clitest.py               |  2 ++
>  virtinst/cli.py                |  1 +
>  virtinst/install/cloudinit.py  | 11 ++++++++++-
>  4 files changed, 14 insertions(+), 1 deletion(-)
>  create mode 100644 tests/cli-test-xml/ssh-key.txt
> 
> diff --git a/tests/cli-test-xml/ssh-key.txt b/tests/cli-test-xml/ssh-key.txt
> new file mode 100644
> index 00000000..385cf112
> --- /dev/null
> +++ b/tests/cli-test-xml/ssh-key.txt
> @@ -0,0 +1 @@
> +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDAuibybX5lw2G/LPIyqgQS5KwgbOnKMA9TZyQPtRmIfKSGypdMJmGQ+Gsf54A9VI8zoL2fnq2t66zQIPJU53XveXU0oqCm7PfsEcoYjnqDeUeiqvCfTye8bIbEmIkLriH0SaISNnzyN0JSfb0VDYIg8Za6iW3/PfPs+tV0PSYyVEm3pBNJ9bHat2liA1/Afk0UWNrhCQG9/5v9kR36aIxNU+4qI9cZ2npFWt61/7t2otz2GcygJDCUYEp6wDlmoW6DHXnaUUj1USjZ9uI1IrgmjZYxfjlt/UaB7AJOJP/3bOC1iPXBe9HKt30YUG23KaVfK9DDeGD/tlAeIklaYK8RZd4zUXSm3eZjeZCGiC3XFYIIyf7e7M/IZE+/DbD1jQEGxGd7AmdVBJZPBFtkAd4Blypaidykx7n1UcfF0WWISfFsae956PHnqnSBKM5OUDh2y5LowgXjWmr/BVJmnaiNbeMhgC3axZVL3EmFSVtvwnup+sRmDwYoHh/RbmInyns=
> diff --git a/tests/clitest.py b/tests/clitest.py
> index c3d0297d..04154e1f 100644
> --- a/tests/clitest.py
> +++ b/tests/clitest.py
> @@ -91,6 +91,7 @@ test_files = {
>      'COLLIDE': "/dev/default-pool/collidevol1.img",
>      'ADMIN-PASSWORD-FILE': "%s/admin-password.txt" % XMLDIR,
>      'USER-PASSWORD-FILE': "%s/user-password.txt" % XMLDIR,
> +    'SSH-KEY-FILE': "%s/ssh-key.txt" % XMLDIR
>  }
>  
>  
> @@ -873,6 +874,7 @@ c.add_compare("--connect %s --os-variant fedora26 --pxe --print-xml" % (utils.UR
>  c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init", "cloud-init-default")  # default --cloud-init behavior is root-password-generate=yes,disable=yes
>  c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init root-password-generate=yes,disable=no", "cloud-init-options")  # --cloud-init options
>  c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init root-password-file=%(ADMIN-PASSWORD-FILE)s,disable=no", "cloud-init-options")  # --cloud-init-options
> +c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init ssh-key=%(SSH-KEY-FILE)s", "cloud-init-options")  # --cloud-init-options
>  c.add_valid("--panic help --disk=? --check=help", grep="path_in_use")  # Make sure introspection doesn't blow up
>  c.add_valid("--connect test:///default --test-stub-command", use_default_args=False)  # --test-stub-command
>  c.add_valid("--nodisks --pxe", grep="VM performance may suffer")  # os variant warning
> diff --git a/virtinst/cli.py b/virtinst/cli.py
> index 77d38c69..6c9555f0 100644
> --- a/virtinst/cli.py
> +++ b/virtinst/cli.py
> @@ -1616,6 +1616,7 @@ class ParserCloudInit(VirtCLIParser):
>          cls.add_arg("root-password-generate", "root_password_generate", is_onoff=True)
>          cls.add_arg("root-password-file", "root_password_file")
>          cls.add_arg("disable", "disable", is_onoff=True)
> +        cls.add_arg("ssh-key", "ssh_key")
>  
>  
>  def parse_cloud_init(optstr):
> diff --git a/virtinst/install/cloudinit.py b/virtinst/install/cloudinit.py
> index 9760e8a6..44b8ed60 100644
> --- a/virtinst/install/cloudinit.py
> +++ b/virtinst/install/cloudinit.py
> @@ -9,6 +9,7 @@ class CloudInitData():
>      root_password_generate = None
>      root_password_file = None
>      generated_root_password = None
> +    ssh_key = None
>  
>      def generate_password(self):
>          self.generated_root_password = ""
> @@ -25,6 +26,8 @@ class CloudInitData():
>              return self.generate_password()
>          elif self.root_password_file:
>              return self._get_password(self.root_password_file)
> +        elif self.ssh_key:
> +            return self._get_password(self.ssh_key)
>  
>  
>  def create_metadata(scratchdir):
> @@ -44,12 +47,18 @@ def create_userdata(scratchdir, cloudinit_data):
>      content = "#cloud-config\n"
>  
>      rootpass = cloudinit_data.get_root_password()
> -    if rootpass:
> +    if cloudinit_data.root_password_generate or cloudinit_data.root_password_file:
>          content += "chpasswd:\n"
>          content += "  list: |\n"
>          content += "    root:%s\n" % rootpass
>          content += "  expire: True\n"
>  
> +    if cloudinit_data.ssh_key:
> +        content += "users:\n"
> +        content += "  - name: root\n"
> +        content += "    ssh-authorized-keys:\n"
> +        content += "      - %s\n" % rootpass
> +

FWIW I tested the patch and it works for me, but there's two issues. One
is that we shouldn't overload get_root_password() here because the
ssh-key isn't really a root password, and it complicates the code below.
Instead add a function like get_ssh_key() and use that here instead, and
don't overload rootpass.

The other issue is that if --cloud-init ssh-key=/path is specified,
virt-install still prints a randomly generated root password and pauses
for 10 seconds. Something is wrong there but I didn't look into it.
Maybe it's related to the above issue in some way

Thanks,
Cole

>      if cloudinit_data.disable:
>          content += "runcmd:\n"
>          content += "- [ sudo, touch, /etc/cloud/cloud-init.disabled ]\n"
> 


- Cole




More information about the virt-tools-list mailing list