Skip to content

Commit

Permalink
vm/adminvm: don't base AdminVM on QubesVM
Browse files Browse the repository at this point in the history
dom0 isn't real VM and most properties doesn't apply to it. Lets make it
more explicit.
  • Loading branch information
marmarek committed Jun 6, 2017
1 parent 67fcda2 commit fa1da42
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 34 deletions.
5 changes: 2 additions & 3 deletions qubes/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,7 @@ def load_initial_values(self):
self.pools[name] = self._get_pool(**config)

self.domains.add(
qubes.vm.adminvm.AdminVM(self, None, qid=0, name='dom0',
label='black'))
qubes.vm.adminvm.AdminVM(self, None, label='black'))

@classmethod
def create_empty_store(cls, *args, **kwargs):
Expand Down Expand Up @@ -1122,7 +1121,7 @@ def on_property_set_default_netvm(self, event, name, newvalue,
oldvalue=None):
# pylint: disable=unused-argument
for vm in self.domains:
if vm.property_is_default('netvm'):
if hasattr(vm, 'netvm') and vm.property_is_default('netvm'):
# fire property-del:netvm as it is responsible for resetting
# netvm to it's default value
vm.fire_event('property-del:netvm',
Expand Down
5 changes: 3 additions & 2 deletions qubes/tests/vm/qubesvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import qubes
import qubes.exc
import qubes.config
import qubes.vm
import qubes.vm.qubesvm

import qubes.tests
Expand Down Expand Up @@ -131,12 +132,12 @@ def test_020_setter_kernel(self):
def test_030_setter_label_object(self):
label = TestApp.labels[1]
self.assertIs(label,
qubes.vm.qubesvm._setter_label(self.vm, self.prop, label))
qubes.vm.setter_label(self.vm, self.prop, label))

def test_031_setter_label_getitem(self):
label = TestApp.labels[1]
self.assertIs(label,
qubes.vm.qubesvm._setter_label(self.vm, self.prop, 'label-1'))
qubes.vm.setter_label(self.vm, self.prop, 'label-1'))

# there is no check for self.app.get_label()

Expand Down
11 changes: 11 additions & 0 deletions qubes/vm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,14 @@ def sanitize(self, *, untrusted_newvalue):
return untrusted_vmname
validate_name(None, self, untrusted_vmname)
return untrusted_vmname


def setter_label(self, prop, value):
''' Helper for setting the domain label '''
# pylint: disable=unused-argument
if isinstance(value, qubes.Label):
return value
if isinstance(value, str) and value.startswith('label-'):
return self.app.labels[int(value.split('-', 1)[1])]

return self.app.get_label(value)
37 changes: 20 additions & 17 deletions qubes/vm/adminvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@
import libvirt
import qubes
import qubes.exc
import qubes.vm.qubesvm
import qubes.vm

class AdminVM(qubes.vm.qubesvm.QubesVM):
class AdminVM(qubes.vm.BaseVM):
'''Dom0'''

dir_path = None

netvm = qubes.property('netvm', setter=qubes.property.forbidden,
default=None,
doc='Dom0 cannot have netvm')
name = qubes.property('name',
default='dom0', setter=qubes.property.forbidden)

kernel = qubes.property('netvm', setter=qubes.property.forbidden,
default=None,
doc='There are other ways to set kernel for Dom0.')
label = qubes.property('label',
setter=qubes.vm.setter_label,
saver=(lambda self, prop, value: 'label-{}'.format(value.index)),
doc='''Colourful label assigned to VM. This is where the colour of the
padlock is set.''')

memory = qubes.property('memory', setter=qubes.property.forbidden,
default=lambda self: self.get_mem(),
doc='Memory currently assigned to dom0.')
qid = qubes.property('qid',
default=0, setter=qubes.property.forbidden)

maxmem = qubes.property('maxmem', setter=qubes.property.forbidden,
default=lambda self: self.get_mem_static_max(),
doc='Maximum dom0 memory size, modify using xen boot options.')
uuid = qubes.property('uuid',
default='00000000-0000-0000-0000-000000000000',
setter=qubes.property.forbidden)

@property
def attached_volumes(self):
Expand All @@ -71,23 +71,26 @@ def libvirt_domain(self):
'''
return None

def is_running(self):
@staticmethod
def is_running():
'''Always :py:obj:`True`.
.. seealso:
:py:meth:`qubes.vm.qubesvm.QubesVM.is_running`
'''
return True

def get_power_state(self):
@staticmethod
def get_power_state():
'''Always ``'Running'``.
.. seealso:
:py:meth:`qubes.vm.qubesvm.QubesVM.get_power_state`
'''
return 'Running'

def get_mem(self):
@staticmethod
def get_mem():
'''Get current memory usage of Dom0.
Unit is KiB.
Expand Down
2 changes: 1 addition & 1 deletion qubes/vm/mix/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def connected_vms(self):
NetVM.
'''
for vm in self.app.domains:
if vm.netvm is self:
if getattr(vm, 'netvm', None) is self:
yield vm

#
Expand Down
12 changes: 1 addition & 11 deletions qubes/vm/qubesvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ def _setter_kernel(self, prop, value):
return value


def _setter_label(self, prop, value):
''' Helper for setting the domain label '''
# pylint: disable=unused-argument
if isinstance(value, qubes.Label):
return value
if isinstance(value, str) and value.startswith('label-'):
return self.app.labels[int(value.split('-', 1)[1])]

return self.app.get_label(value)

def _setter_positive_int(self, prop, value):
''' Helper for setting a positive int. Checks that the int is >= 0 '''
# pylint: disable=unused-argument
Expand Down Expand Up @@ -376,7 +366,7 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM):
#

label = qubes.property('label',
setter=_setter_label,
setter=qubes.vm.setter_label,
saver=(lambda self, prop, value: 'label-{}'.format(value.index)),
doc='''Colourful label assigned to VM. This is where the colour of the
padlock is set.''')
Expand Down

0 comments on commit fa1da42

Please sign in to comment.