-
Notifications
You must be signed in to change notification settings - Fork 2
/
chrootbootstrapper.sh
executable file
·276 lines (248 loc) · 6.77 KB
/
chrootbootstrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
# chrootbootstrapper.sh (Custom Debian live environment scripts) 1.0.0
#
# Copyright (C) 2018 masakoodaa
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by masakoodaa (github.com/masakoodaa).
# Based on Will Haley's articles on creating custom Debian live environments.
#
# Upstream: https://github.com/masakoodaa/custom-debian-live-environment-scripts/
abort() {
printf '%s\n' "$1" >&2
exit 1
}
usage() {
echo "Usage: chrootbootstrapper.sh [-a|--arch=ARCH] -d|--directory=PATH [--hybrid] [-m|--mirror=MIRROR] [-n|--hostname=HOSTNAME] [-s|--suite=SUITE] [--variant=VARIANT]
chrootbootstrapper.sh --install-dependencies [--hybrid]"
}
show_help() {
usage
echo "
Creates and sets up a base Debian environment (for the purpose of creating a live environment) in the given directory.
Mandatory arguments to long options are mandatory for short options too.
-a --arch=ARCH OS architecture (default: amd64)
-d --directory=PATH full path to the directory where
the files for creating the live
environment will be stored,
must be in a partition that is
NOT mounted with noexec or nodev
-h --help display this help and exit
--hybrid hybrid boot partition allows booting on
both EFI and BIOS systems (default: BIOS only)
does NOT work (yet) with chroot2iso.sh
and DOES NOT WORK WITH Debian 8 (Jessie)
--install-dependencies install the required applications via
apt-get and quit, only needs to be ran once
-m --mirror=MIRROR Debian mirror to download files from
default: http://ftp.us.debian.org/debian/
-n --hostname=HOSTNAME chroot hostname (default: debian-live)
-s --suite Debian release codename (default: stretch)
or symbolic name; see DEBOOTSTRAP(8)
--variant default: minbase; see DEBOOTSTRAP(8)
-v --version show version & licence and exit
\"--install-dependencies\" can be used together with \"--hybrid\" to install the extra dependencies needed for building chroots with hybrid boot partitions.
You will need to choose and install a kernel (and set a root password) in the chroot before running chroot2iso.sh (which does NOT support \"--hybrid\" yet!)
More information availabe at https://github.com/masakoodaa/custom-debian-live-environment-scripts/"
}
show_version() {
echo "chrootbootstrapper.sh (Custom Debian live environment scripts) 1.0.0
Copyright (C) 2018 masakoodaa
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by masakoodaa (github.com/masakoodaa).
Based on Will Haley's articles on creating custom Debian live environments."
}
# initialize all option variables
# this makes sure we aren't contamined by variables from the environment
arch="amd64"
hostname="debian-live"
hybrid=false
installdeps=false
mirror="http://ftp.us.debian.org/debian/"
suite="stretch"
variant="minbase"
workdir=
# parse arguments
while :; do
case $1 in
-h|--help)
show_help
exit 0
;;
-a|--arch)
if [ "$2" ]
then
arch="$2"
shift
fi
;;
--arch=?*)
arch="${1#*=}"
;;
--arch=)
abort '"--arch" can be left out but can not be empty!'
;;
-d|--directory)
if [ "$2" ]
then
workdir="$2"
shift
fi
;;
--directory=?*)
workdir="${1#*=}"
;;
--directory=)
abort '"--directory" must specify a path; see --help'
;;
--hybrid)
hybrid=true
echo "WARNING: chroot2iso.sh doesn't support \"--hybrid\" (yet), so you can't build an iso with it."
;;
--install-dependencies)
installdeps=true
;;
-m|--mirror)
if [ "$2" ]
then
mirror="$2"
shift
fi
;;
--mirror=?*)
mirror="${1#*=}"
;;
--mirror=)
abort '"--mirror" can be left out but can not be empty!'
;;
-n|--hostname)
if [ "$2" ]
then
hostname="$2"
shift
fi
;;
--hostname=?*)
hostname="${1#*=}"
;;
--hostname=)
abort '"--hostname" can be left out but can not be empty!'
;;
-u|--usage) # undocumented super secret option ;)
usage
;;
-s|--suite)
if [ "$2" ]
then
suite="$2"
shift
fi
;;
--suite=?*)
suite="${1#*=}"
;;
--suite=)
abort '"--suite" can be left out but can not be empty!'
;;
--variant)
if [ "$2" ]
then
variant="$2"
shift
fi
;;
--variant=?*)
variant="${1#*=}"
;;
--variant=)
abort '"--variant" can be left out but can not be empty!'
;;
-v|--version)
show_version
exit 0
;;
--)
shift
break
;;
-?*)
abort 'Unknown option. See \"--help\" for help or \"--usage\" for usage.'
;;
*)
break
esac
shift
done
# make sure we are root & good to go
if [ "$EUID" -ne 0 ]
then
echo "Missing root privileges. Aborting."
exit 1
fi
dependencies="chroot debootstrap syslinux isolinux squashfs-tools genisoimage memtest86+ rsync"
if [ "$hybrid" = true ]
then
dependencies="$dependencies gdisk grub2-common grub-efi-amd64"
fi
# install dependencies
if [ "$installdeps" = true ]
then
echo "Installing dependencies..."
apt-get update
apt-get install -y $dependencies \
&& echo "Dependencies installed successfully" \
|| abort 'Installation failed!'
fi
# check if the dependencies are installed
for i in $dependencies
do
case $i in
isolinux)
if [ ! -f "/usr/lib/ISOLINUX/isolinux.bin" ]
then
abort "$i is required but is not installed; see \"--help\""
fi
;;
squashfs-tools)
hash mksquashfs 2>&1 || abort "$i is required but is not installed; see \"--help\""
;;
memtest86+)
if [ ! -f "/boot/memtest86+.bin" ]
then
abort "$i is required but is not installed; see \"--help\""
fi
;;
*)
hash $i 2>&1 || abort "$i is required but is not installed; see \"--help\""
;;
esac
done
# check if $workdir is given and whether the directory exists or needs to be created
# remove trailing slash
workdir=${workdir%%+(/)}
if [ "$workdir" ]
then
if [ ! -d "$workdir" ]
then
echo "Creating directory $workdir and subdirectories"
mkdir -p "$workdir"/{chroot,image/{live,isolinux}}
fi
else
abort 'No directory specified!'
fi
# exit on fail or unset variable (just in case :D)
set -e
set -u
# the easy part: actual work
echo "Bootstrapping..."
debootstrap --arch="$arch" --variant="$variant" "$suite" "$workdir"/chroot "$mirror"
echo "Setting hostname..."
echo "$hostname" > "$workdir/chroot/etc/hostname"
echo "Installing live-boot and systemd-sys in chroot..." #TODO: provide other inits
chroot "$workdir"/chroot /bin/bash -c "apt-get update && apt-get install -y --no-install-recommends live-boot systemd-sysv"
echo "All done! Remember to install a kernel (and set root password) before running chroot2iso.sh"
exit 0