-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChrootHelper
executable file
·232 lines (184 loc) · 5.95 KB
/
ChrootHelper
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
#!/usr/bin/env bash
. "${BASH_SOURCE[0]%/*}/function.sh" "" || exit
run() { init && args "$@" && ${command}Command "${args[@]}"; }
init()
{
config="/etc/schroot"
os="/var/chroot"
osBak="/var/chroot/bak"
debianMirror="http://deb.debian.org/debian"
ubuntuMirror="http://archive.ubuntu.com/ubuntu/"
defaultPackages="sudo"
copyOptions=( --archive -–one-file-system --info=progress2 )
}
usage()
{
echot "\
usage: ChrootHelper delete|dir|exists|install|installed|list|run NAME
backup|restore DIST backup or restore a distribution
config [DIST](default) edit distribution configuration
install DIST [SUITE](dist) install a new distribution
The SUITE may be a release code name (eg, sid, stretch, jessie) or a symbolic name (eg, unstable,
testing, stable, oldstable)
-d, --debian use the Debian mirror
-u, --ubuntu use the Ubuntu mirror
run NAME
-a|--arch ARCH use the specified architecture for the distribution"
exit $1
}
args()
{
arch="" command="" noPrompt="" args=()
while [ "$1" != "" ]; do
case "$1" in
-a|--arch) arch="$2"; shift 2; [[ ! $arch ]] && MissingOperand "arch"; continue;;
-h|--help) usage 0;;
config|c) command="config";;
install|i) command="install";;
run|r) command="run";;
*)
[[ ! $command ]] && IsFunction "${1,,}Command" && { command="${1,,}"; shift; continue; }
args+=("$1")
esac
shift
done
[[ ! $command ]] && command="run"
[[ $arch && "$arch" != "armhf" ]] && { EchoErr "ChrootHelper: only ARM emulation is supported"; return 1; }
return 0
}
distArg() { dist="$1"; [[ ! $dist ]] && MissingOperand "dist"; ! distExists "$dist" && { EchoErr "Distribution $dist does not exist"; exit 1; }; }
distDirValidate() { local distDir; distDir="$(distDir "$1")" || return; [[ -d "$distDir" ]] && return; EchoErr "$dist directory \"$distDir\" does not exist"; return 1; }
distExists() { ! InPath schroot && return 0; schroot --list | grep '^chroot:'$1'$' >& /dev/null; }
distDir()
{
! distExists "$1" && return 1
! InPath schroot && { EchoErr "ChrootHelper: unable to local the $dist distribution"; return 1; }
schroot -c "$1" --config | grep '^directory=' | cut -d"=" -f2
}
#
# Commands
#
dirCommand() { distDir "$1"; }
existsCommand() { distExists "$1"; }
installedCommand() { InPath schroot; }
listCommand() { schroot --list --all "$@"; }
backupCommand()
{
local dist; distArg "$1"; shift
initDirs || returns
distDirValidate "$dist" || return
if [[ -d "$osBak/$dist" ]]; then
ask -dr n "Do you want to delete the $dist backup" || return
printf "Removing $dist backup..."
sudo rm -fr "$osBak/$dist" || return
echo "done"
fi
sudo rsync "${copyOptions[@]}" "$(distDir "$dist")/" "$osBak/$dist"
}
configCommand()
{
local dist="default"
[[ $1 ]] && { distArg "$1"; shift; }
[[ $# != 0 ]] && usage
SetTextEditor || return
sudoedit "$config/chroot.d/$dist"
}
deleteCommand()
{
local dist="$1" dir; dir="$(distDir "$dist")" || return
[[ ! -d "$dir" ]] && return
ask -dr n "Do you want to delete the $dist distribution" || return
printf "Removing $dist..."
sudo rm -fr $dir || return
echo "done"
}
fixCommand()
{
local dist; distArg "$1"; shift
sudo rm "$(distDir "$dist")/etc/resolv.conf" || return
}
installCommand()
{
local dist suite mirror
initDirs || return
while (( $# != 0 )); do
case "$1" in "") : ;;
-d|--debian) mirror="$debianMirror";;
-u|--ubuntu) mirror="$ubuntuMirror";;
*)
! IsOption "$1" && [[ ! $dist ]] && { dist="$1"; shift; continue; }
! IsOption "$1" && [[ ! $suite ]] && { suite="$1"; shift; continue; }
UnknownOption "$1"; return
esac
shift
done
[[ ! $dist ]] && { MissingOperand "dist"; return; }
[[ ! $suite ]] && suite="$dist"
# find the mirror if not specified
if [[ ! $mirror ]]; then
if UrlExists "$debianMirror/dists/$suite"; then mirror="$debianMirror"
elif UrlExists "$ubuntuMirror/dists/$suite"; then mirror="$ubuntuMirror"
else EchoErr "Could not find suite $suite"; return 1
fi
fi
# configuration
if ! distExists "$dist"; then
echo "[$dist]
description=$dist ($mirror)
directory=$os/$dist
type=directory
users=$USER
root-users=$USER
root-groups=root" | sudo tee "$config/chroot.d/$dist" || return
fi
deleteCommand "$dist" || return
sudo debootstrap --include=$defaultPackages "$suite" "$(distDir "$dist")" || return
bootstrap-remote "$dist" || return
}
restoreCommand()
{
local dist; distArg "$1"; shift
initDirs || return
[[ ! -d "$osBak/$dist" ]] && { EchoErr "$dist backup does not exist"; return 1; }
deleteCommand "$dist" || return
sudo rsync "${copyOptions[@]}" "$osBak/$dist/" "$(distDir "$dist")/"
}
emulate()
{
local dist; distArg "$1"; shift
local distDir result; distDir="$(distDir "$dist")" || return
! InPath qemu-arm-static && { EchoErr "qemu-arm-static command not found. Try: sudo apt-get install binfmt-support qemu-user-static"; exit 1; }
[[ -f "$distDir/etc/ld.so.preload" ]] && { sudo mv "$distDir/etc/ld.so.preload" "$distDir/etc/ld.so.preload.disabled" || return; }
sudo install -m 0755 "/usr/bin/qemu-arm-static" "$distDir/usr/bin/qemu-arm-static" || return
schroot -c "$dist" "$@"; result=$?
[[ -f "$distDir/etc/ld.so.preload.disabled" ]] && { sudo mv "$distDir/etc/ld.so.preload.disabled" "$distDir/etc/ld.so.preload" || return; }
return $result
}
runCommand()
{
local dist; distArg "$1"; shift
if [[ "$arch" != "$(dpkg --print-architecture)" ]]; then
emulate "$dist" "$@"
elif InPath schroot; then
schroot -c "$dist" "$@"
elif IsPlatform mac; then
sudo HOME=/home/jjbutare chroot -u $USER "$dist" zsh --login "$@"
elif InPath /opt/bin/chroot; then
sudo HOME=/home/$USER /opt/bin/chroot --userspec=$USER . zsh --login "$@"
elif InPath chroot; then
sudo HOME=/home/$USER chroot --userspec=$USER . zsh --login "$@"
else
EchoErr "ChrootHelper: no chroot program found"
return 1
fi
}
#
# helper
#
initDirs()
{
[[ ! -d "$os" ]] && { sudo ${G}mkdir --parents "$os" || return; }
[[ ! -d "$osBak" ]] && { sudo ${G}mkdir --parents "$osBak" || return; }
return 0
}
run "$@"