aboutsummaryrefslogtreecommitdiffstats
path: root/make-vm-debootstrap
blob: 667eeadc72a5e3fd284c19b7764dc75188a33880 (plain)
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
#!/bin/bash

set -eu

#if [ "$UID" != "0" ]; then
#    exec sudo -- "$0" "$@"
#fi

traperror()
{
    echo "args: $*" >&2
    echo "trace: ${FUNCNAME[*]} ${BASH_LINENO[*]}" >&2
    exit 1
}

fail() { echo "${FUNCNAME[1]} ${BASH_LINENO[0]}: $*" >&2; exit 1; }
#trap 'fail unhandled exception' ERR
trap 'traperror $LINENO ${FUNCNAME:-main} $BASH_LINENO; trap - ERR' ERR

exec 3>&1
exec >debootstrap.log
echo "I: log=debootstrap.log">&2

RELEASE=unstable
#MIRROR=http://fr.archive.ubuntu.com/ubuntu/
MIRROR=http://apt:9999/debian/
ARCH=amd64
PKGS="$(sed 's/^#.*$//'<<EOF
apt
aptitude
bash
bash-completion
bind9-host
bmon
busybox
bzip2
curl
ed
grub2
htop
iftop
ifupdown
iotop
iperf
iproute2
iptables
iputils-ping
isc-dhcp-client
less
lftp
linux-image-virtual
locales
locales libui-dialog-perl dialog
locate
ncdu
ncurses-base
ncurses-term
net-tools
netbase
netcat
nload
openssh-client
openssh-server
psmisc
python3
ranger
rsync
runit
screen
sed
sl
socat
socklog
strace
tar
tcpdump
telnet
tmux
tree
tzdata
vim
vim-nox
vim-runtime
w3m
wget
zsh
EOF
)"



    fail I want to fail
#false

test0() {
    traperror test
    #fail test of the week
    echo 'test0' >&2
    false
    echo 'test0-end' >&2
}

test0

false

#echo "Warning: will erase $BLOCKDEV with mkfs.ext4 in 10seconds...">&2
#for i in $(seq 10 -1 0); do sleep 1; printf "%d... " $i>&2; done
#echo>&2
#mkfs -q -F -t ext4 -L rootfs "$BLOCKDEV" || exit 1
#mkdir -p "$DESTDIR"
#echo "mounting block device...">&2
#mount -v -t ext4 "$BLOCKDEV" "$DESTDIR" || \
#    mount -v -t ext4 -o loop "$BLOCKDEV" "$DESTDIR" || exit 1
#echo "In case of error, please remember to umount $DESTDIR">&2

# compacting qcow2 images files
# You have to zerofill the data on the disk first.
#   sfill -fllvz
# Then clone the image file
#   qemu-img convert -p -O qcow2 ./source.img ./packed.

#modprobe nbd max_part=16
#qemu-nbd -c /dev/nbd0 image.qcow2
#partprobe /dev/nbd0
#mount /dev/nbd0p1 /mnt/image

echo "I: debootstraping...">&2
qemu-debootstrap --arch="$ARCH" \
    --include="$PKGS" --components=main,universe \
    "$RELEASE" "$DESTDIR" "$MIRROR"

if [ ! -e "$KERNEL" ];then
    echo "I: wget kernel...">&2
    wget -q "$MIRROR/dists/$RELEASE/main/installer-$ARCH/current/images/netboot/ubuntu-installer/$ARCH/linux" -O "$KERNEL"
fi

echo "I: adjust configuration...">&2
rm -f "$DESTDIR/etc/localtime"
cat "$DESTDIR/usr/share/zoneinfo/Europe/Paris" > "$DESTDIR/etc/localtime"

# network
cat >"$DESTDIR/etc/network/interfaces"<<EOF
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.1.1.252
netmask 255.255.255.0
gateway 10.1.1.254
EOF
echo "127.0.0.1 localhost localhost.localdomain">"$DESTDIR/etc/hosts"
echo "localhost">"$DESTDIR/etc/hostname"
echo "servername 10.1.1.254">"$DESTDIR/etc/resolv.conf"

for f in "$DESTDIR"/etc/init/tty[1-6].conf; do
    echo "manual">${f%.conf}.override
done

chmod +w "$DESTDIR/etc/sudoers"
sed 's,#* *%sudo.*,%sudo ALL=(ALL) NOPASSWD: ALL,' -i "$DESTDIR/etc/sudoers"
chmod -w "$DESTDIR/etc/sudoers"

#UUID="$(blkid "$BLOCKDEV" | sed 's/^.*UUID="\([a-zA-Z0-9-]\+\)".*$/\1/')"
cat >"$DESTDIR/etc/fstab"<<EOF
# <device> <mount point> <type> <options> <dump>  <pass>
/dev/vda   /     ext4 errors=remount-ro,noatime                      0 1
tmpfs /tmp     tmpfs mode=1777,noatime,size=128m,nodev,nosuid,noexec 0 0
tmpfs /var/log tmpfs mode=1777,noatime,size=4m,nodev,nosuid,noexec   0 0
tmpfs /var/tmp tmpfs mode=1777,noatime,size=128m,nodev,nosuid,noexec 0 0

#UUID=$UUID /    ext4 errors=remount-ro,noatime                      0 1
#UUID=use blkid for uuid discovery /home ext4 noatime                0 2
#UUID=6d9ac889-3c25-4e05-90f3-d183f8b14aca none  swap sw,auto        0 0
EOF

mkdir -p "$DESTDIR/etc/sv/ttyS0/"
mkdir -p "$DESTDIR/etc/service/"
cat >"$DESTDIR/etc/sv/ttyS0/run"<<"EOF"
#!/bin/bash
dev=/dev/ttyS0
/bin/stty -F $dev sane clocal -crtscts -hupcl
exec <$dev >$dev 2>$dev
echo
echo "Press enter to continue..."; read
cd /root
/usr/bin/env -i HOME=/root PATH=/usr/sbin:/usr/bin:/sbin:/bin TERM=screen-256color /usr/bin/setsid /bin/bash
exec wait
#exec /bin/bash
#exec /usr/bin/env -i /bin/sh
#exec /usr/bin/setsid /bin/sh
EOF
chmod +x "$DESTDIR/etc/sv/ttyS0/run"
ln -sfv "/etc/sv/ttyS0/" "$DESTDIR/etc/service/"

mkdir -p "$DESTDIR/etc/ssh"
cat >"$DESTDIR/etc/ssh/sshd_config"<<EOF
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 768
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
PasswordAuthentication no
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM yes
AllowUsers calendros
EOF

cat > "$DESTDIR/etc/apt/sources.list" <<EOF
deb $MIRROR $RELEASE main universe
deb http://security.ubuntu.com/ubuntu/ $RELEASE-security main universe
EOF

# /etc/default files:
cat > "$DESTDIR/etc/default/console-setup"<<EOF
VERBOSE_OUTPUT="no"
ACTIVE_CONSOLES="/dev/ttyS0"
CHARMAP="UTF-8"
CODESET="Uni2"
FONTFACE="Fixed"
FONTSIZE="16"
if [ -f /etc/default/keyboard ]; then
    . /etc/default/keyboard
fi
EOF
cat >"$DESTDIR/etc/default/keyboard"<<EOF
XKBMODEL="pc105"
XKBLAYOUT="fr"
XKBVARIANT="bepo"
XKBOPTIONS=""
EOF
cat >"$DESTDIR/etc/default/locale"<<EOF
LANG="en_US.UTF-8"
LC_TIME="en_DK.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
EOF

cat >>"$DESTDIR/etc/bash.bashrc"<<EOF
# enable bash completion in interactive shells
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi
alias ls="ls --color=aut"
alias l="ls -CF"
alias ll="l -lh"
alias la="l -a"
alias e="vim"
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
export PAGER=less
export EDITOR=vim
export VISUAL=vim
EOF

mkdir -p "$DESTDIR/etc/vim"
cat >"$DESTDIR/etc/vim/vimrc"<<EOF
set nocompatible
set modelines=0
set backspace=2
syntax on
set autoindent
set ts=4
set shiftwidth=4
set ruler
set laststatus=2
set statusline=%<%f%h%m%r%=%l,%c\ %P
set wildmode=list:full
set nobackup nowritebackup
set hlsearch
set whichwrap=<,>,[,]
set scrolloff=1
EOF

rm -r "$DESTDIR/etc/skel"
mkdir -p "$DESTDIR/etc/skel"

cat > "$DESTDIR/init-stage2.sh" << EOF
#!/bin/sh
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
mount -no remount,rw /
mount -t proc proc /proc
mount -t sysfs sysfs /sys

/debootstrap/debootstrap --second-stage

# second stage may unmount this...
[ -e /proc/mounts ] || mount -t proc proc /proc
[ -e /sys/kernel ] || mount -t sysfs sysfs /sys
busybox mdev -s

echo '------ mounts:'
cat /proc/mounts
echo '------'

mv /etc/default/extlinux /etc/default/extlinux.dist
cat >/etc/default/extlinux<<EOF2
EXTLINUX_UPDATE="true"
EXTLINUX_ALTERNATIVES="default"
EXTLINUX_DEFAULT="l0"
EXTLINUX_ENTRIES="all"
EXTLINUX_MEMDISK="true"
EXTLINUX_MEMDISK_DIRECTORY="/boot"
EXTLINUX_MENU_LABEL="Ubuntu GNU/Linux, kernel"
EXTLINUX_OS_PROBER="false"
EXTLINUX_PARAMETERS="ro quiet console=ttyS0,115200n8 panic=120 oops=panic"
EXTLINUX_ROOT="root=/dev/vda"
EXTLINUX_THEME="none"
EXTLINUX_TIMEOUT="1" # wait 1 tenth of a second (0 is wait forever...)
EOF2
[ -e /dev/root ] || ln -sv /dev/vda /dev/root
extlinux -i /boot/extlinux/

passwd -d -l root
useradd -M -b /home -d /home/calendros -U -G sudo -s /bin/bash calendros
mkdir -p /home/calendros/.ssh/
touch /home/calendros/.ssh/authorized_keys
chmod 700 /home/calendros/.ssh
chmod 600 /home/calendros/.ssh/authorized_keys
chown calendros:calendros -R /home/calendros
echo '. /etc/bash.bashrc' >~root/.bashrc
echo '. ~/.bashrc' >~root/.bash_profile
echo '. /etc/bash.bashrc' >~calendros/.bashrc
echo '. ~/.bashrc' >~calendros/.bash_profile

locale-gen en_US.UTF-8 en_GB.UTF-8 en_DK.UTF-8 fr_FR.UTF-8

# dhclient may erase /etc/resolv.conf, and is not really useful here
#ip link set eth0 up
#dhclient eth0
#apt-get update

echo "Dropping a shell, press Ctrl-D or type exit to finish."
/bin/bash

rm -v /etc/udev/rules.d/70-persistent-*.rules

echo "Bootstrap ended, poweroff in 10seconds..."
rm /init-stage2.sh
#sleep 10 || exec /bin/bash # for debug
#poweroff & # does not work
sync
fuser -k /
sleep 1
mount -o remount,ro /
exit 0 # will panic, but does not matter now (don't know how to shutdown o/w)
EOF
chmod +x "$DESTDIR/init-stage2.sh"

cat >&2 <<EOF
I: please umount destination blockdev...
I: like: sudo umount -v "$DESTDIR"
I: then run vm, deboostrap phase 2 (adjust blockdev)...
I: export DEV=blockdev; qemu-system-x86_64 -machine accel=kvm -m 256 \\
    -monitor unix:/tmp/debootstrap-monitor,server,nowait \\
    -serial  unix:/tmp/debootstrap-serial,server \\
    -vga vmware -display none \\
    -drive file="\$DEV",if=virtio \\
    -kernel "$KERNEL" \\
    -append "quiet rootdelay=10 root=/dev/vda init=/init-stage2.sh console=ttyS0,115200n8"
I: To copy this image to another host, remember to run:
I: extlinux -i /yourcopyplace/boot/extlinux/
I: end of script...
EOF