将正在运行的Linux系统变成另一台机器上的KVM实例

Cha*_*les 4 virtualization migration physical-to-virtual kvm-virtualization

我有两台想要虚拟化的物理机。

无法(物理上)将硬盘从任何一台机器插入到将充当其虚拟机主机的新机器中,因此我认为复制系统的整个结构来使用dd是不可能的。

我怎样才能最好地将这些机器从其硬件迁移到 KVM 环境?我设置了空的、未格式化的 LVM 逻辑卷来托管其文件系统,并理解为虚拟机提供一个真正的分区来使用比在文件系统上粘贴映像可以获得更高的性能。

我最好创建新的操作系统安装并重新同步差异吗?

FWIW,两台要进行 VM 的机器运行 CentOS 5,主机运行 Ubuntu Server 10.04,没有什么特别重要的原因。我怀疑这太重要了,因为重要的仍然是 KVM 和 libvert。

Kyl*_*yle 6

P2V Linux 物理到虚拟 KVM - 无自动化工具

P将运行 Debian Wheezy 7.11实时迁移到VProxmox VE v5.4 上的 KVM

mdadm 软件 RAID1P磁盘到 KVM virtio 磁盘

P从大磁盘迁移到小V磁盘

前言

这些步骤的目标是获取P运行实时生产的物理 Linux 节点并将其虚拟化。无需创建和分配多 TB 磁盘,也不必在V来宾中使用 md raid,因为目标虚拟机管理程序 (Proxmox 5) 使用 ZoL/ZFS。还希望减少正在运行的P节点上的停机/重新启动。

这些步骤可能并不完美,但它应该让您接近解决方案并引用我在此过程中找到的有用链接。

经过在https://unix.stackexchange.comhttps://serverfault.com上仔细谷歌搜索后,我选择发布关于这个问题的答案。这似乎是与我的回答最相关的问题,尽管在撰写本文时该问题已经有 9 年多了。

以下是我发现的一些相关问题,该答案也旨在解决这些问题:
P2V with rsync
How to create a virtual machine from a live server?
如何将物理系统迁移到仅具有网络访问权限的KVM虚拟服务器?
在不关机的情况下将物理机转换为虚拟机 将
物理机迁移到 KVM
vmware 转换 linux 物理机
如何将裸机 Linux 安装迁移到虚拟机

步骤1

virtio 支持

# 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
Run Code Online (Sandbox Code Playgroud)

第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.
Run Code Online (Sandbox Code Playgroud)

步骤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/
Run Code Online (Sandbox Code Playgroud)

步骤4

同步数据

# 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:/ .
Run Code Online (Sandbox Code Playgroud)

步骤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
Run Code Online (Sandbox Code Playgroud)

步骤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
Run Code Online (Sandbox Code Playgroud)

步骤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: /sf/ask/753047641/
sed 's/\^\[/\o33/g;s/\[1G\[/\[27G\[/' /var/log/boot |less -r
Run Code Online (Sandbox Code Playgroud)