From 7da5daff9280130e3864afb6dd76743f4fedb301 Mon Sep 17 00:00:00 2001 From: lisongmin Date: Wed, 29 Sep 2021 23:50:52 +0800 Subject: [PATCH] cli: --cloud-init add network-config option --- man/virt-install.rst | 3 +++ tests/data/cli/cloudinit/network-config.txt | 4 ++++ tests/test_cli.py | 1 + virtinst/cli.py | 1 + virtinst/install/cloudinit.py | 18 ++++++++++++++++-- 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/data/cli/cloudinit/network-config.txt diff --git a/man/virt-install.rst b/man/virt-install.rst index 1626cf7b..55785f32 100644 --- a/man/virt-install.rst +++ b/man/virt-install.rst @@ -878,6 +878,9 @@ Sub options are: Specify a public key to inject into the guest, providing ssh access to the unprivileged account. Example: ssh-key=/home/user/.ssh/id_rsa.pub +``network-config=`` + Specify a cloud-init netowrk-config file to add directly to the iso. + ``--boot`` diff --git a/tests/data/cli/cloudinit/network-config.txt b/tests/data/cli/cloudinit/network-config.txt new file mode 100644 index 00000000..6425e3c4 --- /dev/null +++ b/tests/data/cli/cloudinit/network-config.txt @@ -0,0 +1,4 @@ +version: 2 +ethernets: + enp1s0: + dhcp4: true diff --git a/tests/test_cli.py b/tests/test_cli.py index ec539ef0..6f9e3e91 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -957,6 +957,7 @@ c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init root-pass 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 root-password-file c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init ssh-key=%(XMLDIR)s/cloudinit/ssh-key.txt", "cloud-init-options") # --cloud-init ssh-key c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init user-data=%(XMLDIR)s/cloudinit/user-data.txt,meta-data=%(XMLDIR)s/cloudinit/meta-data.txt", "cloud-init-options") # --cloud-init user-data=,meta-data= +c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init user-data=%(XMLDIR)s/cloudinit/user-data.txt,meta-data=%(XMLDIR)s/cloudinit/meta-data.txt,network-config=%(XMLDIR)s/cloudinit/network-config.txt", "cloud-init-options") # --cloud-init user-data=,meta-data=,network-config= 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 a03d680d..26e4e8e5 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -1756,6 +1756,7 @@ class ParserCloudInit(VirtCLIParser): cls.add_arg("ssh-key", "ssh_key") cls.add_arg("user-data", "user_data") cls.add_arg("meta-data", "meta_data") + cls.add_arg("network-config", "network_config") def parse_cloud_init(optstr): diff --git a/virtinst/install/cloudinit.py b/virtinst/install/cloudinit.py index 78b55ca6..eb99bbf9 100644 --- a/virtinst/install/cloudinit.py +++ b/virtinst/install/cloudinit.py @@ -20,6 +20,7 @@ class CloudInitData(): ssh_key = None user_data = None meta_data = None + network_config = None def _generate_password(self): if not self.generated_root_password: @@ -91,14 +92,27 @@ def _create_userdata_content(cloudinit_data): return content +def _create_network_config_content(cloudinit_data): + content = "" + if cloudinit_data.network_config: + log.debug("Using network-config content from path=%s", + cloudinit_data.network_config) + content = open(cloudinit_data.network_config).read() + return content + + def create_files(scratchdir, cloudinit_data): metadata = _create_metadata_content(cloudinit_data) userdata = _create_userdata_content(cloudinit_data) + datas = [(metadata, "meta-data"), (userdata, "user-data")] + network_config = _create_network_config_content(cloudinit_data) + if network_config: + datas.append((network_config, 'network-config')) + filepairs = [] try: - for content, destfile in [(metadata, "meta-data"), - (userdata, "user-data")]: + for content, destfile in datas: fileobj = tempfile.NamedTemporaryFile( prefix="virtinst-", suffix=("-%s" % destfile), dir=scratchdir, delete=False)