kvm中的base_image妙用

来源:互联网 发布:火狐量子浏览器 知乎 编辑:程序博客网 时间:2024/06/01 08:54


转载自http://www.linux-kvm.com/content/be-more-productive-base-images-part-1[23]

There are some useful ways in which you can use qemu base images to build template virtual machines. If used wisely you can save yourself from ever having to install the same OS more than once. The main thing that you should be aware of when using base images is the dependency between images but once managed properly, you can save yourself a lot of time.


Qemu/KVM base images

Base imaging was inherited from the qemu userspace component as an added feature of kvm. The idea behind the base image is simple. You build a disk image as you would build a normal image. You then build a new image using the first image as your base image, also known as the backing file. From this point on, kvm will only read from your base image (backing file). If anything needs to be written to your image, kvm will only write to the new image. It is, in effect, similar to a diff where all new data is written only to the new image while preserving the state of your backing file. As a sideffect, your working image will be the same size as your base image. This is where the qemu copy on write term came from.


Building your base image

Your base image should be based on a template disk image that is a practical starting point for your needs. Personally I like to start with a base image that is at a state right after a fresh install; no patches, no applications, just the state of the machine right after installation and registration of the OS if it requires registration. You would start by using the usual qemu-img command as you normally would for creating your base image. For example, to install your base image for a windows xp base image you would create the image with the following command.

qemu-img create -f qcow2 windows-master.qcow2 10G

This image will be your base image. Once your image is created you boot the image and install the OS as you normally would with a command similar to the following:

qemu-system-x86_64 -hda windows-master.qcow2 -m 512 -boot d -cdrom /home/user/isos/en_winxp_pro_with_sp2.iso

Install your OS to completion and back up this image immediately. This is your new base image and you will not have to ever boot this image in life again.


Create a new image from your template base image

Now that you have your base image you are ready to install a new image from this template. You would create your new image with the following command using the –b flag.

qemu-img create -b windows-master.qcow2 -f qcow2 windows-clone.qcow2

This will create a new copy-on-write image called windows-clone.qcow2 which uses your original windows-master as your template base image. You don’t need to specify a filesize with this command as it will automatically set your new image to the same size as your base image. At this point run the following command on your new image and note the size of your new image.

# qemu-img info windows-clone.qcow2

image: windows-clone.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 56K
cluster_size: 4096
backing file: windows-master.qcow2 (actual path: windows-master.qcow2)

Note the size of your new image is 10G , the size of your original base image and note how small your image is, 56 k. At this point, your new image is basically a clone of your base image and dependent on your base image being in the same state it was at the time your clone was created. For this reason, you should try to never boot your base image again as some data may be inadvertently be written to disk causing some inconsistencies. Boot up your new clone image with a command similar to the following

qemu-system-x86_64 -hda windows-clone.qcow2 -m 400

You new clone will boot up and you will see that it is a perfect clone of your original base image. As you install patches, applications and write data to your new image you can see it growing in size but your base image will always remain unchanged from it’s original state. You can use the qemu-img command to verify this. Try installing some applications on your new clone then run the following commands on both your base image and your new clone.

# qemu-img info windows-master.qcow2
# qemu-img info windows-clone.qcow2

You will see that your clone image is growing and your base image has remained the same size.


What next?

Using base images , you should never have to install your operating systems again after the first install. Perform your experiments on your new clone; if something goes wrong, simply delete your clone and recreate another clone. You can also have multiple clones running off of one base image without any problems.

Now that you know how to build a base image and images hanging off of your base image, the next thing that you will want to do is “rebasing” your images. For the next post in this series, I’ll walk through how you can rebase your new image to build new up to date base images for cloning.



In the first part of this series about using base images to be more productive, I discussed how you can use base images to build template disk images from which you can create copy on write clones. As a natural follow up to creating your clones, at some point you’ll want to update your base images with patches or applications that is a better starting point for a clone. Regardless of how well you planned your base images, you may find that you need to update your base images to minimize additional installation on your clones. You can achieve this by “rebasing” your disk images.


What is Rebasing?

Simply put, rebasing is the process of merging your current clone with your base image to create a new base image file that is completely different and independent from your original base image. You new base image will not have any dependency on your original base image and can be used for creating new clones. I think it’s good idea to always keep a base image with a pure install and additional rebased images for different purposes. For example, in a desktop environment one rebased image can be used for marketing department, another for customer service etc all built by merging different clones with an original single install base image. Rebasing can save you loads of time.


Creating your first rebased image

Now that you understand what a rebased image is, let’s create one. I’ll use the same image names from the first part of the series for continuity. In order to rebase your image you only need to issue one single command. The command follows.

qemu-img convert windows-clone.qcow2 –O qcow2 windows-marketing.qcow2

That’s it. Your windows-marketing.qcow2 will be a new base image completely independent from the original base image (windows-master.qcow2) used to create windows-clone.qcow2. It will be a merge of the windows-clone.qcow2 and the original windows-master.qcow2 images from the original post. This means that even if you destroy your windows-master.qcow2, your new rebased image will not be affected since there’s no dependency.


Summary

You now know what a rebased image is and how to create one. It only involves running a single command. Rebasing images can save you time by allowing you to create repurposed base images that you can use to deploy quickly and easily. In the next and last part of the series , I’ll explore another option recently added to qemu-img and how it might be used. 



There’s a lesser known option that was added fairly recently to kvm-71 as an addition to the qemu-img command. It allows you to create a copy on write image while using the convert option. This option will probably not be as useful as basing images and rebasing images but it should at least be an option that you’re aware of. It’s a new b argument for creating copy on write images from base images while converting formats.


The syntax 

The syntax for the new argument when using the convert parameter with qemu-img is as follows:

qemu-img convert -O qcow2 -B master-windows2003-base.qcow2 master-windows2003.qcow2 final.qcow2 

Note the –B argument which tells qemu-img that the newly created image (final.qcow2) will be a copy on write image of the specified base image (master-windows2003-base.qcow2). What you’re basically saying is convert master-windows2003.qcow2 to final.qcow2 and let this converted image be a copy on write of the image specified by the –B argument. This essentially means that the master-windows2003.qcow2 and master-windows2003-base.qcow2 should have the same content but they may differ in location, format etc.


If you do a qemu-img info you’re your final.qcow2 image, it looks like the following.

qemu-img info final.qcow2

image: final.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.2G
cluster_size: 4096
backing file: master-windows2003-base.qcow2 (actual path: master-windows2003-base.qcow2)

Note that unlike copy on write images as described previously, these copy on write images are actually the same size as the backing file and actually take some time to convert.


Summary 

This concludes the short series on base images. In Part 1 and Part 2 of this series you’ve seen how to create images from base templates and rebasing your disk images. In this post, you’ve seen the new optional argument for the qemu-img command and how it might be used. You’ll probably not find yourself using this option very much but I think it’s always good to know your options. Personally, I can’t think of how I might use this option very much as it doesn’t really save much disk space or time and in that case I would probably opt for just a straight conversion. In any case now you know.