Lately I came across a very common problem: how to install debian in virtualized environment automatically. You can always do that by hand, manually. Download Debian Netinstall image, and go through all installation steps. After searching I found out a useful tutorial on the web: Creating a KVM virtual machine using debootstrap. However, it's still a few steps which needs to be done. Can't we do that better? That's why I prepared bash script that automatizes the debootstrap process.

The script is very simple. It replicates all the steps mentioned in the tutorial. You just have to invoke one simple command and there goes all the magic ;)

What to do:

  1. Create virtual machine disk image (eg. using qemu-img):

    qemu-img create -f qcow2 debian-test.qcow2 16G
    
  2. Download the script:

    wget http://ayufan.eu/projects/debootstrap-kvm/qemu-debian-create-image
    chmod +x qemu-debian-create-image
    
  3. Install Debian:

    ./qemu-debian-create-image debian-test.qcow2 debian-test.hostname wheezy
    
  4. Wait a few moments. At the end of the process you will be asked for root password.

    Enter root password:
    Enter new UNIX password: 
    Retype new UNIX password: 
    passwd: password updated successfully
    
  5. If everything goes right, you should see SUCCESS.

    Finishing grub installation...
    Installation finished. No error reported.
    SUCCESS!
    /dev/nbd0 disconnected
    
  6. Example installation log: Show

Details

  1. Arguments:

    ./qm-create-image <image-file> <hostname> <release> [optional debootstrap args]
    
  2. Script assumes that you use the virtio block device. If you need differently, replace all s/vda/sda/g.

  3. Script works with any storage supported by qemu: lvm, raw, qcow2, vmdk

    ./qemu-debian-create-image /dev/vgdisk/vm-106-disk-1 debian-test.hostname wheezy
    
  4. Script creates two partitions:

    /boot   200MB   (bootable)
    /               (rest of the space)
    
  5. Script configures one ethernet as dhcp-client.

  6. Script can be reconfigured to create Ubuntu image. Just change hardcoded mirror URL (http://ftp.pl.debian.org/debian) to any Ubuntish mirror and specify valid release (eg. hardy).

  7. You can install additional packages during debootstrapping:

    ./qemu-debian-create-image debian-test.qcow2 debian-test.hostname wheezy --include apache2 
    

    For more:

    debootstrap --help
    

Requirements

  1. nbd kernel module: it must be loaded with max_parts=16

    modprobe nbd max_parts=16
    
  2. qemu-nbd

  3. debootstrap

  4. sfdisk

Changelog

v.0.1. (2.06.2013) - Initial release

License

GPL