[virt-tools-list] [virt-manager PATCH][v2][RFC] Introduction of cloud-init configuration in virt-install

Cole Robinson crobinso at redhat.com
Wed Jun 26 13:11:49 UTC 2019


On 6/21/19 1:18 PM, Athina Plaskasoviti wrote:
> Triggered by:
> --install is_cloud=yes ... --import
> 
> Signed-off-by: Athina Plaskasoviti <athina.plaskasoviti at gmail.com>
> ---
>  virt-install                        |  9 ++++---
>  virtinst/cli.py                     |  2 ++
>  virtinst/install/cloudinit.py       | 41 +++++++++++++++++++++++++++++
>  virtinst/install/installer.py       | 16 ++++++++++-
>  virtinst/install/installerinject.py | 20 +++++++-------
>  5 files changed, 75 insertions(+), 13 deletions(-)
>  create mode 100644 virtinst/install/cloudinit.py
> 

So from the other discussion I think a reasonable starting point for
this is to use:

--cloud-init root-password=generate

As the starting point. When that's working well, we can commit it, and
discuss next steps from there, including the ssh options Dan layed out,
and virt-manager discussions.

Use the --unattended in cli.py as a guide. cloudinit.py should have a
small CloudinitData class that's filled in on the command line, then
passed to the installer with set_cloudinit_data, similar to
set_unattended_data. The class will only have a root_password value from
the command line for now.

You can generate a random password in the format we suggested, with:

import random

import string

passwd = ""

for dummy in range(16):

    passwd += random.choice(string.ascii_letters + string.digits)

> diff --git a/virt-install b/virt-install
> index ee2b9006..b3608662 100755
> --- a/virt-install
> +++ b/virt-install
> @@ -399,6 +399,7 @@ def build_installer(options, guest, installdata):
>      install_kernel_args = installdata.kernel_args
>      install_os = installdata.os
>      no_install = installdata.no_install
> +    is_cloud = installdata.is_cloud
>      if installdata.kernel_args:
>          if installdata.kernel_args_overwrite:
>              install_kernel_args = installdata.kernel_args
> @@ -417,10 +418,11 @@ def build_installer(options, guest, installdata):
>              no_install = True
>      elif options.pxe:
>          install_bootdev = "network"
> +    elif options.import_install:
> +        no_install = True
>      elif installdata.is_set:
>          pass
> -    elif (options.import_install or
> -          options.xmlonly or
> +    elif (options.xmlonly or
>            options.boot):
>          no_install = True
>  
> @@ -433,7 +435,8 @@ def build_installer(options, guest, installdata):
>              install_kernel=install_kernel,
>              install_initrd=install_initrd,
>              install_kernel_args=install_kernel_args,
> -            no_install=no_install)
> +            no_install=no_install,
> +            is_cloud=is_cloud)
>  
>      if options.unattended:
>          unattended_data = cli.parse_unattended(options.unattended)
> diff --git a/virtinst/cli.py b/virtinst/cli.py
> index 9a1fe2f6..a2a501a5 100644
> --- a/virtinst/cli.py
> +++ b/virtinst/cli.py
> @@ -1580,6 +1580,7 @@ class ParserInstall(VirtCLIParser):
>                  is_onoff=True)
>          cls.add_arg("os", "os")
>          cls.add_arg("no_install", "no_install", is_onoff=True)
> +        cls.add_arg("is_cloud", "is_cloud", is_onoff=True)
>  
>  
>  class InstallData:
> @@ -1592,6 +1593,7 @@ class InstallData:
>          self.os = None
>          self.is_set = False
>          self.no_install = None
> +        self.is_cloud = None
>  
>  
>  def parse_install(optstr):
> diff --git a/virtinst/install/cloudinit.py b/virtinst/install/cloudinit.py
> new file mode 100644
> index 00000000..25b2a79b
> --- /dev/null
> +++ b/virtinst/install/cloudinit.py
> @@ -0,0 +1,41 @@
> +import tempfile
> +from ..logger import log
> +
> +
> +def create_metadata(scratchdir, hostname=None):
> +    if hostname:
> +        instance = hostname
> +    else:
> +        hostname = instance = "localhost"
> +
> +    fileobj = tempfile.NamedTemporaryFile(
> +            prefix="virtinst-", suffix="-metadata",
> +            dir=scratchdir, delete=False)
> +    filename = fileobj.name
> +
> +    with open(filename, "w") as f:
> +        log.debug("Writing instance-id and hostname to file meta-data")
> +        f.writelines(['instance-id: %s\n' % instance,
> +            'hostname: %s\n' % hostname])
> +    return filename
> +
> +
> +def create_userdata(scratchdir, username=None, password=None):
> +    if not password:
> +        password = "password"
> +
> +    fileobj = tempfile.NamedTemporaryFile(
> +            prefix="virtinst-", suffix="-userdata",
> +            dir=scratchdir, delete=False)
> +    filename = fileobj.name
> +
> +    with open(filename, "w+") as f:
> +        f.write("#cloud-config\n")
> +        if username:
> +            log.debug("Writing username to file user-data")
> +            f.write("name: %s\n" % username)
> +        log.debug("Writing password and cmd for disabling of cloud-init to file user-data")
> +        f.writelines(["password: %s\n" % password,
> +            "chpasswd: { expire: False }\n", "runcmd:\n",
> +            "- [ sudo, touch, /etc/cloud/cloud-init.disabled ]"])
> +    return filename

I'd like to have the content we are going to write to be logged. I
attached a patch that reworks this area a bit to accomplish that.

Also, from my research in the other thread, the meta-data file can be
empty, just write "" to it and it should be fine, though I only tested
with Fedora but from reading the cloud-init code I think it should be
fine for other distros too.

Thanks,
Cole
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-logging.patch
Type: text/x-patch
Size: 2523 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/virt-tools-list/attachments/20190626/d7318da2/attachment.bin>


More information about the virt-tools-list mailing list