DOC-24056

Embed Size (px)

Citation preview

  • 8/8/2019 DOC-24056

    1/7

    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 aphysical 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 articlecould 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

    https://www.redhat.com/support/service/sla/
  • 8/8/2019 DOC-24056

    2/7

    How to create a KVM virtual machine from a physical linux 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/).2

    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 63add 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

    http://kbase.redhat.com/faq/docs/DOC-17267http://kbase.redhat.com/faq/docs/DOC-17267http://kbase.redhat.com/faq/docs/DOC-2282http://kbase.redhat.com/faq/docs/DOC-2282
  • 8/8/2019 DOC-24056

    3/7

    How to create a KVM virtual machine from a physical linux 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/).3

    # cat > excluded_list.txt

  • 8/8/2019 DOC-24056

    4/7

    How to create a KVM virtual machine from a physical linux 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/).4

    /dev/vda2 / ext3 defaults 1 1

    ...

    # 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

  • 8/8/2019 DOC-24056

    5/7

    How to create a KVM virtual machine from a physical linux 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/).5

    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

  • 8/8/2019 DOC-24056

    6/7

    How to create a KVM virtual machine from a physical linux 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/).6

    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

  • 8/8/2019 DOC-24056

    7/7

    How to create a KVM virtual machine from a physical linux 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/).7

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

    ~rjones/virt-p2v

    http://people.redhat.com/~rjones/virt-p2vhttp://people.redhat.com/~rjones/virt-p2v