Prepare host system for virtual machines, this includes create new user, loading necessary modules and configure network. Load kvm module, in this example intel module is loaded but depends on host cpu;
# modprobe -a kvm-intel tun virtio
Add users to kvm group;
# usermod -a -G kvm c9admin # usermod -a -G kvm username
Qemu supports multiple disk images types.
Create hard disk image, there is different types, this describes how to create a qcow2 type;
$ qemu-img create -f qcow2 crux-img.qcow2 15G
Qemu disk images can be treated as regular disks using qemu disk network block device server;
$ sudo modprobe nbd $ sudo qemu-nbd -c /dev/nbd0 crux-img.qcow2
Information about preparing partitions and storage administration. You can use image as a normal disk, example how to use parted to create a gpt system table;
parted --script ${DEV} \ mklabel gpt \ unit mib \ mkpart primary 2 4 \ name 1 grub \ mkpart ESP fat32 4 128 \ name 2 efi \ mkpart primary ext4 128 1128 \ name 3 boot \ mkpart primary ext4 1128 12128 \ name 4 root \ mkpart primary ext4 12128 14128 \ name 5 var \ mkpart primary ext4 14128 100% \ name 6 lvm \ set 1 bios_grub on \ set 2 boot on \ set 6 lvm on
# kpartx -a -s -l -u /dev/nbd0
Use /dev/mapper/$(name_of_device) to assign correct blocks;
mkfs.fat -F 32 /dev/mapper/${DEV_NAME}p2 mkfs.ext4 /dev/mapper/${DEV_NAME}p3 mkfs.ext4 /dev/mapper/${DEV_NAME}p4 mkfs.ext4 /dev/mapper/${DEV_NAME}p5 pvcreate /dev/mapper/${DEV_NAME}p6
Read lvm documentation on how to setup virtual group and logic volumes.
Mount partition;
mount /dev/mapper/${DEV_NAME}p4 $CHROOT mkdir -p $CHROOT/proc mkdir -p $CHROOT/sys mkdir -p $CHROOT/dev mkdir -p $CHROOT/media mkdir -p $CHROOT/boot mount /dev/mapper/${DEV_NAME}p3 $CHROOT/boot mkdir -p $CHROOT/boot/efi mount /dev/mapper/${DEV_NAME}p2 $CHROOT/boot/efi mkdir -p $CHROOT/var mount /dev/mapper/${DEV_NAME}p5 $CHROOT/var
Before disconnecting image, clean dev mappings;
$ sudo kpartx -d /dev/nbd0 $ sudo qemu-nbd -d /dev/nbd0
Verify disk image information;
$ qemu-img info c1-storage.qcow2
image: c1-storage.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 7.6G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false $
In this example is added 25G to the image;
$ qemu-img resize c1-storage.qcow2 +25G
Read lvm resize if image is using lvm, or use resize2fs. If size is not provided to resize2fs, by default it will grow file system to all partition;
$ sudo qemu-nbd -c /dev/nbd0 /srv/qemu/img/c1-server.qcow2
# kpartx -a -s -l -u /dev/nbd0 GPT:Primary header thinks Alt. header is not at the end of the disk. GPT:Alternate GPT header not at the end of the disk. GPT: Use GNU Parted to correct# 2-arg version of allocate-array. allocate-array2: # ad: (addr allocation-descriptor), elem-size: int, array-len: int, out: (addr handle array _) # . prologue 55/push-ebp 89/<- %ebp 4/r32/esp # . save registers 50/push-eax # 8b/-> *(ebp+0xc) 0/r32/eax f7 4/subop/multiply-into 0/r32/eax *(ebp+0x10) (allocate-array *(ebp+8) %eax *(ebp+0x14)) $allocate-array2:end: # . restore registers 58/pop-to-eax # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return