image Modify for kvm , openstack

4. Modifying images

guestfish.......................................................................................................................11

guestmount...................................................................................................................12

virt-*tools.....................................................................................................................13

Loopdevices, kpartx, network block devices.................................................................. 14

Onceyou have obtained a virtual machine image, you may want to make some changes

toit before uploading it to the OpenStack Image service. Here we describe severaltools

availablethat allow you to modify images.

Warning

Donot attempt to use these tools to modify an image that is attached to a

runningvirtual machine. These tools are designed to only modify images that

arenot currently running.

guestfish

Theguestfish program is a tool from the libguestfs project that allows you to modify the

filesinside of a virtual machine image.

Notethat guestfish doesn't mount the image directly into the local filesystem.Instead, it

providesyou with a shell interface that allows you to view, edit, and delete files.Many of

theguestfish commands (e.g., touch, chmod, rm) are similar totraditional bash commands.

Example guestfish session

Weoften need to modify a virtual machine image to remove any traces of the MAC

addressthat was assigned to the virtual network interface card when the image was

firstcreated, since the MAC address will be different when it boots the next time.In this

example,we show how we can use guestfish to remove references to the old MAC address

bydeleting the /etc/udev/rules.d/70-persistent-net.rules file and removing

theHWADDR line from the /etc/sysconfig/network-scripts/ifcfg-eth0 file.

Assumewe have a CentOS qcow2 image called centos63_desktop.img. We would

mountthe image in read-write mode by doing, as root:

#guestfish --rw -a centos63_desktop.img

Welcometo guestfish, the libguestfs filesystem interactive shell for

editingvirtual machine filesystems.

Type:'help' for help on commands

'man'to read the manual

'quit'to quit the shell

><fs>

Thisstarts a guestfish session. Note that the guestfish prompt looks like a fish: ><fs>.

ComputeVM Image Guide Sep 6, 2013 current

12

Wemust first use the run command at the guestfish prompt before we can doanything

else.This will launch a virtual machine, which will be used to perform all of thefile

manipulations.

><fs>run

Wecan now view the filesystems in the image using the list-filesystems command:

><fs>list-filesystems

/dev/vda1:ext4

/dev/vg_centosbase/lv_root:ext4

/dev/vg_centosbase/lv_swap:swap

Weneed to mount the logical volume that contains the root partition:

><fs>mount /dev/vg_centosbase/lv_root /

Next,we want to delete a file. We can use the rm guestfish command, whichworks the

sameway it does in a traditional shell.

><fs>rm /etc/udev/rules.d/70-persistent-net.rules

Wewant to edit the ifcfg-eth0 file to remove the HWADDR line. The edit commandwill

copythe file to the host, invoke your editor, and then copy the file back.

><fs>edit /etc/sysconfig/network-scripts/ifcfg-eth0

Le'ssay we want to modify this image to load the 8021q kernel at boot time. We'llneed to

createan executable script in the /etc/sysconfig/modules/ directory. We can use the

touchguestfish command to create an empty file,use the edit command to edit it, and use

thechmod command to make it executable.

><fs>touch /etc/sysconfig/modules/8021q.modules

><fs>edit /etc/sysconfig/modules/8021q.modules

Weadd the following line to the file and save it

modprobe8021q

Thenwe set to executable:

><fs>chmod 0755 /etc/sysconfig/modules/8021q.modules

We'redone, so we can exit using the exit command:

><fs>exit

Going further with guestfish

Thereis an enormous amount of functionality in guestfish and a full treatment isbeyond

thescope of this document. Instead, we recommend that you read the guestfs-recipes

documentationpage for a sense of what is possible with these tools.

guestmount

Forsome types of changes, you may find it easier to mount the image's filesystemdirectly

inthe guest. The guestmount program, also from the libguestfs project,allows you to do

so.

ComputeVM Image Guide Sep 6, 2013 current

13

Forexample, to mount the root partition from our centos63_desktop.qcow2 image to

/mnt,we can do:

#guestmount -a centos63_desktop.qcow2 -m /dev/vg_centosbase/lv_root --rw /mnt

Ifwe didn't know in advance what the mountpoint is in the guest, we could use the-

i(inspect)flag to tell guestmount to automatically determine what mount point to use:

#guestmount -a centos63_desktop.qcow2 -i --rw /mnt

Oncemounted, we could do things like list the installed packages using rpm:

#rpm -qa --dbpath /mnt/var/lib/rpm

Oncedone, we unmount:

#umount /mnt

virt-* tools

Thelibguestfs project has a number of other useful tools,including:

virt-df for displaying free space inside of an image.

virt-resize for resizing an image.

virt-sysprep for preparing an image for distribution(e.g., delete SSH host keys, remove

MACaddress info, remove user accounts).

virt-sparsify for making an image sparse

virt-p2v for converting a physical machine to an imagethat runs on KVM

virt-v2v for converting Xen and VMWare images to KVMimages

Resize an image

Here'sa simple of example of how to use virt-resize to resize an image. Assumewe have a

16GBWindows image in qcow2 format that we want to resize to 50GB. First, we use virtfilesystems

toidentify the partitions:

#virt-filesystems --long --parts --blkdevs -h -a /data/images/win2012.qcow2

NameType MBR Size Parent

/dev/sda1partition 07 350M /dev/sda

/dev/sda2partition 07 16G /dev/sda

/dev/sdadevice - 16G -

Inthis case, it's the /dev/sda2 partition that we want to resize. We create a newqcow2

imageand use the virt-resize command to write a resized copy of the originalinto the new

image

#qemu-img create -f qcow2 /data/images/win2012-50gb.qcw2 50G

#virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 /data/images/

win2012-50gb.qcow2

Examining/data/images/win2012.qcow2 ...

ComputeVM Image Guide Sep 6, 2013 current

14

**********

Summaryof changes:

/dev/sda1:This partition will be left alone.

/dev/sda2:This partition will be resized from 15.7G to 49.7G. The

filesystemntfs on /dev/sda2 will be expanded using the

'ntfsresize'method.

**********

Settingup initial partition table on /data/images/win2012-50gb.qcow2 ...

Copying/dev/sda1 ...

100%###################################################################

00:00

Copying/dev/sda2 ...

100%###################################################################

00:00

Expanding/dev/sda2 using the 'ntfsresize' method ...

Resizeoperation completed with no errors. Before deleting the old

disk,carefully check that the resized disk boots and works correctly.

Loop devices, kpartx, network block devices

Ifyou don't have access to libguestfs, you can mount image file systems directlyin the host

usingloop devices, kpartx, and network block devices.

Warning

Mountinguntrusted guest images using the tools described in this section is a

securityrisk, always use libguestfs tools such as guestfish and guestmount if you

haveaccess to them. See Areminder why you should never mount guest disk

imageson the host OS byDaniel Berrangé for more details.

Mounting a raw image (without LVM)

Ifyou have a raw virtual machine image that is not using LVM to manage itspartitions.

First,use the losetup command to find an unused loop device.

#losetup -f

/dev/loop0

Inthis example, /dev/loop0 is free. Associate a loop device with the raw image:

#losetup /dev/loop0 fedora17.img

Ifthe image only has a single partition, you can mount the loop device directly:

#mount /dev/loop0 /mnt

Ifthe image has multiple partitions, use kpartx to expose the partitionsas separate devices

(e.g.,/dev/mapper/loop0p1), then mount the partition that corresponds to the rootfile

system:

#kpartx -av /dev/loop0

ComputeVM Image Guide Sep 6, 2013 current

15

Ifthe image has, say three partitions (/boot, /, /swap), there should be one newdevice

createdper partition:

$ls -l /dev/mapper/loop0p*

brw-rw----1 root disk 43, 49 2012-03-05 15:32 /dev/mapper/loop0p1

brw-rw----1 root disk 43, 50 2012-03-05 15:32 /dev/mapper/loop0p2

brw-rw----1 root disk 43, 51 2012-03-05 15:32 /dev/mapper/loop0p3

Tomount the second partition, as root:

#mkdir /mnt/image

#mount /dev/mapper/loop0p2 /mnt

Onceyou're done, to clean up:

#umount /mnt

#kpartx -d /dev/loop0

#losetup -d /dev/loop0

Mounting a raw image (with LVM)

Ifyour partitions are managed with LVM, use losetup and kpartx as in the previous

exampleto expose the partitions to the host:

#losetup -f

/dev/loop0

#losetup /dev/loop0 rhel62.img

#kpartx -av /dev/loop0

Next,you need to use the vgscan command to identify the LVM volume groups andthen

vgchangeto expose the volumes as devices:

#vgscan

Readingall physical volumes. This may take a while...

Foundvolume group "vg_rhel62x8664" using metadata type lvm2

#vgchange -ay

2logical volume(s) in volume group "vg_rhel62x8664" now active

#mount /dev/vg_rhel62x8664/lv_root /mnt

Cleanup when you're done:

#umount /mnt

#vgchange -an vg_rhel62x8664

#kpartx -d /dev/loop0

#losetup -d /dev/loop0

Mounting a qcow2 image (without LVM)

Youneed the nbd (network block device) kernel module loaded to mount qcow2 images.

Thiswill load it with support for 16 block devices, which is fine for our purposes.As root:

#modprobe nbd max_part=16

Assumingthe first block device (/dev/nbd0) is not currently in use, we can expose thedisk

partitionsusing the qemu-nbd and partprobe commands. As root:

#qemu-nbd -c /dev/nbd0 image.qcow2

#partprobe /dev/nbd0

ComputeVM Image Guide Sep 6, 2013 current

16

Ifthe image has, say three partitions (/boot, /, /swap), there should be one newdevice

createdper partition:

$ls -l /dev/nbd3*

brw-rw----1 root disk 43, 48 2012-03-05 15:32 /dev/nbd0

brw-rw----1 root disk 43, 49 2012-03-05 15:32 /dev/nbd0p1

brw-rw----1 root disk 43, 50 2012-03-05 15:32 /dev/nbd0p2

brw-rw----1 root disk 43, 51 2012-03-05 15:32 /dev/nbd0p3

Note

Ifthe network block device you selected was already in use, the initial qemunbd

commandwill fail silently, and the /dev/nbd3p{1,2,3} device files will

notbe created.

Ifthe image partitions are not managed with LVM, they can be mounted directly:

#mkdir /mnt/image

#mount /dev/nbd3p2 /mnt

Whenyou're done, clean up:

#umount /mnt

#qemu-nbd -d /dev/g nbd0

Mounting a qcow2 image (with LVM)

Ifthe image partitions are managed with LVM, after you use qemu-nbd and partprobe,

youmust use vgscan and vgchange -ay in order to expose the LVMpartitions as devices

thatcan be mounted:

#modprobe nbd max_part=16

#qemu-nbd -c /dev/nbd0 image.qcow2

#partprobe /dev/nbd0# vgscan

Readingall physical volumes. This may take a while...

Foundvolume group "vg_rhel62x8664" using metadata type lvm2

#vgchange -ay

2logical volume(s) in volume group "vg_rhel62x8664" now active

#mount /dev/vg_rhel62x8664/lv_root /mnt

Whenyou're done, clean up:

#umount /mnt

#vgchange -an vg_rhel62x8664

#qemu-nbd -d /dev/nbd0



转载URL: http://docs.openstack.org/grizzly/openstack-image/content/centos-image.html

你可能感兴趣的:(image,qemu,openstack,guestfish,guestmount,virsh-*)