-
Notifications
You must be signed in to change notification settings - Fork 2
/
chimera-bootstrap
executable file
·252 lines (210 loc) · 6.37 KB
/
chimera-bootstrap
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
#!/bin/sh
#
# Chimera Linux bootstrap script
#
# Copyright 2023-2024 q66 <[email protected]>
#
# License: BSD-2-Clause
#
readonly PROGNAME=$(basename "$0")
MOUNTED_PSEUDO=
ROOT_DIR=
EREPOSF=
REPOSF=
do_trymount() {
if mountpoint -q "${ROOT_DIR}/$1" > /dev/null 2>&1; then
return 0
fi
mount --rbind "/$1" "${ROOT_DIR}/$1" || die "Failed to mount ${1}fs"
MOUNTED_PSEUDO="${MOUNTED_PSEUDO} $1"
}
mount_pseudo() {
do_trymount dev
do_trymount proc
do_trymount sys
do_trymount tmp
}
umount_pseudo() {
sync
for mnt in ${MOUNTED_PSEUDO}; do
[ -n "$mnt" ] || continue
umount -R -f "${ROOT_DIR}/$mnt" > /dev/null 2>&1
done
}
msg() {
printf "\033[1m$@\n\033[m"
}
error_sig() {
umount_pseudo
[ -n "$REPOSF" ] && rm -f "$REPOSF"
[ -n "$EREPOSF" ] && rm -f "$EREPOSF"
exit ${1:=0}
}
trap 'error_sig $? $LINENO' INT TERM 0
die() {
echo "ERROR: $@"
error_sig 1 $LINENO
}
usage() {
cat << EOF
Usage: $PROGNAME [opts] root [packages]...
By default, a network installation is performed. If no packages are given,
the 'base-full' package is used. If passing a list of packages, a base
package should typically be given for network installations.
For local installations, a source root is used and the packages optionally
passed on command line are installed on top. By default, it is assumed
that local installations are performed in live sessions and that there
is a mounted squashfs in the default location. This may be overridden.
In any case, the root directory must exist and be writable.
Options:
-l Perform a local installation.
-L PATH Override the local installation source root.
-a PATH Use a different apk binary.
-i Run apk in interactive mode.
-I Ignore system repositories.
-r REPO Specify additional package repository.
-k DIR Override apk keys directory.
-f Force installation even with non-empty target root.
-u Allow untrusted packages.
-h Print this message.
EOF
exit ${1:=1}
}
INSTALL_LOCAL=0
INSTALL_LOCAL_PATH=
INSTALL_APK="apk"
INSTALL_FORCE=0
INSTALL_IGNORE_REPOS=0
INSTALL_KEYS_DIR="/etc/apk/keys"
INSTALL_APK_ARGS="--no-interactive"
# ensure we run as root
if [ "$(id -u)" != "0" ]; then
die "Must run this as root."
fi
while getopts "lL:a:iIr:k:fuh" opt; do
case "$opt" in
l) INSTALL_LOCAL=1 ;;
L) INSTALL_LOCAL_PATH="$OPTARG" ;;
a) INSTALL_APK="$OPTARG" ;;
i) INSTALL_APK_ARGS="$INSTALL_APK_ARGS --interactive" ;;
I) INSTALL_IGNORE_REPOS=1 ;;
r)
if [ -z "$EREPOSF" ]; then
EREPOSF=$(mktemp)
[ $? -eq 0 ] || die "failed to set up extra repositories file"
fi
echo "$OPTARG" >> "$EREPOSF"
;;
k) INSTALL_KEYS_DIR="$OPTARG" ;;
f) INSTALL_FORCE=1 ;;
u) INSTALL_APK_ARGS="$INSTALL_APK_ARGS --allow-untrusted" ;;
h) usage 0 ;;
*) usage 1 ;;
esac
done
if [ -n "$INSTALL_KEYS_DIR" -a ! -d "$INSTALL_KEYS_DIR" ]; then
die "keys directory does not exist"
fi
shift $((OPTIND - 1))
if ! command -v mountpoint > /dev/null 2>&1; then
die "mountpoint must be present"
fi
if ! command -v "$INSTALL_APK" > /dev/null 2>&1; then
die "apk must be present"
fi
ROOT_DIR="$1"
shift
if [ $# -eq 0 -a "$INSTALL_LOCAL" -eq 0 ]; then
# set a default for network installations
set -- "base-full"
fi
# determine the default local install path
if [ "$INSTALL_LOCAL" -eq 1 -a -z "$INSTALL_LOCAL_PATH" ]; then
for mdir in /run/live/rootfs/filesystem.*; do
if [ -d "$mdir" ]; then
INSTALL_LOCAL_PATH="$mdir"
break
fi
done
fi
if [ "$INSTALL_LOCAL" -eq 1 -a ! -d "$INSTALL_LOCAL_PATH" ]; then
die "local install but no source root to install from"
fi
# ensure the target exists
[ -d "$ROOT_DIR" ] || die "root directory does not exist"
# ensure the target is writable
if ! touch "${ROOT_DIR}/.write-test" > /dev/null 2>&1; then
die "root directory is not writable"
else
rm -f "${ROOT_DIR}/.write-test"
fi
# ensure it's empty (there should be no output at all from the find)
#
# we might want to handle lost+found specially but then installs are
# expected to be done onto a clean filesystem, and having non-empty
# lost+found indicates that the filesystem is probably not clean
#
# directories are okay because it is expected that e.g. if somebody
# has a separate /boot, they will want to pre-mount it before running
# the chimera-bootstrap command
#
if [ "$INSTALL_FORCE" -eq 0 ]; then
for x in $(find "${ROOT_DIR}" '!' -type d -print -quit); do
die "root directory is non-empty"
done
fi
make_reposf() {
[ -n "$REPOSF" ] && return 0
REPOSF=$(mktemp)
[ $? -eq 0 ] || die "failed to generate a repositories file"
if [ "$INSTALL_IGNORE_REPOS" -eq 1 ]; then
if [ -n "$EREPOSF" ]; then
cat "$EREPOSF" > "$REPOSF"
fi
return 0
fi
if [ -f /etc/apk/repositories ]; then
cat /etc/apk/repositories >> "$REPOSF"
fi
for f in /etc/apk/repositories.d/*; do
[ -f "$f" ] || continue
cat "$f" >> "$REPOSF"
done
if [ -n "$EREPOSF" ]; then
cat "$EREPOSF" >> "$REPOSF"
fi
}
if [ "$INSTALL_LOCAL" -eq 1 ]; then
msg "Copying system to ${ROOT_DIR}..."
# copy over the source system as closely as possible
tar -cf - -C "$INSTALL_LOCAL_PATH" . | tar -xpf - -C "$ROOT_DIR"
else
make_reposf
msg "Installing minimal system at ${ROOT_DIR}..."
# install chimerautils
"$INSTALL_APK" --root "$ROOT_DIR" --keys-dir "$INSTALL_KEYS_DIR" \
--repositories-file "$REPOSF" $INSTALL_APK_ARGS \
--initdb add chimerautils
fi
if [ $? -ne 0 ]; then
die "initial installation failed"
fi
if [ $# -gt 0 ]; then
make_reposf
# make it safe to install other things
mount_pseudo
msg "Installing additional packages..."
# install the other desired packages
"$INSTALL_APK" --root "$ROOT_DIR" --keys-dir "$INSTALL_KEYS_DIR" \
--repositories-file "$REPOSF" $INSTALL_APK_ARGS add "$@"
if [ $? -ne 0 ]; then
die "package installation failed"
fi
umount_pseudo
rm -f "$REPOSF" "$EREPOSF"
unset REPOSF EREPOSF
fi
msg "Chimera bootstrap successful at ${ROOT_DIR}."
echo "You can use chimera-chroot to get a shell in the system."
echo "Please perform all post-installation steps now (bootloader etc.)."
exit 0