-
Notifications
You must be signed in to change notification settings - Fork 0
/
debian-image.sh
executable file
·205 lines (149 loc) · 3.65 KB
/
debian-image.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
#!/bin/bash
############################
# Configuration!
# if [ -z "$KEYS" ]; then...
KEYS="$(cat /home/meteo/.ssh/authorized_keys)"
# DANGEROUS - edit me!
ROOTPASSWD=root
FSSIZE=1G
# MIRROR=http://mirror.internode.on.net/pub/debian/
MIRROR=http://ftp.us.debian.org/debian/
DIST=stretch
KERNEL=4.9.0-6-amd64
PYTHON=yes
# Jessie
# DIST=jessie
# KERNEL=3.16.0-4-amd64
############################
# sanity
if [[ $EUID -ne 0 ]]; then
# also, require root for correct debootstrap dir permissions.
echo "Must be root for mount/losetup"
exit 1
fi
if [ $( mount | grep -c loop ) != 0 ] ; then
echo "loop device already in use"
exit 1
fi
if [ -z "$KEYS" ]; then
echo "Need ssh keys!"
exit 1
fi
if [ "$ROOTPASSWD" = "root" ]; then
echo "WARNING change the root password!"
# 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-$KERNEL.img"
mnt="./mnt"
# 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
############################
# create image
rm $target || true
rm -rf "$mnt" || true
mkdir "$mnt"
# image
dd if=/dev/zero of=$target bs=$FSSIZE count=1
chmod 666 $target
# partition
fdisk $target << EOF
n
p
1
2048
a
p
w
EOF
############################
# initial filesystem
cleanup_resources () {
# TODO chaining resource cleanup see, https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
# don't fail fast, during cleanup
set +e
for i in /proc /sys /dev; do
umount "$mnt/$i";
done
umount "$mnt"
losetup -D
rmdir "$mnt"
}
trap cleanup_resources EXIT
losetup -f $target /dev/loop0
losetup -f $target -o $((2048 * 512)) /dev/loop1
# mkfs
mkfs.ext4 /dev/loop1
# grab filesystem uuid for later
UUID=$( blkid -p -s UUID /dev/loop1 | sed 's/.*="\([^"]*\).*/\1/' )
# mount the device
mount /dev/loop1 "$mnt"
# copy debootstrap
cp -rp $cache/* "$mnt"
############################
# install kernel, and configure
# mount systems
for i in /proc /sys /dev; do
mount -B $i "$mnt/$i";
done
# discover kernel version...
# KERNEL=$( apt-cache search linux-image | cut -d ' ' -f 1 | egrep '^linux-image-[0-9\.-]{5,}amd64$' | sed 's/linux-image-\(.*\)/\1/' )
# chroot and install kernel, boot config, ssh
chroot --userspec=0:0 "$mnt" <<- EOF
# fail fast
set -e
# Install kernel
apt-get -y install linux-image-$KERNEL
apt-get -y install extlinux
mkdir -p /boot/syslinux
extlinux --install /boot/syslinux
dd bs=440 conv=notrunc count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/loop0
cat > /boot/syslinux/syslinux.cfg <<- EOF2
CONSOLE 0
SERIAL 0 115200 0
DEFAULT linux
PROMPT 0
LABEL linux
SAY Now booting the kernel from SYSLINUX...
KERNEL /boot/vmlinuz-$KERNEL
APPEND rw root=UUID=$UUID initrd=/boot/initrd.img-$KERNEL vga=normal fb=false console=ttyS0,115200n8 net.ifnames=0 biosdevname=0
EOF2
cat > /etc/network/interfaces << EOF2
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
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