-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchralpine.sh
More file actions
executable file
·403 lines (368 loc) · 9.65 KB
/
chralpine.sh
File metadata and controls
executable file
·403 lines (368 loc) · 9.65 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
#
# setup a simple alpine chroot based on their mini root filesystem archives
#
# if you have docker, use that instead
#
# alpine chroot stuff:
# https://wiki.alpinelinux.org/wiki/Installing_Alpine_Linux_in_a_chroot
# https://github.com/alpinelinux/alpine-chroot-install
# options
# -n name
# -v version
# -i inst (top) dir
# -p port
# maybe
# -u user to create
# -a arch (needs qemu-$arch, not yet?)
# -d download dir
# commands
# setup
# download
# checksum
# extract
# chmod 755 / /home
# chroot apk update
# chroot apk upgrade
# chroot apk add stuff
# start
# make bind mount directories
# do bind mounts
# start ssh?
# stop
# kill ssh?
# unmount bind mounts
# todo
# user creation?
# ssh key exchange?
# check for 'nodev' on target dir and remount,dev it
# cgroups bind mounts?
# lib/modules bind mount?
# we'll use this a few times
progname="chralpine.sh"
if [[ ${BASH_SOURCE[0]} =~ /dev/fd/ ]] ; then
scriptname="${progname}"
else
scriptname="$(basename "${BASH_SOURCE[0]}")"
fi
function scriptecho() {
echo "${scriptname}: ${@}"
}
# default versions/names/arch
chrname="alpine"
alpver="3.14.3"
alparch="$(uname -m | sed 's/^\(arm\).*/\1hf/g;s/^i.86$/x86/g')"
sshport="22222"
# where to download and install
dldir="/tmp"
instdir="/usr/local"
# system/bind mount stuff
erc="etc/resolv.conf"
bmds=( dev dev/mapper dev/pts dev/shm lib/modules proc sys )
bmfs=( ${erc} )
# help/usage options
declare -A opt_help
opt_help["n"]="name : chroot name, default '${chrname}'"
opt_help["v"]="X.Y.Z : alpine version, default '${alpver}'"
opt_help["i"]="/path : top dir for chroot, default '${instdir}'"
opt_help["p"]="##### : dropbear ssh port for the chroot, default '${sshport}'"
# and commands
declare -A cmd_help
cmd_help["setup"]="download, extract and configure a new chroot"
cmd_help["chpass"]="change chroot root user password"
cmd_help["start"]="setup chroot bind mounts"
cmd_help["stop"]="unmount chroot bind mounts"
cmd_help["startssh"]="start dropbear ssh in the chroot"
cmd_help["stopssh"]="stop dropbear ssh in the chroot"
cmd_help["startdocker"]="start docker in the chroot"
cmd_help["stopdocker"]="stop docker in the chroot"
cmd_help["status"]="dump bind mount status for the chroot"
cmd_help["update"]="upgrade packages in existing chroot via apk"
# get that help
function usage() {
echo "usage:"
echo
echo " ${scriptname} <opt> <opt> command"
echo
echo " this script will download, configure, start and stop alpine chroots"
echo " by default an alpine ${alpver} chroot will be installed at ${instdir}/${chrname}"
echo
echo "options:"
for opt in $(echo ${!opt_help[@]} | tr ' ' '\n' | sort) ; do
echo " -${opt} ${opt_help[${opt}]}"
done
echo
echo "commands:"
for cmd in $(echo ${!cmd_help[@]} | tr ' ' '\n' | sort) ; do
echo " ${cmd} : ${cmd_help[${cmd}]}"
done
echo
}
# we need at least one command
if [ ${#} -le 0 ] ; then
scriptecho "please provide exactly one command"
echo
usage
exit 1
fi
# want to be able to run help without root
if [[ ${@} =~ -h ]] ; then
usage
exit 1
fi
# check for root
whoami | grep -qi '^root$' || {
scriptecho "please run this as the root user"
exit 1
}
# parse options
optcount=0
while getopts ":n:v:i:p:h" opt ; do
case ${opt} in
n)
chrname="${OPTARG}"
((optcount++))
;;
v)
alpver="${OPTARG}"
((optcount++))
;;
i)
instdir="${OPTARG}"
((optcount++))
;;
p)
sshport="${OPTARG}"
((optcount++))
;;
h)
usage
exit
;;
:)
scriptecho "-${OPTARG} requires an argument"
exit 1
;;
\?)
scriptecho "invalid option -${OPTARG}"
exit 1
;;
esac
done
# we should only have one more thing left - our command
# XXX - would something like "shift $(($OPTIND - 1))" work here?
shift $((${optcount}*2))
if [ ${#} -ne 1 ] ; then
scriptecho "please provide exactly one commnad"
echo
usage
exit 1
fi
# our script command is the "last" thing on the stack
command="${1}"
# split versions apart
vermaj="${alpver%%.*}"
vermin="${alpver#*.}"
vermin="${vermin%%.*}"
# form dl url/file and sha256 sum file
alpminirootarurl="http://dl-cdn.alpinelinux.org/alpine/v${vermaj}.${vermin}/releases/${alparch}/alpine-minirootfs-${alpver}-${alparch}.tar.gz"
alpminirootarsha256="${alpminirootarurl}.sha256"
alpminirootar="$(basename ${alpminirootarurl})"
# we'll use these multiple times
dlfile="${dldir}/${alpminirootar}"
chrdir="${instdir}/${chrname}"
# download/checksum functions
function download() {
scriptecho "downloading ${alpminirootarurl} to ${dlfile}"
curl -k -L -o ${dlfile} ${alpminirootarurl}
test -e "${dlfile}" || {
scriptecho "download of ${dlfile} failed"
exit 1
}
}
function checksum() {
scriptecho "checksumming ${dlfile}"
local storedsum="$(curl -kLs ${alpminirootarsha256} | awk '{print $1}')"
local localsum="$(sha256sum ${dlfile} | awk '{print $1}')"
if [ ! ${storedsum} == ${localsum} ] ; then
scriptecho "${dlfile} failed sha256sum"
exit
fi
}
# check if chroot dir exists
function chrdircheck() {
test -e "${chrdir}/etc/alpine-release" || {
scriptecho "${chrdir} chroot does not seem to exist"
return 1
}
return 0
}
# setup
function chrsetup() {
test -e "${chrdir}/etc/alpine-release" && {
scriptecho "${chrdir}/etc/alpine-release already exists; cowardly failing"
exit 1
}
download
checksum
mkdir -p "${chrdir}"
test -e "${chrdir}" || {
scriptecho "${chrdir} chroot does not seem to exist"
exit 1
}
scriptecho "extracting ${dlfile} into ${chrdir}"
tar --directory "${chrdir}" -zxf "${dlfile}"
scriptecho "fixing up ${chrdir} and ${chrdir}/home perms"
chmod 755 ${chrdir} ${chrdir}/home
scriptecho "creating bind mount directories: ${bmds[@]}"
for bmd in ${bmds[@]} ; do
mkdir -p ${chrdir}/${bmd}
done
scriptecho "creating bind mount files: ${bmfs[@]}"
for bmf in ${bmfs[@]} ; do
touch ${chrdir}/${bmf}
done
echo
scriptecho "run '${scriptname} -n ${chrname} start' to bind mount directories"
scriptecho "you can enter the chroot with 'sudo chroot ${chrdir} /bin/ash -l'"
scriptecho "run '${scriptname} -n ${chrname} stop' to unmount bound directories"
echo
}
# change password
function chrchpass() {
chrdircheck || exit 1
chroot "${chrdir}" /usr/bin/passwd root
}
# start
function chrstart() {
chrdircheck || exit 1
for bm in ${bmds[@]} ${bmfs[@]} ; do
mount | grep -q " ${chrdir}/${bm} " && {
scriptecho "${chrdir}/${bm} already bind mounted"
continue
}
if [ -e /${bm} ] ; then
scriptecho "bind mounting ${bm}"
mount -o bind /${bm} ${chrdir}/${bm}
else
scriptecho "${bm} not found - not bind mounting"
fi
done
}
# stop
function chrstop() {
chrdircheck || exit 1
chrstopssh
chrstopdocker
mount | grep "${chrdir}" | awk '{print $3}' | tac | while read -r bm ; do
scriptecho "attempting unmount of ${bm}"
umount -f "${bm}"
done
}
# startssh
function chrstartssh() {
chrdircheck || exit 1
chrstart # should be safe to run
mkdir -p "${chrdir}/etc/dropbear"
test -e "${chrdir}/etc/dropbear" || {
scriptecho "could not create dropbear keys directory in chroot"
exit 1
}
test -e "${chrdir}/usr/sbin/dropbear" || {
scriptecho "installing dropbear"
chroot "${chrdir}" /sbin/apk update
chroot "${chrdir}" /sbin/apk add dropbear dropbear-scp dropbear-dbclient dropbear-convert dropbear-ssh psmisc
}
chroot "${chrdir}" /usr/sbin/dropbear -B -R -p ${sshport}
}
# stop ssh in the chroot
function chrstopssh() {
chrdircheck || exit 1
chroot "${chrdir}" test -e /var/run/dropbear.pid \
&& kill -KILL $(chroot "${chrdir}" cat /var/run/dropbear.pid) 2>/dev/null
sshpid="$(chroot "${chrdir}" /usr/bin/fuser -n tcp ${sshport} 2>/dev/null)"
if [ ! -z "${sshpid}" ] ; then
kill -KILL ${sshpid}
fi
}
# start docker
function chrstartdocker() {
chrdircheck || exit 1
chrstart
test -e "${chrdir}/usr/bin/dockerd" || {
scriptecho "installing dropbear"
chroot "${chrdir}" /sbin/apk update
chroot "${chrdir}" /sbin/apk add docker curl wget psmisc
}
mkdir -p "${chrdir}/opt/docker/scripts"
test -e "${chrdir}/opt/docker/scripts/cgroupfs-mount" || {
chroot "${chrdir}" wget -P /opt/docker/scripts/ https://github.com/tianon/cgroupfs-mount/raw/master/cgroupfs-mount
}
chmod 755 "${chrdir}/opt/docker/scripts/cgroupfs-mount"
chroot "${chrdir}" /opt/docker/scripts/cgroupfs-mount
chroot "${chrdir}" /usr/bin/dockerd -s vfs >"${chrdir}/tmp/docker.log" 2>&1 &
}
# stop docker
function chrstopdocker() {
chrdircheck || exit 1
chroot "${chrdir}" test -e /var/run/docker.pid \
&& kill -KILL $(chroot "${chrdir}" cat /var/run/docker.pid) 2>/dev/null
dockerpid="$(chroot "${chrdir}" /usr/bin/fuser /var/run/docker.sock 2>/dev/null)"
if [ ! -z "${dockerpid}" ] ; then
kill -KILL ${dockerpid}
fi
}
# update/upgrade via apk
function chrupdate() {
chrdircheck || exit 1
chrstart # should be safe to run
chroot "${chrdir}" /sbin/apk update
chroot "${chrdir}" /sbin/apk upgrade
}
# just dump our bind mount status for now
function chrstatus() {
chrdircheck || exit 1
for bm in ${bmds[@]} ${bmfs[@]} ; do
mount | grep -q " ${chrdir}/${bm} " || {
scriptecho "${chrdir}/${bm} does not appear to be bind mounted"
}
done
}
case ${command} in
setup)
chrsetup
;;
chpass)
chrchpass
;;
start)
chrstart
;;
stop)
chrstop
;;
startssh)
chrstartssh
;;
stopssh)
chrstopssh
;;
startdocker)
chrstartdocker
;;
stopdocker)
chrstopdocker
;;
status)
chrstatus
;;
update)
chrupdate
;;
*)
scriptecho "command ${command} not understood"
echo
usage
exit 1
;;
esac