QEMU 简单使用
来源:互联网 发布:办公网络方案 编辑:程序博客网 时间:2024/06/01 10:10
- 在host上安装qemu:
yum install qemu
或者
wget http://wiki.qemu-project.org/download/qemu-2.7.0.tar.bz2
./configure –target-list=x86_64-softmmu –enable-kvm
make -j8
make install- 创建image文件:
qemu-img create -f qcow2 guest.qcow2 3G
- 安装linux guest:
1)下载一个镜像文件,http://download-node-02.eng.bos.redhat.com/composes/latest-RHEL7/compose/Server/ppc64le/iso/RHEL-7.3-20160817.1-Server-ppc64le-dvd1.iso
2)安装:qemu-system-x86_64 -m 4G -hda guest.qcow2 -cdrom RHEL-7.3-20160817.1-Server-ppc64le-dvd1.iso- 网络设置:
qemu常用网络模式是user、tap。
user使用有局限性,
1) 由于其在QEMU内部实现所有网络协议栈,因此其性能较差。
2) 不支持部分网络功能(如ICMP),所以不能在客户机中使用ping命令测试外网连通性。
3) 不能从宿主机或外部网络直接访问客户机。,需要作地址重定向。
tap方式网络没有这些限制。
通过tap又可以实现bridge和nat方式的网络连接。
user模式:
-netdev user,id=id[,option][,option][,...]-net user[,option][,option][,...]默认创建一个dhcp服务器地址是10.0.2.15其中常见的选项(option)及其意义如下:• vlan=n,将用户模式网络栈连接到编号为n的VLAN中(默认值为0)。• name=name,分配一个在QEMU monitor中会用到的名字(如在monitor的“info network”命令中 可看到这个网卡的name)。• net=addr[/mask],设置客户机可以看到的IP地址(客户机所在子网),其默认值是10.0.2.0/24。其中,子网掩码(mask)有两种形式可选,一种是类似于255.255.255.0这样地址,另一种是32位IP地址中前面被置位为1的位数(如10.0.2.0/24)。• host=addr,指定客户机可见宿主机的地址,默认值为客户机所在网络的第2个IP地址(如10.0.2.2)。• restrict=y|yes|n|no,如果将此选项打开(为y或yes),则客户机将会被隔离,客户机不能与宿主机通信,其IP数据包也不能通过宿主机而路由到外部网络中。这个选项不会影响“hostfwd”显示地指定的转发规则,“hostfwd”选项始终会生效。默认值为n或no,不会隔离客户机。• hostname=name,设置在宿主机DHCP服务器中保存的客户机主机名。• dhcpstart=addr,设置能够分配给客户机的第一个IP,在QEMU内嵌的DHCP服务器有16个IP地址可供分配。在客户机中IP地址范围的默认值是子网中的第15到第30个IP地址(如10.0.2.15 ~ 10.0.2.30)。• dns=addr,指定虚拟DNS的地址,这个地址必须与宿主机地址(在“host=addr”中指定的)不相同,其默认值是网络中的第3个IP地址(如10.0.2.3)。• tftp=dir,激活QEMU内嵌的TFTP服务器,目录dir是TFTP服务的根目录。不过,在客户机使用TFTP客户端连接TFTP服务后需要使用binary模式来操作。• hostfwd=[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport,将访问宿主机的hostpot端口的TCP/UDP连接重定向到客户机(IP为guestaddr)的guestport端口上。如果没有设置guestaddr,那么默认使用x.x.x.15(DHCP服务器可分配的第一个IP地址)。如果指定了hostaddr的值,则可以根据宿主机上的一个特定网络接口的IP端口来重定向。如果没有设置连接类型为TCP或UDP,则默认使用TCP连接。“hostfwd=…”这个选项在一个命令行中可以多次重复使用。• guestfwd=[tcp]:server:port-dev,将客户机中访问IP地址为server的port端口的连接转发到宿主机的dev这个字符设备上。“guestfwd=…”这个选项也可以在一个命令行中多次重复使用。[root@dhcp-12-166 qemuimage]# qemu-system-x86_64 -m 2G -smp 2 -hda RHEL-7.3-20160817.1.qcow2 -enable-kvm -nographic -netdev user,id=mytap,hostfwd=tcp::5022-:22 -device e1000,netdev=mytap客户机可以通过10.0.2.2访问宿主机客户机可以访问外网:wget baidu.com外网机器可以通过连接宿主机的5022端口访问客户机的22端口
tap模式:
-netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,helper=helper]-net tap[,vlan=n][,name=name][,fd=h][,ifname=name][,script=file][,downscript=dfile][,helper=helper]//script、downscript和helper是用来自动创建bridge和tap接口的脚本qemu-system-x86_64 -m 2G -hda RHEL-7.3-20160817.1.qcow2 -enable-kvm -nographic -vga none -netdev tap,id=mytap,ifname=tap0,script=/etc/qemu-ifupnew,downscript=/etc/qemu-ifdownnew -device e1000,netdev=mytapqemu-system-i386 linux.img -net nic -net tapqemu-system-i386 linux.img -net nic -net tap,"helper=/path/to/qemu-bridge-helper"
#bridge方式1、创建/etc/qemu-ifup.sh#!/bin/bashIFNAME=enp5s0IP1=192.168.10.100/24IP2=192.168.10.101/24set -xif [ -n "$1" ];then#create bridge, add physical interface to bridge ip link set $IFNAME down ip link add br0 type bridge ip link set br0 up ip link set $IFNAME master br0 ip link set $IFNAME up# ip addr add $IP1 dev br0# ip addr add $IP2 dev $IFNAME#add tap device to bridge# ip tuntap add $1 mode tap user `whoami` ip link set $1 up sleep 0.5s ip link set $1 master br0#config ip fro bridge pkill dhclient sleep 5 dhclient -v br0 exit 0else echo "ERROR: no interface specified" exit 1fi2、创建/etc/qemu-ifdown.sh#!/bin/bashif [ -n "$1" ];then IP1=192.168.10.100/24 IP2=192.168.10.101/24 IFNAME=enp5s0 ip link set $IFNAME down ip link set $1 down ip link set br0 down ip link set $1 nomaster ip link set $IFNAME nomaster ip link del br0# ip tuntap del $1 mode tap #ip addr del $IP2 dev $IFNAME ip link set $IFNAME up pkill dhclient sleep 5 dhclient -v $IFNAMEelse echo "ERROR:no interface specified"fi3、启动虚拟机,注意通过script和downscript参数指定脚本名称qemu-system-x86_64 -m 2G -hda RHEL-7.3-20160817.1.qcow2 -enable-kvm -nographic -vga none -netdev tap,id=mytap,ifname=tap0,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -device e1000,netdev=mytap4、给虚拟机配置静态地址,或者dhcp方式获取地址注意:如果host有可用的dhcp,那么guest可以通过dhcp获取地址。当然也可以在guest上手动配置静态地址。
#nat方式步骤与bridge方式相同,就是脚本不一样//qemu-ifup-nat#!/bin/bash# qemu-ifup script for QEMU/KVM with NAT netowrk mode# set your bridge nameBRIDGE=virbr0IFNAME=enp5s0# Network informationNETWORK=192.168.122.0NETMASK=255.255.255.0# GATEWAY for internal guests is the bridge in hostGATEWAY=192.168.122.1DHCPRANGE=192.168.122.2,192.168.122.254# Optionally parameters to enable PXE supportTFTPROOT=BOOTP=function check_bridge() { if brctl show | grep "^$BRIDGE" &> /dev/null; then return 1 else return 0 fi}function create_bridge() { brctl addbr "$BRIDGE" brctl stp "$BRIDGE" on brctl setfd "$BRIDGE" 0 ifconfig "$BRIDGE" "$GATEWAY" netmask "$NETMASK" up}function enable_ip_forward() { echo 1 > /proc/sys/net/ipv4/ip_forward}function add_filter_rules() { iptables -t nat -A POSTROUTING -s "$NETWORK"/"$NETMASK" \ ! -d "$NETWORK"/"$NETMASK" -j MASQUERADE -o $IFNAME}function start_dnsmasq() { # don't run dnsmasq repeatedly ps -ef | grep "dnsmasq" | grep -v "grep" &> /dev/null if [ $? -eq 0 ]; then echo "Warning:dnsmasq is already running. No need to run it again." return 1 fi 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"}}function setup_bridge_nat(){ check_bridge "$BRIDGE" if [ $? -eq 0 ]; then create_bridge fi enable_ip_forward add_filter_rules "$BRIDGE" start_dnsmasq "$BRIDGE"}# need to check $1 arg before setupif [ -n "$1" ]; then setup_bridge_nat ifconfig "$1" 0.0.0.0 up brctl addif "$BRIDGE" "$1" exit 0else echo "Error: no interface specified." exit 1fi______________________________________________//qemu-ifdown-nat !/bin/bash# qemu-ifdown script for QEMU/KVM with NAT network mode# set your bridge nameBRIDGE="virbr0"if [ -n "$1" ]; then echo "Tearing down network bridge for $1" > /tmp/temp-nat.log ip link set $1 down brctl delif "$BRIDGE" $1 ip link set "$BRIDGE" down brctl delbr "$BRIDGE" iptables -t nat -F exit 0else echo "Error: no interface specified" > /tmp/temp-nat.log exit 1fi---------------------------------------------[root@dhcp-12-166 qemuimage]# qemu-system-x86_64 -m 2G -hda RHEL-7.3-20160817.1.qcow2 -enable-kvm -nographic -netdev tap,id=mytap,script=/home/qemuimage/qemu-ifup-nat,downscript=/home/qemuimage/qemu-ifdown-nat -device e1000,netdev=mytap--------------------------------------------//在host上清空防火墙filter表,guest就可以主动访问外网了[root@localhost qemuimage]# iptables -t filter -F//在host上添加目的nat,外网就可以主动访问guest了[root@localhost qemuimage]# iptables -t nat -A PREROUTING -p tcp -d 10.66.12.166 --dport 22 -j DNAT --to-destination 192.168.122.10:22//必要时在guset添加默认路由指向host
如何安装openvpn:yum -y install openssl-develyum -y install lzo-develyum -y install pam-devel下载openvpn源码: https://openvpn.net/index.php/download/community-downloads.html./configuremakemake install
0 0
- QEMU 简单使用
- qemu 使用
- qemu使用
- qemu 使用
- QEMU使用笔记
- qemu 使用两则
- QEMU安装与使用
- Openwrt使用Qemu仿真
- QEMU中的tracing使用
- 虚拟机qemu的使用
- 使用 QEMU 模拟 ARM
- QEMU使用上手
- 使用QEMU创建虚拟机
- qemu-img命令使用
- 使用qemu安装虚拟机
- qemu-img 的使用
- QEMU使用简介
- KVM虚拟机磁盘镜像文件管理命令qemu-img介绍及简单使用总结
- 程序员技术晋升非正式攻略
- 我的MYSQL学习心得(十二) 触发器
- 清除history内容
- 我的MYSQL学习心得(十三) 权限管理
- 中序式变成后序式的分析
- QEMU 简单使用
- UserInterfaceState.xcuserstate 在 Source tree 上面忽略的方法
- JS 实现瀑布流
- pedis
- 坑爹的C语言
- 我的MYSQL学习心得(十四) 备份和恢复
- 追着嘟嘟学c#系列基础第二篇-----c#结构体【蓝鸥出品】
- shell基本知识 - 控制流与函数
- Java中|、||、&、&&的区别