bootstrap-debootstrap 5.82 KB
Newer Older
1
#!/usr/bin/python3
2

3
## live-build(7) - Live System Build Components
4
## Copyright (C) 2006-2014 Daniel Baumann <mail@daniel-baumann.ch>
5
##
6
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
7
8
9
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.

10

11
12
import argparse
import configparser
13
import glob
14
15
16
import os
import shutil
import subprocess
17
import sys
18
19


20
# TODO:
21
22
23
#   * logfile output
#   * lockfile handling
#   * use gettext for i18n
24
25
#   * debootstrap-options from config/options/debootstrap
#   * take mirrors from config/archives/mirror.{bootstrap,chroot}
26

27
28
29
def main():
	## Parsing Arguments
	arguments = argparse.ArgumentParser(
30
		prog            = 'lb bootstrap-debootstrap',
31
		usage           = '%(prog)s [arguments]',
32
		description     = '''live-build contains the components to build a live system from a configuration directory.
33
34
		                     The bootstrap-debootstrap command bootstraps the chroot system with debootstrap.''',
		epilog          = 'See \'man lb-bootstrap-debootstrap\' for more information.',
35
36
		formatter_class = argparse.ArgumentDefaultsHelpFormatter
	)
37

38
39
	arguments.add_argument('--version',             help='show program\'s version number and exit', action='version', version='live-build 4')
	arguments.add_argument('--verbose',             help='set verbose option',                      action='store_true')
40
	arguments.add_argument('--debootstrap-options', help='set debootstrap(8) options' )
41

42
	args = arguments.parse_args()
43

44
45
46
	## Parsing Configuration
	if not os.path.isfile('config/build'):
		print('E: config/build - no such file', file=sys.stderr)
47

48
		sys.exit(1)
49

50
	config = configparser.ConfigParser()
51

52
	config.read('config/build')
53

54
55
56
57
58
59
60
61
62
	try:
		architecture     = config.get('Image', 'Architecture')
		archive_areas    = config.get('Image', 'Parent-Archive-Areas')
		distribution     = config.get('Image', 'Parent-Distribution')
		mirror_bootstrap = config.get('Image', 'Parent-Mirror-Bootstrap')
	except:
		archive_areas    = config.get('Image', 'Archive-Areas')
		distribution     = config.get('Image', 'Distribution')
		mirror_bootstrap = config.get('Image', 'Mirror-Bootstrap')
63

64
65
	# --verbose
	verbose = args.verbose
66

67
68
	# --debootstrap-options
	debootstrap_options_late = distribution + ' chroot ' + mirror_bootstrap
69

70
	debootstrap_options_early = ''
71

72
73
	if (architecture) and (not architecture == 'auto'):
		debootstrap_options_early = debootstrap_options_early + ' --arch=' + architecture
74

75
76
	if (archive_areas) and (not archive_areas == 'main'):
		debootstrap_options_early = debootstrap_options_early + ' --components=' + archive_areas.replace(' ',',')
77

78
79
80
81
	if args.debootstrap_options:
		debootstrap_options = debootstrap_options_early + ' ' + args.debootstrap_options + ' ' + debootstrap_options_late
	else:
		debootstrap_options = debootstrap_options_early + ' ' + debootstrap_options_late
82

83
	## Calling debootstrap
84

85
86
87
88
	# stagefile
	if os.path.isfile('.build/bootstrap'):
		if verbose:
			print('I: bootstrap already done - nothing to do')
89

90
		sys.exit(0)
91

92
93
94
	# dependencies
	if not os.path.isfile('/usr/sbin/debootstrap'):
		print('E: /usr/sbin/debootstrap - no such file', file=sys.stderr)
95

96
		if verbose:
97
			print('I: debootstrap can be obtained from:\n'
98
99
100
101
			      'I:   http://anonscm.debian.org/gitweb/?p=d-i/debootstrap.git\n'
			      'I:   http://ftp.debian.org/debian/pool/main/d/debootstrap/\n'
			      'I: On Debian based systems, debootstrap can be installed with:\n'
			      'I:   # sudo apt-get install debootstrap')
102

103
		sys.exit(1)
104

105
106
107
	# clean
	if os.path.exists('chroot'):
		print('E: chroot already exists - unclean build', file=sys.stderr)
108

109
110
111
112
		if verbose:
			print('I: use \'lb clean\' to clean up a previously incomplete build')

		sys.exit(1)
113
114
115
116
117
118
119
120

	# stage cache
	if os.path.exists('cache/bootstrap'):
		if verbose:
			print('I: Copying cache/bootstrap to chroot')

		# Notes:
		#  * there's no Python equivalent to 'cp -a' that handels both symlinks and device nodes properly.
121
		cache = subprocess.check_call('cp -a cache/bootstrap chroot', shell=True)
122
123
124
125
126
127

		os.makedirs('.build', exist_ok=True)
		open('.build/bootstrap', 'w').close()

		sys.exit(0)

128
	# packages cache
129
	if glob.glob('cache/packages.bootstrap/*.deb'):
130
131
132
		if verbose:
			print('I: Copying cache/packages.bootstrap/*.deb to chroot/var/cache/apt/archives/*.deb')

133
134
		# Notes:
		#   * copy instead of move to make cache survive incomplete build
135
136
137
138
139
140
141
142
143
144
		os.makedirs('chroot/var/cache/apt/archives', exist_ok=True)

		for package in glob.glob('cache/packages.bootstrap/*.deb'):
			os.link(package, os.path.join('chroot/var/cache/apt/archives/' + os.path.basename(package)))

	# debootstrap
	if not os.path.exists('chroot/bin'):
		if verbose:
			print('I: Calling \'/usr/sbin/debootstrap ' + debootstrap_options + '\'')

145
		debootstrap = subprocess.check_call('/usr/sbin/debootstrap ' + debootstrap_options, shell=True)
146
147
148
149
150
151

	# package cache
	if glob.glob('chroot/var/cache/apt/archives/*.deb'):
		if verbose:
			print('I: Copying chroot/var/cache/apt/archives/*.deb to cache/packages.bootstrap')

152
153
154
		# Notes:
		#   * remove first to keep cache minimal,
		#   * remove files instead of directory to work with symlinked directory
155
156
157
158
159
		for package in glob.glob('cache/packages.bootstrap/*.deb'):
			os.remove(package)

		os.makedirs('cache/packages.bootstrap', exist_ok=True)

160
161
		# Notes:
		#   * move instead of copy to keep stage minimal
162
163
164
		for package in glob.glob('chroot/var/cache/apt/archives/*.deb'):
			shutil.move(package, 'cache/packages.bootstrap')

165
166
167
168
169
170
171
	# stage cache
	if not os.path.exists('cache/bootstrap'):
		if verbose:
			print('I: Copying chroot to cache/bootstrap')

		# Notes:
		#  * there's no Python equivalent to 'cp -a' that handels both symlinks and device nodes properly.
172
		cache = subprocess.check_call('cp -a chroot cache/bootstrap', shell=True)
173
174

	# stagefile
175
176
177
178
179
180
	os.makedirs('.build', exist_ok=True)
	open('.build/bootstrap', 'w').close()


if __name__ == '__main__':
	main()