Skip to content

Instantly share code, notes, and snippets.

@dirkakrid
Forked from gdamjan/0-image.sh
Created October 30, 2016 04:02
Show Gist options
  • Save dirkakrid/ede2439fafd31f922db767ebed5d98a8 to your computer and use it in GitHub Desktop.
Save dirkakrid/ede2439fafd31f922db767ebed5d98a8 to your computer and use it in GitHub Desktop.
grub imagelet to start VMs from an image without bootloader
# Create the grub boot image
grub-mkimage -O i386-pc -o grub.img -c grub.cfg \
at_keyboard configfile biosdisk ext2 linux test serial halt minicmd terminal cat acpi
# to create the unbootable linux image, no partitions
qemu-img create -f qcow2 unbootable.img 10G
sudo modprobe nbd max_part=16
sudo qemu-nbd -c /dev/nbd0 unbootable.img
sudo mkfs.ext4 -L ROOT /dev/nbd0
sudo mount /dev/nbd0 /mnt/
sudo debootstrap --arch=amd64 --variant=minbase --include=linux-image-amd64,iproute2,vim \
jessie /mnt http://debian.blizoo.mk/debian
sudo systemd-nspawn --setenv=RUNLEVEL=1 -D /mnt /usr/bin/passwd
sudo umount /mnt
sudo qemu-nbd -d /dev/nbd0
# to run a VM
qemu-system-x86_64 -enable-kvm -kernel grub.img -hda unbootable.img
# optional: -drive file=unbootable.img,if=virtio
# optional: -net user -net nic,model=virtio
linux (hd0)/vmlinuz root=LABEL=ROOT rw
initrd (hd0)/initrd.img
boot
sudo systemd-nspawn -D /mnt
systemctl enable systemd-networkd
systemctl enable systemd-resolved
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
# or in systemd >216 modify /etc/nsswitch.conf to have
# hosts: files mymachines resolve myhostname
cat > /etc/systemd/network/wired.network <<EOF
[Match]
Name=eth0
[Network]
DHCP=yes
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment