7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VMware から KVM へのコンバート(CentOS5)

Last updated at Posted at 2014-10-27

VMware から KVM への移行にあたって
CentOS5のconvertはCentOS6より手間がかかったので記録

  • Host OSはCentOS6
  • Host OSはLVMを使用していない
  • Guest OSがCentOS5.6 (kernel-2.6.18-274)
  • Guest OSでLVMを使用している
  • image形式はrawなのでそのまま使う
  • MACは手動で割当

VMwareからのイメージの取得手順は省きます。
必要なのはimagefile.vmdk
imagefile.vmx はVMware用の構成ファイルなのでKVMでは使わない。

HOST OSの作成方法も省きます。

イメージのマウント

# losetup -f (空きloopback deviceを確認)
 /dev/loop0

# mv imagefile.vmdk imagefile.img
# losetup /dev/loop0 imagefile.img
# losetup -a 
 /dev/loop0: [0803]:15 (/DATA/imagefile.img)

# kpartx -a /dev/loop0 && vgchange -ay && lvscan 

# ls -l /dev/mapper/
 total 0
 crw-rw----  1 root root 10, 58 Jul  9 16:16 control
 lrwxrwxrwx  1 root root      7 Jul 10 11:07 loop0p1 -> ../dm-0
 lrwxrwxrwx  1 root root      7 Jul 10 11:07 loop0p2 -> ../dm-1
 lrwxrwxrwx  1 root root      7 Jul 10 11:07 VolGroup00 -> ../dm-2
 lrwxrwxrwx  1 root root      7 Jul 10 11:07 VolGroup01 -> ../dm-3

# fdisk -l (サイズあたりで中身を判断)

Disk /dev/mapper/loop0p1: 106 MB, 106896384 bytes
255 heads, 63 sectors/track, 12 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/loop0p2: 53.6 GB, 53579473920 bytes
255 heads, 63 sectors/track, 6514 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup00: 47.3 GB, 47311749120 bytes
255 heads, 63 sectors/track, 5751 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup01: 6241 MB, 6241124352 bytes
255 heads, 63 sectors/track, 758 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x30307800

# mount /dev/mapper/VolGroup00 /mnt
# mount /dev/mapper/loop0p1 /mnt/boot
# mount --bind /dev /mnt/dev
# mount --bind /sys /mnt/sys
# mount --bind /proc /mnt/proc

確認
# df
 Filesystem           1K-blocks      Used Available Use% Mounted on
 /dev/mapper/VolGroup00
                      44755372  15522320  26922908  37% /mnt
 /dev/mapper/loop0p1    101086     23562     72305  25% /mnt/boot

# mount
 /dev/mapper/VolGroup00-LogVol00 on /mnt type ext3 (rw)
 /dev/mapper/loop2p1 on /mnt/boot type ext3 (rw)
 /dev on /mnt/dev type none (rw,bind)
 /sys on /mnt/sys type none (rw,bind)
 /proc on /mnt/proc type none (rw,bind)

各ファイル編集

# vi /mnt/etc/sysconfig/network-scripts/ifcfg-eth0  MACアドレスやらIPアドレスやら修正
# vi /mnt/etc/sysconfig/network-scripts/ifcfg-eth1  MACアドレスやらIPアドレスやら修正
# vi /mnt/etc/sysconfig/network   GateWayやら修正

その他のconf等も変える必要があれば変更

KVM Console用設定

起動後に $ virsh conxole imagename でログインしたいので
http://aikotobaha.blogspot.jp/2010/08/rhel-kvm-2.html もやる

# vi /mnt/boot/grub/grub.conf
---------
hiddenmenu
serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1 [追記]
title Red Hat Enterprise Linux Server (2.6.18-194.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00 console=ttyS0,115200n8 [追記]
---------

# vi /mnt/etc/inittab
---------
# Run gettys in standard runlevels
 S0:12345:respawn:/sbin/agetty ttyS0 115200 [追記]
---------

virtIO用にkernel 再コンパイル

# cp -fp /mnt/etc/modprobe.conf /mnt/etc/.modprobe.conf.org
# sed -i '4i alias scsi_hostadapter1 virtio_blk' /mnt/etc/modprobe.conf

# chroot /mnt
# export _KERNEL=`ls -1 /boot/*img | sed -e's|/boot/initrd-||' -e 's|.img||'`
# echo $_KERNEL
 2.6.XX-XXX.el5

# cp -ip /boot/initrd-${_KERNEL}.img /boot/initrd-${_KERNEL}.img.org
# mkinitrd --with=virtio_pci --with=virtio_blk -v -f /boot/initrd-${_KERNEL}.img ${_KERNEL}
# exit

アンマウント

# umount /mnt/boot /mnt/sys /mnt/dev /mnt/proc
# umount /mnt
# vgchange -an && kpartx -d /dev/loop0 && losetup -d /dev/loop0 && losetup -f

KVM用設定ファイルの編集

# uuidgen
# vi /etc/libvirt/qemu/imagefile.xml 適当なコピーから改変。☆部分
---------
<domain type='kvm'>
  <name>imagefilename</name> ☆
  <uuid>uuidgen で作成したもの</uuid> ☆
  <memory unit='KiB'>2097152</memory> ☆
  <vcpu placement='static'>4</vcpu> ☆
  <os>
    <type arch='x86_64' machine='rhel5.5.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <source file='/path/imagefilename.img'/> ☆
      <target dev='vda' bus='virtio'/> ☆
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </disk>
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='MAC_eth0'/> ☆
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <interface type='bridge'>
      <mac address='MAC_eth1'/> ☆
      <source bridge='br1'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
  </devices>
</domain>
---------

# virsh define /etc/libvirt/qemu/imagefile.xml 
# virsh list --all
# virsh start

うまくいけばこれで完成。

MACアドレスをudevで認識できず
ifcfg-eth[01]がリネームされてネットワークが起動しないことがある。

/etc/sysconfig/network-scripts/ifcfg-eth0
↓ 勝手にリネーム
/mnt/etc/sysconfig/network-scripts/ifcfg-eth0.bak

そんなときは、もう一度リネームして再起動すると直る。

Host# virsh console imagefile
 Connected to domain pd1-azfront
 Escape character is ^]
 imagefile login: Guest OS に ログイン

Guest# cd /mnt/etc/sysconfig/network-scripts/
Guest# mv ifcfg-eth0.bak  ifcfg-eth0
Guest# shutdown -h now

ctl+]でぬける

Host# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     imagefile                     shut off

Host# virsh start imagefile
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?