Making a Debootstrap image bootable

Submitted by olaf on 2016-03-24

You can build a small Debian or Ubuntu system with debootstrap, e.g.

debootstrap --variant=minbase trusty basedir.d

This creates a minimal system in a directory basedir.d, which can be extended and used in a chroot or Linux container environment. What you cannot do, is booting into this system with QEMU or VirtualBox. For this, you need a bootable disk image.

Virtual disk image

Creating a sparse, raw 10 GiB virtual disk and partitioning it

dd if=/dev/zero of=disk.img bs=1024 count=1 seek=10239k
parted -s disk.img -- mklabel msdos mkpart primary 1m 10g toggle 1 boot

You can also use fdisk disk.img to create a suitable partition, if you prefer.

You can access the new partition with the help of a loop device.

losetup --show -f disk.img

This prints the associated loop device, e.g. /dev/loop0 or /dev/loop1. To make the partition known to the system, use

partprobe /dev/loop0

This creates an additional device /dev/loop0p1 for the first partition. Now we create a filesystem on this partition and mount it.

mkfs -t ext2 /dev/loop0p1
mount /dev/loop0p1 /mnt

We’re ready to install a minimal system on our virtual disk partition

debootstrap --variant=minbase --include=linux-image-generic trusty /mnt

This is almost the same as above, but since we want a bootable system, we need to install a Linux kernel (linux-image-generic) and boot loader (grub-pc) too.

Configuring GRUB is done in two steps.

  • Install GRUB to the master boot record (MBR)

      grub-install --boot-directory=/mnt/boot --modules=part_msdos /dev/loop0
    

    The option --modules=part_msdos ensures, that GRUB can read the partition table and find the created partitions.

  • Create a GRUB config file

      mount --bind /proc /mnt/proc
      mount --bind /dev /mnt/dev
      mount --bind /sys /mnt/sys
      chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
    

Setting a root password, so we can login to the new system.

chroot /mnt passwd

Cleaning up

umount /mnt/sys
umount /mnt/dev
umount /mnt/proc
umount /mnt
losetup -d /dev/loop0

Booting

qemu-system-x86_64 -hda test.img

This gives us a console login prompt at our shiny new system.

VirtualBox

You can do the same with VirtualBox of course, but you must create a virtual machine

vboxmanage createvm --name VboxTest --register --ostype Ubuntu_64
vboxmanage modifyvm VboxTest --memory 4096
vboxmanage storagectl VboxTest --name SATA --add sata --bootable on

convert the disk image

vboxmanage convertfromraw --format vdi disk.img disk.vdi

and attach it

vboxmanage storageattach VboxTest --storagectl SATA --port 0 --device 0 --type hdd --medium disk.vdi

before booting …

vboxmanage startvm VboxTest

Post a comment

All comments are held for moderation; Markdown and basic HTML formatting accepted. If you want to stay anonymous, leave name, e-mail and website empty.