cli: Add --os-variant require=on|off

If specified, this errors if no OS name was detected or manually set.
So --os-variant detect=on,require=on will error if no OS is detected.

name= can be used as a fallback, so test and document this case

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-09-14 20:28:02 -04:00
parent 17f47a44c5
commit a242894182
6 changed files with 218 additions and 1 deletions

View File

@ -992,7 +992,29 @@ syntax as well:
install media. Detection is presently only attempted for URL and
CDROM installs, and is not 100% reliable.
By default, virt-install will use ``--os-variant detect=on``.
``require=on|off``
If ``on``, virt-install errors if no OS value is set or detected.
Some interesting examples:
``--os-variant detect=on,require=on``
This tells virt-install to attempt detection from install media,
but explicitly fail if that does not succeed. This will ensure
your virt-install invocations don't fallback to a poorly performing
config
``--os-variant detect=on,name=OSNAME``
Attempt OS detection from install media, but if that fails, use
OSNAME as a fallback.
By default, virt-install will do ``--os-variant detect=on,name=generic``,
using the detected OS if found, and falling back to the stub ``generic``
value otherwise, and printing a warning.
If any manual ``--os-variant`` value is specified, the default is
all settings off or unset.
Use the command "osinfo-query os" to get the list of the accepted OS
variant names.

View File

@ -0,0 +1,95 @@
<domain type="test">
<name>win7</name>
<uuid>00000000-1111-2222-3333-444444444444</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://microsoft.com/win/7"/>
</libosinfo:libosinfo>
</metadata>
<memory>65536</memory>
<currentMemory>65536</currentMemory>
<vcpu>2</vcpu>
<os>
<type arch="i686">hvm</type>
<boot dev="network"/>
</os>
<features>
<pae/>
<hyperv>
<relaxed state="on"/>
<vapic state="on"/>
<spinlocks state="on" retries="8191"/>
</hyperv>
</features>
<clock offset="localtime"/>
<on_reboot>destroy</on_reboot>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/bin/test-hv</emulator>
<controller type="usb" model="ich9-ehci1"/>
<controller type="usb" model="ich9-uhci1">
<master startport="0"/>
</controller>
<controller type="usb" model="ich9-uhci2">
<master startport="2"/>
</controller>
<controller type="usb" model="ich9-uhci3">
<master startport="4"/>
</controller>
<interface type="user">
<mac address="00:11:22:33:44:55"/>
<model type="e1000"/>
</interface>
<console type="pty"/>
</devices>
</domain>
<domain type="test">
<name>win7</name>
<uuid>00000000-1111-2222-3333-444444444444</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://microsoft.com/win/7"/>
</libosinfo:libosinfo>
</metadata>
<memory>65536</memory>
<currentMemory>65536</currentMemory>
<vcpu>2</vcpu>
<os>
<type arch="i686">hvm</type>
<boot dev="network"/>
</os>
<features>
<pae/>
<hyperv>
<relaxed state="on"/>
<vapic state="on"/>
<spinlocks state="on" retries="8191"/>
</hyperv>
</features>
<clock offset="localtime"/>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/bin/test-hv</emulator>
<controller type="usb" model="ich9-ehci1"/>
<controller type="usb" model="ich9-uhci1">
<master startport="0"/>
</controller>
<controller type="usb" model="ich9-uhci2">
<master startport="2"/>
</controller>
<controller type="usb" model="ich9-uhci3">
<master startport="4"/>
</controller>
<interface type="user">
<mac address="00:11:22:33:44:55"/>
<model type="e1000"/>
</interface>
<console type="pty"/>
</devices>
</domain>

View File

@ -0,0 +1,87 @@
<domain type="test">
<name>rhel6.0</name>
<uuid>00000000-1111-2222-3333-444444444444</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://redhat.com/rhel/6.0"/>
</libosinfo:libosinfo>
</metadata>
<memory>65536</memory>
<currentMemory>65536</currentMemory>
<vcpu>2</vcpu>
<os>
<type arch="i686">hvm</type>
<kernel>/VIRTINST-TESTSUITE/vmlinuz</kernel>
<initrd>/VIRTINST-TESTSUITE/initrd.img</initrd>
<cmdline>method=https://foobar.com</cmdline>
</os>
<features>
<pae/>
</features>
<clock offset="utc"/>
<on_reboot>destroy</on_reboot>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/bin/test-hv</emulator>
<controller type="usb" model="ich9-ehci1"/>
<controller type="usb" model="ich9-uhci1">
<master startport="0"/>
</controller>
<controller type="usb" model="ich9-uhci2">
<master startport="2"/>
</controller>
<controller type="usb" model="ich9-uhci3">
<master startport="4"/>
</controller>
<interface type="user">
<mac address="00:11:22:33:44:55"/>
<model type="e1000"/>
</interface>
<console type="pty"/>
</devices>
</domain>
<domain type="test">
<name>rhel6.0</name>
<uuid>00000000-1111-2222-3333-444444444444</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://redhat.com/rhel/6.0"/>
</libosinfo:libosinfo>
</metadata>
<memory>65536</memory>
<currentMemory>65536</currentMemory>
<vcpu>2</vcpu>
<os>
<type arch="i686">hvm</type>
<boot dev="hd"/>
</os>
<features>
<pae/>
</features>
<clock offset="utc"/>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/bin/test-hv</emulator>
<controller type="usb" model="ich9-ehci1"/>
<controller type="usb" model="ich9-uhci1">
<master startport="0"/>
</controller>
<controller type="usb" model="ich9-uhci2">
<master startport="2"/>
</controller>
<controller type="usb" model="ich9-uhci3">
<master startport="4"/>
</controller>
<interface type="user">
<mac address="00:11:22:33:44:55"/>
<model type="e1000"/>
</interface>
<console type="pty"/>
</devices>
</domain>

View File

@ -869,7 +869,10 @@ c.add_compare("--cdrom http://example.com/path/to/some.iso", "cdrom-url")
c.add_compare("--pxe --print-step all --os-variant none", "simple-pxe") # Diskless PXE install
c.add_compare("--location ftp://example.com --os-variant auto", "fake-ftp") # fake ftp:// install using urlfetcher.py mocking
c.add_compare("--location https://foobar.com --os-variant detect=no", "fake-http") # fake https:// install using urlfetcher.py mocking, but also hit --os-variant detect=no
c.add_compare("--location https://foobar.com --os-variant detect=yes,name=win7", "os-detect-success-fallback") # os detection succeeds, so fallback should be ignored
c.add_compare("--pxe --os-variant detect=yes,name=win7", "os-detect-fail-fallback") # os detection succeeds, so fallback should be ignored
c.add_compare("--connect %(URI-KVM)s --install fedora26", "osinfo-url") # getting URL from osinfo
c.add_invalid("--pxe --os-variant detect=yes,require=yes", grep="An --os-variant is required") # No os-variant detected, but require=yes
c.add_invalid("--pxe --virt-type bogus") # Bogus virt-type
c.add_invalid("--pxe --arch bogus") # Bogus arch
c.add_invalid("--livecd") # LiveCD with no media

View File

@ -1795,6 +1795,7 @@ class OSVariantData(object):
self._name = None
self._id = None
self._detect = False
self._require = False
def set_compat_str(self, rawstr):
if rawstr is None or rawstr == "auto":
@ -1822,6 +1823,8 @@ class OSVariantData(object):
return self._detect is False or self._name == "generic"
def is_detect(self):
return self._detect
def is_require(self):
return self._require
def get_name(self):
return self._name
@ -1837,6 +1840,7 @@ class ParserOSVariant(VirtCLIParser):
cls.add_arg("short-id", "_name")
cls.add_arg("id", "_id")
cls.add_arg("detect", "_detect", is_onoff=True)
cls.add_arg("require", "_require", is_onoff=True)
def parse(self, inst):
if "=" not in str(self.optstr):

View File

@ -530,20 +530,26 @@ def set_explicit_guest_options(options, guest):
def installer_detect_distro(guest, installer, osdata):
os_set = False
try:
# OS name has to be set firstly whenever --os-variant is passed,
# otherwise it won't be respected when the installer creates the
# Distro Store.
if osdata.get_name():
os_set = True
guest.set_os_name(osdata.get_name())
# This also validates the install location
autodistro = installer.detect_distro(guest)
if osdata.is_detect() and autodistro:
os_set = True
guest.set_os_name(autodistro)
except ValueError as e:
fail(_("Error validating install location: %s") % str(e))
if not os_set and osdata.is_require():
fail(_("An --os-variant is required, but no value was set or detected."))
def _build_options_guest(conn, options):
guest = Guest(conn)