2017-05-25 16:51:28 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
''' Tool for importing rpm-installed template'''
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import grp
|
|
|
|
|
|
|
|
import qubesadmin
|
|
|
|
import qubesadmin.exc
|
|
|
|
import qubesadmin.tools
|
|
|
|
try:
|
|
|
|
# pylint: disable=wrong-import-position
|
|
|
|
import qubesadmin.events.utils
|
|
|
|
have_events = True
|
|
|
|
except ImportError:
|
|
|
|
have_events = False
|
|
|
|
|
|
|
|
parser = qubesadmin.tools.QubesArgumentParser(
|
|
|
|
description='Postprocess template package')
|
|
|
|
parser.add_argument('--really', action='store_true', default=False,
|
|
|
|
help='Really perform the action, YOU SHOULD REALLY KNOW WHAT YOU ARE DOING')
|
|
|
|
parser.add_argument('--skip-start', action='store_true',
|
|
|
|
help='Do not start the VM - do not retrieve menu entries etc.')
|
|
|
|
parser.add_argument('--keep-source', action='store_true',
|
2017-06-26 18:51:53 +02:00
|
|
|
help='Do not remove source data (*dir* directory) after import')
|
2020-06-27 21:07:08 +02:00
|
|
|
parser.add_argument('--no-installed-by-rpm', action='store_true',
|
|
|
|
help='Do not set installed_by_rpm')
|
|
|
|
parser.add_argument('--allow-pv', action='store_true',
|
|
|
|
help='Allow setting virt_mode to pv in configuration file.')
|
2020-07-29 14:55:56 +02:00
|
|
|
parser.add_argument('--pool',
|
|
|
|
help='Specify pool to store created VMs in.')
|
2017-05-25 16:51:28 +02:00
|
|
|
parser.add_argument('action', choices=['post-install', 'pre-remove'],
|
|
|
|
help='Action to perform')
|
|
|
|
parser.add_argument('name', action='store',
|
|
|
|
help='Template name')
|
|
|
|
parser.add_argument('dir', action='store',
|
|
|
|
help='Template directory')
|
|
|
|
|
|
|
|
|
|
|
|
def get_root_img_size(source_dir):
|
|
|
|
'''Extract size of root.img to be imported'''
|
|
|
|
root_path = os.path.join(source_dir, 'root.img')
|
2020-06-27 21:07:08 +02:00
|
|
|
# deal with both cases: split tar and non-split tar
|
|
|
|
part_path = root_path + '.part.00'
|
|
|
|
tar_path = root_path + '.tar'
|
|
|
|
if os.path.exists(part_path) or os.path.exists(tar_path):
|
2017-05-25 16:51:28 +02:00
|
|
|
# get just file root_size from the tar header
|
2020-06-27 21:07:08 +02:00
|
|
|
path = part_path if os.path.exists(part_path) else tar_path
|
|
|
|
p = subprocess.Popen(['tar', 'tvf', path],
|
2017-05-25 16:51:28 +02:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
|
|
|
(stdout, _) = p.communicate()
|
|
|
|
# -rw-r--r-- 0/0 1073741824 1970-01-01 01:00 root.img
|
|
|
|
root_size = int(stdout.split()[2])
|
|
|
|
elif os.path.exists(root_path):
|
|
|
|
root_size = os.path.getsize(root_path)
|
|
|
|
else:
|
|
|
|
raise qubesadmin.exc.QubesException('root.img not found')
|
|
|
|
return root_size
|
|
|
|
|
|
|
|
|
|
|
|
def import_root_img(vm, source_dir):
|
|
|
|
'''Import root.img into VM object'''
|
|
|
|
|
2018-03-19 11:40:58 +01:00
|
|
|
# Try not break existing data in the volume in case of import failure. If
|
|
|
|
# volume needs to be extended, do it before import, if reduced - after.
|
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
root_size = get_root_img_size(source_dir)
|
2018-03-19 11:40:58 +01:00
|
|
|
if vm.volumes['root'].size < root_size:
|
|
|
|
vm.volumes['root'].resize(root_size)
|
2017-05-25 16:51:28 +02:00
|
|
|
|
|
|
|
root_path = os.path.join(source_dir, 'root.img')
|
|
|
|
if os.path.exists(root_path + '.part.00'):
|
|
|
|
input_files = glob.glob(root_path + '.part.*')
|
|
|
|
cat = subprocess.Popen(['cat'] + sorted(input_files),
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
tar = subprocess.Popen(['tar', 'xSOf', '-'],
|
|
|
|
stdin=cat.stdout,
|
|
|
|
stdout=subprocess.PIPE)
|
2017-05-26 19:09:29 +02:00
|
|
|
cat.stdout.close()
|
2017-05-25 16:51:28 +02:00
|
|
|
vm.volumes['root'].import_data(stream=tar.stdout)
|
|
|
|
if tar.wait() != 0:
|
|
|
|
raise qubesadmin.exc.QubesException('root.img extraction failed')
|
|
|
|
if cat.wait() != 0:
|
|
|
|
raise qubesadmin.exc.QubesException('root.img extraction failed')
|
2020-06-27 21:07:08 +02:00
|
|
|
elif os.path.exists(root_path + '.tar'):
|
|
|
|
tar = subprocess.Popen(['tar', 'xSOf', root_path + '.tar'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
vm.volumes['root'].import_data(stream=tar.stdout)
|
|
|
|
if tar.wait() != 0:
|
|
|
|
raise qubesadmin.exc.QubesException('root.img extraction failed')
|
2017-05-25 16:51:28 +02:00
|
|
|
elif os.path.exists(root_path):
|
|
|
|
if vm.app.qubesd_connection_type == 'socket':
|
|
|
|
# check if root.img was already overwritten, i.e. if the source
|
|
|
|
# and destination paths are the same
|
|
|
|
vid = vm.volumes['root'].vid
|
|
|
|
pool = vm.app.pools[vm.volumes['root'].pool]
|
2018-02-12 22:51:01 +01:00
|
|
|
if (pool.driver in ('file', 'file-reflink')
|
|
|
|
and root_path == os.path.join(pool.config['dir_path'],
|
|
|
|
vid + '.img')):
|
2017-05-25 16:51:28 +02:00
|
|
|
vm.log.info('root.img already in place, do not re-import')
|
|
|
|
return
|
|
|
|
with open(root_path, 'rb') as root_file:
|
|
|
|
vm.volumes['root'].import_data(stream=root_file)
|
|
|
|
|
2018-03-19 11:40:58 +01:00
|
|
|
if vm.volumes['root'].size > root_size:
|
|
|
|
try:
|
|
|
|
vm.volumes['root'].resize(root_size)
|
|
|
|
except qubesadmin.exc.QubesException as err:
|
|
|
|
vm.log.warning(
|
|
|
|
'Failed to resize root volume of {} from {} to {} after '
|
|
|
|
'import: {}'.format(vm.name, vm.volumes['root'].size,
|
|
|
|
root_size, str(err)))
|
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
|
2019-07-29 18:48:19 +02:00
|
|
|
def reset_private_img(vm):
|
|
|
|
'''Clear private volume'''
|
2020-07-13 19:48:19 +02:00
|
|
|
vm.volumes['private'].clear_data()
|
2019-07-29 18:48:19 +02:00
|
|
|
|
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
def import_appmenus(vm, source_dir):
|
|
|
|
'''Import appmenus settings into VM object (later: GUI VM)'''
|
|
|
|
if os.getuid() == 0:
|
|
|
|
try:
|
|
|
|
qubes_group = grp.getgrnam('qubes')
|
|
|
|
user = qubes_group.gr_mem[0]
|
|
|
|
cmd_prefix = ['runuser', '-u', user, '--', 'env', 'DISPLAY=:0']
|
|
|
|
except KeyError as e:
|
|
|
|
vm.log.warning('Default user not found, not importing appmenus: ' +
|
|
|
|
str(e))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
cmd_prefix = []
|
|
|
|
|
2020-06-30 06:08:16 +02:00
|
|
|
# store the whitelists in VM features
|
|
|
|
# separated by spaces should be ok as there should be no spaces in the file
|
|
|
|
# name according to the FreeDesktop spec
|
2020-06-27 21:07:08 +02:00
|
|
|
with open(os.path.join(source_dir, 'vm-whitelisted-appmenus.list'), 'r') \
|
2020-08-08 08:57:22 +02:00
|
|
|
as fd:
|
|
|
|
vm.features['default-menu-items'] = ' '.join([x.rstrip() for x in fd])
|
2020-06-27 21:07:08 +02:00
|
|
|
with open(os.path.join(source_dir, 'whitelisted-appmenus.list'), 'r') \
|
2020-08-08 08:57:22 +02:00
|
|
|
as fd:
|
|
|
|
vm.features['menu-items'] = ' '.join([x.rstrip() for x in fd])
|
2020-06-27 21:07:08 +02:00
|
|
|
with open(
|
|
|
|
os.path.join(source_dir, 'netvm-whitelisted-appmenus.list'), 'r') \
|
2020-08-08 08:57:22 +02:00
|
|
|
as fd:
|
|
|
|
vm.features['netvm-menu-items'] = ' '.join([x.rstrip() for x in fd])
|
2020-06-27 21:07:08 +02:00
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
# TODO: change this to qrexec calls to GUI VM, when GUI VM will be
|
|
|
|
# implemented
|
2017-06-25 15:09:57 +02:00
|
|
|
try:
|
|
|
|
subprocess.check_call(cmd_prefix + ['qvm-appmenus',
|
|
|
|
'--set-default-whitelist={}'.format(os.path.join(source_dir,
|
|
|
|
'vm-whitelisted-appmenus.list')), vm.name])
|
|
|
|
subprocess.check_call(cmd_prefix + ['qvm-appmenus',
|
|
|
|
'--set-whitelist={}'.format(os.path.join(source_dir,
|
|
|
|
'whitelisted-appmenus.list')), vm.name])
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
vm.log.warning('Failed to set default application list: %s', e)
|
2017-05-25 16:51:28 +02:00
|
|
|
|
2020-06-27 21:07:08 +02:00
|
|
|
def parse_template_config(path):
|
|
|
|
'''Parse template.conf from template package. (KEY=VALUE format)'''
|
2020-08-08 08:57:22 +02:00
|
|
|
with open(path, 'r') as fd:
|
|
|
|
return dict(line.rstrip('\n').split('=', 1) for line in fd)
|
2020-06-27 21:07:08 +02:00
|
|
|
|
2017-06-26 14:04:37 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def call_postinstall_service(vm):
|
|
|
|
'''Call qubes.PostInstall service
|
|
|
|
|
|
|
|
And adjust related settings (netvm, features).
|
|
|
|
'''
|
|
|
|
# just created, so no need to save previous value - we know what it was
|
|
|
|
vm.netvm = None
|
|
|
|
# temporarily enable qrexec feature - so vm.start() will wait for it;
|
|
|
|
# if start fails, rollback it
|
|
|
|
vm.features['qrexec'] = True
|
|
|
|
try:
|
|
|
|
vm.start()
|
|
|
|
except qubesadmin.exc.QubesException:
|
|
|
|
del vm.features['qrexec']
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
vm.run_service_for_stdio('qubes.PostInstall')
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
vm.log.error('qubes.PostInstall service failed')
|
|
|
|
vm.shutdown()
|
|
|
|
if have_events:
|
|
|
|
try:
|
|
|
|
# pylint: disable=no-member
|
2017-07-05 14:12:27 +02:00
|
|
|
yield from asyncio.wait_for(
|
|
|
|
qubesadmin.events.utils.wait_for_domain_shutdown([vm]),
|
|
|
|
qubesadmin.config.defaults['shutdown_timeout'])
|
|
|
|
except asyncio.TimeoutError:
|
2017-06-26 14:04:37 +02:00
|
|
|
vm.kill()
|
|
|
|
else:
|
|
|
|
timeout = qubesadmin.config.defaults['shutdown_timeout']
|
|
|
|
while timeout >= 0:
|
|
|
|
if vm.is_halted():
|
|
|
|
break
|
|
|
|
yield from asyncio.sleep(1)
|
|
|
|
timeout -= 1
|
|
|
|
if not vm.is_halted():
|
2018-06-27 01:29:20 +02:00
|
|
|
try:
|
|
|
|
vm.kill()
|
|
|
|
except qubesadmin.exc.QubesVMNotStartedError:
|
|
|
|
pass
|
2017-06-26 14:04:37 +02:00
|
|
|
finally:
|
|
|
|
vm.netvm = qubesadmin.DEFAULT
|
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
|
2017-06-25 18:22:28 +02:00
|
|
|
@asyncio.coroutine
|
2017-05-25 16:51:28 +02:00
|
|
|
def post_install(args):
|
|
|
|
'''Handle post-installation tasks'''
|
|
|
|
|
|
|
|
app = args.app
|
2018-03-19 11:44:57 +01:00
|
|
|
vm_created = False
|
2018-04-21 15:07:21 +02:00
|
|
|
# reinstall and running in dom0, using the same directory as qubes core
|
|
|
|
local_reinstall = False
|
2017-05-25 16:51:28 +02:00
|
|
|
try:
|
|
|
|
# reinstall
|
|
|
|
vm = app.domains[args.name]
|
2018-04-21 15:07:21 +02:00
|
|
|
if app.qubesd_connection_type == 'socket' and \
|
|
|
|
args.dir == '/var/lib/qubes/vm-templates/' + args.name:
|
|
|
|
# VM exists and use use the same directory as target vm - on
|
|
|
|
# final cleanup remove only some files, not the whole directory
|
|
|
|
local_reinstall = True
|
2017-05-25 16:51:28 +02:00
|
|
|
except KeyError:
|
|
|
|
if app.qubesd_connection_type == 'socket' and \
|
|
|
|
args.dir == '/var/lib/qubes/vm-templates/' + args.name:
|
|
|
|
# vm.create_on_disk() need to create the directory on its own,
|
2018-04-21 15:07:21 +02:00
|
|
|
# move it away from its way
|
2017-05-25 16:51:28 +02:00
|
|
|
tmp_sourcedir = os.path.join('/var/lib/qubes/vm-templates',
|
|
|
|
'tmp-' + args.name)
|
|
|
|
shutil.move(args.dir, tmp_sourcedir)
|
|
|
|
args.dir = tmp_sourcedir
|
|
|
|
|
|
|
|
vm = app.add_new_vm('TemplateVM',
|
|
|
|
name=args.name,
|
2020-07-29 14:55:56 +02:00
|
|
|
label=qubesadmin.config.defaults['template_label'],
|
|
|
|
pool=args.pool)
|
2018-03-19 11:44:57 +01:00
|
|
|
vm_created = True
|
2017-05-25 16:51:28 +02:00
|
|
|
|
|
|
|
vm.log.info('Importing data')
|
2017-06-25 15:09:57 +02:00
|
|
|
try:
|
|
|
|
import_root_img(vm, args.dir)
|
|
|
|
except:
|
|
|
|
# if data import fails, remove half-created VM
|
2018-03-19 11:44:57 +01:00
|
|
|
if vm_created:
|
|
|
|
del app.domains[vm.name]
|
2017-06-25 15:09:57 +02:00
|
|
|
raise
|
2019-07-29 18:48:19 +02:00
|
|
|
if not vm_created:
|
|
|
|
vm.log.info('Clearing private volume')
|
|
|
|
reset_private_img(vm)
|
2020-06-27 21:07:08 +02:00
|
|
|
vm.installed_by_rpm = not args.no_installed_by_rpm
|
2017-05-25 16:51:28 +02:00
|
|
|
import_appmenus(vm, args.dir)
|
|
|
|
|
2020-06-27 21:07:08 +02:00
|
|
|
conf_path = os.path.join(args.dir, 'template.conf')
|
|
|
|
if os.path.exists(conf_path):
|
|
|
|
conf = parse_template_config(conf_path)
|
|
|
|
# Import qvm-feature tags
|
|
|
|
for key in (
|
|
|
|
'no-monitor-layout',
|
|
|
|
'pci-e820-host',
|
|
|
|
'linux-stubdom',
|
|
|
|
'gui',
|
|
|
|
'gui-emulated'
|
|
|
|
'qrexec'):
|
2020-06-30 06:09:22 +02:00
|
|
|
if key in conf:
|
|
|
|
if conf[key] == '1':
|
|
|
|
vm.features[key] = conf[key]
|
|
|
|
else:
|
|
|
|
vm.log.warning(
|
|
|
|
'ignoring boolean config flags that are not \'1\'')
|
|
|
|
for key in (
|
|
|
|
'net.fake-ip',
|
|
|
|
'net.fake-gateway',
|
|
|
|
'net.fake-netmask'):
|
2020-06-27 21:07:08 +02:00
|
|
|
if key in conf:
|
|
|
|
vm.features[key] = conf[key]
|
|
|
|
if 'virt-mode' in conf:
|
|
|
|
if conf['virt-mode'] == 'pv' and args.allow_pv:
|
|
|
|
vm.virt_mode = 'pv'
|
2020-06-30 06:14:50 +02:00
|
|
|
elif conf['virt-mode'] == 'pv':
|
2020-06-27 21:07:08 +02:00
|
|
|
vm.log.warning(
|
|
|
|
'--allow-pv not set, ignoring request to change virt-mode')
|
2020-06-30 06:14:50 +02:00
|
|
|
elif conf['virt-mode'] in ('pvh', 'hvm'):
|
|
|
|
vm.virt_mode = conf['virt-mode']
|
|
|
|
|
2020-06-27 21:07:08 +02:00
|
|
|
if 'kernel' in conf:
|
|
|
|
if conf['kernel'] == '':
|
|
|
|
vm.kernel = ''
|
|
|
|
else:
|
|
|
|
vm.log.warning(
|
|
|
|
'Currently only supports setting kernel to (none)')
|
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
if not args.skip_start:
|
2017-06-26 14:04:37 +02:00
|
|
|
yield from call_postinstall_service(vm)
|
2017-05-25 16:51:28 +02:00
|
|
|
|
2017-09-05 03:57:55 +02:00
|
|
|
if not args.keep_source:
|
2018-04-21 15:07:21 +02:00
|
|
|
if local_reinstall:
|
|
|
|
# remove only imported root img
|
|
|
|
root_path = os.path.join(args.dir, 'root.img')
|
|
|
|
for root_part in glob.glob(root_path + '.part.*'):
|
|
|
|
os.unlink(root_part)
|
|
|
|
else:
|
|
|
|
shutil.rmtree(args.dir)
|
2018-03-02 02:26:26 +01:00
|
|
|
# if running as root, tell underlying storage layer about just freed
|
|
|
|
# data blocks
|
|
|
|
if os.getuid() == 0:
|
|
|
|
subprocess.call(['sync', '-f', os.path.dirname(args.dir)])
|
|
|
|
subprocess.call(['fstrim', os.path.dirname(args.dir)])
|
2017-09-05 03:57:55 +02:00
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def pre_remove(args):
|
|
|
|
'''Handle pre-removal tasks'''
|
|
|
|
app = args.app
|
|
|
|
try:
|
|
|
|
tpl = app.domains[args.name]
|
|
|
|
except KeyError:
|
2018-01-02 06:42:47 +01:00
|
|
|
parser.error('No Qube with this name exists')
|
2017-05-25 16:51:28 +02:00
|
|
|
for appvm in tpl.appvms:
|
2018-01-02 06:42:47 +01:00
|
|
|
parser.error('Qube {} uses this template'.format(appvm.name))
|
2017-05-25 16:51:28 +02:00
|
|
|
|
2018-01-02 07:19:06 +01:00
|
|
|
tpl.installed_by_rpm = False
|
2017-05-25 16:51:28 +02:00
|
|
|
del app.domains[args.name]
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2017-05-29 05:50:10 +02:00
|
|
|
def is_chroot():
|
|
|
|
'''Detect if running inside chroot'''
|
|
|
|
try:
|
|
|
|
stat_root = os.stat('/')
|
|
|
|
stat_init_root = os.stat('/proc/1/root/.')
|
|
|
|
return (
|
|
|
|
stat_root.st_dev != stat_init_root.st_dev or
|
|
|
|
stat_root.st_ino != stat_init_root.st_ino)
|
|
|
|
except IOError:
|
|
|
|
print('Stat failed, assuming not chroot', file=sys.stderr)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
def main(args=None, app=None):
|
|
|
|
'''Main function of qvm-template-postprocess'''
|
|
|
|
args = parser.parse_args(args, app=app)
|
2017-05-29 05:50:10 +02:00
|
|
|
|
|
|
|
if is_chroot():
|
|
|
|
print('Running in chroot, ignoring request. Import template with:',
|
|
|
|
file=sys.stderr)
|
|
|
|
print(' '.join(sys.argv), file=sys.stderr)
|
|
|
|
return
|
|
|
|
|
2017-05-25 16:51:28 +02:00
|
|
|
if not args.really:
|
|
|
|
parser.error('Do not call this tool directly.')
|
|
|
|
if args.action == 'post-install':
|
2017-06-25 18:22:28 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
try:
|
|
|
|
loop.run_until_complete(post_install(args))
|
|
|
|
loop.stop()
|
|
|
|
loop.run_forever()
|
|
|
|
finally:
|
|
|
|
loop.close()
|
2017-05-25 16:51:28 +02:00
|
|
|
elif args.action == 'pre-remove':
|
|
|
|
pre_remove(args)
|
|
|
|
else:
|
|
|
|
parser.error('Unknown action')
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|