来源:天空软件网 更新:2023-09-14
用手机看
熟练掌握虚拟机的基本管理、克隆、快照、磁盘管理、配置调整、虚拟机迁移。
主机名 | IP地址 | CPU、内存 | 硬盘 | 网卡 |
kvm | 192.168.200.206 | 2vCPU开启虚拟化、4G | 20G+50G | net |
关闭防火墙、selinux、iptables、查看系统是否支持虚拟化
# systemctl stop firewalld# systemctl disable firewalldRemoved symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.# setenforce 0# vi /etc/selinux/configSELINUX=disabled# iptables -F# iptables -X# iptables -Z# iptables-save# Generated by iptables-save v1.4.21 on Wed Mar 10 18:10:10 2021*filter:INPUT ACCEPT <23:1560>:FORWARD ACCEPT <0:0>:OUTPUT ACCEPT <13:1272>COMMIT# Completed on Wed Mar 10 18:10:10 2021# iptables -nvLChain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destinationChain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destinationChain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination# 查看内存大小4G# freetotal used free shared buff/cache availableMem: 3866948 110560 3614428 8744 141960 3583752Swap: 2097148 0 2097148# 查看CPU是否支持虚拟化 vmx:intel\svm:AMD# cat /proc/cpuinfo |grep vmxflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xsavesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xsaves# cat /proc/cpuinfo |grep svm# 这里因为不是AMD的CPU,所以没有输出显示。
# 格式化硬盘# mkfs.ext4 /dev/sdbmke2fs 1.42.9 (28-Dec-2013)/dev/sdb is entire device, not just one partition!Proceed anyway? (y,n) yFilesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks3276800 inodes, 13107200 blocks655360 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=2162163712400 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,4096000, 7962624, 11239424Allocating group tables: doneWriting inode tables: doneCreating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done# 检查是否格式化成功# blkid /dev/sdb/dev/sdb: UUID="4f8488d6-1095-4dd8-af07-c7f58ef9a246" TYPE="ext4"# 挂载硬盘# mkdir /kvm_data# mount /dev/sdb /kvm_data/# 设置开机自动挂载# vi /etc/fstab/dev/sdb /kvm_data ext4 defaults 0 0
# yum install -y virt-* libvirt bridge-utils qemu-img
复制一块网卡,命名为br0
分别修改两块网卡内容,重启网卡
# cp ifcfg-eno16777736 ifcfg-br0# vi ifcfg-br0TYPE=BridgeBOOTPROTO=noneNAME=br0DEVICE=br0ONBOOT=yesIPADDR=192.168.200.206NETMASK=255.255.255.0GATEWAY=192.168.200.2DNS1=114.114.114.114# vi ifcfg-eno16777736TYPE=EthernetBOOTPROTO=staticNAME=eno16777736DEVICE=eno16777736ONBOOT=yesBRIDGE=br0# systemctl restart network# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWNlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000link/ether 00:0c:29:87:40:d7 brd ff:ff:ff:ff:ff:ff3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UPlink/ether 00:0c:29:87:40:d7 brd ff:ff:ff:ff:ff:ffinet 192.168.200.206/24 brd 192.168.200.255 scope global br0valid_lft forever preferred_lft foreverinet6 fe80::60e0:29ff:fe15:bfec/64 scope linkvalid_lft forever preferred_lft forever# 启动服务# systemctl start libvirtd# ps -ef |grep libvirtroot 14300 1 0 04:54 ? 00:00:00 /usr/sbin/libvirtdnobody 14406 1 0 04:55 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelperroot 14407 14406 0 04:55 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelperroot 14438 2673 0 04:55 pts/0 00:00:00 grep --color=auto libvirt# 查看网卡# brctl showbridge name bridge id STP enabled interfacesbr0 8000.000c298740d7 no eno16777736virbr0 8000.000000000000 yes
# virt-install --name=test --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --disk path=/kvm_data/test.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"ERROR Host does not support any virtualization options
安装虚拟机遇到一个报错:通过yum install -y openssl 解决
# 再次执行安装命令,执行成功,进入设置界面# virt-install --name=test --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --disk path=/kvm_data/test.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
第一个设置是设置语言、第二个设置是选择时区、第三个是设置安装源、第四个是软件的选择、第五个是安装得目标、第七个是network不需要设置、第八个是root密码、第九个是否要创建普通用户。
这里我们从1开始设置
输入1回车进入语言界面
回车显示全部、按68回车选择中文、按1显示简体中文
回车继续2选择亚洲、上海
回车继续选择5、安装目标
回车继续 使用所有的空间c
继续 标准分区选1
c继续
选8设置root密码
密码设置太简单,yes
按b开始安装
安装完成,回车退出,重启
CTRL+】 返回宿主机
检查虚拟机是否被挂载
# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 18G 5.4G 13G 31% /devtmpfs 1.9G 0 1.9G 0% /devtmpfs 1.9G 0 1.9G 0% /dev/shmtmpfs 1.9G 8.8M 1.9G 1% /runtmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup/dev/sdb 50G 1.2G 46G 3% /kvm_data/dev/sda1 497M 127M 370M 26% /boottmpfs 378M 0 378M 0% /run/user/0查看虚拟机占用的磁盘# ll -h /kvm_data/total 1.1Gdrwx------ 2 root root 16K Mar 11 04:13 lost+found-rw------- 1 qemu qemu 11G Mar 11 17:44 test.img
不加 –-all 只列举出开机状态的虚拟机,
# virsh listId Name State----------------------------------------------------2 test running
virsh start 虚拟机名字
# virsh console testConnected to domain testEscape character is ^>CentOS Linux 7 (Core)Kernel 3.10.0-327.el7.x86_64 on an x86_64# 输入用户名和密码即可localhost login:
dhclient eth0# dhclient eth0# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWNlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 52:54:00:50:b0:bc brd ff:ff:ff:ff:ff:ffinet 192.168.200.128/24 brd 192.168.200.255 scope global dynamic eth0valid_lft 1801sec preferred_lft 1801secinet6 fe80::5054:ff:fe50:b0bc/64 scope linkvalid_lft forever preferred_lft forever
virsh shutdown test //关闭虚拟机virsh start test //开启虚拟机
ls /etc/libvirt/qemu/
virsh destroy test/
virsh undefine test//彻底销毁虚拟机,会删除虚拟机配置文件,virsh list --all就看不到了
virsh autostart test//宿主机开机该虚拟机也开机virsh autostart --disable test//解除开机启动
virsh suspend test//挂起virsh resume test//恢复
先关闭虚拟机
# virsh shutdown testDomain test is being shutdown# 关闭后,就会发现console进不去虚拟机了# virsh console testerror: The domain is not running# 克隆速度很快-- original 源目标 --name 被克隆的名字 ---file 被克隆存放路径# virt-clone --original test --name test02 --file /kvm_data/test02.imgAllocating 'test02.img' | 10 GB 00:15Clone 'test02' created successfully.# 被克隆的虚拟机同样可以使用dhclient eth0获取IP地址。
raw格式的虚拟磁盘不支持做快照,qcow2支持
# virsh snapshot-create testDomain snapshot 1615482601 created
# qemu-img info /kvm_data/test.imgimage: /kvm_data/test.imgfile format: qcow2virtual size: 10G (10737418240 bytes)disk size: 1.1Gcluster_size: 65536Snapshot list:ID TAG VM SIZE DATE VM CLOCK1 1615482601 0 2021-03-12 01:10:01 00:00:00.000Format specific information:compat: 1.1lazy refcounts: true
列出快照
# virsh snapshot-list testName Creation Time State------------------------------------------------------------1615482601 2021-03-12 01:10:01 +0800 shutoff查看快照信息# qemu-img info /kvm_data/test.imgimage: /kvm_data/test.imgfile format: qcow2virtual size: 10G (10737418240 bytes)disk size: 1.1Gcluster_size: 65536Snapshot list:ID TAG VM SIZE DATE VM CLOCK1 1615482601 0 2021-03-12 01:10:01 00:00:00.000Format specific information:compat: 1.1lazy refcounts: true
# virsh snapshot-create testDomain snapshot 1615484184 created# virsh snapshot-current test<domainsnapshot><name>1615484184</name><state>shutoff</state><creationTime>1615484184</creationTime><memory snapshot='no'/><disks><disk name='vda' snapshot='internal'/><disk name='hda' snapshot='no'/></disks><domain type='kvm'><name>test</name><uuid>61e88908-c0c4-4aed-86a7-503f524f1b4b</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='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><controller type='pci' index='0' model='pci-root'/><interface type='bridge'><mac address='52:54:00:50:b0:bc'/><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></domainsnapshot>
# ls /var/lib/libvirt/qemu/snapshot/test1615484184.xml
# virsh snapshot-revert test 1615482601
# virsh snapshot-delete test 1615482601Domain snapshot 1615482601 deleted
查看虚拟磁盘格式,命令如下:
# qemu-img info /kvm_data/test.imgimage: /kvm_data/test.imgfile format: qcow2virtual size: 10G (10737418240 bytes)disk size: 1.1Gcluster_size: 65536Format specific information:compat: 1.1lazy refcounts: true
创建2 GB的RAW格式磁盘,命令如下:
# qemu-img create -f raw /kvm_data/test_1.img 2GFormatting '/kvm_data/test_1.img', fmt=raw size=2147483648RAW格式的磁盘转换为qcow2格式,命令如下:# qemu-img convert -O qcow2 /kvm_data/test_1.img/kvm_data/test_1.qcow2
查看test1.img大小,命令如下:
# ls -lh /kvm_data/test_1.img-rw-r--r-- 1 root root 2.0G May 3 15:11 /kvm_data/test_1.img# ls -lh /kvm_data/test_1.qcow2-rw-r--r-- 1 root root 193K May 3 15:38 /kvm_data/test_1.qcow2//可以看到qcow2文件比较小,raw文件大小和我们指定空间大小一样是2G//raw格式的磁盘性能比qcow2要好,但是raw格式的磁盘无法做快照
将test02转成raw格式,命令如下:
# qemu-img convert -O raw/kvm_data/test02.img /kvm_data/test02_2.raw
更改磁盘格式和文件路径,然后启动test02虚拟机,命令如下:
# virsh edit test02…………<disk type='file' device='disk'><driver name='qemu' type='qcow'/><source file='/kvm_data/test02.img'/><target dev='vda' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/></disk>//找到上面这部分将qcow修改为raw,/kvm_data/test02.img改为/kvm_data/test02_2.raw<disk type='file' device='disk'><driver name='qemu' type='raw'/><source file='/kvm_data/test02_2.raw'/><target dev='vda' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/></disk>………………
查看磁盘所属用户和组,命令如下:
# ls -l /kvm_data/total 3310628drwx------ 2 root root 16384 Apr 29 00:20 lost+found-rw-r--r-- 1 root root 10737418240 May 3 16:14 test02_2.raw-rw------- 1 root root 1133772800 May 3 16:02 test02.img-rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img-rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2-rw------- 1 root root 10739384832 May 3 14:14 test.img
启动虚拟机test02,然后再次查看磁盘所属用户和组,命令如下:
# virsh start test02Domain test02 started# ls -l /kvm_data/total 3310628drwx------ 2 root root 16384 Apr 29 00:20 lost+found-rw-r--r-- 1 qemu qemu 10737418240 May 3 16:14 test02_2.raw-rw------- 1 root root 1133772800 May 3 16:02 test02.img-rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img-rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2-rw------- 1 root root 10739384832 May 3 14:14 test.img//启动后所属组变化成qemu的是test02_2.raw,证明这个磁盘正在被使用
我们知道,在这里磁盘一共有两种格式,一种是RAW格式,一种是qcow2格式,接下来我们分别给这两种磁盘格式进行扩容。
首先扩容RAW格式,命令如下:
# qemu-img resize /kvm_data/test02_2.raw +2GImage resized.
查看test02_2.raw信息,命令如下:
# qemu-img info /kvm_data/test02_2.rawimage: /kvm_data/test02_2.rawfile format: rawvirtual size: 12G (12884901888 bytes)disk size: 1.0G
进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:
# virsh console test02# fdisk -lDisk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0005b865Device Boot Start End Blocks Id System/dev/vda1 * 2048 1026047 512000 83 Linux/dev/vda2 1026048 20971519 9972736 8e Linux LVMDisk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes//可以看到磁盘还是10 GB
退出终端,关闭test02虚拟机,重新启动,然后再次进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:
# virsh destroy test02Domain test02 destroyed# virsh start test02Domain test02 started# virsh console test02# fdisk -lDisk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0005b865Device Boot Start End Blocks Id System/dev/vda1 * 2048 1026047 512000 83 Linux/dev/vda2 1026048 20971519 9972736 8e Linux LVMDisk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes
可以看到已经变成12 GB了。
接下来,我们怎么使用新增加的这2 GB磁盘,那就是分区,命令如下:
# fdisk /dev/vdaWelcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Command (m for help): n//新建分区Partition type:p primary (2 primary, 0 extended, 2 free)e extendedSelect (default p): pPartition number (3,4, default 3)://按Enter键First sector (20971, default 20971520)://按Enter键Using default value 20971520Last sector, +sectors or +size{K,M,G} (20971, default 25165823)://按Enter键Using default value 25165823Partition 3 of type Linux and of size 2 GiB is setCommand (m for help): p//查看分区列表Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0005b865Device Boot Start End Blocks Id System/dev/vda1 * 2048 1026047 512000 83 Linux/dev/vda2 1026048 20971519 9972736 8e Linux LVM/dev/vda3 20971520 25165823 2097152 83 LinuxCommand (m for help): w//保存退出并退出终端除了对已有磁盘扩容外,还可以额外增加磁盘,命令如下:# qemu-img create -f raw /kvm_data/test02_3.raw 5GFormatting '/kvm_data/test02_3.raw', fmt=raw size=5368709120
使用virsh edit编辑test02虚拟机,将新磁盘增加到test02虚拟机,命令如下
# virsh edit test02………………<disk type='file' device='disk'><driver name='qemu' type='raw'/><source file='/kvm_data/test02_2.raw'/><target dev='vda' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x06'function='0x0'/></disk>//找到这一部分内容,在下面增加以下内容<disk type='file' device='disk'><driver name='qemu' type='raw'/><source file='/kvm_data/test02_3.raw'/><target dev='vdb' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x09'function='0x0'/></disk>………………
关闭test02虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘,命令如下:
# virsh destroy test02Domain test02 destroyed# virsh start test02Domain test02 started# virsh console test02# fdisk -lDisk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0005b865Device Boot Start End Blocks Id System/dev/vda1 * 2048 1026047 512000 83 Linux/dev/vda2 1026048 20971519 9972736 8e Linux LVM/dev/vda3 20971520 25165823 2097152 83 LinuxDisk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes
可以看到多了一块5 GB的磁盘,退出终端。
接下来,我们扩容qcow2格式的磁盘,命令如下:
# qemu-img resize /kvm_data/test.img +2GImage resized.//若提示qemu-img: Can't resize an image which has snapshots,需要删除快照
查看test.img信息,命令如下:
# qemu-img info /kvm_data/test.imgimage: /kvm_data/test.imgfile format: qcow2virtual size: 12G (12884901888 bytes)disk size: 1.1Gcluster_size: 65536Format specific information:compat: 1.1lazy refcounts: true
关闭test虚拟机,重新启动,然后再次进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:
# virsh destroy testDomain test destroyed# virsh start testDomain test started# virsh console test# fdisk -lDisk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0005b865Device Boot Start End Blocks Id System/dev/vda1 * 2048 1026047 512000 83 Linux/dev/vda2 1026048 20971519 9972736 8e Linux LVMDisk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes
分区新增加的2 GB磁盘,命令如下:
# fdisk /dev/vdaWelcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Command (m for help): nPartition type:p primary (2 primary, 0 extended, 2 free)e extendedSelect (default p): pPartition number (3,4, default 3):First sector (20971, default 20971520):Using default value 20971520Last sector, +sectors or +size{K,M,G} (20971, default 25165823):Using default value 25165823Partition 3 of type Linux and of size 2 GiB is setCommand (m for help): w/保存并退出终端
新增一块qcow2格式的磁盘,命令如下:
# qemu-img create -f qcow2 /kvm_data/test_2.img 5GFormatting '/kvm_data/test_2.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off
使用virsh edit编辑test虚拟机,将新磁盘增加到test虚拟机,命令如下:
# virsh edit test………………<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='disk'><driver name='qemu' type='qcow2'/><source file='/kvm_data/test_2.img'/><target dev='vdb' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/></disk>………………
关闭test虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘,命令如下:
# virsh destroy testDomain test destroyed# virsh start testDomain test started# virsh console test# fdisk -lDisk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0005b865Device Boot Start End Blocks Id System/dev/vda1 * 2048 1026047 512000 83 Linux/dev/vda2 1026048 20971519 9972736 8e Linux LVM/dev/vda3 20971520 25165823 2097152 83 LinuxDisk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes
退出终端
查看虚拟机配置信息,命令如下:
# virsh dominfo testId: 8Name: testUUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5OS Type: hvmState: runningCPU(s): 1CPU time: 20.8sMax memory: 1048576 KiBUsed memory: 524288 KiBPersistent: yesAutostart: disableManaged save: noSecurity model: noneSecurity DOI: 0
编辑虚拟机内存,命令如下:
# virsh edit test…………<memory unit='KiB'>1048576</memory>//最大内存<currentMemory unit='KiB'>524288</currentMemory>//可用内存<vcpu placement='static' current='1'>2</vcpu>//最大cpu………………//将以上内容修改为如下内容…………<memory unit='KiB'>1048576</memory><currentMemory unit='KiB'>624288</currentMemory><vcpu placement='static' current='1'>2</vcpu>…………
关闭虚拟机,重启动虚拟机,命令如下:
# virsh destroy testDomain test destroyed# virsh start testDomain test started
查看虚拟机配置信息,看是否修改成功,命令如下:
# virsh dominfo testId: 10Name: testUUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5OS Type: hvmState: runningCPU(s): 1CPU time: 21.4sMax memory: 1048576 KiBUsed memory: 624288 KiBPersistent: yesAutostart: disableManaged save: noSecurity model: noneSecurity DOI: 0
除了这种修改方式之外,还有一种动态修改,命令如下:
# virsh setmem test 800m//动态修改内存# virsh setvcpus test 2//动态修改cpu,只可以增加不可以减少
查看配置信息,看是否修改成功,命令如下:
# virsh dominfo testId: 10Name: testUUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5OS Type: hvmState: runningCPU(s): 2CPU time: 22.8sMax memory: 1048576 KiBUsed memory: 819200 KiBPersistent: yesAutostart: disableManaged save: noSecurity model: noneSecurity DOI: 0# virsh dumpxml test > /etc/libvirt/qemu/test.xml//需要把配置写入到配置文件里
查看网卡列表,命令如下:
# virsh domiflist testInterface Type Source Model MAC-------------------------------------------------------vnet1 bridge br0 virtio 52:54:00:93:bf:07
增加一块新的网卡,并设置为NAT网络模式(virbr0类似VMware的VMnet8),这里如果写--source br0,则网络模式为桥接,命令如下:
# virsh attach-interface test --type bridge --source virbr0Interface attached successfully# virsh domiflist testInterface Type Source Model MAC-------------------------------------------------------vnet1 bridge br0 virtio 52:54:00:93:bf:07vnet2 bridge virbr0 rtl8139 52:54:00:29:ed:8b# virsh dumpxml test > /etc/libvirt/qemu/test.xml
进入test虚拟机,查看网卡信息,命令如下:
# virsh console test# ifconfigens10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.122.8 netmask 255.255.255.0 broadcast 192.168.122.255inet6 fe80::5054:ff:fe29:ed8b prefixlen 64 scopeid 0x20<link>ether 52:54:00:29:ed:8b txqueuelen 1000 (Ethernet)RX packets 5186 bytes 7413267 (7.0 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 1480 bytes 84889 (82.8 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ens11: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.16.129 netmask 255.255.255.0 broadcast 192.168.16.255inet6 fe80::5054:ff:fe15:abbd prefixlen 64 scopeid 0x20<link>ether 52:54:00:15:ab:bd txqueuelen 1000 (Ethernet)RX packets 13 bytes 1328 (1.2 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 16 bytes 1668 (1.6 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500ether 52:54:00:93:bf:07 txqueuelen 1000 (Ethernet)RX packets 55 bytes 8520 (8.3 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::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 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
首先关闭虚拟机,命令如下:
# virsh shutdown testDomain test is being shutdown
查看虚拟机磁盘所在目录,命令如下:
# virsh domblklist testTarget Source------------------------------------------------vda /kvm_data/test.imgvdb /kvm_data/test_2.imghda -# virsh dumpxml test > /etc/libvirt/qemu/test03.xml //如果是远程机器,需要把该配置文件拷贝到远程机器上# rsync -av /kvm_data/test.img /kvm_data/test03.img//-bash: rsync: command not found则yum install -y rsync//如果是迁移到远程,则需要把该磁盘文件拷贝到远程机器上sending incremental file listtest.imgsent 10,742,006,844 bytes received 35 bytes 60,179,310.25 bytes/sectotal size is 10,739,384,832 speedup is 1.00
因为是迁移到本机,配置文件用的是test子机的配置,不改会有冲突,所以需要修改该文件。如果是远程机器不用修改,命令如下:
# vi /etc/libvirt/qemu/test03.xml <name>test03</name>//修改domname://修改uuid(随便改一下数字,位数不要变)<disk type='file' device='disk'><driver name='qemu' type='qcow2'/><source file='/kvm_data/test03.img'/><target dev='vda' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/></disk>//修改磁盘路径定义新虚拟机,命令如下:# virsh define /etc/libvirt/qemu/test03.xml Domain test03 defined from /etc/libvirt/qemu/test03.xml
查看虚拟机列表,会发现新迁移的虚拟机test03,命令如下:
# virsh list --allId Name State----------------------------------------------------6 test02 running- test shut off- test03 shut off
解决方法:
管理员运行cmd
执行:bcdedit /set hypervisorlaunchtype off
重启就可以开启了,但是过一段时间,再重启打开虚拟机还是会遇见这个问题,只能再重启解决。
尝试把VMware升级到16也无法解决这个问题。
# dhclient eth0# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWNlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 52:54:00:50:b0:bc brd ff:ff:ff:ff:ff:ff
排查问题发现我习惯关闭VMware本地DHCP服务,所以没有获取到IP地址,开启后就获取到IP地址了。
# dhclient eth0# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWNlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 52:54:00:50:b0:bc brd ff:ff:ff:ff:ff:ffinet 192.168.200.128/24 brd 192.168.200.255 scope global dynamic eth0valid_lft 1801sec preferred_lft 1801secinet6 fe80::5054:ff:fe50:b0bc/64 scope linkvalid_lft forever preferred_lft forever
KVM虚拟化的安装和基本管理就讲到这里了,如有疑问欢迎多多评论。