將正在執行的 Linux 系統變成另一台機器上的 KVM 實例
我有兩台要虛擬化的物理機。
我不能(物理上)將任一機器的硬碟驅動器插入將充當其 VM 主機的新機器,因此我認為過度使用複制系統的整個結構是不可能
dd
的。我怎樣才能最好地將這些機器從它們的硬體遷移到 KVM 環境?我已經設置了空的、未格式化的 LVM 邏輯卷來託管它們的文件系統,並理解為 VM 提供一個真正的分區可以實現比在文件系統上粘貼圖像更高的性能。
我會更好地創建新的作業系統安裝並重新同步差異嗎?
FWIW,要進行 VM 的兩台機器執行的是 CentOS 5,而主機執行的是 Ubuntu Server 10.04,沒有什麼特別重要的原因。我懷疑這太重要了,因為它仍然是 KVM 和 libvert。
您可以通過 SSH 隧道將 dd 的輸出通過管道傳輸到目標機器。我知道它在 VMWare 虛擬機中相對成功。
此處的主要答案中有很好的詳細資訊,並提供了在 SSH 未執行時該怎麼做的說明(許多 liveCD 上都有 SSH 伺服器,所以應該不是問題): 如何使用 dd 設置磁碟複製,netcat 和 ssh 隧道?
P2V Linux 物理到虛擬 KVM - 沒有自動化工具
P
在 Proxmox VE v5.4 上將執行的 Debian Wheezy 7.11實時遷移到V
KVMmdadm 軟體 RAID1
P
磁碟到 KVM virtio 磁碟從大
P
磁碟遷移到小V
磁碟前言
這些步驟的目標是獲取一個
P
執行實時生產的物理 Linux 節點並將其虛擬化。無需創建和分配數 TB 的磁碟,也不必在V
來賓中使用 md raid,因為目標管理程序(Proxmox 5)使用了 ZoL/ZFS。還想減少正在執行的P
節點上的停機時間/重啟。這些步驟可能並不完美,但它應該讓您接近解決方案並引用我在此過程中發現的有用連結。
在對https://unix.stackexchange.com和https://serverfault.com進行仔細的Google搜尋後,我選擇發布關於這個問題的答案。這似乎是與我的答案最相關的問題,儘管在撰寫本文時它已經 9 歲以上。
以下是我發現的一些相關問題,這個答案也旨在解決:
How to create a virtual machine from a live server?
第1步
虛擬支持
# On the P node # check the kernel has the virtio support grep -i virtio /boot/config-$(uname -r) # when no, that is an issue out of scope of these instructions. contact me. # if lsinitrd is available, check if the initramfs already has the modules lsinitrd /boot/initrd.img-$(uname -r) | grep virtio # when yes, your virtio is already in the initramfs, continue to next step # when no, add the modules to the initramfs # backup existing initrd.img cp -iv /boot/initrd.img-$(uname -r) /boot/BACKUP_initrd.img-$(uname -r) # non Debian way mkinitrd --with virtio_console --with virtio_pci --with virtio_blk -f /boot/initrd.img-$(uname -r) $(uname -r) # The Debian way # https://wiki.debian.org/DebianKVMGuests echo -e 'virtio_console\nvirtio_blk\nvirtio_pci' >> /etc/initramfs-tools/modules # check correctly append new lines etc, correct manually if needed cat /etc/initramfs-tools/modules # compile new initramfs update-initramfs -u # OPTIONAL if safe # !!! WARNING DOWNTIME -- reboot P node to test everything is ok with the new initramfs shutdown -r now
第2步
KVM 準備 - BIOS 分區
# boot a new KVM guest on SystemRescueCD or similar # create the BIOS/UEFI partition(s) # https://help.ubuntu.com/community/DiskSpace#BIOS-Boot_or_EFI_partition_.28required_on_GPT_disks.29 # https://help.ubuntu.com/community/Installation/UEFI-and-BIOS/stable-alternative#Create_a_partition_table # follow the linked guides above to create the relevant BIOS partitions/disks.
第 3 步
KVM 準備 - BOOT 和 DATA 分區
# BOOT partition # inspect the P boot partition - note the parameters # CRITICAL the new V boot partition should be identical to the partition on the P. # make the V boot partition using your preferred partitioning tool # on P node, make a copy of the boot partition # umount the boot fs for backup umount /boot # backup boot partition gzip --stdout /dev/md1 > ~user/boot.disk.md1.img.gz # re-mount boot fs mount /boot # on the KVM live CD cd /tmp # or somewhere with > some space for the boot image scp user@hostname.com:boot.disk.md1.img.gz . gunzip boot.disk.md1.img.gz # copy the P boot partition to the V boot partition dd if=boot.disk.md1.img of=/dev/vda1 # verify consistency fsck.ext3 /dev/vda1 # list the detected file systems, visual check for the expected results fsarchiver probe simple # on the KVM live CD make your data partitions, the size you wish # mirroring the P is not required, obviously needs to be enough space for the data. # CRITICAL the binaries/tools used to make the data file systems must be for the same kernel generation i.e. from the node being converted, otherwise the system will fail to mount the rootfs during boot. # https://unix.stackexchange.com/questions/267658/ # CRITICAL target file systems must have enough inodes mkefs -t ext4 -N 1500000 /dev/vda2 mkefs -t ext4 -N 1500000 /dev/vda3 cd /mnt/ mkdir linux mount /dev/vda2 linux/ cd linux/ mkdir -p var boot mount /dev/vda3 var/
第四步
rsync 數據
# consider mounting the fs ro, or at the very least stopping services for the final rsync nohup rsync --bwlimit=7m --human-readable --itemize-changes --verbose --archive --compress --rsync-path='sudo rsync' --rsh='ssh -p22345' --exclude=/mnt --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/boot --exclude=/var/spool user@hostname:/ . 1>../rsync.stdout 2>../rsync.stderr # check the logs are ok, and as expected # final sync, stop services, and/or ro the fs(s) rsync --bwlimit=7m --human-readable --itemize-changes --verbose --archive --compress --rsync-path='sudo rsync' --rsh='ssh -p22345' --exclude=/mnt --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/boot --exclude=/var/spool user@hostname:/ .
第 5 步
更新 grub
mount /dev/vda1 boot/ mkdir -p proc dev sys mount -o bind /proc /mnt/linux/proc mount -o bind /dev /mnt/linux/dev mount -o bind /sys /mnt/linux/sys chroot /mnt/linux /bin/bash export PATH=$PATH:/bin:/sbin:/usr/sbin/ # check what is required for your P grub-install /dev/vda grub-install --recheck /dev/vda update-grub # update /etc/fstab with the new id/names/options vim /etc/fstab # Only required if you're P node had md RAID # https://dertompson.com/2007/11/30/disabling-raid-autodetection-in-ubuntu/ aptitude purge mdadm # required mount points mkdir -p /mnt /proc /sys /dev /run # required because I didn't rsync /var/spool mkdir -p /var/spool/cron/atspool /var/spool/cron/crontabs /var/spool/cron/atjobs /var/spool/postfix /var/spool/rsyslog
第 6 步
測試 kvm 並在 P2V 後進行所需的更改
# reboot to KVM guest normal boot disk # fix a screen bug that appeared after P2V aptitude reinstall screen # ensure boot logging is enabled # https://wiki.debian.org/bootlogd aptitude install bootlogd # networking # check that the MAC address assigned to the KVM matches what the KVM kernel reports ip link # modify net interfaces, taking note of the interface name with the correct MAC address vim /etc/network/interfaces # update DNS if required vim /etc/resolv.conf # update apache2 envvars if required vim /etc/apache2/envvars # update hosts vim /etc/hosts # reboot shutdown -r now
第 7 步
最終測試和驗證
#### post reboot # check dmesg and/or kvm console for boot it issues dmesg -x dmesg -x --level=err --level=warn # check boot log for issues, useful if physical console cannot be easily viewed # formatting: https://stackoverflow.com/q/10757823 sed 's/\^\[/\o33/g;s/\[1G\[/\[27G\[/' /var/log/boot |less -r