set -eux
suite=xenial
debootstrap_dir=$suite
img_file=${suite}.ext4.img
kernel=/vmlinuz
initrd=/initrd.img
sudo apt install -y \
debootstrap \
qemu-system-x86 \
;
if [ ! -d "$debootstrap_dir" ]; then
# Create debootstrap directory.
sudo debootstrap \
"$suite" \
"$debootstrap_dir" \
http://archive.ubuntu.com/ubuntu \
;
# Set root password.
echo 'root:root' | sudo chroot "$debootstrap_dir" chpasswd
# Remount root filesystem as rw.
cat << EOF | sudo tee "$debootstrap_dir/etc/fstab"
/dev/sda / ext4 errors=remount-ro,acl 0 1
EOF
# Automaticaly start networking.
# Otherwise network commands fail with:
# Temporary failure in name resolution
# https://gist.github.com/corvax19/6230283#gistcomment-1940694
cat << EOF | sudo tee "$debootstrap_dir/etc/systemd/system/dhclient.service"
[Unit]
Description=DHCP Client
Documentation=man:dhclient(8)
Wants=network.target
Before=network.target
[Service]
Type=forking
PIDFile=/var/run/dhclient.pid
ExecStart=/sbin/dhclient -4 -q
[Install]
WantedBy=multi-user.target
EOF
sudo ln -sf /etc/systemd/system/dhclient.service \
"${debootstrap_dir}/etc/systemd/system/multi-user.target.wants/dhclient.service"
fi
if [ ! -f "$img_file" ]; then
# Create disk image
dd if=/dev/null of="$img_file" bs=1M seek=2048
mkfs.ext4 "$img_file"
mnt_dir="${suite}_mnt"
mkdir "$mnt_dir"
sudo mount -t ext4 "$img_file" "$mnt_dir"
sudo cp -r "$debootstrap_dir/." "$mnt_dir"
sudo umount "$mnt_dir"
rmdir "$mnt_dir"
fi
sudo qemu-system-x86_64 \
-append 'console=ttyS0 root=/dev/sda' \
-drive "file=${img_file},format=raw" \
-enable-kvm \
-nographic \
-serial mon:stdio \
-m 2G \
-kernel "$kernel" \
-initrd "$initrd" \
;
댓글 없음:
댓글 쓰기