在Qemu运行的虚拟机中经常需要挂载NFS文件系统,在CenterOS 6.0系统中做了一次配置,在虚拟机中运行一个initramfs并在其中成功地挂载了NFS。下面是实验步骤的记录。
一、建立开发环境
yum groupinstall "Development Tools"
yum install glibc-static
二、安装Qemu
yum install zlib
yum install SDL SDL-devel
yum install gnutls-devel
cd ~
wget
tar zxvf qemu-0.15.1.tar.gz
cd qemu-0.15.1
./configure; make; make install
注意:编译过程中可能出现"out of memory"错误,可以尝试在Makefile中加入
CFLAGS += $(CFLAGS) -pipe
来解决。
三、 制作一个initramfs用于启动虚拟机
cd ~
mkdir image
wget
tar jxvf busybox-1.19.3.tar.bz2
cd busybox-1.19.3
make defconfig
make menuconfig
Build options
--> Build BusyBox as a static binary (no shared libs)
make
make install CONFIG_PREFIX=~/image
cd ~/image
mkdir proc sys dev etc mnt
mknod -m 600 dev/console c 5 1
mknod -m 600 dev/null c 1 3
mkdir etc/init.d
emacs etc/init.d/rcS
#!/bin/sh
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mdev -s
chmod +x etc/init.d/rcS
emacs etc/inittab
::sysinit:/etc/init.d/rcS
tty1::askfirst:/bin/sh
tty2::askfirst:/bin/sh
tty3::askfirst:/bin/sh
tty4::askfirst:/bin/sh
tty5::askfirst:/bin/sh
tty6::askfirst:/bin/sh
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
mv linuxrc init
find . | cpio -o -H newc | gzip > ~/image.cpio.gz
cd /usr/src
wget
tar jxvf linux-2.6.34.tar.bz2
ln -s linux-2.6.34 linux
cd /usr/src/linux
make menuconfig
Device Drivers
--> Block devices
--> RAM block device support (使用独立的initrd)
make
make modules_install
qemu -kernel arch/x86/boot/bzImage -initrd ~/image.cpio.gz -vnc :1
四、配置虚拟网桥和TAP设备,使宿主机与虚拟机可以通信
1. 如果内核不支持TAP设备或虚拟网桥就需要编译内核。Center OS 6.0默认是支持的,所以无需编译内核。
yum install kernel
yum install ncurses-devel
Device Drivers
--> Network device support
--> Universal TUN/TAP device driver support (支持TAP设备)
Networking support
--> Networking options
--> 802.1d Ethernet Bridging (支持虚拟网桥)
注:CenterOS有自己的一套编译内核的方法。参见:
2. 安装相关软件并进行虚拟网桥和TAP设备的配置。
yum install brctl
yum install tunctl
emacs /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="dhcp"
HWADDR="00:0C:29:20:dc:25"
NM_CONTROLLED="yes"
ONBOOT="yes"
BRIDGE="br0"
emacs /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
TYPE="Bridge"
BOOTPROTO="dhcp"
ONBOOT="yes"
emacs /etc/qemu-ifup
#!/bin/sh
if [ -n "$1" ];then
brctl addif br0 tap0
ifconfig tap0 0.0.0.0 up
exit 0
else
echo "Error: no interface specified"
exit 1
fi
emacs /etc/qemu-ifdown
#! /bin/sh
if [ -n "$1" ];then
ifconfig tap0 down
brctl delif br0 tap0
exit 0
else
echo "Error: no interface specified"
exit 1
fi
chmod +x /etc/qemu-ifup
chmod +x /etc/qemu-ifdown
3. 测试虚拟机与宿主机的连通性。
首先需要将宿主机的防火墙关闭:
/etc/init.d/iptables stop
chkconfig iptables off
调整虚拟机initramfs内容以支持DHCP客户端:
cd ~/image
cp ~/busybox-1.19.3/examples/udhcp/simple.script usr/share/udhcpc/default.script
find . | cpio -o -H newc | gzip > ~/image.cpio.gz
虚拟机内核中需要编入至少一种网卡驱动:
cd /usr/src/linux
make menuconfig
Device drivers
--> Network device support
--> Ethernet (1000Mbit)
--> Intel(R) PRO/1000 Gigabit Ethernet support (Intel 82540EM,Qemu默认网卡)
Device drivers
--> Network device support
--> Ethernet (10 or 100Mbit)
--> AMD PCnet32 PCI support (PCNet32)
make
make modules_install
qemu -kernel arch/x86/boot/bzImage -initrd ~/image.cpio.gz -net nic -net tap,ifname=tap0 -vnc :1
在虚拟机中输入:
ifconfig eth0 0.0.0.0 up
udhcpc -i eth0
五、在虚拟机中挂载NFS
首先安装并设置宿主机中的NFS服务器
yum install nfs-utils
emacs /etc/exports
/ 192.168.0.0/24(rw,no_root_squash)
/etc/init.d/nfs start
chkconfig nfs on
需要编译虚拟机内核中对NFS客户端的支持:
cd /usr/src/linux
make menuconfig
File systems
--> Network File systems
--> NFS client support
make
make modules_install
qemu -kernel arch/x86/boot/bzImage -initrd ~/image.cpio.gz -net nic -net tap,ifname=tap0 -vnc :1
在虚拟机中输入:
ifconfig eth0 0.0.0.0 up
udhcpc -i eth0
mount -t nfs -o nolock 192.168.0.24:/ /mnt
mount -t proc proc /mnt/proc
mount -t sysfs sysfs /mnt/sys
mount -v -o bind /mnt/dev dev
chroot /mnt /bin/bash --login
参考文献
1.
2.
3.
安装qemu虚拟机
4.
qemu虚拟机与外部网络的通信
阅读(2136) | 评论(0) | 转发(0) |