目录

VBox 镜像迁移 QEMU

目录

电脑重装不想再安装 vbox,试试 qume 能不能使用原来的镜像。

找到原来镜像路径,我的镜像文件名是 centos-disk001.vdi,将其转换为 img

qemu-img convert -f vdi -O raw centos-disk001.vdi centos.img

尝试使用 qemu 启动:

qemu-system-x86_64 -m 2048 -boot d -enable-kvm -smp 3 -hda centos.img -net nic

启动会自动弹出 qemu 窗口,查看是否能正常登录。

修改镜像在终端中启动,先拷贝出镜像中内核,位置一般在 /boot 目录下:

[root@localhost ~]# ls /boot/
config-3.10.0-1062.4.1.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-64fdeb4a6b984cc8a60bbd21fea1cd3c.img
initramfs-3.10.0-1062.4.1.el7.x86_64.img
initramfs-3.10.0-1062.4.1.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-1062.4.1.el7.x86_64.gz
System.map-3.10.0-1062.4.1.el7.x86_64
vmlinuz-0-rescue-64fdeb4a6b984cc8a60bbd21fea1cd3c
vmlinuz-3.10.0-1062.4.1.el7.x86_64

我的拷贝出 vmlinuz-3.10.0-1062.4.1.el7.x86_64initramfs-3.10.0-1062.4.1.el7.x86_64.img 即可,
然后查看镜像 root 位置,在启动按 e 可查看引导信息:root=/dev/mapper/centos-root
修改启动为:

qemu-system-x86_64 -kernel vmlinuz-3.10.0-1062.4.1.el7.x86_64 -initrd initramfs-3.10.0-1062.4.1.el7.x86_64.img -append "root=/dev/mapper/centos-root console=ttyS0 selinux=0" -m 2048 -enable-kvm -smp 3 -hda centos.img -nographic -net nic

修改网络1

最后启动脚本如下:

#!/bin/bash

# Source function library.
if [[ $EUID -ne 0 ]]; then
	echo ONLY ROOT CAN ACCESS
	exit 1
fi

function  up_br0() {
    ifconfig enp0s25 down               # 首先关闭宿主机网卡接口
    brctl addbr br0                     # 添加一座名为 br0 的网桥
    brctl addif br0 enp0s25             # 在 br0 中添加一个接口
    brctl stp br0 off                   # 如果只有一个网桥,则关闭生成树协议
    brctl setfd br0 1                   # 设置 br0 的转发延迟
    brctl sethello br0 1                # 设置 br0 的 hello 时间
    ifconfig br0 0.0.0.0 promisc up     # 启用 br0 接口
    ifconfig enp0s25 0.0.0.0 promisc up # 启用网卡接口
    dhclient br0                        # 从 dhcp 服务器获得 br0 的 IP 地址
    dhclient enp0s25
}

function up_tap0() {
    tunctl -t tap0 -u root              # 创建一个 tap0 接口,只允许 root 用户访问
    brctl addif br0 tap0                # 在虚拟网桥中增加一个 tap0 接口
    ifconfig tap0 0.0.0.0 promisc up    # 启用 tap0 接口
}

function clear() {
    ifconfig tap0 down
    brctl delif br0 tap0
    tunctl -d tap0
    ifconfig enp0s25 down
    ifconfig br0 down
    brctl delif br0 enp0s25
    brctl delbr br0
    ifconfig enp0s25 up
}

trap clear EXIT INT TERM

up_br0
up_tap0

qemu-system-x86_64 -kernel vmlinuz-3.10.0-1062.4.1.el7.x86_64 -initrd initramfs-3.10.0-1062.4.1.el7.x86_64.img -append "root=/dev/mapper/centos-root console=ttyS0 selinux=0" -m 2048 -enable-kvm -smp 3 -hda centos.img -nographic -net nic -net tap,ifname=tap0,script=no,downscript=no

PS:退出 console 使用快捷键 Ctrl+A 再按 X