dimanche 12 avril 2015

[qemu] Create a complete system image without booting in the emulator

Create an empty disk image:
qemu-img create -f qcow2 debian_bootstrap.qcow2 10G
Use the nbd kernel module (Network Block Device) to create and acceed partitions on the qcow2 disk image.
modinfo nbd

filename:       /lib/modules/3.18.4-1-ARCH/kernel/drivers/block/nbd.ko.gz
license:        GPL
description:    Network Block Device
depends:      
intree:         Y
vermagic:       3.18.4-1-ARCH SMP preempt mod_unload modversions
parm:           nbds_max:number of network block devices to initialize (default: 16) (int)
parm:           max_part:number of partitions per device (default: 0) (int)
parm:           debugflags:flags for controlling debug output (int)

sudo modprobe nbd max_part=16 
Associate the qcow2 disk image to the /dev/nbd0 disk node.
sudo qemu-nbd -c /dev/nbd0 debian_bootstrap.qcow2 
Create your partitions:
sudo parted /dev/nbd0
Or to get a swap + rootfs partition scheme, you can use:
sfdisk /dev/nbd0 -D -uM << EOF
,512,82
;
EOF
If you used parted with gpt partitions, you need to set up the first partition as a 2MB partition with the bios_grub flag set If no new disk file nbd0p1 shows up, you can usethe following command to force then to appear.
 partx -a /dev/nbd0
Format your partitions with the filesystems of your choice. (You can also do this in parted if you wish)
mkswap /dev/nbd0p1
mkfs.ext4 /dev/nbd0p2
Mount your root file system, you are almost ready to bootstrap a new debian in your qcow2 disk image.
mount /dev/nbd0p2 /mnt/
That's it, debootstrap this little rootfs ! (Choose the closest ftp to you to speed up the download)
debootstrap --include=less,locales-all,vim,sudo,openssh-server stable /mnt http://ftp.us.debian.org/debian
chroot in your new debian rootfs:
mount --bind /dev/ /mnt/dev
LANG=C chroot /mnt/ /bin/bash
mount -t proc none /proc
mount -t sysfs none /sys
You might need to set the path correctly if your host distribution doesn't use the same binaries path
PATH=$PATH:/bin:/sbin:/usr/sbin:/usr/bin
Install the latest kernel and grub-pc (beware, you want to install grub2 and not grub legacy)
apt-get install linux-image-amd64 grub-pc
When installing grub, choose the /dev/nbp0p2, you wouldn't want to install it on your own disk.
# To be sure grub is installed ^^
grub-install /dev/nbd0
update-grub
Set a passwd for root
passwd root
exit the chroot
umount /proc/ /sys/ /dev/
exit
# Some say you can fix the grub installation with those commands. Didn't work for me.
grub-install /dev/nbd0 --root-directory=/mnt --modules="biosdisk part_msdos" 
sed -e 's/nbd0p2/sda2/g' boot/grub/grub.cfg > boot/grub/grub.cfg
# I think if you want to have a working grub, just write the configuration yourself, 
# I fix this by hand at the first boot.
If you have other partition than the rootfs, you will need to edit your fstab.conf accordingly.
You are finished, clean your mess.
umount /mnt
qemu-nbd -d /dev/nbd0 
Start your VM You might get the grub console instead of a beautiful grub menu because the grub configuration is still broken! To launch linux you can do:
ls
#identify the rootfs partition
ls (hd0,gpt2)
ls (hd0,gpt2)/
#You should see your filesystem here, names can change
set root = (hd0,gpt2)
linux /boot/vmlinux-... root=/dev/sda2 # very important, specify your rootfs partition here
initrd boot/initrd-...
boot
Launch your newly created VM ! update-grub inside to fix the grub, this will eventually fix your grub.

Aucun commentaire:

Enregistrer un commentaire