第1章 KVM安装部署
1.1 虚拟机/服务器开启虚拟化功能
1.1.1 开启BIOS虚拟化
1、 进入BIOS,选择【Configuration】选项,选择【Intel Virtual Technology】并回车。
提示:若无VT选项或不可更改,则表示你的电脑不支持VT技术。
2、将光标移动至【Enabled】处,并回车确定。
3、此时该选项将变为Enabled,最后按F10热键保存并退出即可开启VT功能。
1.1.2 升级虚拟机
如果VMware WorkStation版本升级过,则旧版本的虚拟机需要升级一下解决虚拟硬件兼容问题,才可以继续使用KVM功能,否则可能会产生未知错误。
1.1.3 虚拟机开启虚拟化
1.2 系统环境
<code>[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [[email protected] ~]# uname -r 3.10.0-327.el7.x86_64 [[email protected] ~]# getenforce Disabled [[email protected] ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) [[email protected] ~]# hostname -I 10.0.0.201 172.16.1.201 192.168.122.1 </code>
1.3 安装KVM
KVM已经集成在CentOS 7的系统内核中,所以不需要额外安装。
<code>[[email protected] ~]# lsmod | grep kvm # 检查系统中是否已经包含KVM模块 kvm_intel 162153 0 kvm 525259 1 kvm_intel [[email protected] ~]# grep -Eo '(vmx|svm)' /proc/cpuinfo | wc -l # 查看虚拟机核心数 4 </code>
1.4 安装qemu-kvm及相关软件作用
[[email protected] ~]# yum -y install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install [[email protected] ~]# rpm -qa|grep -E 'qemu|libvirt|virt' python-virtinst-0.600.0-29.el6.noarch # 一套Python的虚拟机安装工具 gpxe-roms-qemu-0.9.7-6.14.el6.noarch # 虚拟机iPXE的启动固件,支持虚拟机从网络启动 virt-manager-0.9.0-29.el6.x86_64 # 基于Libvirt的图像化虚拟机管理软件,需要图形界面操作系统 qemu-img-0.12.1.2-2.479.el6.x86_64 # 用于操作虚拟机硬盘镜像的创建、查看和格式转化 libvirt-python-0.10.2-54.el6.x86_64 # libvirt为Python提供的API qemu-kvm-0.12.1.2-2.479.el6.x86_64 # KVM在用户空间运行的程序 qemu-kvm-tools-0.12.1.2-2.479.el6.x86_64 libvirt-0.10.2-54.el6.x86_64 # 用于管理虚拟机,它提供了一套虚拟机操作API libvirt-client-0.10.2-54.el6.x86_64 # Libvirt的客户端,最重要的功能之一就是就在宿主机关机时可以# 通知虚拟机也关机,使虚拟机系统正常关机,而不是被强制关机防止造成数据丢失 virt-top-1.0.4-3.15.el6.x86_64 # 类似于top命令,查看虚拟机的资源使用情况 virt-what-1.11-1.2.el6.x86_64 # 在虚拟机内部执行,查看虚拟机运行的虚拟化平台 virt-viewer-0.5.6-8.el6.x86_64 # 显示虚拟机的控制台console
1.5 启动Libvirtd
<code>[[email protected] ~]# systemctl enable libvirtd Created symlink from /etc/systemd/system/sockets.target.wants/virtlockd.socket to /usr/lib/systemd/system/virtlockd.socket. [[email protected] ~]# systemctl start libvirtd [[email protected] ~]# systemctl status libvirtd ● libvirtd.service - Virtualization daemon Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2017-10-23 09:47:17 CST; 26min ago Docs: man:libvirtd(8) http://libvirt.org Main PID: 2141 (libvirtd) CGroup: /system.slice/libvirtd.service ├─1433 /sbin/dnsmasq --conf-file=/... ├─1434 /sbin/dnsmasq --conf-file=/... └─2141 /usr/sbin/libvirtd Oct 23 09:47:12 CentOS7-GUI systemd[1]: Start... Oct 23 09:47:17 CentOS7-GUI libvirtd[2141]: 2... Oct 23 09:47:17 CentOS7-GUI libvirtd[2141]: 2... Oct 23 09:47:17 CentOS7-GUI libvirtd[2141]: 2... Oct 23 09:47:17 CentOS7-GUI systemd[1]: Start... Oct 23 09:47:18 CentOS7-GUI dnsmasq[1433]: re... Oct 23 09:47:18 CentOS7-GUI dnsmasq[1433]: re... Oct 23 09:47:18 CentOS7-GUI dnsmasq-dhcp[1433]: ... Oct 23 10:14:10 CentOS7-GUI systemd[1]: Start... Hint: Some lines were ellipsized, use -l to show in full. # libvirt自动创建了网卡 [[email protected] ~]# ifconfig | grep vnet vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 # libvirt自动启动了dnsmasq服务 [[email protected] ~]# ps -ef | grep dns nobody 1433 1 0 09:39 00:00:00 /sbin/dnsmasq --conf-file=/var/lib/libvirtdnsmasq /default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper root 1434 1433 0 09:39 00:00:00 /sbin/dnsmasq --conf-file=/var/lib/libvirtdnsmasq /default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper </code>
第2章 KVM的使用
2.1 创建虚拟机
2.1.1 创建硬盘
2.1.1.1 创建存放虚拟磁盘目录
<code>[[email protected] ~]# mkdir -p /server/kvm_data [[email protected] ~]# cd /server/kvm_data </code>
2.1.1.2 创建虚拟磁盘
<code>[[email protected] kvm_data]# qemu-img create -f qcow2 /server/kvm_data/moban.qcow2 20G Formatting '/server/kvm_data/moban.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off </code>
2.1.1.3 查看虚拟磁盘详细信息
<code>[[email protected] kvm_data]# qemu-img info moban.qcow2 image: moban.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 196K cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false </code>
2.1.2 安装虚拟机
2.1.2.1 挂载光盘镜像
2.1.2.2 导入光盘镜像到宿主机
- 使用xftp直接上传镜像
- 使用dd命令导入镜像
[[email protected] ~]# mkdir -p /server/tools/ [[email protected] ~]# dd if=/dev/sr0 of=/server/tools/CentOS-7-x86_64-DVD-1511.iso
2.1.2.3 安装虚拟机
<code>[[email protected] ~]# virt-install --virt-type=kvm --name=CentOS7.2-moban --vcpus=1 -r 1024 -c /server/tools/CentOS-7-x86_64-DVD-1511.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel7 --disk path=/server/kvm_data/moban.qcow2,size=20,format=qcow2 </code>
2.1.2.4 virt-install命令参数
<code>[[email protected] ~]# virt-install --help usage: virt-install --name NAME --memory MB STORAGE INSTALL [options] 从指定安装源创建新虚拟机。 optional arguments: -h, --help show this help message and exit --version show program's version number and exit --connect URI 通过 libvirt URI 连接到虚拟机管理程序 通用选项: -n NAME, --name NAME 客户机实例名称,虚拟机名称 -r --memory MEMORY 为客户机分配内存大小. 例如: --memory 1024 (in MiB) --memory 512,maxmemory=1024 --memory 512,maxmemory=1024,hotplugmemorymax=2048,hotplugmemoryslots=2 --vcpus VCPUS 配置客户机虚拟 CPU(vcpu) 数量。例如:--vcpus 5 --vcpus 5,maxcpus=10,cpuset=1-4,6,8 --vcpus sockets=2,cores=4,threads=2 --cpu CPU CPU 模型和特性。例如:--cpu coreduo,+x2apic或 --cpu host --metadata METADATA 配置客户机元数据。例如: --metadata name=foo,title="My pretty title",uuid=... --metadata description="My nice long description" 安装方法选项: -c --cdrom CDROM 光驱安装介质 -l LOCATION, --location LOCATION 安装源 (例如:nfs:host:/path, http://host/path,ftp://host/path) --pxe 使用 PXE 协议从网络引导 --import 在已有的磁盘镜像中构建客户机 --livecd 将光驱介质视为 Live CD -x EXTRA_ARGS, --extra-args EXTRA_ARGS 将附加参数添加到由 --location 引导的内核中 --initrd-inject INITRD_INJECT 添加指定文件到由 --location 指定的 initrd根中 --os-type=DISTRO_TYPE 虚拟机的类型,例如:'linux','unix'.'windows'等 --os-variant DISTRO_VARIANT 在客户机上安装的操作系统,例如:'fedora18'、'rhel6'、'winxp' 等。 --boot BOOT 配置客户机引导设置。例如: --boot hd,cdrom,menu=on --boot init=/sbin/init (针对容器) --idmap IDMAP 为 LXC 容器启用用户名称空间。例如: --idmap uid_start=0,uid_target=1000,uid_count=10 设备选项: --disk DISK 指定存储的各种选项。例如:--disk size=10 (在默认位置创建 10GiB 镜像) --disk /my/existing/disk,cache=none --disk device=cdrom,bus=scsi --disk= --nodisks 不使用磁盘 -w NETWORK, --network NETWORK 配置客户机网络接口。例如: --network bridge=mybr0 --network network=my_libvirt_virtual_net --network network=mynet,model=virtio,mac=00:11... --network none --network help --nonetworks 不为虚拟机创建网络接口 --graphics GRAPHICS 配置客户机显示设置。例如:--graphics vnc --graphics spice,port=5901,tlsport=5902 --graphics none --graphics vnc,password=foobar,port=5910,keymap=ja --controller CONTROLLER 配置客户机控制器设备。例如:--controller type=usb,model=ich9-ehci1 --input INPUT 配置客户机输入设备。例如: --input tablet --input keyboard,bus=usb --serial SERIAL 配置客户机串口设备 --parallel PARALLEL 配置客户机并口设备 --channel CHANNEL 配置客户机通信通道 --console CONSOLE 配置文本控制台连接主机与客户机 --hostdev HOSTDEV 配置物理 USB/PCI 等主机设备与客户机共享 --filesystem FILESYSTEM 传递主机目录到客户机。例如: --filesystem /my/source/dir,/dir/in/guest --filesystem template_name,/,type=template --sound [SOUND] 配置客户机声音设备仿真 --watchdog WATCHDOG 配置客户机 watchdog 设备 --video VIDEO 配置客户机视频硬件。 --smartcard SMARTCARD 配置客户机智能卡设备。例如:--smartcard mode=passthrough --redirdev REDIRDEV 配置客户机重定向设备。例如:--redirdev usb,type=tcp,server=192.168.1.1:4000 --memballoon MEMBALLOON 配置客户机 memballoon 设备。例如:--memballoon model=virtio --tpm TPM 配置客户机 TPM 设备。例如:--tpm /dev/tpm --rng RNG 配置客户机随机数生成器设备。例如: --rng /dev/random --panic PANIC 配置客户机 panic 设备。例如:--panic default --memdev MEMDEV Configure a guest memory device. Ex: --memdev dimm,target_size=1024 客户机配置选项: --security SECURITY 设置域安全驱动配置。 --numatune NUMATUNE 为域进程调整 NUMA 策略。 --memtune MEMTUNE 为域进程调整内存策略。 --blkiotune BLKIOTUNE 为域进程调整 blkio 策略。 --memorybacking MEMORYBACKING 为域进程设置内存后备策略。例如:--memorybacking hugepages=on --features FEATURES 设置域 <features> XML。例如:--features acpi=off --features apic=on,eoi=on --clock CLOCK 设置域 <clock> XML。例如:--clock offset=localtime,rtc_tickpolicy=catchup --pm PM 配置 VM 电源管理功能 --events EVENTS 配置 VM 生命周期管理策略 --resource RESOURCE 配置 VM 资源分区(cgroups) --sysinfo SYSINFO Configure SMBIOS System Information. Ex: --sysinfo emulate --sysinfo host --sysinfo bios_vendor=Vendor_Inc.,bios_version=1.2.3-abc,... --sysinfo system_manufacturer=System_Corp.,system_product=Computer,... --sysinfo baseBoard_manufacturer=Baseboard_Corp.,baseBoard_product=Motherboard,... --qemu-commandline QEMU_COMMANDLINE Pass arguments directly to the qemu emulator. Ex: --qemu-commandline='-display gtk,gl=on' --qemu-commandline env=DISPLAY=:0.1 虚拟化平台选项: -v, --hvm 这个客户机应该是一个全虚拟化客户机 -p, --paravirt 这个客户机应该是一个半虚拟化客户机 --container 这个客户机应该是一个容器客户机 --virt-type HV_TYPE 要使用的管理程序名称 (kvm, qemu, xen, ...) --arch ARCH 模拟 CPU 架构 --machine MACHINE 机器类型为仿真类型 -u UUID 指定UUID 其它选项: --autostart 主机启动时自动启动域。 --transient Create a transient domain. --wait WAIT 请等待数分钟以便完成安装。 --noautoconsole 不要自动尝试连接到客户端控制台 --noreboot 安装完成后不启动客户机。 --print-xml [XMLONLY] 打印生成的 XML 域,而不是创建客户机。 --dry-run 运行安装程序,但不创建设备或定义客户机。 --check CHECK 启用或禁用验证检查。例如:--check path_in_use=off或--check all=off -q, --quiet 抑制非错误输出 -d, --debug 输入故障排除信息 </code>
2.1.3 安装错误汇总
2.1.3.1 locale.Error:unsupported locale setting
【问题描述】:
【原因分析】:
字符集错误。
【解决方法】:
修改字符集为UTF-8:
<code>localectl set-locale LANG=en_US.UTF-8 </code>
2.1.3.2 No domains available for virt type 'hvm',arch 'x86_64',domain type 'kvm'
【问题描述】:
<code>No domains available for virt type 'hvm',arch 'x86_64',domain type 'kvm' </code>
【原因分析】:
BIOS的硬件虚拟化没有打开,或者硬件不支持虚拟化。
【解决方法】:
开启BIOS硬件虚拟化或更换支持虚拟化的硬件。
2.1.3.3 虚拟机不显示图形化安装界面
【问题描述】:
通过VNC viewer或TightVNC连接KVM虚拟机时不显示图形化的安装界面:
【原因分析】:
虚拟机分配内存太小导致安装时无法显示图形界面。
【解决方法】:
修改创建的虚拟机内存大于1GB。
2.2 查看安装的虚拟机
2.2.1 查看新建虚拟机的状态及配置信息
<code>[[email protected] ~]# virsh list # 查看正在运行的虚拟机 Id Name State ---------------------------------------------------- [[email protected] ~]# virsh list --all # 查看所有的虚拟机 Id Name State ---------------------------------------------------- 2 CentOS7.2-moban shut off [[email protected] ~]# cd /etc/libvirt/qemu/ # 存放虚拟机配置文件目录 [[email protected] qemu]# ls CentOS7.2-moban.xml networks [[email protected] qemu]# vim CentOS7.2-moban.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 CentOS7.2-moban or other application using the libvirt API. --> <domain type='kvm'> <name>CentOS7.2-moban</name> <uuid>509d0a3c-b5b4-40a6-8f82-2bbb52cdc2fd</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'/> </os> <features> <acpi/> <apic/> </features> <cpu mode='custom' match='exact' check='partial'> <model fallback='allow'>Broadwell</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>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='/server/kvm_data/moban.qcow2'/> <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='network'> <mac address='52:54:00:df:fa:2d'/> <source network='default'/> <model type='virtio'/> <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> <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='tablet' bus='usb'> <address type='usb' bus='0' port='1'/> </input> <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> <model type='cirrus' vram='16384' heads='1' primary='yes'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </memballoon> </devices> </domain> </code>
2.2.2 查看新创建虚拟机的虚拟磁盘信息
<code># qcow2是用多少占多少,因此优化完系统后压缩磁盘空间,利于存储 [[email protected] ~]# ll -h /server/kvm_data/moban.qcow2 -rw-r--r-- 1 qemu qemu 1.1G Oct 23 15:03 /server/kvm_data/moban.qcow2 # 查看qcow2虚拟磁盘的详细信息 [[email protected] ~]# qemu-img info /server/kvm_data/moban.qcow2 image: /server/kvm_data/moban.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 1.0G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false </code>
2.3 启动虚拟机
<code>[[email protected] ~]# virsh start CentOS7.2-moban [[email protected] ~]# virsh list # 查看虚拟机运行状态 Id Name State ---------------------------------------------------- 2 CentOS7.2-moban running [[email protected] ~]# netstat -lntup | grep kvm # 查看虚拟机使用的端口 tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 43456/qemu-kvm [[email protected] ~]# ps -ef | grep [k]vm # 查看虚拟机进程 root 765 2 0 09:38 00:00:00 [kvm-irqfd-clean] qemu 43456 1 0 12:06 00:00:54 /usr/libexec/qemu-kvm -name CentOS7.2-clone -S -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off -cpu Broadwell,+rtm,+hle -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid ebefe196-7d06-4b94-8ba9-5135b8d0be25 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-3-CentOS7.2-clone/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=/server/kvm_data/clone.qcow2,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=25,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:df:fa:2e,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-3-CentOS7.2-clone/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 usb-tablet,id=input0,bus=usb.0,port=1 -vnc 0.0.0.0:0 -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -msg timestamp=on </code>
2.4 查看虚拟机相关信息
<code>[[email protected] ~]# ssh 192.168.122.84 # 通过宿主机的命令行连接到KVM虚拟机中 [email protected]'s password: Last login: Mon Oct 23 11:15:46 2017 from 192.168.122.1 [[email protected] ~]# cat /proc/cpuinfo | grep name # 查看CPU信息 model name : Intel Core Processor (Broadwell) [[email protected] ~]# lscpu # 查看CPU信息 Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 1 On-line CPU(s) list: 0 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 61 Model name: Intel Core Processor (Broadwell) Stepping: 2 CPU MHz: 2712.002 BogoMIPS: 5424.00 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0 [[email protected] ~]# free -m # 查看内存信息 total used free shared buff/cache available Mem: 993 93 773 6 126 767 Swap: 2047 0 2047 [[email protected] ~]# df -h # 查看磁盘信息 Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 18G 855M 17G 5% / devtmpfs 487M 0 487M 0% /dev tmpfs 497M 0 497M 0% /dev/shm tmpfs 497M 6.6M 491M 2% /run tmpfs 497M 0 497M 0% /sys/fs/cgroup /dev/vda1 497M 108M 390M 22% /boot tmpfs 100M 0 100M 0% /run/user/0 </code>
2.5 虚拟机的管理命令
管理命令都是调用libvirtd的API接口。
2.5.1 常用管理命令
命令 | 作用 |
---|---|
virt-install | 生成KVM虚拟机 |
virsh list | 查看在运行的虚拟机 |
virsh list --all | 查看所有虚拟机 |
virsh dumpxml name | 查看KVM虚拟机配置文件 |
virsh start name | 启动KVM虚拟机 |
virsh shutdown name | 正常关机 |
virsh destroy name | 非正常关机(相当于物理机直接拔掉电源) |
virsh undefine name | 彻底删除,如果需要恢复则备份/etc/libvirt/qemu的xml文件 |
virsh define file-name.xml | 根据配置文件定义虚拟机 |
virsh suspend name | 挂起,终止 |
virsh resume name | 恢复挂起状态 |
2.5.2 virsh命令行选项快速参考
命令 | 描述 |
---|---|
help | 打印基本帮助信息 |
list | 查看在运行的虚拟机 |
dumpxml | 查看KVM虚拟机配置文件 |
create | 从XML配置文件生成客户端并启动新客户端 |
start | 启动KVM虚拟机 |
destroy | 强制关机 |
define | 为客户端输出XML配置文件 |
domid | 显示客户端ID |
domuuid | 显示客户端UUID |
dominfo | 显示客户端信息 |
domname | 显示客户端名称 |
domstate | 显示客户端状态 |
quit | 退出这个互动终端 |
reboot | 重新启动客户端 |
restore | 恢复以前保存在文件中的客户端 |
resume | 恢复暂停的客户端 |
save | 将客户端当前状态保存到某个文件中 |
shutdown | 正常关机 |
suspend | 暂停客户端 |
undefine | 删除与客户端关联的所有文件 |
migrate | 将客户端迁移到另一台主机中 |

我的微信
如果有技术上的问题可以扫一扫我的微信