鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 服务器相关 > 批处理 > >

Qemu 联网问题

来源:互联网 作者:佚名 时间:2015-09-27 08:36
转载请注明原文出处:http://blog.csdn.net/roddick621 在前面以前写了怎么安装mipsel debian 系统了。但是安装好,并不能够 联网 。现在要做的就是让QEmu通过NAT方式 联网 。 参考资料:http://wiki.qemu.org/Documentation/Networking/NAT 这里需要一个文

转载请注明原文出处:http://blog.csdn.net/roddick621


在前面以前写了怎么安装mipsel debian 系统了。但是安装好,并不能够联网。现在要做的就是让QEmu通过NAT方式联网


参考资料:http://wiki.qemu.org/Documentation/Networking/NAT


这里需要一个文件qemu-ifup,可以从上面的地址获取,我这里也顺便贴出来吧,以免那个网站打不开:

#!/bin/sh
#
# Copyright IBM, Corp. 2010  
#
# Authors:
#  Anthony Liguori <aliguori@us.ibm.com>
#
# This work is licensed under the terms of the GNU GPL, version 2.  See
# the COPYING file in the top-level directory.

# Set to the name of your bridge
BRIDGE=br0

# Network information
NETWORK=192.168.53.0
NETMASK=255.255.255.0
GATEWAY=192.168.53.1
DHCPRANGE=192.168.53.2,192.168.53.254

# Optionally parameters to enable PXE support
TFTPROOT=
BOOTP=

do_brctl() {
    brctl "$@"
}

do_ifconfig() {
    ifconfig "$@"
}

do_dd() {
    dd "$@"
}

do_iptables_restore() {
    iptables-restore "$@"
}

do_dnsmasq() {
    dnsmasq "$@"
}

check_bridge() {
    if do_brctl show | grep "^$1" > /dev/null 2> /dev/null; then
	return 1
    else
	return 0
    fi
}

create_bridge() {
    do_brctl addbr "$1"
    do_brctl stp "$1" off
    do_brctl setfd "$1" 0
    do_ifconfig "$1" "$GATEWAY" netmask "$NETMASK" up
}

enable_ip_forward() {
    echo 1 | do_dd of=/proc/sys/net/ipv4/ip_forward > /dev/null
}

add_filter_rules() {
do_iptables_restore <<EOF
# Generated by iptables-save v1.3.6 on Fri Aug 24 15:20:25 2007
*nat
:PREROUTING ACCEPT [61:9671]
:POSTROUTING ACCEPT [121:7499]
:OUTPUT ACCEPT [132:8691]
-A POSTROUTING -s $NETWORK/$NETMASK -j MASQUERADE 
COMMIT
# Completed on Fri Aug 24 15:20:25 2007
# Generated by iptables-save v1.3.6 on Fri Aug 24 15:20:25 2007
*filter
:INPUT ACCEPT [1453:976046]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1605:194911]
-A INPUT -i $BRIDGE -p tcp -m tcp --dport 67 -j ACCEPT 
-A INPUT -i $BRIDGE -p udp -m udp --dport 67 -j ACCEPT 
-A INPUT -i $BRIDGE -p tcp -m tcp --dport 53 -j ACCEPT 
-A INPUT -i $BRIDGE -p udp -m udp --dport 53 -j ACCEPT 
-A FORWARD -i $1 -o $1 -j ACCEPT 
-A FORWARD -s $NETWORK/$NETMASK -i $BRIDGE -j ACCEPT 
-A FORWARD -d $NETWORK/$NETMASK -o $BRIDGE -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A FORWARD -o $BRIDGE -j REJECT --reject-with icmp-port-unreachable 
-A FORWARD -i $BRIDGE -j REJECT --reject-with icmp-port-unreachable 
COMMIT
# Completed on Fri Aug 24 15:20:25 2007
EOF
}

start_dnsmasq() {
    do_dnsmasq \
	--strict-order \
	--except-interface=lo \
	--interface=$BRIDGE \
	--listen-address=$GATEWAY \
	--bind-interfaces \
	--dhcp-range=$DHCPRANGE \
	--conf-file="" \
	--pid-file=/var/run/qemu-dnsmasq-$BRIDGE.pid \
	--dhcp-leasefile=/var/run/qemu-dnsmasq-$BRIDGE.leases \
	--dhcp-no-override \
	${TFTPROOT:+"--enable-tftp"} \
	${TFTPROOT:+"--tftp-root=$TFTPROOT"} \
	${BOOTP:+"--dhcp-boot=$BOOTP"}
}

setup_bridge_nat() {
    if check_bridge "$1" ; then
	create_bridge "$1"
	enable_ip_forward
	add_filter_rules "$1"
	start_dnsmasq "$1"
    fi
}

setup_bridge_vlan() {
    if check_bridge "$1" ; then
	create_bridge "$1"
	start_dnsmasq "$1"
    fi
}

setup_bridge_nat "$BRIDGE"

if test "$1" ; then
    do_ifconfig "$1" 0.0.0.0 up
    do_brctl addif "$BRIDGE" "$1"
fi


start脚本:

顺便写了一个启动脚本start:
#!/bin/sh
VMLINUX_VERSION=`ls vmlinux*`
INITRD_VERSION=`ls initrd*`
HARDDISK=`ls *.img`

software=qemu-system-mipsel
OPTIONS_BOOT="-m 256 -net tap -net nic  -nographic"
OPTIONS_INSTALL="-m 256  -nographic"
print_message() {
        echo "kernel=$VMLINUX_VERSION"
        echo "initrd=$INITRD_VERSION"
        echo "disk=$HARDDISK"
        echo "bootup options = $OPTIONS_BOOT"
        echo "install options = $OPTIONS_INSTALL"
}

usage() {
        echo "Usage : ./start [install | bootup | print]"
        echo "  install : install debian "
        echo "  bootup  : start the system"
        echo "  print   : print information"
        exit 0
}

boot() {
        if [ ! -x "/etc/qemu-ifup" ];then
                echo "/etc/qemu-ifup is not exist,do you want to copy it to /etc [y/n]"
                read copy_y_n
                if [ "x$copy_y_n" = "xy" ];then
                        echo "copying......"
                        sleep 1
                        sudo cp -f ./qemu-ifup /etc/qemu-ifup
                fi
        else
                echo "file /etc/qemu-ipup is all ready"
        fi

        echo "sudo $software $OPTIONS_BOOT -kernel $VMLINUX_VERSION -hda $HARDDISK "
        sudo $software $OPTIONS_BOOT -kernel $VMLINUX_VERSION -hda $HARDDISK -append "root=/dev/sda1 console=ttyS0"
}
install() {
        echo "install system"
        echo "$software $OPTIONS_INSTALL -kernel $VMLINUX_VERSION -initrd $INITRD_VERSION -hda $HARDDISK"
        $software $OPTIONS_INSTALL -kernel $VMLINUX_VERSION -initrd $INITRD_VERSION -hda $HARDDISK -append "root=/dev/ram console=ttyS0"
}

if [ ! $# -eq 1 ];then
        usage
elif [ "x$1" = "xinstall" ];then
        install
elif [ "x$1" = "xbootup" ];then
        boot
elif [ "x$1" = "xprint" ];then
        print_message
else
        usage
fi

然后把前面的几个文件全部放在同一个文件夹里面:

debian-mipsel.img    initrd.gz     qemu-ifup     start    vmlinux-3.2.0-4-4kc-malta


这样直接执行./start bootup就可以启动debian系统,并且可以用NAT方式联网了。








网友评论
<