-
Notifications
You must be signed in to change notification settings - Fork 0
/
debian-nspawn.sh
executable file
·117 lines (81 loc) · 1.97 KB
/
debian-nspawn.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
#!/bin/bash
############################
# Configuration!
KEYS="$(cat /home/meteo/.ssh/authorized_keys)"
# DANGEROUS - edit me!
ROOTPASSWD=root
MIRROR=http://ftp.us.debian.org/debian/
# stretch
DIST=stretch
PYTHON=yes
############################
# check privs
if [[ "$EUID" -ne 0 ]]; then
# also, require root for correct debootstrap dir permissions, during copy.
echo "Must run as root for mount privs"
exit 1
fi
if [ -z "$KEYS" ]; then
echo "Need ssh keys!"
exit 1
fi
if [ "$ROOTPASSWD" = "root" ]; then
echo "WARNING change the root password!"
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
# install config, ssh
chroot --userspec=0:0 $target <<- EOF
# fail fast
set -e
cat > /etc/network/interfaces << EOF2
auto lo
iface lo inet loopback
# container nic
# setting hwaddress here works with nspawn
auto host0
iface host0 inet dhcp
hwaddress ether 02:01:02:03:04:08
EOF2
if [ -n "$ROOTPASSWD" ]; then
echo root:$ROOTPASSWD | chpasswd
fi
if [ -n "$KEYS" ]; then
apt-get -y install ssh
mkdir /root/.ssh
echo "$KEYS" > /root/.ssh/authorized_keys
chmod 400 /root/.ssh/authorized_keys
sed -i 's/PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
fi
if [ $PYTHON = "yes" ]; then
apt-get -y install python-minimal
fi
EOF