-
Notifications
You must be signed in to change notification settings - Fork 12
/
unrootfs.sh
executable file
·117 lines (94 loc) · 2.43 KB
/
unrootfs.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
#!/bin/sh
#
# Chimera Linux device rootfs extraction tool
#
# This script installs a Chimera system from a device tarball into
# a mounted filesystem, set up e.g. via the mkpart.sh script.
#
# Copyright 2023 q66 <[email protected]>
#
# License: BSD-2-Clause
#
readonly PROGNAME=$(basename "$0")
msg() {
printf "\033[1m$@\n\033[m"
}
die() {
msg "ERROR: $@"
exit 1
}
if [ "$(id -u)" != "0" ]; then
die "must be run as root"
fi
usage() {
cat <<EOF
Usage: $PROGNAME tarball mountpoint device
The tarball is the Chimera device rootfs tarball. The mountpoint
is where to unpack it. The device is where to install the bootloader,
assuming one is needed; if not given, no bootloader will be installed,
if given, it needs to be the whole block device (not a partition).
Options:
-h Print this message.
EOF
exit ${1:=1}
}
IN_FILES="$1"
shift
ROOT_DIR="$1"
shift
BL_DEV="$1"
shift
if [ -z "$IN_FILES" ]; then
die "input file(s) not given"
fi
OLD_IFS=$IFS
IFS=;
for tfile in $IN_FILES; do
if [ ! -r "$tfile" ]; then
die "could not read input file: $tfile"
fi
done
IFS=$OLD_IFS
if ! mountpoint -q "$ROOT_DIR"; then
die "$ROOT_DIR is not a mount point"
fi
if [ -n "$BL_DEV" -a ! -b "$BL_DEV" ]; then
die "$BL_DEV given but not a block device"
fi
BOOT_UUID=$(findmnt -no uuid "${ROOT_DIR}/boot")
ROOT_UUID=$(findmnt -no uuid "${ROOT_DIR}")
BOOT_FSTYPE=$(findmnt -no fstype "${ROOT_DIR}/boot")
ROOT_FSTYPE=$(findmnt -no fstype "${ROOT_DIR}")
msg "Unpacking rootfs tarball..."
_tarargs=
if [ -n "$(tar --version | grep GNU)" ]; then
_tarargs="--xattrs-include='*'"
fi
OLD_IFS=$IFS
IFS=;
for tfile in $IN_FILES; do
tar -pxf "$tfile" --xattrs $_tarargs -C "$ROOT_DIR" ||\
die "could not extract input file: $file"
done
IFS=$OLD_IFS
# use fsck for all file systems other than f2fs
case "$ROOT_FSTYPE" in
f2fs) _fpassn="0";;
*) _fpassn="1";;
esac
# generate fstab
FSTAB=$(mktemp)
TMPL=$(tail -n1 "${ROOT_DIR}/etc/fstab")
# delete tmpfs line
echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 ${_fpassn}" > "$FSTAB"
if [ -n "$BOOT_UUID" ]; then
echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults 0 2" >> "$FSTAB"
fi
# overwrite old
cat "$FSTAB" > "${ROOT_DIR}/etc/fstab"
rm -f "$FSTAB"
msg "Setting up bootloader..."
if [ -n "$BL_DEV" -a -r "${ROOT_DIR}/etc/default/u-boot-device" ]; then
"${ROOT_DIR}/usr/bin/install-u-boot" "${BL_DEV}" "${ROOT_DIR}"
fi
msg "Successfully installed Chimera."