kvm--虚拟机管理

1. KVM基本管理

安装完虚拟机,要退出虚拟机使用快捷键ctrl + ]
完成虚拟机安装,已经退回到宿主机,接下来我们来通过宿主机virsh命令进行管理刚才安装的CentOS 7虚拟机。

查看下挂载情况
[root@kvm ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 5.6G 12G 32% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 913M 0 913M 0% /dev/shm
tmpfs 913M 8.6M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sdb 50G 1.2G 46G 3% /kvm_data
/dev/sda1 497M 126M 372M 26% /boot
tmpfs 183M 0 183M 0% /run/user/0

[root@kvm ~]# ls /kvm_data/
lost+found test.img

查看kvm进程
[root@kvm ~]# ps aux |grep kvm
root        839  0.0  0.0      0     0 ?        S<   01:19   0:00 [kvm-irqfd-clean]
qemu      22188  2.3 27.1 1822572 507692 ?      Sl   02:08   0:43 /usr/libexec/qemu-kvm -name test -S -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off -cpu Broadwell-IBRS,+md-clear,+ssbd,-hle,-rtm,+spec-ctrl -m 1024 -realtime mlock=off -smp 1,maxcpus=2,sockets=2,cores=1,threads=1 -uuid f237969e-edb6-4b8d-a235-41f91e7813fe -display none -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-2-test/monitor.sock,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot strict=on -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x5 -drive file=/kvm_data/test.img,format=qcow2,if=none,id=drive-virtio-disk0 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x6,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=28 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:00:0f:8c,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/channel/target/domain-2-test/org.qemu.guest_agent.0,server,nowait -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -object rng-random,id=objrng0,filename=/dev/urandom -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pci.0,addr=0x8 -msg timestamp=on
root      22194  0.0  0.0      0     0 ?        S    02:08   0:00 [kvm-pit/22188]
root      32519  0.0  0.0 112820   968 pts/0    S+   02:38   0:00 grep --color=auto kvm
查看虚拟机列表,只能看到运行的虚拟机
[root@kvm ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     test                           running

查看虚拟机列表,包括未运行的虚拟机
[root@kvm ~]#  virsh list --all 
 Id    Name                           State
----------------------------------------------------
 2     test                           running
[root@kvm ~]# virsh console test
Connected to domain test
Escape character is ^]
//如果迟迟未动敲下回车就ok

下面就是正常登录时需要输入的用户名密码
CentOS Linux 7 (Core)
Kernel 3.10.0-327.el7.x86_64 on an x86_64

localhost login: root
password:123456
若发生以下报错信息,命令如下:
[root@kvm ~]# virsh console test
Connected to domain test
Escape character is ^]
error: operation failed: Active console session exists for this domain
解决方式,命令如下:
[root@kvm ~]# ps -ef |grep console
root      11167  10987  0 01:55 tty1     00:00:00 virsh console test
root      16017  15750  0 10:01 pts/1    00:00:00 grep --color=auto console
[root@kvm ~]# kill -9 11167
//杀死进程virsh console test
查看网卡情况
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:00:0f:8c brd ff:ff:ff:ff:ff:ff
  
 在指定网络接口上发出DHCP请求: 
[root@localhost ~]# dhclient eth0

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:00:0f:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.153.129/24 brd 192.168.153.255 scope global dynamic eth0
       valid_lft 1803sec preferred_lft 1803sec
    inet6 fe80::5054:ff:fe00:f8c/64 scope link 
       valid_lft forever preferred_lft forever

[root@localhost ~]# yum install -y net-tools
在这里插入图片描述

ifconfig命令用于显示或设置网络设备。
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.153.129  netmask 255.255.255.0  broadcast 192.168.153.255
        inet6 fe80::5054:ff:fe00:f8c  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:0f:8c  txqueuelen 1000  (Ethernet)
        RX packets 1127  bytes 13082612 (12.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 758  bytes 46531 (45.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 free -m:以m为单位来显示内存的信息。
[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            480         101         112           6         266         227
Swap:          1023           0        1023
查看cpu信息
[root@localhost ~]# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 61
model name      : Intel Core Processor (Broadwell, IBRS)
stepping        : 2
microcode       : 0x1
cpu MHz         : 2419.202
cache size      : 4096 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch fsgsbase bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap xsaveopt
bogomips        : 4838.40
clflush size    : 64
cache_alignment : 64
address sizes   : 45 bits physical, 48 bits virtual
power management:
查看文件系统类型情况
[root@localhost ~]# df -T
文件系统       类型       1K-块   已用    可用 已用% 挂载点
/dev/vda3      xfs      8913920 944116 7969804   11% /
devtmpfs       devtmpfs  499072      0  499072    0% /dev
tmpfs          tmpfs     508368      0  508368    0% /dev/shm
tmpfs          tmpfs     508368   6672  501696    2% /run
tmpfs          tmpfs     508368      0  508368    0% /sys/fs/cgroup
/dev/vda1      xfs       508588 119892  388696   24% /boot
tmpfs          tmpfs      49248      0   49248    0% /run/user/0
显示目前磁盘空间和使用情况
[root@localhost ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/vda3       8.6G  923M  7.7G   11% /
devtmpfs        488M     0  488M    0% /dev
tmpfs           497M     0  497M    0% /dev/shm
tmpfs           497M  6.6M  490M    2% /run
tmpfs           497M     0  497M    0% /sys/fs/cgroup
/dev/vda1       497M  118M  380M   24% /boot
tmpfs            49M     0   49M    0% /run/user/0
以树形格式列出所有块设备
[root@localhost ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom  
vda    253:0    0   10G  0 disk 
├─vda1 253:1    0  500M  0 part /boot
├─vda2 253:2    0    1G  0 part [SWAP]
└─vda3 253:3    0  8.5G  0 part /

退出终端在虚拟机上使用 top命令
用于实时显示 process 的动态
在这里插入图片描述

查看qemu-kvm的进程
[root@kvm ~]# ps -ef |grep qemu-kvm
qemu      22188      1  1 02:08 ?        00:00:55 /usr/libexec/qemu-kvm -name test -S -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off -cpu Broadwell-IBRS,+md-clear,+ssbd,-hle,-rtm,+spec-ctrl -m 1024 -realtime mlock=off -smp 1,maxcpus=2,sockets=2,cores=1,threads=1 -uuid f237969e-edb6-4b8d-a235-41f91e7813fe -display none -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-2-test/monitor.sock,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot strict=on -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x5 -drive file=/kvm_data/test.img,format=qcow2,if=none,id=drive-virtio-disk0 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x6,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=28 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:00:0f:8c,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/channel/target/domain-2-test/org.qemu.guest_agent.0,server,nowait -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -object rng-random,id=objrng0,filename=/dev/urandom -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pci.0,addr=0x8 -msg timestamp=on
root      32697   1773  0 03:14 pts/0    00:00:00 grep --color=auto qemu-kvm
centos 7虚拟机的一些常用命令:
virsh shutdown test //关闭虚拟机
virsh start test //开启虚拟机
virsh destroy test//类似stop,这个是强制停止
virsh undefine test//彻底销毁虚拟机,会删除虚拟机配置文件,virsh list --all就看不到了
ls /etc/libvirt/qemu/  //可以查看虚拟机配置文件
virsh autostart test//宿主机开机该虚拟机也开机
virsh autostart --disable test//解除开机启动
virsh suspend test//挂起
virsh resume test//恢复
查看虚拟机配置文件
[root@kvm ~]# ls /etc/libvirt/qemu
networks  test.xml
[root@kvm ~]# cd !$ // !$意思是上一条命令的后半段
cd /etc/libvirt/qemu

[root@kvm ~]# cd !$ // !$意思是上一条命令的后半段
cd /etc/libvirt/qemu

查看虚拟机配置文件具体内容
[root@kvm qemu]# cat test.xml

[root@kvm qemu]# cat  test.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit test
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>test</name>
  <uuid>f237969e-edb6-4b8d-a235-41f91e7813fe</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <vcpu placement='static' current='1'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Broadwell-noTSX-IBRS</model>
    <feature policy='require' name='md-clear'/>
    <feature policy='require' name='spec-ctrl'/>
    <feature policy='require' name='ssbd'/>
  </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>destroy</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='/kvm_data/test.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' 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' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' 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>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:00:0f:8c'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </rng>
  </devices>
</domain>

版权声明:本文为qq_51085767原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。