This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arch-chroot
executable file
·159 lines (131 loc) · 3.59 KB
/
arch-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
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
#!/bin/bash
shopt -s extglob
out() { printf "$1 $2\n" "${@:3}"; }
error() { out "==> ERROR:" "$@"; } >&2
msg() { out "==>" "$@"; }
msg2() { out " ->" "$@";}
die() { error "$@"; exit 1; }
in_array() {
local i
for i in "${@:2}"; do
[[ $1 = "$i" ]] && return
done
}
api_fs_mount() {
if ! mountpoint -q "$1"; then
mount -B "$1" "$1" && ROOT_IS_BIND=1
fi &&
mount -t proc proc "$1/proc" -o nosuid,noexec,nodev &&
mount -t sysfs sys "$1/sys" -o nosuid,noexec,nodev &&
mount -t devtmpfs udev "$1/dev" -o mode=0755,nosuid &&
mount -t devpts devpts "$1/dev/pts" -o mode=0620,gid=5,nosuid,noexec &&
mount -t tmpfs shm "$1/dev/shm" -o mode=1777,nosuid,nodev &&
mount -t tmpfs run "$1/run" -o nosuid,nodev,mode=0755 &&
mount -t tmpfs tmp "$1/tmp" -o mode=1777,strictatime,nodev,nosuid,size=50M
}
api_fs_umount() {
umount \
"$1/tmp" \
"$1/run" \
"$1/dev/shm" \
"$1/dev/pts" \
"$1/dev" \
"$1/sys" \
"$1/proc"
(( ROOT_IS_BIND )) && umount "$1"
}
valid_number_of_base() {
local base=$1 len=${#2} i=
for (( i = 0; i < len; i++ )); do
(( (${2:i:1} & ~(base - 1)) == 0 )) || return
done
}
mangle() {
local i= chr= out=
unset {a..f} {A..F}
for (( i = 0; i < ${#1}; i++ )); do
chr=${1:i:1}
case $chr in
[[:space:]\\])
printf -v chr '%03o' "'$chr"
out+=\\
;;&
# fallthrough
*)
out+=$chr
;;
esac
done
printf '%s' "$out"
}
unmangle() {
local i= chr= out= len=$(( ${#1} - 4 ))
unset {a..f} {A..F}
for (( i = 0; i < len; i++ )); do
chr=${1:i:1}
case $chr in
\\)
if valid_number_of_base 8 "${1:i+1:3}" ||
valid_number_of_base 16 "${1:i+1:3}"; then
printf -v chr '%b' "${1:i:4}"
(( i += 3 ))
fi
;;&
# fallthrough
*)
out+=$chr
esac
done
printf '%s' "$out${1:i}"
}
fstype_is_pseudofs() {
# list taken from util-linux source: libmount/src/utils.c
local pseudofs_types=('anon_inodefs'
'autofs'
'bdev'
'binfmt_misc'
'cgroup'
'configfs'
'cpuset'
'debugfs'
'devfs'
'devpts'
'devtmpfs'
'dlmfs'
'fuse.gvfs-fuse-daemon'
'fusectl'
'hugetlbfs'
'mqueue'
'nfsd'
'none'
'pipefs'
'proc'
'pstore'
'ramfs'
'rootfs'
'rpc_pipefs'
'securityfs'
'sockfs'
'spufs'
'sysfs'
'tmpfs')
in_array "$1" "${pseudofs_types[@]}"
}
usage() {
cat <<EOF
usage: ${0##*/} chroot-dir
EOF
}
if [[ -z $1 || $1 = @(-h|--help) ]]; then
usage
exit $(( $# ? 0 : 1 ))
fi
(( EUID == 0 )) || die 'This script must be run with root privileges'
chrootdir=$1
[[ -d $chrootdir ]] || die "Can't create chroot on non-directory %s" "$1"
trap '{ api_fs_umount "$chrootdir"; umount "$chrootdir/etc/resolv.conf"; } 2>/dev/null' EXIT
api_fs_mount "$chrootdir" || die "failed to setup API filesystems in chroot %s" "$1"
mount -B /etc/resolv.conf "$chrootdir/etc/resolv.conf"
#SHELL=/bin/sh chroot "$@"
chroot /mnt /bin/sh -c 'HDD=$HDD source ./chroot.sh'
umount "$chrootdir/etc/resolv.conf"