Вы находитесь на странице: 1из 7

How to create a KVM virtual machine from a

physical linux machine?

Issue
• How to create a KVM virtual machine from a physical linux machine?
• How to migrate from my physical machine to the virtual machine (P2V) ?

Environment
• Red Hat Enterprise Linux 5.4
• KVM hypervisor

Resolution

Warning : The following information is outside the scope of Red Hat posted Service Level
Agreements ( https://www.redhat.com/support/service/sla/ ) and support procedures. Any
configuration settings or installed applications made from the information in this article
could make your Operating System unsupported by Red Hat Support Services. The intent
of this article is to provide you with information to accomplish your system needs. Use the
information in this article at your own risk.

Assumption : This article assume that the virtual machine will use para-virtualized device
drivers under the KVM hypervisor.

1. Create a disk image for a virtual machine

Copyright (c) 2009 by Red Hat, Inc. This material may be distributed only subject to the terms and conditions set
forth in the Open Publication License, v1.0 or later (available at http://www.opencontent.org/openpub/).
1
How to create a KVM virtual machine from a physical linux machine?

First, create a disk image to replace your physical machine. The both disk sizes don't need
to match exactly, as long as there is plenty of room to shore all the files from the physical
machine.

# dd of=/var/lib/libvirt/images/p2v-example.img bs=1M count=0 seek=10240


In this example, the image file is about 10GiB in size.

For more information about creating a disk image, refer the How can I create sparse files
under Red Hat Enterprise Linux?

2. Partition the virtual machine's disk

Then, need to partition, format and mount the new partitions for this virtual machine. To do
so, use a lookback device as shown below:

# losetup /dev/loop1 p2v-example.img


# fdisk /dev/loop1
# fdisk -ul /dev/loop1
# kpartx -av /dev/loop1
add map loop1p1 : 0 1012032 linear /dev/loop1 63
add map loop1p2 : 0 19952730 linear /dev/loop1 1012095
# mkswap /dev/mapper/loop1p1
# mkfs.ext3 /dev/mapper/loop1p2
Two partitions are now ready to be used as a swap and a root filesystem.

For more information, refer the Using Red Hat Enterprise Linux virtualization, how do I
mount a partition on a file-based storage?

3. Make a list containing excluded files and directories when the synchronization

Copyright (c) 2009 by Red Hat, Inc. This material may be distributed only subject to the terms and conditions set
forth in the Open Publication License, v1.0 or later (available at http://www.opencontent.org/openpub/).
2
How to create a KVM virtual machine from a physical linux machine?

# cat > excluded_list.txt << EOF


proc/*
tmp/*
lost+found/
etc/mtab
sys/*
EOF
If there are directories and files you don't want to synchronize, just insert them to that file.

4. Synchronize the file systems in both the physical machine and the virtual machine

It needs to be able to access SSH on the physical machine. To reduce the chance of data
changing during the synchronizing task, make sure to run the physical machine in off-line or
shundown as many services as possible.

# mkdir /mnt/p2v
# mount /dev/mapper/loop1p2 /mnt/p2v

# rsync -av -e ssh -S -H -W -D --exclude-from=excluded_list.txt root@physicalhost:/ /mnt/p2v

For more information about rsync options, refer its manual page.

5. Configure GRUB and system configuration files for the virtual machine

Run chroot command on the virtual machine:

# chroot /mnt/p2v

To fit the new virtual environment, need to modify several configuration files, such as hosts
file, ifcfg-* files, and passwd file, in the virtual machine. Here are two examples:

# vi /etc/fstab
/dev/vda1 swap swap defaults 00

Copyright (c) 2009 by Red Hat, Inc. This material may be distributed only subject to the terms and conditions set
forth in the Open Publication License, v1.0 or later (available at http://www.opencontent.org/openpub/).
3
How to create a KVM virtual machine from a physical linux machine?

/dev/vda2 / ext3 defaults 11


...

# vi /boot/grub/grub.conf
...
splashimage=(hd0,1)/boot/grub/splash.xpm.gz
...
title Red Hat Enterprise Linux Server (2.6.18-164.2.1.el5)
root (hd0,1)
kernel /boot/vmlinuz-2.6.18-164.el5 ro root=/dev/vda2 rhgb selinux=0 3
initrd /boot/initrd-2.6.18-164.el5.img
....
The block device "vda" is a device provided by KVM hypervisor with para-virtualized device
drivers.

6. Rebuild the initial ram disk image

There are basically no kernel modules for para-virtualized devices in the initail ram disk
image provided by Red Hat Enterprise Linux. To use para-virtualized block and network
devices, you should rebuild that image with the modules.

# cat > /etc/modprobe.conf << EOF


alias eth0 virtio_net
alias scsi_hostadapter ata_piix
alias scsi_hostadapter1 virtio_blk
EOF

Run the mkinitrd command with "--with" options because it could not access the /sys/block
directory after running the chroot command.

# mkinitrd -v -f initrd-$(uname -r).img $(uname -r) \


--with=virtio_blk --with=virtio_net --with=virtio_pci

Now, escape from a root partition in the virtual machine:

# exit

Copyright (c) 2009 by Red Hat, Inc. This material may be distributed only subject to the terms and conditions set
forth in the Open Publication License, v1.0 or later (available at http://www.opencontent.org/openpub/).
4
How to create a KVM virtual machine from a physical linux machine?

7. Install GRUB into the virtual machine's disk

# umount /mnt/pv2
# kpartx -d /dev/loop1
# losetup -d /dev/loop1

# grub --device-map=/dev/null
grub> device (hd0) /var/lib/libvirt/images/p2v-example.img
grub> root (hd0,1)
grub> setup (hd0)
grub> quit

For detail information, refer the How to install GRUB on a disk image file?

8. Create the virtual machine's difinition file

# cat > /etc/libvirt/qemu/p2v-example.xml << EOF


<domain type='kvm'>
<name>p2v-example</name>
<memory>524288</memory>
<currentMemory>524288</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch='x86_64' machine='pc-0.11'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/bin/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>

Copyright (c) 2009 by Red Hat, Inc. This material may be distributed only subject to the terms and conditions set
forth in the Open Publication License, v1.0 or later (available at http://www.opencontent.org/openpub/).
5
How to create a KVM virtual machine from a physical linux machine?

<source file='/var/lib/libvirt/images/p2v-example.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='network'>
<source network='default'/>
<model type='virtio'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/5'/>
<target port='0'/>
</serial>
<console type='pty' tty='/dev/pts/5'>
<source path='/dev/pts/5'/>
<target port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>
<sound model='es1370'/>
<video>
<model type='cirrus' vram='9216' heads='1'/>
</video>
</devices>
</domain>
EOF

This machine is going to use a display device emulated to cirrus, a sound device emulated
to es1370, and para-virtualized block device and network device.

9. change SELinux contexts

# chcon -R system_u:object_r:virt_etc_rw_t:s0 /etc/libvirt/qemu/p2v-example.xml


# chcon -R system_u:object_r:virt_image_t:s0 /var/lib/libvirt/images/p2v-example.img

10. start the virtual machine

# service libvirtd reload


# virsh start p2v-example

Copyright (c) 2009 by Red Hat, Inc. This material may be distributed only subject to the terms and conditions set
forth in the Open Publication License, v1.0 or later (available at http://www.opencontent.org/openpub/).
6
How to create a KVM virtual machine from a physical linux machine?

Note: There is a project to develop a tool for P2V, visit to the http://people.redhat.com/
~rjones/virt-p2v

Copyright (c) 2009 by Red Hat, Inc. This material may be distributed only subject to the terms and conditions set
forth in the Open Publication License, v1.0 or later (available at http://www.opencontent.org/openpub/).
7

Вам также может понравиться