-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmk-rootfs
executable file
·87 lines (68 loc) · 2.31 KB
/
mk-rootfs
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
#!/bin/sh
#
# SPDX-FileCopyrightText: 2020 Anuradha Weeraman <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
set -e
basedir=$(dirname $(readlink -f $0))
. ${basedir}/config/env.sh
check_availability_of() {
prog=$1
if ! command -v ${prog} 2>&1 > /dev/null; then
echo "$prog not found in path. Please install and try again."
exit 1
fi
}
log_status() {
status=$1
if [ $status -ne 0 ]; then
echo "failed."
exit 1
else
echo "ok."
fi
}
check_availability_of sudo
check_availability_of qemu-img
check_availability_of /sbin/mkfs.ext4
check_availability_of /sbin/debootstrap
if [ -e "${basedir}/rootfs.img" ]; then
echo -n "Proceed to recreate ${basedir}/rootfs.img (y/N)? "
read input
if [ "$input" != "y" ]; then
exit 1
fi
fi
echo -n "Creating ${rootfs_size} disk image... "
qemu-img create ${basedir}/rootfs.img ${rootfs_size} >> ${basedir}/log 2>&1
log_status $?
echo -n "Creating ext4 filesystem... "
/sbin/mkfs.ext4 ${basedir}/rootfs.img >> ${basedir}/log 2>&1
log_status $?
if [ ! -e ${rootfs} ]; then
echo "Creating ${rootfs}..."
sudo mkdir ${rootfs}
fi
echo "Mounting ${basedir}/rootfs.img... "
sudo umount ${rootfs} >> ${basedir}/log 2>&1 || true
sudo mount -o loop ${basedir}/rootfs.img ${rootfs}
echo "Bootstrapping filesystem... "
sudo /sbin/debootstrap unstable ${rootfs} http://deb.debian.org/debian/ >> ${basedir}/log 2>&1
echo "Setting hostname: ${hostname}... "
sudo sh -c "echo '${hostname}' > ${rootfs}/etc/hostname"
echo "Setting the root password... "
sudo sh -c "chroot ${rootfs} passwd root"
if [ ! -z "${packages_to_install}" ]; then
echo "Running apt update... "
sudo sh -c "chroot ${rootfs} apt update" >> ${basedir}/log 2>&1
echo "Installing packages: ${packages_to_install}... "
sudo chroot ${rootfs} /bin/sh -c "export TMPDIR=/tmp; apt install -y ${packages_to_install}" >> ${basedir}/log 2>&1
fi
sudo cp ${confdir}/dhcp.network ${rootfs}/etc/systemd/network/
echo "Enabling systemd-networkd.service... "
sudo sh -c "chroot ${rootfs} systemctl enable systemd-networkd.service" >> ${basedir}/log 2>&1
echo "Enabling systemd-resolved.service... "
sudo sh -c "chroot ${rootfs} systemctl enable systemd-resolved.service" >> ${basedir}/log 2>&1
echo "Unmounting ${rootfs}... "
sudo umount ${rootfs}
echo "Deleting ${rootfs}... "
sudo rmdir ${rootfs}