Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
For more information about creating a disk image, refer the How can I create sparse files
under Red Hat Enterprise Linux?
Then, need to partition, format and mount the new partitions for this virtual machine. To do
so, use a lookback device as shown below:
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?
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
For more information about rsync options, refer its manual page.
5. Configure GRUB and system configuration files for 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?
# 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.
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.
Run the mkinitrd command with "--with" options because it could not access the /sys/block
directory after running the chroot command.
# 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?
# 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?
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.
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