-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup-debootstrap.sh
442 lines (369 loc) · 13.6 KB
/
setup-debootstrap.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
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
311
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
354
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
#!/bin/bash
# Step 1: Setting up required env
dependencies=( debootstrap binfmt-support qemu-user-static )
ssh_packages=( ssh openssh-server )
debpkg_default='kmod dbus apt apt-utils xz-utils isc-dhcp-server dialog net-tools locales iproute2 iputils-ping ifupdown ssh nano pciutils i2c-tools dosfstools wget man-db'
debpkg_powerpcspe='systemd-sysv udev kmod dbus apt apt-utils xz-utils isc-dhcp-server dialog locales debian-ports-archive-keyring net-tools iproute2 iputils-ping ifupdown ssh nano pciutils i2c-tools dosfstools wget man-db'
debpkg_s390x='kmod dbus apt apt-utils xz-utils isc-dhcp-server dialog locales net-tools iproute2 iputils-ping ifupdown openssh-client nano pciutils i2c-tools dosfstools wget man-db'
rootfs_suffix=debian-rootfs
# Available architectures with their associated qemu
declare -A qemu_static
#qemu_static[amd64]=qemu-x86_64-static #=> see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=703825
qemu_static[arm64]=qemu-aarch64-static
qemu_static[armel]=qemu-arm-static
qemu_static[armhf]=qemu-arm-static
qemu_static[i386]=qemu-i386-static
qemu_static[alpha]=qemu-alpha-static
qemu_static[m68k]=qemu-m68k-static
qemu_static[mips]=qemu-mips-static
qemu_static[mipsel]=qemu-mipsel-static
qemu_static[powerpc]=qemu-ppc-static
qemu_static[powerpcspe]=qemu-ppc-static
qemu_static[ppc64el]=qemu-ppc64le-static
qemu_static[riscv32]=qemu-riscv32-static
qemu_static[riscv64]=qemu-riscv64-static
qemu_static[s390x]=qemu-s390x-static
qemu_static[sh4]=qemu-sh4-static
qemu_static[sparc64]=qemu-sparc64-static
# Prints the archs list
print_archs() {
echo " - $host_arch"
for i in ${!qemu_static[@]}; do
if [[ $i != $host_arch ]]; then
echo " - $i"
fi
done
}
# Check script is sourced
#if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# echo "`basename $0` needs to be sourced"
# exit 1
#fi
# Get caller script
caller_script=`basename $(caller | awk '{print $2}')`
exit_or_return=`[[ $caller_script != NULL ]] && echo exit|| echo return`
# Make sure only root can run our script
if [[ $(id -u) != 0 ]]; then
echo "This script must be run as root"
$exit_or_return 1
fi
# Required packages to build rootfs
for i in ${build_packages[@]}; do
if ! dpkg -s $i 2>/dev/null | grep -q "Status: install ok installed"; then
echo "$i package is required, please install it"
$exit_or_return 1
fi
done
# Get host architecture
host_arch=`dpkg --print-architecture`
case $host_arch in
aarch64) host_arch=arm64 ;;
*) host_arch=$host_arch
esac
containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
# Print usage
function show_usage (){
running_script=`[[ $caller_script != NULL ]] && echo $caller_script ||\
echo "source \`basename ${BASH_SOURCE[0]}\`"`
echo "usage: $running_script [-harRiev?]"
echo ""
echo "Arguments:"
echo " -h, --help : Shows this help menu."
echo " -a, --arch : Sets the architecture to make the rootfs of."
echo " -r, --release : Sets the Debian rootfs release. (default: stable)"
echo " -R, --repo : Gets packages from the given repository."
echo " -i, --include : Includes the packages to be installed in the rootfs."
echo " -e, --exclude : Excludes the packages to be not installed. (It is dangerous to exclude important packages.)"
echo " -v, --variant : Sets the variant of debian to install."
echo ""
echo " arch can be:"
print_archs
$exit_or_return 0
}
# Set command arguments
while [[ $# -gt 0 ]]; do
opt="$1"
shift;
current_arg="$1"
if [[ "$current_arg" =~ ^-{1,2}.* ]]; then
echo "WARNING: You may have left an argument blank. Double check your command."
fi
case "$opt" in
"-h"|"--help"|"?" ) show_usage
$exit_or_return 0;;
"-a"|"--arch" ) arch="$1"; shift;;
"--arch=*" ) arch="${0/--arch=/''}"; shift;;
"-r"|"--release" ) release="$1"; shift;;
"-R"|"--repo" ) repo="$1"; shift;;
"-i"|"--include" ) include="$1"; shift;;
"-e"|"--exclude" ) exclude="$1"; shift;;
"-v"|"--variant" ) variant="$1"; shift;;
* ) show_usage >&2
$exit_or_return 1;;
esac
done
# we can also do this
# (I'm jus including this line for my learning experience :P)
#while (( "$#" )); do
# case "${1}" in
# --help ) show_usage && exit 0;;
# -h ) show_usage && exit 0;;
# --zip ) make_dry=1; shift;;
# --arch=* ) arch=${1/--arch=/''}; shift;;
# --arch* ) arch=${2}; shift; shift;;
# --repo=* ) repo=${1/--repo=/''}; shift;;
# --repo* ) repo=${2}; shift; shift;;
# * ) show_usage >&2; $exit_or_return 1;;
# esac
#done
if [[ ! $arch ]]; then
arch=$host_arch
fi
if [[ ! $release ]]; then
case $arch in
"source" | "ia64" | "powerpc" | "powerpcspe" | "m68k" | "riscv64" | "sh4" | "sparc64" ) release=unstable ;;
"mips" ) release=oldstable ;;
* ) release=stable
esac
fi
if [[ ! $repo ]]; then
case $arch in
"source" | "ia64" | "powerpc" | "powerpcspe" | "m68k" | "riscv64" | "sh4" | "sparc64" ) repo="http://ftp.ports.debian.org/debian-ports/" ;;
* ) repo="http://ftp.debian.org/debian/"
esac
fi
if [[ ! $variant ]]; then
variant=minbase
fi
# Default packages
case $arch in
powerpcspe) instpkgs=$debpkg_powerpcspe ;;
s390x) instpkgs=$debpkg_s390x ;;
*) instpkgs=$debpkg_default
esac
if [[ ! $include ]]; then
includepkg="$instpkgs"
else
includepkg="$instpkgs $include"
fi
if [[ ! $exclude ]]; then
excludepkg=""
else
excludepkg="--exclude $exclude"
fi
#excludeArch=("all" "source")
# Check architecture is suppported
if [[ $arch != "all" || $arch != $host_arch ]]; then
if [[ ! ${qemu_static[$arch]} ]]; then
echo "$arch not valid, architectures supported are:"
print_archs
$exit_or_return 1
fi
# Find qemu binary
qemu_path=`which ${qemu_static[$arch]}`
case $arch in
powerpcspe)
# Set qemu-ppc-static to support powerpcspe
export QEMU_CPU=e500v2
;;
s390x)
# qemu-s390x-static cannot install openssh-server
for i in ${ssh_packages[@]}; do
if grep -q "\b$i\b" $conf_file; then
echo "$i package in $conf_file cannot be installed for s390x"
$exit_or_return 1
fi
done
;;
esac
fi
# Create build directory
main_dir=build
build_dir=$main_dir/$arch
mkdir -p $build_dir 2>/dev/null
mkdir -p $main_dir/logs 2>/dev/null
# Set rootfs directory
rootfs_dir=$rootfs_suffix\-$arch
# Step 2: Installing and configuring the rootfs
# Get current UTC time
utc_time=`date -u -d"$(wget -qO- --save-headers http://www.debian.org |\
sed '/^Date: /!d;s///;q')" +%Y%m%dT%H%M%SZ`
rootfs_dir_utc=$rootfs_dir-$utc_time
# Log the output into a file (best one I tried yet)
#LOG_FILE=build/logs/$rootfs_dir_utc.log
#if [[ -f "./log4bash.sh" ]]; then
# source ./log4bash.sh
# exec > >(
# while read -r line
# do
# if echo "$line" | grep -wq 'W:\|warning:'; then
# log_warning "$line"
# elif echo "$line" | grep -wq 'E:\|error:'; then
# log_error "$line"
# else
# log "$line"
# fi | tee -a $LOG_FILE
# done
# )
# exec 2> >(while read -r line; do log_error "$line" | tee -a $LOG_FILE; done >&2)
#fi
#exec > >(while read -r line; do printf '%s %s\n' "$(date --utc +"%Y-%m-%d %H:%M:%S")" "[INFO]:" "$line" | tee -a $LOG_FILE; done)
#exec 2> >(while read -r line; do printf '%s %s\n' "$(date --utc +"%Y-%m-%d %H:%M:%S")" "[ERROR]:" "$line" | tee -a $LOG_FILE; done >&2)
# Previous ones I tried :P
#exec 3>&1 4>&2
#trap 'exec 2>&4 1>&3' 0 1 2 3
#exec 1>$LOG_FILE 2>&1
#exec > >(tee -a ${LOG_FILE} )
#exec 2> >(tee -a ${LOG_FILE} >&2)
# Cleanup when interrupt signal is received
trap 'exumount; $exit_or_return 1' SIGINT
function exumount() {
if mount | grep $build_dir/$rootfs_dir_utc/dev > /dev/null; then
umount $build_dir/$rootfs_dir_utc/dev
else
:
fi
}
if [[ $arch == $host_arch ]]; then
# Create /dev in rootfs
#mkdir -p $build_dir/$rootfs_dir_utc/dev 2>/dev/null
# Mount /dev in rootfs
#mount --bind /dev $build_dir/$rootfs_dir_utc/dev
# Create root file system and configure debian packages
if debootstrap --verbose --arch $arch $excludepkg "$release" "$build_dir/$rootfs_dir_utc" $repo
then
echo "I: debootstrap successfully finished"
else
#if [[ $? != 0 ]]; then
echo "E: debootstrap failed" >&2
#umount "$build_dir/$rootfs_dir_utc/dev"
exumount
rm -rf "$build_dir/$rootfs_dir_utc"
exit 1
fi
else
# Create root file system
if debootstrap --verbose --variant=$variant --foreign --arch $arch $excludepkg "$release" "$build_dir/$rootfs_dir_utc" $repo
then
echo "I: Debootstrap successfully finished"
else
#if [[ $? != 0 ]]; then
echo "E: Debootstrap failed" >&2
rm -rf "$build_dir/$rootfs_dir_utc"
exit 1
fi
# Set environment variables
export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
export LC_ALL=C LANGUAGE=C LANG=C
# Copy qemu binary to rootfs
cp $qemu_path $build_dir/$rootfs_dir_utc$qemu_path
# Mount /dev in rootfs
mount --bind /dev "$build_dir/$rootfs_dir_utc/dev"
# Complete the configure of dash
chroot "$build_dir/$rootfs_dir_utc" /var/lib/dpkg/info/dash.preinst install
# Configure debian packages
chroot "$build_dir/$rootfs_dir_utc" dpkg --configure -a
fi
# Run debootstrap second stage if it exists
if [[ -f "$build_dir/$rootfs_dir_utc/debootstrap/debootstrap" ]]; then
chroot "$build_dir/$rootfs_dir_utc" /debootstrap/debootstrap --second-stage
fi
# Empty root password
chroot $build_dir/$rootfs_dir_utc passwd -d root
# Get packages installed
chroot $build_dir/$rootfs_dir_utc dpkg -l | awk '{if (NR>3) {print $2" "$3}}' > $build_dir/$rootfs_dir_utc\-packages
# Install packages
chroot $build_dir/$rootfs_dir_utc /bin/bash -c "apt-get update && apt-get upgrade -y && apt-get install -y $includepkg"
# Generate locale
chroot $build_dir/$rootfs_dir_utc locale-gen en_US.UTF-8
# Kill processes running in rootfs
#fuser -sk $build_dir/$rootfs_dir_utc
# Umount /dev in rootfs
if mount | grep $build_dir/$rootfs_dir_utc/dev > /dev/null; then
umount $build_dir/$rootfs_dir_utc/dev
fi
# Latest rootfs is the current one
ln -sfn $rootfs_dir_utc $build_dir/$rootfs_dir
# Step 3: Configure debian rootfs
# Set hostname
filename=$build_dir/$rootfs_dir/etc/hostname
echo "debian" > $filename
# Set hosts
filename=$build_dir/$rootfs_dir/etc/hosts
echo "127.0.0.1 localhost" > $filename
echo "127.0.1.1 debian" >> $filename
echo >> $filename
echo "# The following lines are desirable for IPv6 capable hosts" >> $filename
echo "::1 ip6-localhost ip6-loopback" >> $filename
echo "fe00::0 ip6-localnet" >> $filename
echo "ff00::0 ip6-mcastprefix" >> $filename
echo "ff02::1 ip6-allnodes" >> $filename
echo "ff02::2 ip6-allrouters" >> $filename
echo "ff02::3 ip6-allhosts" >> $filename
# DNS.WATCH servers
filename=$build_dir/$rootfs_dir/etc/resolv.conf
echo "# DNS.WATCH servers" > $filename
echo "nameserver 84.200.69.80" >> $filename
echo "nameserver 84.200.70.40" >> $filename
# Set default locale
filename=$build_dir/$rootfs_dir/etc/default/locale
echo "LANG=en_US.UTF-8" > $filename
# Set apt repository
filename=$build_dir/$rootfs_dir/etc/apt/sources.list
echo "deb http://deb.debian.org/debian stable main contrib non-free" > $filename
echo "deb-src http://deb.debian.org/debian stable main contrib non-free" >> $filename
echo "" >> $filename
echo "deb http://deb.debian.org/debian-security/ stable-security main contrib non-free" >> $filename
echo "deb-src http://deb.debian.org/debian-security/ stable-security main contrib non-free" >> $filename
echo "" >> $filename
echo "deb http://deb.debian.org/debian stable-updates main contrib non-free" >> $filename
echo "deb-src http://deb.debian.org/debian stable-updates main contrib non-free" >> $filename
# Keep the rootfs up-to-date with the repos
chroot $build_dir/$rootfs_dir_utc /bin/bash -c "apt-get update && apt-get upgrade -y && apt autoremove -y"
# Enable root autologin
filename=$build_dir/$rootfs_dir/lib/systemd/system/[email protected]
autologin='--autologin root'
execstart='ExecStart=-\/sbin\/agetty'
if [[ ! $(grep -e "$autologin" $filename) ]]; then
sed -i "s/$execstart/$execstart $autologin/" $filename
fi
# Set systemd logging
filename=$build_dir/$rootfs_dir/etc/systemd/system.conf
for i in 'LogLevel=warning'\
'LogTarget=journal'\
; do
sed -i "/${i%=*}/c\\$i" $filename
done
# Enable root to connect to ssh with empty password
filename=$build_dir/$rootfs_dir/etc/ssh/sshd_config
if [[ -f $filename ]]; then
for i in 'PermitRootLogin yes'\
'PermitEmptyPasswords yes'\
'UsePAM no'\
; do
sed -ri "/^#?${i% *}/c\\$i" $filename
done
fi
# Clean apt cache
chroot $build_dir/$rootfs_dir_utc /bin/bash -c "apt clean"
# Clean bash history
chroot $build_dir/$rootfs_dir_utc /bin/bash -c "history -c && history -w"
# Remove qemu binary from rootfs
rm $build_dir/$rootfs_dir_utc$qemu_path 2>/dev/null
echo
echo "$build_dir/`readlink $build_dir/$rootfs_dir` configured"
# Step 4: Archive the rootfs
TAR_EXTENSION=.tar.gz
rootfs_dir_utc=`readlink $build_dir/$rootfs_dir`
tar_name=$rootfs_dir_utc$TAR_EXTENSION
cd $build_dir
#tar cpfz $tar_name $rootfs_dir_utc
tar cpfz $tar_name -C $rootfs_dir_utc/ .
cd - >/dev/null
echo
echo "$build_dir/$tar_name created"