This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake-root-chroot.sh
executable file
·223 lines (205 loc) · 5.11 KB
/
fake-root-chroot.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
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
#!/bin/bash
if [[ "${UID}" != 0 ]] ; then
unshare -rm "${BASH_SOURCE[0]}" "${@}"
exit $?
fi
usage() {
cat <<EOF
${ANSI_BRIGHT}${ANSI_REVERSE}Usage:${ANSI_RESET}
${ANSI_BRIGHT}${BASH_SOURCE[0]}${ANSI_RESET} ${ANSI_BRIGHT}${ANSI_FG_CYAN}[<options>]${ANSI_RESET}
${ANSI_BRIGHT}${ANSI_REVERSE}Options:${ANSI_RESET}
${ANSI_BRIGHT}${ANSI_FG_CYAN}-n ${ANSI_RESET}Dry run, only print what would be done (non-verbose by default).
${ANSI_BRIGHT}${ANSI_FG_CYAN}-v ${ANSI_RESET}Verbose, print all commands that are/would be invoked.
${ANSI_BRIGHT}${ANSI_FG_CYAN}-r DIR ${ANSI_RESET}Use DIR as a fake root. Must be empty.
It is created if it doesn't exist.
${ANSI_BRIGHT}${ANSI_FG_CYAN}-w DIR ${ANSI_RESET}Add DIR to the lsit of directories which need to be writable to the
fake root.
EOF
exit 0
}
function log() {
_log "" "${@}"
}
function logn() {
_log "-n" "${@}"
}
function _log() {
local echoargs="${1}" level="${2}" colors ansi_col
shift 2
colors_v="ANSI_${level}"
read -a colors <<<"${!colors_v}"
for c in "${colors[@]}"; do
col="ANSI_${c}"
ansi_col+="${!col}"
done
echo ${echoargs} "${ansi_col}${*}${ANSI_RESET}"
}
bind_mount() {
for src in "${@}" ; do
dest="${CHROOT}${src}"
is_writable=0
for wrt in "${WRITABLES[@]}" ; do
if [[ "${wrt}" == "${src}" || -d "${src}" && "${wrt#${src}/}" != "${wrt}" ]] ; then
is_writable=1
break
fi
done
if [[ "${is_writable}" -eq 1 ]] ; then
if [[ -d "${src}" ]] ; then
log INFO "${src} contains locations which should be writable"
cmd mkdir "${CHROOT}${src}"
bind_mount "${src}"/* || return 1
else
log INFO "${src} should be writable, copying"
cmd cp "${src}" "${dest}" || return 1
fi
continue
fi
if [[ -d "${src}" ]] ; then
cmd mkdir -p "${dest}" || return 1
cmd chmod --reference="${src}" "${dest}" || return 1
MOUNTED+=("${dest}")
cmd mount --rbind "${src}" "${dest}" || return 1
else
cmd ln "${src}" "${dest}" || return 1
fi
done
}
cmd() {
if [[ "${VERBOSE}" -eq 1 ]] ; then
log DEBUG "${@}"
fi
if [[ "${DRY_RUN}" -eq 0 ]] ; then
"${@}"
else
true
fi
}
############################################################
ANSI_RESET=$'\x1b[0m'
ANSI_BRIGHT=$'\x1b[1m'
ANSI_DIM=$'\x1b[2m'
ANSI_UNDERSCORE=$'\x1b[4m'
ANSI_BLINK=$'\x1b[5m'
ANSI_REVERSE=$'\x1b[7m'
ANSI_HIDDEN=$'\x1b[8m'
ANSI_FG_BLACK=$'\x1b[30m'
ANSI_FG_RED=$'\x1b[31m'
ANSI_FG_GREEN=$'\x1b[32m'
ANSI_FG_YELLOW=$'\x1b[33m'
ANSI_FG_BLUE=$'\x1b[34m'
ANSI_FG_MAGENTA=$'\x1b[35m'
ANSI_FG_CYAN=$'\x1b[36m'
ANSI_FG_WHITE=$'\x1b[37m'
ANSI_BG_BLACK=$'\x1b[40m'
ANSI_BG_RED=$'\x1b[41m'
ANSI_BG_GREEN=$'\x1b[42m'
ANSI_BG_YELLOW=$'\x1b[43m'
ANSI_BG_BLUE=$'\x1b[44m'
ANSI_BG_MAGENTA=$'\x1b[45m'
ANSI_BG_CYAN=$'\x1b[46m'
ANSI_BG_WHITE=$'\x1b[47m'
ANSI_DEBUG="FG_BLUE"
ANSI_INFO="BRIGHT FG_WHITE"
ANSI_ERROR="BRIGHT FG_WHITE BG_RED"
VERBOSE=0
DRY_RUN=0
CHROOT=
WRITABLES=()
############################################################
while [[ $# -gt 0 ]] ; do
case "${1}" in
-n)
DRY_RUN=1
;;
-v)
VERBOSE=1
;;
-h)
usage
;;
-r)
CHROOT="${2%/}"
shift
;;
-w)
while [[ $# -gt 1 ]] ; do
case "${2}" in
-*)
break
;;
*)
if [[ -d "${2}" ]] ; then
# ensure trailing /
WRITABLES+=("${2%/}/")
else
WRITABLES+=("${2}")
fi
;;
esac
shift
done
;;
-*)
log ERROR "Unknown option ${1}"
usage
;;
*)
log ERROR "Unknown argument ${1}"
usage
;;
esac
shift
done
############################################################
if [[ -z "${CHROOT}" ]] ; then
logn INFO "Assuming that current working directory is the chroot, is this correct? (y/n) "
read -n 1 ans
echo ""
while [[ ! "${ans}" =~ [yYnN] ]] ; do
logn INFO "Answer with y or n "
read -n 1 ans
echo ""
done
if [[ "${ans}" =~ [nN] ]] ; then
exit 1
fi
CHROOT=.
fi
if test -n "$(shopt -s nullglob; echo "${CHROOT}"/*)" ; then
log ERROR "chroot directory must be empty, aborting." >&2
exit 1
fi
log DEBUG "Writables:" "${WRITABLES[@]}"
shopt -s dotglob
MOUNTED=()
cmd mkdir -p "${CHROOT}"
bind_mount /*
rc=$?
cmd chroot "${CHROOT}"
log INFO "Cleaning up"
for m in "${MOUNTED[@]}" ; do
cmd umount --lazy "${m}"
this_rc=$?
if [[ "${rc}" -eq 0 ]] ; then
rc="${this_rc}"
fi
done
if [[ "${DRY_RUN}" -eq 0 ]] ; then
find "${CHROOT}" ! -type d |
while IFS= read fake_path ; do
real_path="${fake_path#${CHROOT}}"
if [[ -e "${real_path}" ]] ; then
fake_inode="$(stat --format '%i' "${fake_path}")"
real_inode="$(stat --format '%i' "${real_path}")"
if [[ "${fake_inode}" == "${real_inode}" ]] ; then
cmd rm "${fake_path}"
else
log INFO "${fake_path} differs from that on the real root, keeping"
fi
else
log INFO "${fake_path} does not exist on the real root, keeping"
fi
done
fi
cmd find "${CHROOT}" -empty -delete