forked from sgielen/picl-k3os-image-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.resizefs
91 lines (79 loc) · 2.89 KB
/
init.resizefs
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
#!/bin/sh
set -e
IMAGE_TYPE="@IMAGE_TYPE@"
# sleep a second for devices to settle
sleep 1
mount -t proc none /proc
mount -t sysfs none /sys
# Unpack tools for resizing root FS
mount -t tmpfs -o size=50m none /tmp
tar -xJf /root-resize.tar.xz --strip 1 -C /tmp
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp/sbin:/tmp/bin:/tmp/usr/sbin:/tmp/usr/bin
export LD_LIBRARY_PATH=/tmp/lib:/tmp/lib/aarch64-linux-gnu:/tmp/usr/lib:/tmp/usr/lib/aarch64-linux-gnu
if [ "$IMAGE_TYPE" = "raspberrypi" ]; then
PARTUUID=$(cat /proc/cmdline | sed -r 's/^.*PARTUUID=([^ ]+).*$/\1/')
ROOTDEVICE=$(blkid | grep PARTUUID=\"$PARTUUID\" | awk -F: '{print $1}')
elif [ "$IMAGE_TYPE" = "orangepipc2" ]; then
ROOTDEVICE=$(cat /proc/cmdline | sed -r 's/.*root=([^ ]+) .*/\1/')
fi
ROOTDISK=/dev/$(basename $(dirname $(readlink /sys/class/block/$(basename $ROOTDEVICE))))
PARTNUM=$(cat /sys/class/block/$(basename $ROOTDEVICE)/partition)
echo "== Performing filesystem check on root device $ROOTDEVICE... =="
e2fsck -pv $ROOTDEVICE
sleep 5
echo "== Resizing root filesystem on $ROOTDISK... =="
parted -s $ROOTDISK resizepart $PARTNUM 100%
sleep 5
mount -o remount,rw /
resize2fs $ROOTDEVICE
sleep 5
echo "== Setting proper init... =="
if [ "$IMAGE_TYPE" = "raspberrypi" ]; then
mount ${ROOTDISK}p1 /boot
sed -i 's# init=/sbin/init.resizefs# init=/sbin/init.preinit#' /boot/cmdline.txt
NEW_PARTUUID=$(blkid -o export $ROOTDEVICE | grep PARTUUID)
sed -i "s# root=PARTUUID=$PARTUUID # root=$NEW_PARTUUID #" /boot/cmdline.txt
umount /boot
elif [ "$IMAGE_TYPE" = "orangepipc2" ]; then
sed -i 's# init=/sbin/init.resizefs# init=/sbin/init.preinit#' /boot/env.txt
fi
echo "== Linking correct config... =="
if [ "$IMAGE_TYPE" = "orangepipc2" ]; then
modprobe dwmac_sun8i
sleep 1
fi
# TODO: what if this device doesn't have eth0, but has other interfaces? Loop through them?
MY_MAC=$(cat /sys/class/net/eth0/address)
if [ -r "/k3os/system/config/${MY_MAC}.yaml" ]; then
ln -s /k3os/system/config/${MY_MAC}.yaml /k3os/system/config.yaml
else
# try again using - as MAC separator.
# Needed to support configs created on windows which does not allows : in paths
MY_MAC=$(cat $MY_MAC | sed 's/:/-/')
if [ -r "/k3os/system/config/${MY_MAC}.yaml" ]; then
ln -s /k3os/system/config/${MY_MAC}.yaml /k3os/system/config.yaml
else
# TODO: retrieve config from a master?
echo "Error: No configuration found for MAC ${MY_MAC}." >&2
echo "Ensure that config/${MY_MAC}.yaml exists." >&2
exit 1
fi
fi
echo "== Cleaning up and rebooting... =="
rm /root-resize.tar.xz
rm /sbin/init.resizefs
# exec into another script so that /sbin/init.resizefs (this script) can be deleted,
# so the filesystem can be mounted read-only
cat <<EOF >/tmp/reboot
#!/bin/sh
set -e
sync
sleep 5
mount -o remount,ro / || echo u > /proc/sysrq-trigger || true
sleep 5
e2fsck -fyv $ROOTDEVICE || true
sleep 5
reboot -f
EOF
chmod +x /tmp/reboot
exec /tmp/reboot