命令行安装kvm虚拟机、桥接网络、用virt-manager管理

命令安装kvm虚拟机、桥接⽹络、⽤virt-manager管理
宿主机CentOS Linux release 7.2.1511 (Core),内核3.10.0-327.el7.x86_64
1、配置宿主机⽹络桥接
想让虚拟机有⾃⼰的ip且外⽹可访问,需要在安装虚拟机前配置宿主机⽹络(桥接BRIDGE):
在⽬录/etc/sysconfig/network-scripts下,将原始的ifcfg-enp2s0重命名ifcfg-enp2s0.old当备份,然后新建ifcfg-enp2s0内容如下:
TYPE=Ethernet
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
NAME=enp2s0
DEVICE=enp2s0
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
再新建ifcgf-br0内容如下:
TYPE=Bridge
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=br0
DEVICE=br0
NM_CONTROLLED=no
ONBOOT=yes
IPADDR=223.
PREFIX=25
GATEWAY=223.
DNS1=8.8.8.8
IPV6_PRIVACY=no
ZONE=public
重启⽹络:systemctl restart network
有时候会报错:
Restarting network (via systemctl):  Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
ifconfig看到如下信息(要是不对可以尝试重启机器...)
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 223.  netmask 255.  broadcast 223.
inet6 fe80::4a5b:39ff:fea4:3ef0  prefixlen 64  scopeid 0x20<link>
ether 48:5b:39:a4:3e:f0  txqueuelen 0  (Ethernet)
RX packets 4016129  bytes 379659216 (362.0 MiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 7392459  bytes 11078132347 (10.3 GiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet6 fe80::4a5b:39ff:fea4:3ef0  prefixlen 64  scopeid 0x20<link>
ether 48:5b:39:a4:3e:f0  txqueuelen 1000  (Ethernet)
RX packets 4260879  bytes 791804516 (755.1 MiB)
RX errors 0  dropped 1092  overruns 0  frame 0
TX packets 7528373  bytes 11088764715 (10.3 GiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
2、命令⾏安装kvm虚拟机
安装必要的软件这⾥就不说了...
ubuntu下(前两条⽤于安装,第三条⽤于启动):
1 qemu-img create -f qcow
2 /home/kvm/centos7-1511.img 20G
2 qemu-system-x86_64 -m 2048 -enable-kvm -hda /home/kvm/centos7-1511.img -cdrom /home/isos/CentOS-7-x86_64-DVD-1511.iso -boot d
3 qemu-system-x86_6
4 -enable-kvm -m 1024 -boot menu=on /home/kvm/centos7-1511.img
centos下:
1 qemu-img create -f qcow
2 /home/kvm/centos7-1511.img 20G
2 /usr/libexec/qemu-kvm -m 2048 -enable-kvm -hda /home/kvm/centos7-1708.img -cdrom /home/isos/CentOS-7-x86_64-DVD-1511.iso -boot d
3 /usr/libexec/qemu-kvm -enable-kvm -m 102
4 -boot menu=on /home/kvm/centos7-1511.img
如果⽤ubuntu下的命令2在centos下运⾏,会出现问题:
1、bash: qemu-system-x86_64: 未到命令...
解决:qemu-system-x86_64是在安装qemu(注意不是qemu-kvm)时⽣成的命令,⽽centos下默认安装的是qemu-kvm包,对应的命令是qemu-kvm。通过rpm -ql qemu-kvm查看输出:/usr/libexec/qemu-kvm,⽤此替换得到centos下正确的命令2。
正确命令2运⾏后,输出信息是:
br16
2、VNC server running on `::1:5900'
解决:在xshell下运⾏:vncviewer :5900,会出现
....
Can't open display:
所以⽤vncviewer连接到⽬标主机,然后在图形界⾯下运⾏vncviewer :5900,即会跳出安装centos的界⾯,顺着安装即可(记得在SOFTWARE SELECTION选择要最⼩安装还是图形界⾯)
3、让virt-manager可以管理刚才新建的虚拟机
运⾏完上述的命令1和2后,如果不想每次都通过命令3启动虚拟机,可以通过libvirt管理(这样以后直接virsh start 虚拟机名,即可开启虚拟机)
libvirt(包括virsh)使⽤xml⽂件对虚拟机进⾏配置,其中包括虚拟机名称、分配内存、vcpu等多种信息
下⾯是⼀个名为l的xml⽂件,其中定义了⼀个名为centos7-1511的kvm虚拟机(如果是32位要写i686)
<domain type='kvm' xmlns:qemu='/schemas/domain/qemu/1.0'>
<name>centos7-1511</name>
<uuid>af939aef-c0eb-4b83-96ca-c23aecd3e057</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes'/>
<bios useserial='yes' rebootTimeout='0'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom'>
<model fallback='allow'></model>
</cpu>
<clock offset='utc'>
蓝领人才<timer name='rtc' tickpolicy='catchup'/>
温州眼镜大王
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/home/kvm/centos7-1511.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>
<disk type='block' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='network'>
<mac address='48:5b:39:a4:3e:f1'/>
<source network='default'/>
<model type='e1000'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'/>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>sciencechina
<model type='cirrus' vram='16384' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>
然后运⾏下⾯的命令即可通过virt-manager管理名为centos7-1511的kvm虚拟机
1 [root@localhost kvm]# virsh l
定义域 centos7-1511(从 l)
2 [root@localhost kvm]# virsh list --all
Id    名称状态
----------------------------------------------------
-    centos7-1511                  关闭
3 [root@localhost kvm]# virsh start centos7-1511
域 centos7-1511 已开始
命令3可能会有问题:
[root@localhost kvm]# virsh start centos7-1511
错误:开始域 centos7-1511 失败
错误:internal error: process exited while connecting to monitor: qemu-kvm: -machine pc-i440fx-2.0,accel=kvm,usb=off,dump-guest-core=off: Unsupported machine type Use -machine help to list supported machines!
通过以下命令:
[root@localhost kvm]# /usr/libexec/qemu-kvm -machine help
Supported machines are:
none                empty machine
pc                  RHEL 7.0.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.0.0)
pc-i440fx-rhel7.0.0  RHEL 7.0.0 PC (i440FX + PIIX, 1996) (default)
rhel6.6.0            RHEL 6.6.0 PC
rhel6.5.0            RHEL 6.5.0 PC
rhel6.4.0            RHEL 6.4.0 PC
rhel6.3.0            RHEL 6.3.0 PC
rhel6.2.0            RHEL 6.2.0 PC
rhel6.1.0            RHEL 6.1.0 PC
rhel6.0.0            RHEL 6.0.0 PC
将xml⽂件中machine部分替换,然后通过下⾯的undefine命令取消刚才的虚拟机,再重新运⾏上⾯的define即可
可通过:
virsh undefine centos7-1511取消定义虚拟机,之后系统⽆法识别centos7-1511(此步骤与virsh l对应)
virsh destroy centos7-1511直接销毁虚拟机,取消定义,之后⽆法执⾏undefine
注意:⽤virt-manager第⼀次启动虚拟机时别忘了修改为桥接,否则即使你在虚拟机⾥配置了ip等信息,ping⽹关啥的都会出现:Destination Host Unreachable
4、虚拟机⽹络配置
修改⽹络配置⽂件:
1、ubuntu虚拟机中修改⽹络配置⽂件(因为有时候图形界⾯操作时,会出现⽆法save的情况)
修改/etc/network/interfaces⽂件为:
# eth0是你虚拟机的⽹卡,可以通过ifconfig查看
auto eth0
iface eth0 inet static
#ip
address 223.
# ⼦⽹和⽹关
netmask 255.
成长的烦恼初中作文gateway 223.
修改/etc/f.d/base⽂件,设置dns
nameserver 8.8.8.8
重启虚拟机,通过ifconfig看到信息:
eth0      Link encap:Ethernet  HWaddr 52:54:00:7c:da:c9
inet addr:223.  Bcast:223.  Mask:255.
inet6 addr: fe80::5054:ff:fe7c:dac9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:219111 errors:0 dropped:0 overruns:0 frame:0
TX packets:9266 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:31389489 (31.3 MB)  TX bytes:761327 (761.3 KB)
姜堰市溱潼中学
lo        Link encap:Local Loopback
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:65536  Metric:1
RX packets:64 errors:0 dropped:0 overruns:0 frame:0
TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:5632 (5.6 KB)  TX bytes:5632 (5.6 KB)
此时⽤xshell连接,会出现connection failed
因为ubuntu虚拟机中必须安装openssh-server软件,这样才可以通过ssh连接
安装命令:
1 sudo apt-get install openssh-server
2 sudo /etc/init.d/ssh restart  //重启ssh服务
安装好后,⽤以下命令查看22端⼝是否处于LISTEN状态,如果是,则说明已经配置好了ubuntu的ssh服务
root@jin:/home/jin# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address          Foreign Address        State      PID/Program name
tcp        000.0.0.0:220.0.0.0:*              LISTEN      2645/sshd
2、centos下
这个没试,下⾯是通过图形界⾯配置后,相应⽂件内容
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# cat ifcfg-ens3
TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="ens3"
UUID="3b886635-d2a9-4569-9763-7468890a64d8" DEVICE="ens3"
ONBOOT="yes"
DNS1="8.8.8.8"
IPADDR=223.
PREFIX=25
GATEWAY=223.
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no
补充
1、桥接和NAT
。。。。
2、创建虚拟机时各命令含义
。。。。占坑之后写
3、libvirt
。。。

本文发布于:2024-09-23 19:26:13,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/104030.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:安装   命令   配置   定义   新建   出现
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议