Skip to content

Commit 0321da6

Browse files
committed
debootstrap-ubuntu-qemu.sh
1 parent 929296c commit 0321da6

File tree

4 files changed

+183
-8
lines changed

4 files changed

+183
-8
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Output and temporary files.
2+
*.tmp
23
_out/
34
_tmp/
4-
*.tmp
5+
tmp
56

67
# Compiled stuff.
78
*.slo
@@ -14,4 +15,3 @@ _tmp/
1415
*.la
1516
*.a
1617
*.s
17-

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,14 @@ Includes Linux concepts and utilities that work on Linux, not necessarily in the
287287
1. [Pandoc](pandoc/)
288288
1. [RST](rst/)
289289
1. [Virtualization](virtualization.md)
290-
1. [Docker](docker/)
291-
1. [QEMU](qemu.md)
292-
1. [Xen](xen.md)
293-
1. [Vagrant](vagrang/)
294-
1. [Intel SDE](intel-sde.md)
295-
1. [gem5](gem5.md)
290+
1. [Docker](docker/)
291+
1. [QEMU](qemu.md)
292+
1. [debootstrap-ubuntu-qemu.sh](debootstrap-ubuntu-qemu.sh)
293+
1. [debootstrap-debian-qemu.sh](debootstrap-debian-qemu.sh)
294+
1. [Xen](xen.md)
295+
1. [Vagrant](vagrang/)
296+
1. [Intel SDE](intel-sde.md)
297+
1. [gem5](gem5.md)
296298
1. [Configuration automation](config-automation.md)
297299
1. [chef](chef/)
298300
1. [puppet](puppet.md)

debootstrap-debian-qemu.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
# https://unix.stackexchange.com/questions/275429/creating-bootable-debian-image-with-debootstrap/473256#473256
4+
5+
set -eux
6+
7+
debootstrap_dir=debootstrap
8+
root_filesystem=img.ext2.qcow2
9+
10+
sudo apt-get install \
11+
debootstrap \
12+
libguestfs-tools \
13+
qemu-system-x86 \
14+
;
15+
16+
if [ ! -d "$debootstrap_dir" ]; then
17+
# Create debootstrap directory.
18+
# - linux-image-amd64: downloads the kernel image
19+
sudo debootstrap \
20+
--include linux-image-amd64 \
21+
stretch \
22+
"$debootstrap_dir" \
23+
http://deb.debian.org/debian/ \
24+
;
25+
fi
26+
27+
linux_image="$(printf "${debootstrap_dir}/boot/vmlinuz-"*)"
28+
29+
if [ ! -f "$root_filesystem" ]; then
30+
31+
# Set root password.
32+
echo 'root:root' | sudo chroot "$debootstrap_dir" chpasswd
33+
34+
# Remount root filesystem as rw.
35+
# Otherwise, systemd shows:
36+
# [FAILED] Failed to start Create Volatile Files and Directories.
37+
# and then this leads to further failures in the network setup.
38+
cat << EOF | sudo tee "${debootstrap_dir}/etc/fstab"
39+
/dev/sda / ext4 errors=remount-ro,acl 0 1
40+
EOF
41+
42+
# Network.
43+
# We use enp0s3 because the kernel boot prints:
44+
# 8139cp 0000:00:03.0 enp0s3: renamed from eth0
45+
# This can also be observed with:
46+
# ip link show
47+
# Without this, systemd shows many network errors, the first of which is:
48+
# [FAILED] Failed to start Network Time Synchronization.
49+
cat << EOF | sudo tee "${debootstrap_dir}/etc/network/interfaces.d/00mytest"
50+
auto lo
51+
iface lo inet loopback
52+
auto enp0s3
53+
iface enp0s3 inet dhcp
54+
EOF
55+
56+
# Generate image file from debootstrap directory.
57+
# Leave 1Gb extra empty space in the image.
58+
sudo virt-make-fs \
59+
--format qcow2 \
60+
--size +1G \
61+
--type ext2 \
62+
"$debootstrap_dir" \
63+
"$root_filesystem" \
64+
;
65+
sudo chmod 666 "$root_filesystem"
66+
fi
67+
68+
linux_img="${debootstrap_dir}/boot/vmlinuz-"*
69+
70+
# Build the Linux kernel.
71+
git clone --depth 1 --branch v4.18 git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
72+
cd linux
73+
wget https://gist.githubusercontent.com/cirosantilli/6e2f4975c1929162a86be09f839874ca/raw/6d151d231a233408a6e1b541bf4a92fd55bf5338/.config
74+
make olddefconfig
75+
make -j`nproc`
76+
cd -
77+
linux_img="linux/arch/x86_64/boot/bzImage"
78+
79+
qemu-system-x86_64 \
80+
-append 'console=ttyS0 root=/dev/sda' \
81+
-drive "file=img.ext2.qcow2,format=qcow2" \
82+
-enable-kvm \
83+
-serial mon:stdio \
84+
-m 2G \
85+
-kernel="$linux_img"
86+
-device rtl8139,netdev=net0 \
87+
-netdev user,id=net0 \
88+
;

debootstrap-ubuntu-qemu.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
3+
# https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171
4+
5+
set -eux
6+
7+
debootstrap_dir=debootstrap
8+
root_filesystem=debootstrap.ext2.qcow2
9+
10+
sudo apt-get install \
11+
debootstrap \
12+
libguestfs-tools \
13+
qemu-system-x86 \
14+
;
15+
16+
if [ ! -d "$debootstrap_dir" ]; then
17+
# Create debootstrap directory.
18+
# - linux-image-generic: downloads the kernel image we will use under /boot
19+
# - network-manager: automatically starts the network at boot for us
20+
sudo debootstrap \
21+
--include linux-image-generic \
22+
bionic \
23+
"$debootstrap_dir" \
24+
http://archive.ubuntu.com/ubuntu \
25+
;
26+
sudo rm -f "$root_filesystem"
27+
fi
28+
29+
linux_image="$(printf "${debootstrap_dir}/boot/vmlinuz-"*)"
30+
31+
if [ ! -f "$root_filesystem" ]; then
32+
# Set root password.
33+
echo 'root:root' | sudo chroot "$debootstrap_dir" chpasswd
34+
35+
# Remount root filesystem as rw.
36+
cat << EOF | sudo tee "${debootstrap_dir}/etc/fstab"
37+
/dev/sda / ext4 errors=remount-ro,acl 0 1
38+
EOF
39+
40+
# Automaticaly start networking.
41+
# Otherwise network commands fail with:
42+
# Temporary failure in name resolution
43+
# https://askubuntu.com/questions/1045278/ubuntu-server-18-04-temporary-failure-in-name-resolution/1080902#1080902
44+
cat << EOF | sudo tee "$debootstrap_dir/etc/systemd/system/dhclient.service"
45+
[Unit]
46+
Description=DHCP Client
47+
Documentation=man:dhclient(8)
48+
Wants=network.target
49+
Before=network.target
50+
[Service]
51+
Type=forking
52+
PIDFile=/var/run/dhclient.pid
53+
ExecStart=/sbin/dhclient -4 -q
54+
[Install]
55+
WantedBy=multi-user.target
56+
EOF
57+
sudo ln -sf "$debootstrap_dir/etc/systemd/system/dhclient.service" \
58+
"${debootstrap_dir}/etc/systemd/system/multi-user.target.wants/dhclient.service"
59+
60+
# Why Ubuntu, why.
61+
# https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
62+
sudo chmod +r "${linux_image}"
63+
64+
# Generate image file from debootstrap directory.
65+
# Leave 1Gb extra empty space in the image.
66+
sudo virt-make-fs \
67+
--format qcow2 \
68+
--size +1G \
69+
--type ext2 \
70+
"$debootstrap_dir" \
71+
"$root_filesystem" \
72+
;
73+
sudo chmod 666 "$root_filesystem"
74+
fi
75+
76+
qemu-system-x86_64 \
77+
-append 'console=ttyS0 root=/dev/sda' \
78+
-drive "file=${root_filesystem},format=qcow2" \
79+
-enable-kvm \
80+
-serial mon:stdio \
81+
-m 2G \
82+
-kernel "${linux_image}" \
83+
-device rtl8139,netdev=net0 \
84+
-netdev user,id=net0 \
85+
;

0 commit comments

Comments
 (0)