1#
2# Copyright 2010, 2013 Red Hat, Inc.
3#
4# This work is licensed under the GNU GPLv2 or later.
5# See the COPYING file in the top-level directory.
6
7from ..xmlbuilder import XMLBuilder, XMLProperty, XMLChildProperty
8
9
10class _InitArg(XMLBuilder):
11    XML_NAME = "initarg"
12    val = XMLProperty(".")
13
14
15class _BootDevice(XMLBuilder):
16    XML_NAME = "boot"
17    dev = XMLProperty("./@dev")
18
19
20class DomainOs(XMLBuilder):
21    """
22    Class for generating boot device related XML
23    """
24    BOOT_DEVICE_HARDDISK = "hd"
25    BOOT_DEVICE_CDROM = "cdrom"
26    BOOT_DEVICE_FLOPPY = "fd"
27    BOOT_DEVICE_NETWORK = "network"
28    BOOT_DEVICES = [BOOT_DEVICE_HARDDISK, BOOT_DEVICE_CDROM,
29                    BOOT_DEVICE_FLOPPY, BOOT_DEVICE_NETWORK]
30
31    def is_hvm(self):
32        return self.os_type == "hvm"
33    def is_xenpv(self):
34        return self.os_type in ["xen", "linux"]
35    def is_container(self):
36        return self.os_type == "exe"
37
38    def is_x86(self):
39        return self.arch == "x86_64" or self.arch == "i686"
40    def is_q35(self):
41        return (self.is_x86() and
42                self.machine and
43                "q35" in self.machine)
44
45    def is_arm32(self):
46        return self.arch == "armv7l"
47    def is_arm64(self):
48        return self.arch == "aarch64"
49    def is_arm(self):
50        return self.is_arm32() or self.is_arm64()
51    def is_arm_machvirt(self):
52        return self.is_arm() and str(self.machine).startswith("virt")
53
54    def is_ppc64(self):
55        return self.arch == "ppc64" or self.arch == "ppc64le"
56    def is_pseries(self):
57        return self.is_ppc64() and str(self.machine).startswith("pseries")
58
59    def is_s390x(self):
60        return self.arch == "s390x"
61
62    def is_riscv(self):
63        return self.arch == "riscv64" or self.arch == "riscv32"
64    def is_riscv_virt(self):
65        return self.is_riscv() and str(self.machine).startswith("virt")
66
67    XML_NAME = "os"
68    _XML_PROP_ORDER = ["arch", "os_type", "loader", "loader_ro", "loader_type",
69                       "nvram", "nvram_template", "kernel", "initrd",
70                       "initdir", "inituser", "initgroup",
71                       "kernel_args", "dtb", "bootdevs", "smbios_mode"]
72
73    def _get_bootorder(self):
74        return [dev.dev for dev in self.bootdevs]
75    def _set_bootorder(self, newdevs):
76        for dev in self.bootdevs:
77            self.remove_child(dev)
78
79        for d in newdevs:
80            dev = self.bootdevs.add_new()
81            dev.dev = d
82    bootdevs = XMLChildProperty(_BootDevice)
83    bootorder = property(_get_bootorder, _set_bootorder)
84
85    initargs = XMLChildProperty(_InitArg)
86    def set_initargs_string(self, argstring):
87        import shlex
88        for obj in self.initargs:
89            self.remove_child(obj)
90        for val in shlex.split(argstring):
91            obj = self.initargs.add_new()
92            obj.val = val
93
94    enable_bootmenu = XMLProperty("./bootmenu/@enable", is_yesno=True)
95    rebootTimeout = XMLProperty("./bios/@rebootTimeout")
96    useserial = XMLProperty("./bios/@useserial", is_yesno=True)
97
98    kernel = XMLProperty("./kernel", do_abspath=True)
99    initrd = XMLProperty("./initrd", do_abspath=True)
100    dtb = XMLProperty("./dtb", do_abspath=True)
101    kernel_args = XMLProperty("./cmdline")
102
103    init = XMLProperty("./init")
104    initdir = XMLProperty("./initdir")
105    inituser = XMLProperty("./inituser")
106    initgroup = XMLProperty("./initgroup")
107    loader = XMLProperty("./loader")
108    loader_ro = XMLProperty("./loader/@readonly", is_yesno=True)
109    loader_type = XMLProperty("./loader/@type")
110    loader_secure = XMLProperty("./loader/@secure", is_yesno=True)
111    smbios_mode = XMLProperty("./smbios/@mode")
112    nvram = XMLProperty("./nvram", do_abspath=True)
113    nvram_template = XMLProperty("./nvram/@template")
114    arch = XMLProperty("./type/@arch")
115    machine = XMLProperty("./type/@machine")
116    os_type = XMLProperty("./type")
117    firmware = XMLProperty("./@firmware")
118
119
120    ##################
121    # Default config #
122    ##################
123
124    def set_defaults(self, guest):
125        if self.is_container() and not self.init:
126            if guest.is_full_os_container():
127                self.init = "/sbin/init"
128            else:
129                self.init = "/bin/sh"
130