Newer
Older
#!/usr/bin/python3
# flatdeb — build Flatpak runtimes from Debian packages
#
# Copyright © 2016-2017 Simon McVittie
# Copyright © 2017 Collabora Ltd.
#
# Partially derived from vectis, copyright © 2015-2017 Simon McVittie
#
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Create Flatpak runtimes from Debian packages.
- python3
- python3-gi
- python3-yaml
Requires (on worker, possibly the same machine as the host):
- flatpak-builder
- ostree
- sudo
- systemd-container
"""
import argparse
import json
import os
import re
import subprocess
from contextlib import ExitStack, suppress
from tempfile import TemporaryDirectory
import yaml
from gi.repository import GLib
from flatdeb.worker import HostWorker, NspawnWorker, SshWorker, SudoWorker
class Builder:
"""
Main object
"""
def __init__(self):
#: The Debian suite to use
self.apt_suite = 'stretch'
#: The Flatpak branch to use for the runtime, or None for apt_suite
self.runtime_branch = None
#: The Flatpak branch to use for the app
self.app_branch = 'master'
#: The freedesktop.org cache directory
self.xdg_cache_dir = os.getenv(
'XDG_CACHE_DIR', os.path.expanduser('~/.cache'))
self.remote_repo = None
#: Where to write output
self.build_area = os.path.join(
self.xdg_cache_dir, 'flatdeb',
)
self.remote_build_area = None
self.repo = os.path.join(self.build_area, 'repo')
self.__dpkg_arch = None
self.flatpak_arch = None
self.__dpkg_arch_matches_cache = {}
self.suite_details = {}
self.runtime_details = {}
self.root_worker = None
self.worker = None
self.ostree_mode = 'archive-z2'
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
@staticmethod
def get_flatpak_arch(arch=None):
"""
Return the Flatpak architecture name corresponding to uname
result arch.
If arch is None, return the Flatpak architecture name
corresponding to the machine where this script is running.
"""
if arch is None:
arch = os.uname()[4]
if re.match(r'^i.86$', arch):
return 'i386'
elif re.match(r'^arm.*', arch):
if arch.endswith('b'):
return 'armeb'
else:
return 'arm'
elif arch in ('mips', 'mips64'):
import struct
if struct.pack('i', 1).startswith(b'\x01'):
return arch + 'el'
return arch
@staticmethod
def dpkg_to_flatpak_arch(arch):
"""
Return the Flatpak architecture name corresponding to the given
dpkg architecture name.
"""
if arch == 'amd64':
return 'x86_64'
elif arch == 'arm64':
return 'aarch64'
elif arch in ('armel', 'armhf'):
return 'arm'
elif arch == 'powerpc':
return 'ppc'
elif arch == 'powerpc64':
return 'ppc64'
elif arch == 'powerpcel':
return 'ppcle'
elif arch == 'ppc64el':
return 'ppc64le'
return arch
@property
def dpkg_arch(self):
"""
The Debian architecture we are building a runtime for, such as
i386 or amd64.
"""
return self.__dpkg_arch
@dpkg_arch.setter
def dpkg_arch(self, value):
self.__dpkg_arch_matches_cache = {}
self.__dpkg_arch = value
def dpkg_arch_matches(self, arch_spec):
"""
Return True if arch_spec matches dpkg_arch (or
equivalently, if dpkg_arch is one of the architectures
described by arch_spec). For example, any-amd64 matches amd64
but not i386.
"""
if arch_spec not in self.__dpkg_arch_matches_cache:
exit_code = self.worker.call(
['dpkg-architecture', '--host-arch', self.dpkg_arch,
'--is', arch_spec])
self.__dpkg_arch_matches_cache[arch_spec] = (exit_code == 0)
return self.__dpkg_arch_matches_cache[arch_spec]
def run_command_line(self):
"""
Run appropriate commands for the command-line arguments
"""
parser = argparse.ArgumentParser(
description='Build Flatpak runtimes',
)
parser.add_argument('--remote', default=None)
'--ostree-mode', default=self.ostree_mode,
)
parser.add_argument(
'--remote-ostree-mode', default=None,
parser.add_argument(
'--export-bundles', action='store_true', default=False,
)
parser.add_argument('--build-area', default=self.build_area)
parser.add_argument(
'--remote-build-area', default=self.remote_build_area,
)
parser.add_argument('--repo', default=self.repo)
parser.add_argument('--remote-repo', default=self.remote_repo)
parser.add_argument('--suite', '-d', default=self.apt_suite)
parser.add_argument(
'--architecture', '--arch', '-a', default=self.dpkg_arch)
parser.add_argument('--runtime-branch', default=self.runtime_branch)
subparsers = parser.add_subparsers(dest='command', metavar='command')
subparser = subparsers.add_parser(
'base',
help='Build a fresh base tarball',
)
subparser = subparsers.add_parser(
'runtimes',
help='Build runtimes',
)
subparser.add_argument('prefix')
subparser = subparsers.add_parser(
'app',
help='Build an app',
)
parser.add_argument('--app-branch', default=self.app_branch)
subparser.add_argument('prefix')
subparser = subparsers.add_parser(
'print-flatpak-architecture',
help='Print the Flatpak architecture',
)
args = parser.parse_args()
self.build_area = args.build_area
self.apt_suite = args.suite
self.repo = args.repo
self.remote_repo = args.remote_repo
self.export_bundles = args.export_bundles
if args.remote is not None:
self.worker = SshWorker(args.remote)
if self.remote_build_area is None:
self.remote_build_area = self.worker.check_output([
'sh', '-euc',
'mkdir -p "${XDG_CACHE_HOME:="$HOME/.cache"}/flatdeb"; '
'echo "$XDG_CACHE_HOME/flatdeb"',
]).decode('utf-8').rstrip('\n')
if self.remote_repo is None:
self.remote_repo = '{}/repo'.format(self.remote_build_area)
self.remote_ostree_mode = args.remote_ostree_mode
if self.remote_ostree_mode is None:
self.remote_ostree_mode = self.ostree_mode
else:
self.worker = HostWorker()
self.remote_build_area = self.build_area
self.remote_repo = self.repo
self.remote_ostree_mode = self.ostree_mode
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
self.root_worker = SudoWorker(self.worker)
if args.architecture is None:
self.dpkg_arch = self.worker.check_output(
['dpkg-architecture', '-q', 'DEB_HOST_ARCH'],
).decode('utf-8').rstrip('\n')
else:
self.dpkg_arch = args.architecture
self.flatpak_arch = self.dpkg_to_flatpak_arch(self.dpkg_arch)
os.makedirs(self.build_area, exist_ok=True)
os.makedirs(os.path.dirname(self.repo), exist_ok=True)
if args.command is None:
parser.error('A command is required')
with open(self.apt_suite + '.yaml') as reader:
self.suite_details = yaml.safe_load(reader)
getattr(
self, 'command_' + args.command.replace('-', '_'))(**vars(args))
def command_print_flatpak_architecture(self, **kwargs):
print(self.flatpak_arch)
@property
def apt_uris(self):
for source in self.suite_details['sources']:
yield source['apt_uri']
def command_base(self, **kwargs):
with ExitStack() as stack:
stack.enter_context(self.worker)
stack.enter_context(self.root_worker)
base_chroot = '{}/base'.format(self.root_worker.scratch)
argv = [
'env',
'http_proxy=http://192.168.122.1:3142',
'debootstrap',
'--variant=minbase',
'--arch={}'.format(self.dpkg_arch),
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
]
if self.suite_details.get('can_merge_usr', False):
argv.append('--merged-usr')
keyring = self.suite_details['sources'][0].get('keyring')
if keyring is not None:
dest = '{}/{}'.format(
self.root_worker.scratch,
os.path.basename(keyring),
)
self.root_worker.install_file(os.path.abspath(keyring), dest)
argv.append('--keyring=' + dest)
argv.append(self.suite_details['sources'][0].get(
'apt_suite', self.apt_suite,
))
argv.append(base_chroot)
argv.append(self.suite_details['sources'][0]['apt_uri'])
script = self.suite_details.get('debootstrap_script')
if script is not None:
argv.append('/usr/share/debootstrap/scripts/' + script)
try:
self.root_worker.check_call(argv)
except:
with suppress(Exception):
self.root_worker.check_call([
'cat',
'{}/debootstrap/debootstrap.log'.format(base_chroot),
])
raise
# Merge /usr the hard way, if necessary. We are counting on
# the assumption that most packages are actually usrmergeable,
# and those that historically weren't are not upgraded often
# enough to be a practical problem...
if not self.suite_details.get('can_merge_usr', False):
self.root_worker.check_call([
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
'chroot', base_chroot,
'sh',
'-euc',
'usrmerge () {\n'
' local f="$1"\n'
'\n'
' ls -dl "$f" "/usr/$f" >&2 || true\n'
' if [ "$(readlink "$f")" = "/usr$f" ]; then\n'
' echo "Removing $f in favour of /usr$f" >&2\n'
' rm -v -f "$f"\n'
' elif [ "$(readlink "/usr$f")" = "$f" ]; then\n'
' echo "Removing /usr$f in favour of $f" >&2\n'
' rm -v -f "/usr$f"\n'
' else\n'
' echo "Cannot merge $f with /usr$f" >&2\n'
' exit 1\n'
' fi\n'
'}\n'
'\n'
'find /bin /sbin /lib* -not -xtype d |\n'
'while read f; do\n'
' if [ -e /usr/"$f" ]; then\n'
' usrmerge "$f"\n'
' fi\n'
'done\n'
'',
'sh', # argv[0]
base_chroot,
])
self.root_worker.check_call([
'sh', '-euc',
'cd "$1"; tar -cf- bin sbin lib* | tar -C usr -xf-',
'sh', base_chroot,
])
self.root_worker.check_call([
'sh', '-euc', 'cd "$1"; rm -fr bin sbin lib*',
'sh', base_chroot,
])
self.root_worker.check_call([
'sh', '-euc', 'cd "$1"; ln -vs usr/bin usr/sbin usr/lib* .',
'sh', base_chroot,
])
self.configure_base(base_chroot)
self.configure_apt(base_chroot)
tarball = 'base-{}-{}.tar.gz'.format(
self.apt_suite,
self.dpkg_arch,
)
self.root_worker.check_call([
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
'tar', '-zcf', '{}/{}'.format(
self.remote_build_area, tarball,
),
'-C', base_chroot,
'--exclude=./etc/.pwd.lock',
'--exclude=./etc/group-',
'--exclude=./etc/passwd-',
'--exclude=./etc/shadow-',
'--exclude=./home',
'--exclude=./root',
'--exclude=./tmp',
'--exclude=./var/cache',
'--exclude=./var/lock',
'--exclude=./var/tmp',
'.',
])
if not isinstance(self.worker, HostWorker):
output = os.path.join(self.build_area, tarball)
with open(output + '.new', 'wb') as writer:
self.root_worker.check_call([
'cat',
'{}/{}'.format(self.remote_build_area, tarball),
], stdout=writer)
os.rename(output + '.new', output)
def ensure_remote_repo(self):
self.worker.check_call([
'ostree',
'--repo=' + self.remote_repo,
'init',
'--mode={}'.format(self.remote_ostree_mode),
])
def ensure_local_repo(self):
subprocess.check_call([
'ostree',
'--repo=' + self.repo,
'init',
'--mode={}'.format(self.ostree_mode),
])
def command_runtimes(self, *, prefix, **kwargs):
if self.runtime_branch is None:
self.runtime_branch = self.apt_suite
# Be nice to people using tab-completion
if prefix.endswith('.yaml'):
prefix = prefix[:-5]
with open(prefix + '.yaml') as reader:
self.runtime_details = yaml.safe_load(reader)
tarball = 'base-{}-{}.tar.gz'.format(
self.apt_suite,
self.dpkg_arch,
)
with ExitStack() as stack:
stack.enter_context(self.worker)
stack.enter_context(self.root_worker)
base_chroot = '{}/base'.format(self.root_worker.scratch)
self.root_worker.check_call([
'install', '-d', base_chroot,
])
# TODO: Upload tarball from host to remote worker
'tar', '-zxf',
'{}/{}'.format(self.remote_build_area, tarball),
'-C', base_chroot,
'.',
])
# We do common steps for both the Platform and the Sdk
# in the base directory, then copy it.
self.configure_base(base_chroot)
platform_chroot = '{}/platform'.format(self.root_worker.scratch)
sdk_chroot = '{}/sdk'.format(self.root_worker.scratch)
self.root_worker.check_call([
'cp', '-a', '--reflink=auto', base_chroot, platform_chroot,
])
self.root_worker.check_call([
'mv', base_chroot, sdk_chroot,
])
self.ostreeify(
prefix,
platform_chroot,
)
self.ostreeify(
prefix,
sdk_chroot,
sdk=True,
)
self.worker.check_call([
'flatpak',
'build-update-repo',
self.remote_repo,
])
if self.export_bundles:
for suffix in ('.Platform', '.Sdk'):
'time',
'flatpak',
'build-bundle',
'--runtime',
self.remote_repo,
bundle = '{}-{}-{}.bundle'.format(
prefix + suffix,
self.flatpak_arch,
)
output = os.path.join(self.build_area, bundle)
with open(output + '.new', 'wb') as writer:
self.worker.check_call([
'cat',
'{}/bundle'.format(self.worker.scratch),
], stdout=writer)
os.rename(output + '.new', output)
def configure_apt(self, base_chroot):
"""
Configure apt. We only do this once, so that all chroots
created from the same base have their version numbers
aligned.
"""
with TemporaryDirectory(prefix='flatdeb-apt.') as t:
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# Set up the apt sources
to_copy = os.path.join(t, 'sources.list')
with open(to_copy, 'w') as writer:
for source in self.suite_details['sources']:
suite = source.get('apt_suite', self.apt_suite)
suite = suite.replace('*', self.apt_suite)
components = source.get(
'apt_components',
self.suite_details.get(
'apt_components',
['main']))
for prefix in ('deb', 'deb-src'):
writer.write('{} {} {} {}\n'.format(
prefix,
source['apt_uri'],
suite,
' '.join(components),
))
keyring = source.get('keyring')
if keyring is not None:
self.root_worker.install_file(
os.path.abspath(keyring),
'{}/etc/apt/trusted.gpg.d/{}'.format(
base_chroot,
os.path.basename(keyring),
),
)
self.root_worker.install_file(
to_copy,
'{}/etc/apt/sources.list'.format(base_chroot),
)
self.root_worker.check_call([
'rm', '-fr',
'{}/etc/apt/sources.list.d'.format(base_chroot),
])
with NspawnWorker(
self.root_worker,
base_chroot,
env=['http_proxy=http://192.168.122.1:3142'],
) as nspawn:
nspawn.check_call([
'apt-get', '-y', '-q', 'update',
])
nspawn.check_call([
'apt-get', '-y', '-q', 'dist-upgrade',
])
def configure_base(self, base_chroot):
"""
Configure the common chroot that will be copied to make both the
Platform and the Sdk.
"""
with TemporaryDirectory(prefix='flatdeb-base-install.') as t:
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# Disable starting services. This container has no init
# anyway.
to_copy = os.path.join(t, 'policy-rc.d')
with open(to_copy, 'w') as writer:
writer.write('#!/bin/sh\n')
writer.write('exit 101\n')
self.root_worker.install_file(
to_copy,
'{}/usr/sbin/policy-rc.d'.format(base_chroot),
permissions=0o755,
)
with open(to_copy, 'w') as writer:
writer.write('#!/bin/sh\n')
writer.write('exit 0\n')
self.root_worker.install_file(
to_copy,
'{}/usr/sbin/initctl'.format(base_chroot),
permissions=0o755,
)
# There is some cleanup that we can do in the base
# tarball rather than in every runtime individually.
# See https://github.com/debuerreotype/debuerreotype
# for further ideas.
to_copy = os.path.join(t, 'flatpak-runtime')
with open(to_copy, 'w') as writer:
writer.write('force-unsafe-io\n')
writer.write('path-exclude /usr/share/doc/*/*\n')
# For license compliance, we should keep the copyright
# files intact
writer.write('path-include /usr/share/doc/*/copyright\n')
self.root_worker.check_call([
'find', '{}/usr/share/doc'.format(base_chroot), '-xdev',
'-not', '-name', 'copyright', '-not', '-type', 'd',
'-delete'
])
self.root_worker.check_call([
'find', '{}/usr/share/doc'.format(base_chroot), '-depth',
'-xdev', '-type', 'd', '-empty', '-delete'
])
for d in (
'doc-base', 'groff', 'info', 'linda', 'lintian', 'man',
):
writer.write(
'path-exclude /usr/share/{}/*\n'.format(d),
)
self.root_worker.check_call([
'rm', '-fr', '{}/usr/share/{}'.format(base_chroot, d),
])
self.root_worker.check_call([
'install', '-d',
'{}/etc/dpkg/dpkg.cfg.d'.format(base_chroot),
])
self.root_worker.install_file(
to_copy,
'{}/etc/dpkg/dpkg.cfg.d/flatpak-runtime'.format(base_chroot),
)
to_copy = os.path.join(t, 'flatpak-runtime')
with open(to_copy, 'w') as writer:
writer.write('Acquire::Languages "none";\n')
writer.write('Acquire::GzipIndexes "true";\n')
writer.write('Acquire::CompressionTypes::Order:: "gz";\n')
writer.write('APT::InstallRecommends "false";\n')
writer.write(
'APT::AutoRemove::SuggestsImportant "false";\n')
# We rely on autoremove not taking effect immediately
writer.write('APT::Get::AutomaticRemove "false";\n')
writer.write('Aptitude::Delete-Unused "false";\n')
self.root_worker.check_call([
'install', '-d',
'{}/etc/apt/apt.conf.d'.format(base_chroot),
])
self.root_worker.install_file(
to_copy,
'{}/etc/apt/apt.conf.d/flatpak-runtime'.format(base_chroot),
)
if not self.runtime_details:
return
with NspawnWorker(
self.root_worker,
base_chroot,
env=['http_proxy=http://192.168.122.1:3142'],
) as nspawn:
nspawn.check_call([
'install', '-d',
'/var/cache/apt/archives/partial',
'/var/lock',
])
# We use aptitude to help prepare the Platform runtime, and
# it's a useful thing to have in the Sdk runtime
nspawn.check_call([
'apt-get', '-y', 'install', 'aptitude',
])
# All packages will be removed from the platform runtime
# unless they are Essential, depended-on, or in the
# add_packages list.
nspawn.check_call([
'aptitude', '-y', 'markauto', '?installed'
])
# Ubuntu precise doesn't like apt being up for autoremoval.
nspawn.check_call([
'aptitude', '-y', 'unmarkauto', 'apt'
])
packages = self.runtime_details.get('add_packages', [])
if packages:
nspawn.check_call([
'apt-get', '-q', '-y', 'install',
] + packages)
def sdkize(self, sdk_chroot):
"""
Transform a copy of the chroot into a Sdk runtime.
"""
sdk_details = self.runtime_details.get('sdk', {})
with NspawnWorker(
self.root_worker,
sdk_chroot,
env=['http_proxy=http://192.168.122.1:3142'],
) as nspawn:
packages = sdk_details.get('add_packages', [])
if packages:
nspawn.check_call([
'apt-get', '-q', '-y', 'install',
] + packages)
script = sdk_details.get('post_script', [])
if script:
nspawn.check_call([
'sh', '-c', script,
])
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
nspawn.write_manifest()
installed = set(nspawn.check_output([
'dpkg-query', '--show', '-f', '${Package}\\n',
]).split())
return installed
def platformize(self, platform_chroot):
"""
Transform a copy of the chroot into a Platform runtime.
"""
platform_details = self.runtime_details.get('platform', {})
with NspawnWorker(
self.root_worker,
platform_chroot,
env=[
'SUDO_FORCE_REMOVE=yes',
'http_proxy=http://192.168.122.1:3142'
],
) as nspawn:
nspawn.check_call([
'aptitude', '-y', 'purge',
'?and(?installed,?section(devel))',
'?and(?installed,?section(libdevel))',
])
installed = set(nspawn.check_output([
'dpkg-query', '--show', '-f', '${Package}\\n',
]).split())
unwanted = []
for package in [
'aptitude',
'fakeroot',
'libfakeroot',
]:
if package in installed:
unwanted.append(package)
if unwanted:
nspawn.check_call([
'apt-get', '-y', 'purge', unwanted,
])
nspawn.check_call([
'apt-get', '-y', '--purge', 'autoremove',
])
installed = set(nspawn.check_output([
'dpkg-query', '--show', '-f', '${Package}\\n',
]).split())
unwanted = []
# These are Essential (or at least important) but serve no
# purpose in an immutable runtime with no init. Note that
# order is important: adduser needs to be removed before
# debconf.
for package in [
'adduser',
'apt',
'busybox-initramfs',
'debconf',
'debian-archive-keyring',
'e2fsprogs',
'gnupg',
'ifupdown',
'init',
'init-system-helpers',
'initramfs-tools',
'initramfs-tools-bin',
'initscripts',
'insserv',
'iproute',
'login',
'lsb-base',
'module-init-tools',
'mount',
'mountall',
'passwd',
'plymouth',
'systemd',
'systemd-sysv',
'sysv-rc',
'tcpd',
'ubuntu-archive-keyring',
'ubuntu-keyring',
'udev',
'upstart',
]:
if package in installed:
unwanted.append(package)
if 'perl' not in installed:
unwanted.append('perl-base')
if 'python' not in installed:
unwanted.append('python-minimal')
unwanted.append('python2.7-minimal')
if unwanted:
nspawn.check_call([
'dpkg', '--purge', '--force-remove-essential',
'--force-depends',
] + unwanted)
installed = set(nspawn.check_output([
'dpkg-query', '--show', '-f', '${Package}\\n',
]).split())
# We have to do this before removing dpkg :-)
nspawn.write_manifest()
# This has to be last for obvious reasons!
nspawn.check_call([
'dpkg', '--purge', '--force-remove-essential',
'--force-depends',
'dpkg',
])
return installed
def ostreeify(self, prefix, chroot, sdk=False, packages=()):
"""
Move things around to turn a chroot into a runtime.
"""
if sdk:
installed = self.sdkize(chroot)
else:
installed = self.platformize(chroot)
with NspawnWorker(
self.root_worker,
chroot,
) as nspawn:
nspawn.check_call([
'find', '/', '-xdev', '(',
'-lname', '/etc/alternatives/*', '-o',
'-lname', '/etc/locale.alias',
')', '-exec', 'sh', '-euc',
'set -e\n'
'while [ $# -gt 0 ]; do\n'
' if target="$(readlink -f "$1")"; then\n'
' echo "Making $1 a hard link to $target"\n'
' rm -f "$1"\n'
' cp -al "$target" "$1"\n'
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
' fi\n'
' shift\n'
'done'
'',
'sh', # argv[0] for the one-line shell script
'{}', '+',
])
self.root_worker.check_call([
'chmod', '-R', '--changes', 'a-s,o-t,u=rwX,og=rX', chroot,
])
self.root_worker.check_call([
'chown', '-R', '--changes', 'root:root', chroot,
])
self.root_worker.check_call([
'rm', '-fr', '--one-file-system',
'{}/usr/local'.format(chroot),
])
if sdk:
runtime = prefix + '.Sdk'
self.root_worker.check_call([
'rm', '-fr', '--one-file-system',
'{}/etc/group-'.format(chroot),
'{}/etc/gshadow-'.format(chroot),
'{}/etc/passwd-'.format(chroot),
'{}/etc/shadow-'.format(chroot),
'{}/etc/subuid-'.format(chroot),
'{}/etc/subgid-'.format(chroot),
'{}/var/backups'.format(chroot),
'{}/var/cache'.format(chroot),
'{}/var/lib/dpkg/status-old'.format(chroot),
'{}/var/lib/dpkg/statoverride'.format(chroot),
])
self.root_worker.check_call([
'install', '-d',
'{}/var/cache/apt/archives/partial'.format(chroot),
'{}/var/lib/extrausers'.format(chroot),
])
self.root_worker.check_call([
'touch', '{}/var/cache/apt/archives/partial/.exists'.format(chroot),
])
# This is only useful if the SDK has libnss-extrausers
self.root_worker.check_call([
'cp', '{}/etc/passwd'.format(chroot),
'{}/var/lib/extrausers/passwd'.format(chroot),
])
self.root_worker.check_call([
'cp', '{}/etc/group'.format(chroot),
'{}/var/lib/extrausers/groups'.format(chroot),
])
self.root_worker.check_call([
'mv', '{}/etc'.format(chroot),
'{}/usr/etc'.format(chroot),
])
self.root_worker.check_call([
'mv', '{}/var'.format(chroot),
'{}/usr/var'.format(chroot),
])
else:
runtime = prefix + '.Platform'
self.root_worker.check_call([
'rm', '-fr', '--one-file-system',
'{}/etc'.format(chroot),
'{}/share/bash-completion'.format(chroot),
'{}/share/bug'.format(chroot),
'{}/var'.format(chroot),
])
# TODO: Move lib/debug, zoneinfo, locales into extensions
# TODO: Hook point for GL, instead of just Mesa
# TODO: GStreamer extension
# TODO: Icon theme, Gtk theme extension
# TODO: VAAPI extension