-
Notifications
You must be signed in to change notification settings - Fork 0
/
debian-nspawn2.sh
executable file
·77 lines (51 loc) · 1.39 KB
/
debian-nspawn2.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
#!/bin/bash
############################
# Configuration!
MIRROR=http://ftp.us.debian.org/debian/
# stretch
DIST=buster
############################
# check privs
if [[ "$EUID" -ne 0 ]]; then
# also, require root for correct debootstrap dir permissions, during copy.
echo "Must be root for mount privs"
exit 1
fi
############################
# debootstrap download and cache
# log
set -x
# fail fast
set -e
[ -d ./cache ] || mkdir ./cache
[ -d ./build ] || mkdir ./build
cache="./cache/$DIST"
target="./build/$DIST.chroot"
# maybe delete stale cache. eg. 1 day.
if [ -d "$cache" ]; then
now=$(date +%s)
file=$(stat -c %Y "$cache")
if [ $now -gt $(( $file + 86400 )) ]; then
echo "deleting stale cache!"
rm -rf "$cache"
else
echo "cache ok!"
fi
fi
# download bootstrap files locally. note use || exit
[ -d "$cache" ] || debootstrap "$DIST" "$cache/" $MIRROR
############################
# build filesystems
rm -rf "$target" || true
# copy debootstrap files across to mnt
cp -rp "$cache" $target
############################
# install kernel, and configure
# this will give us ssh root login. expect root ssh pubkeys to be injected. can then do python minimal-install, and then run ansible...
chroot --userspec=0:0 $target <<- EOF
# fail fast
set -e
apt-get -y install ssh
mkdir /root/.ssh
sed -i 's/PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
EOF