Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit bf11098

Browse files
committed
Add 'Seclabel' module for building <seclabel> XML
1 parent 287acf3 commit bf11098

5 files changed

Lines changed: 119 additions & 2 deletions

File tree

tests/xmlconfig-xml/boot-many-devices.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<features>
1212
<acpi/><apic/>
1313
</features>
14-
<clock offset="utc"/>
14+
<clock offset="localtime"/>
1515
<on_poweroff>destroy</on_poweroff>
1616
<on_reboot>restart</on_reboot>
1717
<on_crash>restart</on_crash>
@@ -60,4 +60,8 @@
6060
</source>
6161
</hostdev>
6262
</devices>
63+
<seclabel type='static' model='selinux'>
64+
<label>foolabel</label>
65+
<imagelabel>imagelabel</imagelabel>
66+
</seclabel>
6367
</domain>

tests/xmlconfig.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,15 @@ def testManyDevices(self):
556556
g.add_device(vdev1)
557557
g.add_device(vdev2)
558558

559+
g.clock.offset = "localtime"
560+
561+
seclabel = virtinst.Seclabel(g.conn)
562+
seclabel.type = seclabel.SECLABEL_TYPE_STATIC
563+
seclabel.model = "selinux"
564+
seclabel.label = "foolabel"
565+
seclabel.imagelabel = "imagelabel"
566+
g.seclabel = seclabel
567+
559568
g.installer = virtinst.PXEInstaller(type="xen", os_type="hvm",
560569
conn=g.conn)
561570
self._compare(g, "boot-many-devices", False)

virtinst/Guest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from VirtualDevice import VirtualDevice
3232
from VirtualDisk import VirtualDisk
3333
from Clock import Clock
34+
from Seclabel import Seclabel
3435

3536
import osdict
3637
from virtinst import _virtinst as _
@@ -138,6 +139,7 @@ def __init__(self, type=None, connection=None, hypervisorURI=None,
138139
self._os_autodetect = False
139140
self._autostart = False
140141
self._clock = Clock(self.conn)
142+
self._seclabel = None
141143
self.features = None
142144

143145
self._os_type = None
@@ -185,6 +187,18 @@ def get_clock(self):
185187
return self._clock
186188
clock = property(get_clock)
187189

190+
def get_seclabel(self):
191+
return self._seclabel
192+
def set_seclabel(self, val):
193+
if val and not isinstance(val, Seclabel):
194+
raise ValueError("'seclabel' must be a Seclabel() instance.")
195+
196+
if val:
197+
# Check for validation purposes
198+
val.get_xml_config()
199+
self._seclabel = val
200+
seclabel = property(get_seclabel, set_seclabel)
201+
188202
# Domain name of the guest
189203
def get_name(self):
190204
return self._name
@@ -681,6 +695,16 @@ def _get_clock_xml(self):
681695
"""
682696
return self.clock.get_xml_config()
683697

698+
def _get_seclabel_xml(self):
699+
"""
700+
Return <seclabel> XML
701+
"""
702+
xml = ""
703+
if self.seclabel:
704+
xml = self.seclabel.get_xml_config()
705+
706+
return xml
707+
684708
def _get_osblob(self, install):
685709
"""
686710
Return os, features, and clock xml (Implemented in subclass)
@@ -751,6 +775,7 @@ def get_config_xml(self, install = True, disk_boot = False):
751775
xml = add(" <devices>")
752776
xml = add("%s" % self._get_device_xml(install))
753777
xml = add(" </devices>")
778+
xml = add("%s" % self._get_seclabel_xml())
754779
xml = add("</domain>\n")
755780

756781
return xml

virtinst/Seclabel.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#
2+
# Copyright 2010 Red Hat, Inc.
3+
# Cole Robinson <crobinso@redhat.com>
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program; if not, write to the Free Software
17+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
# MA 02110-1301 USA.
19+
20+
class Seclabel(object):
21+
"""
22+
Class for generating <seclabel> XML
23+
"""
24+
25+
SECLABEL_TYPE_DYNAMIC = "dynamic"
26+
SECLABEL_TYPE_STATIC = "static"
27+
SECLABEL_TYPES = [SECLABEL_TYPE_DYNAMIC, SECLABEL_TYPE_STATIC]
28+
29+
def __init__(self, conn):
30+
self.conn = conn
31+
32+
self._type = self.SECLABEL_TYPE_DYNAMIC
33+
self._model = None
34+
self._label = None
35+
self._imagelabel = None
36+
37+
def get_type(self):
38+
return self._type
39+
def set_type(self, val):
40+
self._type = val
41+
type = property(get_type, set_type)
42+
43+
def get_model(self):
44+
return self._model
45+
def set_model(self, val):
46+
self._model = val
47+
model = property(get_model, set_model)
48+
49+
def get_label(self):
50+
return self._label
51+
def set_label(self, val):
52+
self._label = val
53+
label = property(get_label, set_label)
54+
55+
def get_imagelabel(self):
56+
return self._imagelabel
57+
def set_imagelabel(self, val):
58+
self._imagelabel = val
59+
imagelabel = property(get_imagelabel, set_imagelabel)
60+
61+
def get_xml_config(self):
62+
if not self.type or not self.model:
63+
raise RuntimeError("Security type and model must be specified")
64+
65+
if (self.type == self.SECLABEL_TYPE_STATIC and not self.label):
66+
raise RuntimeError("A label must be specified for static "
67+
"security type.")
68+
69+
xml = " <seclabel type='%s' model='%s'>\n" % (self.type, self.model)
70+
71+
if self.label:
72+
xml += " <label>%s</label>\n" % self.label
73+
if self.imagelabel:
74+
xml += " <imagelabel>%s</imagelabel>\n" % self.imagelabel
75+
76+
xml += " </seclabel>"
77+
78+
return xml

virtinst/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def _virtinst(msg):
5050
from CloneManager import CloneDesign
5151
from User import User
5252
from Clock import Clock
53+
from Seclabel import Seclabel
5354
import util
5455
import support
5556

@@ -60,6 +61,6 @@ def _virtinst(msg):
6061
"VirtualDisk", "XenDisk", "FullVirtGuest", "ParaVirtGuest",
6162
"DistroInstaller", "PXEInstaller", "LiveCDInstaller",
6263
"ImportInstaller", "ImageInstaller", "CloneDesign", "Storage",
63-
"User", "util", "support", "VirtualDevice", "Clock",
64+
"User", "util", "support", "VirtualDevice", "Clock", "Seclabel",
6465
"VirtualHostDevice", "VirtualHostDeviceUSB", "VirtualVideoDevice",
6566
"VirtualHostDevicePCI", "VirtualCharDevice", "VirtualInputDevice"]

0 commit comments

Comments
 (0)