From 6fddae3b9bbdb588051be867d4ae725d3253c67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Wed, 20 Nov 2013 02:57:17 +0100 Subject: [PATCH] Support for autostart VMs (#724) --- core-modules/000QubesVm.py | 17 ++++++++++++++++- linux/systemd/qubes-vm@.service | 12 ++++++++++++ qvm-tools/qvm-prefs | 10 ++++++++++ rpm_spec/core-dom0.spec | 2 ++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 linux/systemd/qubes-vm@.service diff --git a/core-modules/000QubesVm.py b/core-modules/000QubesVm.py index 0e6fa908..82e14ff2 100644 --- a/core-modules/000QubesVm.py +++ b/core-modules/000QubesVm.py @@ -120,6 +120,7 @@ class QubesVm(object): "debug": { "default": False }, "default_user": { "default": "user" }, "qrexec_timeout": { "default": 60 }, + "autostart": { "default": False, "attr": "_autostart" }, ##### Internal attributes - will be overriden in __init__ regardless of args "config_file_template": { "eval": 'system_path["config_template_pv"]' }, "icon_path": { "eval": 'os.path.join(self.dir_path, "icon.png") if self.dir_path is not None else None' }, @@ -137,7 +138,7 @@ class QubesVm(object): 'uses_default_kernel', 'kernel', 'uses_default_kernelopts',\ 'kernelopts', 'services', 'installed_by_rpm',\ 'uses_default_netvm', 'include_in_backups', 'debug',\ - 'default_user', 'qrexec_timeout' ]: + 'default_user', 'qrexec_timeout', 'autostart' ]: attrs[prop]['save'] = 'str(self.%s)' % prop # Simple paths for prop in ['conf_file', 'root_img', 'volatile_img', 'private_img']: @@ -444,6 +445,20 @@ class QubesVm(object): for hook in self.hooks_post_rename: hook(self, old_name) + @property + def autostart(self): + return self._autostart + + @autostart.setter + def autostart(self, value): + if value: + retcode = subprocess.call(["sudo", "systemctl", "enable", "qubes-vm@%s.service" % self.name]) + else: + retcode = subprocess.call(["sudo", "systemctl", "disable", "qubes-vm@%s.service" % self.name]) + if retcode != 0: + raise QubesException("Failed to set autostart for VM via systemctl") + self._autostart = bool(value) + @property def template(self): return self._template diff --git a/linux/systemd/qubes-vm@.service b/linux/systemd/qubes-vm@.service new file mode 100644 index 00000000..1770d6d9 --- /dev/null +++ b/linux/systemd/qubes-vm@.service @@ -0,0 +1,12 @@ +[Unit] +Description=Start Qubes VM %i +After=qubes-netvm.service + +[Service] +Type=oneshot +ExecStart=/usr/bin/qvm-start --no-guid %i +Group=qubes +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target diff --git a/qvm-tools/qvm-prefs b/qvm-tools/qvm-prefs index a186b73d..6de70400 100755 --- a/qvm-tools/qvm-prefs +++ b/qvm-tools/qvm-prefs @@ -42,6 +42,7 @@ def do_list(vm): if vm.netvm is not None: print fmt.format ("netvm", vm.netvm.name) print fmt.format ("updateable?", vm.updateable) + print fmt.format ("autostart", vm.autostart) print fmt.format ("installed by RPM?", vm.installed_by_rpm) print fmt.format ("include in backups", vm.include_in_backups) print fmt.format ("dir", vm.dir_path) @@ -360,6 +361,14 @@ def set_guiagent_installed(vms, vm, args): vm.guiagent_installed = bool(eval(args[0].capitalize())) return True +def set_autostart(vms, vm, args): + if len (args) != 1: + print >> sys.stderr, "Missing value (True/False)!" + return False + + vm.autostart = bool(eval(args[0].capitalize())) + return True + def set_qrexec_timeout(vms, vm, args): if len (args) != 1: print >> sys.stderr, "Missing timeout value (seconds)!" @@ -401,6 +410,7 @@ properties = { "qrexec_timeout": set_qrexec_timeout, "timezone": set_timezone, "internal": set_internal, + "autostart": set_autostart, } diff --git a/rpm_spec/core-dom0.spec b/rpm_spec/core-dom0.spec index a31aaac1..e3877d0c 100644 --- a/rpm_spec/core-dom0.spec +++ b/rpm_spec/core-dom0.spec @@ -91,6 +91,7 @@ cp linux/systemd/qubes-setupdvm.service $RPM_BUILD_ROOT%{_unitdir} cp linux/systemd/qubes-meminfo-writer.service $RPM_BUILD_ROOT%{_unitdir} cp linux/systemd/qubes-netvm.service $RPM_BUILD_ROOT%{_unitdir} cp linux/systemd/qubes-qmemman.service $RPM_BUILD_ROOT%{_unitdir} +cp linux/systemd/qubes-vm@.service $RPM_BUILD_ROOT%{_unitdir} mkdir -p $RPM_BUILD_ROOT/usr/bin/ cp qvm-tools/qvm-* $RPM_BUILD_ROOT/usr/bin @@ -299,6 +300,7 @@ fi %{_unitdir}/qubes-meminfo-writer.service %{_unitdir}/qubes-netvm.service %{_unitdir}/qubes-qmemman.service +%{_unitdir}/qubes-vm@.service %attr(0770,root,qubes) %dir /var/lib/qubes %attr(0770,root,qubes) %dir /var/lib/qubes/vm-templates %attr(0770,root,qubes) %dir /var/lib/qubes/appvms