-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxbps-chroot
executable file
·65 lines (45 loc) · 1.38 KB
/
xbps-chroot
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
#!/bin/bash
SELF_SRC="$(realpath ${BASH_SOURCE})"
SELF_DIR="$(dirname "${SELF_SRC}")"
function guard() {
local code="${1}"
local message="${2}"
if [ "${code}" -ne 0 ]; then
echo "${message} (${code})"
exit 1
fi
}
function purge() {
local rootfs="${1}"
local buildfs="${2}"
mkdir -p "${rootfs}" "${buildfs}"
umount -qR "${rootfs}"
find "${rootfs}" -mindepth 1 -delete
find "${buildfs}" -mindepth 1 -delete
}
function setup() {
local rootfs="${1}"
local buildfs="${2}"
mount -t overlay overlay -o lowerdir=/,upperdir="${buildfs}",workdir="${rootfs}" "${rootfs}"
guard $? 'could not mount overlayfs onto root/build directories'
mount -t proc /proc "${rootfs}/proc"
guard $? 'could not mount procfs into overlayfs'
mount -t sysfs /sys "${rootfs}/sys"
guard $? 'could not mount sysfs into overlayfs'
mount --rbind /dev "${rootfs}/dev"
guard $? 'could not bind mount /dev into overlayfs'
mount --rbind /run "${rootfs}/run"
guard $? 'could not bind mount /run into overlayfs'
mount --bind "${SELF_DIR}" "${rootfs}/${SELF_DIR}"
touch "${rootfs}/.chroot"
}
function main() {
local tmpdir='/tmp/xbps-src'
test ! -f '/.chroot'
guard $? 'already in a chroot'
purge "${tmpdir}"/{rootfs,buildfs}
setup "${tmpdir}"/{rootfs,buildfs}
chroot "${tmpdir}"/rootfs "${SELF_DIR}/xbps-src" "${@:1}"
purge "${tmpdir}"/{rootfs,buildfs}
}
main "${@}"