virt-resize, virt-sparsify, virt-customize, virt-sysprep, virt-builder ---------------------------------------------------------------------- Audience: - Interested in manipulating disk images. - May be familiar with disk images, formats, but may not be. - Not developers. 0000: Introduction ---------------------------------------------------------------------- Libguestfs is a fairly low-level C library for accessing disk images. To perform everyday operations we've written "virt tools" over this. Virt tools use libguestfs, qemu, qemu-img and many other programs and perform operations that would hard to do by hand. In the case of virt-resize, the tool does calculations (like partition sizing) that require hard work and experience, so the virt-resize tool captures a lot of knowledge about how not to break Windows bootloaders and how to align partitions for performance that would be hard to teach. 1000: Resizing a disk image ---------------------------------------------------------------------- Show a disk image, use 'qemu-img info' to get data about it. $ qemu-img info fedora-22.qcow2 --> Format --> Size : virtual and physical may not be related What the guest would see when booted up: $ virt-df -a fedora-22.qcow2 -h Always use 'qemu-img info', not 'ls -l'. What if the guest needs more space? There are various ways to resize a disk image: - Just make the container bigger ('qemu-img resize' or even 'truncate'). But the partitions won't get bigger. [illustrate with picture] - Run a tool inside the guest, like parted. It won't usually work if the disk is mounted. - Run a tool alongside the guest, but still in a VM, eg. PartitionMagic, GParted Live. - Use virt-resize on the host for offline resizing. $ qemu-img create -f qcow2 fedora-22-bigger.qcow2 10G $ virt-resize fedora-22.qcow2 fedora-22-bigger.qcow2 --expand sda3 $ virt-df -a fedora-22-bigger.qcow2 -h virt-resize is quite flexible: For example you could give /dev/sda1 500 MB and give the rest to /dev/sda3, or you could tell virt-resize to put the new space into an extra partition. Also works for Windows. 2000: Sparsifying a disk image ---------------------------------------------------------------------- Show a fully allocated disk image, this time with 'ls -lh': $ qemu-img info fedora-22.img $ du -sh fedora-22.img $ virt-df -a fedora-22.img -h Sparsify it in place: $ virt-sparsify --inplace fedora-22.img $ qemu-img info fedora-22.img $ du -sh fedora-22.img Explain the two modes for sparsification. Also works for Windows. 3000: Customizing a disk image ---------------------------------------------------------------------- The situation is that you have an existing guest operating system, and you want to _offline_ install packages, set the root password, inject files, set the hostname, or about a dozen other things. $ virt-customize -a fedora-22.img --install gcc,gdb --root-password password:123456 --edit /etc/selinux/config:s/^SELINUX=.*/SELINUX=permissive/ $ virt-cat -a fedora-22.img /etc/selinux/config Also works for a limited range of operations on Windows. On Windows, most work is done using "firstboot" batch files that run once at the next boot. 4000: Sysprepping a disk image ---------------------------------------------------------------------- SYSPREP.EXE is a Microsoft proprietary program for preparing a Windows system for duplication. It removes the identity of the system. We borrow the name of this program for the next tool, virt-sysprep, which does a similar job for Linux systems, turning them into templates from which you can clone further guests. This is a tool you'll probably either use all the time or never need to use at all. $ virt-sysprep -a fedora-22.img It runs on the guest in-place. It performs a serious of sysprepping operations, which you can read about in the manual. There are some operations which are not enabled by default: $ virt-sysprep --list-operations You'll want to read the manual before using this. This deletes the SSH host keys, because you wouldn't want those to be duplicated across clones. But it can't set up a fresh random seed for each clone. For that you need to set a random seed after the clone, using virt-customize. Virt-builder which I'll talk about next also sets a fresh random seed automatically. 5000: Building a disk image ---------------------------------------------------------------------- Show a diagram of the whole process and how the tools are related. preparing templates for virt-builder virt-install ---> sysprep ---> sparsify ---> compress ---> templates (OS installer) repository running virt-builder as an end user download a template ---> uncompress ---> resize/format ---> customize ---> result or a cloud image $ virt-builder -l $ virt-builder --notes rhel-7.1 $ virt-builder rhel-7.1 Speed is important! Any virt-customize options can be used: $ virt-builder rhel-7.1 --install gcc --selinux-relabel If you use the --size option, virt-resize is invoked which takes a bit longer: $ virt-builder rhel-7.1 --size 20G $ virt-df -a rhel-7.1.img -h virt-builder only builds disk images. It doesn't run them or upload them into glance. Other tools have been built around virt-builder.