10. kvm - p2v

最後更新: 2019-05-29

目錄

  • P2V - Linux To KVM

 


P2V - Linux To KVM

 

Test with

 * CentOS 6

*** Both Side ***

* Sync Time First

ntpdate stdtime.gov.hk

Source(Phyiscal):

Step1: 為 eth0 增加多一個 LAN IP

ifconfig eth0:1 192.168.234.1 netmask 255.255.255.252

Step2: 設定 rsync

# 看看原本有沒有 rsync 服務

netstat -ntlp | grep 873

沒有的話, 就開始設定 rsync

# Install rsync

yum install rsync

            OR

rpm -Uvh rsync-3.1.1-1.el5.rfx.i386.rpm

rsync --version

rsync  version 3.1.1  protocol version 31

 * 如果 rsync 太舊, 就自行 compile 啦

/etc/rsyncd-p2v.conf

uid = root
gid = root
list = no
port = 8888
[rootfs]
    path = /
    read only = yes
    hosts allow = 192.168.234.2
    hosts deny = *

# Start Service

rsync --config=/etc/rsyncd-p2v.conf --daemon

# Firewall setting

iptables -I INPUT -p tcp --dport 8888 -j ACCEPT

# Check Local rsync service

netstat -ntlp | grep 8888

iptables -nL | grep 8888

# Remote Test

rsync rsync://192.168.234.1:8888

Step3: Allow FW

iptables -I INPUT -m tcp -p tcp -s 192.168.234.2 --dport 8888 -j ACCEPT

Step4: File for test rsync

echo testing >> /root/testing.txt

 

Destitation:

Step 0: boot with LiveCD

Boot with LiveCD(sysresccd)

Step 1: Create new partition

# 用 parted 建立好所需的 Partition

parted /dev/sda

mklabel msdos

mkpart pri ext3 0% 200MB

mkpart pri linux-swap 200MB 2500MB

mkpart pri ext3 2500MB 100%

print

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  200MB   199MB   primary
 2      200MB   2500MB  2300MB  primary
 3      2500MB  42.9GB  40.4GB  primary

mkfs.ext? /dev/sda1

mkfs.ext? /dev/sda3

mkswa? /dev/sda2

Step 2: Config network & ssh

# Disable NetworkManager

/etc/init.d/NetworkManager stop

# Enable network

ip link

ifconfig eth0 192.168.234.2

route add route gw x.x.x.x

# set root password

passwd

# Enable ssh

ssh-keygen -A

setenv.sh

echo "vbell off" > /root/.screenrc
echo "set bg=dark" > /root/.vimrc
ifconfig eth0 192.168.18.4
route add default gw 192.168.18.254
echo "nameservers 8.8.8.8" >> /etc/resolv.conf
echo 'root:888888' | chpasswd
ssh-keygen -A

Step 3: mount p2v_root

buildfs.sh

#!/bin/bash

boot_dev=sda1          # boot device
root_dev=sda3          # root device

######### Code ###########
mkdir /mnt/p2v_root
mount /dev/$root_dev /mnt/p2v_root

mkdir /mnt/p2v_root/boot 2> /dev/null
mount /dev/$boot_dev /mnt/p2v_root/boot

# folder store p2v script
mkdir -p /mnt/p2v_root/root/p2v

echo "Done"

bash buildfs.sh

Step 4: 建立 Migation Script

cd /mnt/p2v_root/root/tools

p2v.sh

#!/bin/bash
# version 4

HOST="192.168.234.1"
SOURCE="rootfs"
PORT=8888
# 尾一定要有 "/"
mountpoint="/mnt/p2v_root/"

ntpdate stdtime.gov.hk > /dev/null

################# Code ########################
echo "---------------------------------"
echo "Source IP: $HOST"
echo "Source Module: $SOURCE"
echo "Local Dest: $mountpoint"
echo "---------------------------------"
echo -n "Are you want to run migration script ??(y/n) "

read input

if [ "$input" == "y" ]
then
        echo -n "Are you first time run this script ??(y/n) "
        read input
        if [ "$input" == "y" ]
        then
                echo "First time running" | tee -a file.log
                rsync -avXAH --inplace --delete --numeric-ids \
                        --exclude-from=p2v_first.txt \
                        rsync://$HOST:$PORT/$SOURCE/ $mountpoint > file.log 2> err.log
        else
                echo "Last time running" | tee -a file.log
                echo "Start: `date +%H:%M:%S`"
                rsync -avXAH --inplace --delete --numeric-ids \
                        --exclude-from=p2v_second.txt \
                        rsync://$HOST:$PORT/$SOURCE/ $mountpoint > file.log 2> err.log
        fi
        echo "End: `date +%H:%M:%S`"
fi

echo "Done"

rsync opts:

-v log sync which file

-a will preserve many file attributes like owner and permissions

-H will preserve hardlinks (沒有包括在 -a 內)

-X will preserve extended attributes like setuid (沒有包括在 -a 內)

-A preserve ACLs (沒有包括在 -a 內)

chmod 700 p2v.sh

p2v_first.txt

# sys
/proc/*
/sys/*
/selinux/*
/dev/*
/run/*.pid
/var/log/*.gz
/tmp/*
/mnt/*
lost+found/
#### 有用的 Scripts
/tools/
#### 假設所有 data 放在 /home
/root/*
/home/*

P.S.

        /dev/* 都要 sync 過去, 因為舊 Linux  沒有它是 boot 唔起的.

        如果漏了, 可以用以下 cmd 再 rsync 過

        rsync -av rsync://SRC/rootfs/dev/ ./

p2v_second.txt

#### system ####
lost+found/
/proc/*
/sys/*
/tmp/*
/selinux/*
/dev/*
#### Folder need to check ###
/mnt/*
#### 有用的 Scripts
/tools/*
#### 不去 sync 已修改好的 Folder ####
/etc/
/boot/

Step 5: ssh server & test rsync

rsync rsync://x.x.x.x:8888/rootfs/root/testing.txt /root/test

Step 6: First sync in screen

echo "vbell off" >> /root/.sceenrc

screen -S p2v

cd /mnt/p2v/tools

df -h | grep p2v                    # double check disk 已經 mount 好

./p2v.sh

Step 7: Chroot

chroot.sh

#!/bin/bash

_ROOTPATH="/mnt/v2v"

cd $_ROOTPATH

mkdir proc sys dev > /dev/null 2>&1

mount --bind /proc $_ROOTPATH/proc > /dev/null 2>&1
mount --bind /sys  $_ROOTPATH/sys  > /dev/null 2>&1
mount --bind /dev  $_ROOTPATH/dev  > /dev/null 2>&1

chroot $_ROOTPATH /bin/bash

Step 8: Change fstab

blkid /dev/sda*

/etc/fstab

Step 9: Reinstall Grub

修改

/boot/grub/device.map

# this device map was generated by anaconda
(hd0)     /dev/vda

/etc/sysconfig/grub

boot=/dev/vda
forcelba=0

cp /proc/mounts /etc/mtab

grub-install /dev/sda

P.S.

Troubleshoot: "Could not find device for"

 * Check your /etc/fstab, /boot must not be listed.

 * Check your /etc/mtab, if its missing,

如果失敗, 就要嘗試人手 step by step

grub
device (hd0) /dev/vda
root (hd0,0)
setup (hd0)
quit

Step 10: Disable useless service

找出在行的 Service

chkconfig --list | grep '3:on'

Centos6

chkconfig lvm2-monitor off
chkconfig smartd off

Step 11: Fix grub setting

/boot/grub/menu.lst

title CentOS (2.6.18-194.32.1.el5PAE)
        root (hd0,0)
        kernel /vmlinuz-? ro root=/dev/sda3
        initrd /initrd-?.img

Step 12: Fix Network

Centos6:

/etc/udev/rules.d/70-persistent-net.rules

# 改成另一個 IP 先

/etc/sysconfig/network-scripts/ifcfg-eth0

Step 13: Test Boot

disable_service.sh

#!/bin/bash
# Sxript to disable Centos service
STATUS=off

chkconfig httpd $STATUS
chkconfig vsftpd $STATUS
chkconfig pure-ftpd $STATUS
chkconfig postfix $STATUS
chkconfig dovecot $STATUS
chkconfig courier-imap $STATUS
chkconfig fail2ban $STATUS
chkconfig zabbix-agentd $STATUS
chkconfig named $STATUS
chkconfig mysqld $STATUS
chkconfig crond $STATUS
chkconfig rsyslog $STATUS
chkconfig syslog $STATUS

Step 14: 在 vmware 有機會要 rebuild "initrd"

# pvscsi: VMware Paravirtual controllers on RHEL/CentOS 5

rpm -qa | grep pvscsi

ls -1 /boot/initrd-*

/boot/initrd-2.6.18-194.17.4.el5.img
/boot/initrd-2.6.18-194.el5.img

mv /boot/initrd-2.6.18-194.17.4.el5.img /boot/initrd-2.6.18-194.17.4.el5.img.bak

# rpm -qa | grep kernel

kernel-2.6.18-194.17.4.el5
kernel-2.6.18-194.el5

/sbin/mkinitrd -f /boot/initrd-2.6.18-194.17.4.el5.img 2.6.18-194.17.4.el5

詳見: https://datahunter.org/initramfs

Step 15: Second sync

好處:

* 預到及縮短 "Final Sync" 的時間

Step 16: Final Sync

Disable service on source

Check running service

netstat -ntlp

stop_service.sh

#!/bin/bash
# Centos service
service httpd stop
service vsftpd stop
service pure-ftpd stop
service postfix stop
service dovecot stop
service courier-imap stop
service fail2ban stop
service zabbix-agentd stop
service named stop
# Last
service mysqld stop
service crond stop
service rsyslog stop
service syslog stop

Sync again

Last: Cleanup

# Disable rsync

/etc/rsyncd.conf

# sync time

/etc/crontab

4 4 * * * root /usr/sbin/ntpdate stdtime.gov.hk > /dev/null 2> /dev/null

Troubleshoot

Q1: Can't ping VM

A1: ping 8.8.8.8 by vm

 


快樂 V2V

 

Source

mirror.sh

#!/bin/bash

HOST="x.x.x.x"
PORT=1873
USER=tim

cd /root/scripts
echo -n "Are you want to run migration script ??(y/n)"
read input
if [ "$input" == "y" ]
then
        echo "Start: `date +%H:%M:%S`"
        rsync -avXAH --delete --numeric-ids / \
                        --exclude-from=/root/scripts/exclude.txt \
                        --bwlimit=6024 \
                        rsync://$USER@$HOST:$PORT/rootfs/ > file.log 2> err.log
        echo "End: `date +%H:%M:%S`"
fi
echo "Done"

/root/scripts/exclude.txt

##### First time
/etc
/boot
##### 一定不用
/sys
/proc
/dev
lost+found

 



Troubleshoot

 

Windows

XP

intelppm.sys 問題

原因:

Drivers (intelppm.sys) are attempting to perform an unsupported operation inside of the virtual machine

解決方法:

用 safe mode 去 Backup 起在 C:\windows\system\driver\ 內的 intelppm.sys 及 processr.sys 即可

P.S.

由 1個 vcpu 換成 2 個 vcpu 都有此情況

 


時鐘問題

 

<clock offset='localtime'/>

 

 

 

附加檔案大小
mergeide-xp.reg6.77 KB