个人工具
登录
查看“UbuntuHelp:KVM/Directly”的源代码 - Ubuntu中文
UbuntuHelp
讨论
查看源代码
历史
搜索
导航
首页
最近更改
随机页面
页面分类
帮助
编辑
编辑指南
沙盒
新闻动态
字词处理
工具
链入页面
相关更改
特殊页面
页面信息
查看“UbuntuHelp:KVM/Directly”的源代码
来自Ubuntu中文
←
UbuntuHelp:KVM/Directly
跳转至:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{{From|https://help.ubuntu.com/community/KVM/Directly}} {{Languages|UbuntuHelp:KVM/Directly}} #title Using KVM Directly <<Include(KVM/Header)>> == Using KVM Directly == While the rest of this documentation focuses on using KVM through libvirt, it is also possible to work with KVM directly. This is not the recommended way but can be useful in some cases. KVM is very similar to qemu and it is possible to run machines from the command line. The basic syntax is : <pre><nowiki> kvm -m 512 -hda disk.img -cdrom ubuntu.iso -boot d -smp 2 </nowiki></pre> * -m = memory (in MB) * -hda = first hard drive * You can use a number of image file types including .img, .cow * You can also boot a hard drive. '''Be careful with this option as you do not want to boot the host root partition''' * Syntax -hda /dev/sda * This will call your grub menu from your MBR when you boot kvm. * -cdrom can be an iso image or a CD/DVD drive. * -boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n) * -smp = number of CPU * -alt-grab change Ctrl-Alt mouse grab combination for Ctrl-Alt-Shift (very practical if you often use some control key combinations like Ctrl-Alt-Del or Windows-E) There are a number of additional options and additional help is available with : <pre><nowiki> kvm --help </nowiki></pre> === Bridged Networking === In order to run KVM using bridged networking as a user we need to perform some configuration. 1. First bridge your network card as above [[UbuntuHelp:KVM/Networking#Creating a network bridge on the host|KVM/Networking#Creating a network bridge on the host]] 2. Install uml-utilities <pre><nowiki> sudo apt-get install uml-utilities </nowiki></pre> 3. Set permissions on your tun device. Using any editor, edit ''/etc/udev/rules.d/40-permissions.rules'', add this line at the bottom of the file : <pre><nowiki> KERNEL=="tun", GROUP="kvm", MODE="0660" </nowiki></pre> 4. Reboot (to bring up your bridge and tun device). 5. Edit ''/etc/kvm/kvm-ifup'' adding sudo in front of the ifconfig and brctl commands <pre><nowiki> #!/bin/sh switch=$(ip route ls | awk '/^default / { for(i=0;i<NF;i++) { if ($(i) == "dev") print $(i+1) }}') '''sudo''' /sbin/ifconfig $1 0.0.0.0 up '''sudo''' /usr/sbin/brctl addif ${switch} $1 exit 0 </nowiki></pre> 6. We need a wrapper script for launching kvm. I put this script in ~/bin and call it '''kvm-bridge'''. If ~/bin is on your path you can call the command directly with '''kvm-bridge'''. This script was modified from a number of sources <pre><nowiki> #!/usr/bin/env bash # script to manage tap interface allocation # for linux kernels >= 2.6.18 # modified by bodhi.zazen from : # http://calamari.reverse-dns.net:980/cgi-bin/moin.cgi/FrequentlyAskedQuestions#head-2511814cb92c14dbe1480089c04f83c281117a86 # http://ubuntuforums.org/showthread.php?t=528046 # http://www.howtoforge.com/using-kvm-on-ubuntu-gutsy-gibbon # set up a tap interface for qemu # USERID - uid qemu is being run under. USERID=`whoami` # generate a random mac address for the qemu nic # shell script borrowed from user pheldens @ qemu forum ranmac=$(echo -n DE:AD:BE:EF ; for i in `seq 1 2` ; \ do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done) # specify which NIC to use - see qemu.org for others # model=r8169 # Set model based on this how-to # http://www.howtoforge.com/using-kvm-on-ubuntu-gutsy-gibbon model=rtl8139 iface=`sudo tunctl -b -u $USERID` # start kvm with our parameters # echo "Bringing up interface $iface with mac address $ranmac" # nohup added to allow kvm to run independent of the terminal nohup kvm -net nic,vlan=0,macaddr=$ranmac -net tap,vlan=0,ifname=$iface $@ # kvm has stopped - no longer using tap interface sudo tunctl -d $iface &> /dev/null </nowiki></pre> 7. Set the executable bit on the new script you just created: <pre><nowiki> chmod 0755 ~/bin/kvm-bridge </nowiki></pre> 8. Modify sudoers to allow members of the kvm group to run the wrapper kvm-bridge and create a bridged network interface without running KVM as root. <pre><nowiki> visudo </nowiki></pre> Add these line at the end of the file : <pre><nowiki> # Allow members of the kvm group to configure a bridged virtual network interface %kvm ALL=(ALL) NOPASSWD: /sbin/ifconfig, /usr/sbin/brctl, /usr/sbin/tunctl </nowiki></pre> 9. Now start kvm from the command line. '''You do not need to declare a network interface''' <pre><nowiki> kvm-bridge -m 512 -hda disk.img -cdrom ubuntu.iso -boot -d -smp 2 </nowiki></pre> === iptables === Add these rules to iptables : <pre><nowiki> # allow incoming packets for kvm guest IPTABLES -A FORWARD -d $IPADDR_FROM_GUEST_OS -j ACCEPT # allow outgoing packets from kvm IPTABLES -A FORWARD -s $IPADDR_FROM_GUEST_OS -j ACCEPT </nowiki></pre> Change "$IPADDR_FROM_GUEST_OS" to the actual ip address of the kvm guest (I advise you configure your guests to have a static IP address). If you use ufw, add these rules to ''/etc/ufw/before.rules'' * See [https://help.ubuntu.com/8.04/serverguide/C/firewall.html Ubuntu Server Guide Firewall] <<Include(KVM/Header)>> ---- [[category:CategoryVirtualization]] [[category:UbuntuHelp]]
该页面使用的模板:
模板:From
(
查看源代码
)
模板:Languages
(
查看源代码
)(受保护)
模板:Languages/Lang
(
查看源代码
)(受保护)
返回至
UbuntuHelp:KVM/Directly
。