• No results found

Introducing virt-builder

In document Mastering KVM Virtualization.pdf (Page 125-131)

virt-builder is a command-line tool that creates disk images using cleanly prepared, digitally signed OS templates and customizes them to quickly build new virtual machines. virt-builder can build images for Fedora, Ubuntu, CentOS, Debian, and a few others.

This utility is provided by the libguestfs-tools-c package and can be installed by running the yum install libguestfs-tools-c -y command.

Chapter 4

Please note that virt-builder by default downloads OS templates from the http://libguestfs.org/download/builder/ repository; to access this repository Internet connectivity is mandatory. Without this, the tool will not work unless there is a local repository available. virt-builder's local repository creation is beyond the scope of this book. However, the procedure is well documented in the virt-builder man page.

For instance, if you want to create a CentOS 7.1 Guest with a 50 GB disk, using virt-builder this is as easy as running the following command:

# cd /var/lib/libvirt/qemu/ ; /usr/bin/virt-builder centos-7.1 --format raw --size 50G

[ 1.0] Downloading: http://libguestfs.org/download/builder/centos-7.1.xz

[ 2.0] Planning how to build this image [ 2.0] Uncompressing

[ 14.0] Resizing (using virt-resize) to expand the disk to 50.0G [ 149.0] Opening the new disk

[ 179.0] Setting a random seed [ 180.0] Setting passwords

virt-builder: Setting random password of root to Arw83LnDi66eMcmh [ 198.0] Finishing off Now enter the second command:

#virt-install --name centos --ram 1028 --vcpus=2 --disk path=/var/lib/

libvirt/qemu/centos-7.1.img --import

As you can see, it first downloaded the template, uncompressed it, resized the disk image to fit the given size, seeded data from the template to the image, customized it (set a random root password), and then finished. The resulting VM has no user accounts, has a random root password, and only uses the minimum amount of disk space required by the OS itself, but will grow up to 50 GB if needed.

The image is stored in the /var/lib/libvirt/qemu/ directory with centos-7.1.img as the name.

The second command—virt-install—just imported the image and created a virtual machine out of it.

Running virsh list --all will list the newly created virtual machine and

#virsh start <vmname> will start it. To log in as the root user use the random root password displayed in the output; your virtual machine is now ready.

In this example, root password is the only customization that is done but there are many other customizations that can be done—for example, installing software, setting the hostname, editing arbitrary files, creating users, and so on. To learn more about the possible customization that can be done for a guest, refer to the man page for virt-builder and #virt-builder --note <guest > as they list the kickstart and installation scripts used for that particular guest.

virt-builder caches the downloaded template in the current user's home directory.

The location of the cache is $XDG_CACHE_HOME/virt-builder/ or $HOME/.cache/

virt-builder.

You can print out information about the cache directory, including which guests are currently cached, by running the virt-builder --print-cache command:

# virt-builder --print-cache

Chapter 4

Here you can see that the centos-7.1 template is cached. The next time you create a centos-7.1 guest it will use the cached template and create the virtual machine even faster.

The cache can be deleted by running the following command to free up space:

#virt-builder --delete-cache

You can even download all (current) templates to the local cache by executing the virt-builder --cache-all-templates command.

Use the --verbose switch if you encounter any problems with virt-builder to produce verbose output.

While virt-builder is very fast, it only works with Linux guests. However, this utility is limited for Linux guests only and lacks Windows guest support; this is where the oz utility comes into the picture. If you want something more flexible, use oz.

Introducing oz

oz is another utility for creating Just Enough Operating System (JEOS) guests. It facilitates the automatic installation of operating systems with only minimal up-front input from the end user. The input for oz is a template (TDL format) which describes the instructions for creating the image:

• The ISO or URI on which the image will be based

• Disk size

• The extra packages to install

• The commands to execute after the image is created

• The files to inject after the image is created

It can automatically install a wide variety of OSes, including Windows. Under the hood it uses a set of predefined kickstart files for Red Hat-based systems, preseed files for Debian-based systems, and XML files that allow unattended Windows installs to automate the installation.

Currently, it supports the i386 and x86_64 architectures. The following is a list of OSes that it supports:

• Debian: 5, 6, 7

• Fedora Core: 1, 2, 3, 4, 5, 6

• Fedora: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21

• FreeBSD: 10

• Mageia: 4

• Mandrake: 8.2, 9.1, 9.2, 10.0, 10.1

• Mandriva: 2005, 2006.0, 2007.0, 2008.0

• OpenSUSE: 10.3, 11.0, 11.1, 11.2, 11.3, 11.4, 12.1, 12.2, 12.3, 13.1

• RHEL 2.1: GOLD, U2, U3, U4, U5, U6

• RHEL 7: Beta, 0

• RHEL/CentOS 3: GOLD, U1, U2, U3, U4, U5, U6, U7, U8, U9

• RHEL/CentOS/Scientific Linux 4: GOLD, U1, U2, U3, U4, U5, U6, U7, U8, U9

• RHEL/OL/CentOS/Scientific Linux{,CERN} 5: GOLD, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11

• RHEL/OL/CentOS/Scientific Linux{,CERN} 6: 0, 1, 2, 3, 4, 5

• RHL: 7.0, 7.1, 7.2, 7.3, 8, 9

• Ubuntu: 5.04, 5.10, 6.06[.1,.2], 6.10, 7.04, 7.10, 8.04[.1,.2,.3,.4], 8.10, 9.04, 9.10, 10.04[.1,.2,.3], 10.10, 11.04, 11.10, 12.04[.1,.2,.3,.4,.5], 12.10, 13.04, 13.10, 14.04[.1], 14.10

• Windows: 2000, XP, 2003, 7, 2008, 2012, 8, 8.1

Chapter 4 The procedure for creating a virtual machine using oz is as follows:

1. Install the oz and libguestfs-tools packages using the following command:

#yum install -y oz libguestfs-tools

2. Get the ISO media of the desired operating system that you wish to install using oz. For Linux guests, a network-based installation tree exposed over HTTP can also be used. For example:

° For Fedora 22: http://dl.fedoraproject.org/pub/fedora/

linux/releases/22/Server/x86_64/os/

° For CentOS 7: http://mirrors.dcarsat.com.ar/centos/7/os/

x86_64/

3. Create a simple TDL (Template Definition Language) file. All the supported attributes for a TDL file can be found here: https://github.com/

clalancette/oz/wiki/Oz-template-description-language 4. Run the oz-install command to build an image:

#oz-install -u -d3 TDL_FILE_PATH Syntax:

• u: After installation, perform the customization

• d: Turn up the logging level. The levels are:

° 0: Errors only (this is the default) ° 1: Errors and warnings

° 2: Errors, warnings, and information ° 3: All messages

° 4: All messages, prepended with the level and classname This will result in a libvirt XML file (containing the image path and other parameters), which you can use to immediately boot the guest:

virsh define <xml_fike>

virsh start <vm_name>

In document Mastering KVM Virtualization.pdf (Page 125-131)