一、安装KVM

1.调整虚拟机

创建新的虚拟机;内存设置为4GB;勾选“虚拟化 Intel VT-x/EPT 或 AMD-V/RVI”选项;添加一块50G的新硬盘。

2.检查CPU参数

查看内存是否更改成功:

  1. [root@kvm ~]# free
  2. total used free shared buff/cache available
  3. Mem: 1868688 125360 1582372 8752 160956 1584208
  4. Swap: 097148 0 2097148

检查CPU虚拟化是否支持:
有vmx表示虚拟化开启成功

  1. [root@kvm ~]# grep -Ei 'vmx|svm' /proc/cpuinfo
  2. flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss 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 mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves
  3. flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss 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 mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves

显示为空,则要查看虚拟机设置,是否打“√”

3.挂载新磁盘

检查硬盘是否新增成功:

  1. [root@kvm ~]# lsblk
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  3. sda 8:0 0 20G 0 disk
  4. ├─sda1 8:1 0 500M 0 part /boot
  5. └─sda2 8:2 0 19.5G 0 part
  6. ├─centos-root 253:0 0 17.5G 0 lvm /
  7. └─centos-swap 253:1 0 2G 0 lvm [SWAP]
  8. sdb 8:16 0 50G 0 disk
  9. sr0 11:0 1 4G 0 rom

成功新增了一块名为sdb的大小为50G的磁盘
格式化该磁盘为ext4格式:

  1. [root@kvm ~]# mkfs.ext4 /dev/sdb
  2. mke2fs 1.42.9 (28-Dec-2013)
  3. /dev/sdb is entire device, not just one partition!
  4. Proceed anyway? (y,n) y
  5. Filesystem label=
  6. OS type: Linux
  7. Block size=4096 (log=2)
  8. Fragment size=4096 (log=2)
  9. Stride=0 blocks, Stripe width=0 blocks
  10. 3276800 inodes, 13107200 blocks
  11. 655360 blocks (5.00%) reserved for the super user
  12. First data block=0
  13. Maximum filesystem blocks=2162163712
  14. 400 block groups
  15. 32768 blocks per group, 32768 fragments per group
  16. 8192 inodes per group
  17. Superblock backups stored on blocks:
  18. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  19. 4096000, 7962624, 11239424
  20. Allocating group tables: done
  21. Writing inode tables: done
  22. Creating journal (32768 blocks): done
  23. Writing superblocks and filesystem accounting information: done
  24. [root@kvm ~]# blkid /dev/sdb
  25. //blkid+磁盘:查看磁盘信息
  26. /dev/sdb: UUID="dab6d659-e334-41ed-a15a-96ce05b48c1a" TYPE="ext4"

格式化完成后,挂载磁盘,先创建挂载目录/kvm_data,然后进行挂载:

  1. [root@kvm ~]# mkdir /kvm_data
  2. [root@kvm ~]# mount /dev/sdb /kvm_data/

设置开机自动挂载:

  1. [root@kvm ~]# vi /etc/fstab
  2. [root@kvm ~]# cat /etc/fstab
  3. #
  4. # /etc/fstab
  5. # Created by anaconda on Thu Aug 22 22:51:46 2019
  6. #
  7. # Accessible filesystems, by reference, are maintained under '/dev/disk'
  8. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  9. #
  10. /dev/mapper/centos-root / xfs defaults 0 0
  11. UUID=34f3cd91-b7f0-44dd-9334-2bb66e939898 /boot xfs defaults 0 0
  12. /dev/mapper/centos-swap swap swap defaults 0 0
  13. /dev/sdb /kvm_data ext4 defaults 0 0

4.关闭防火墙和selinux

防火墙:

  1. [root@kvm ~]# systemctl stop firewalld
  2. [root@kvm ~]# systemctl disable firewalld
  3. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  4. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

selinux:

  1. [root@kvm ~]# vi /etc/selinux/config
  2. [root@kvm ~]# cat /etc/selinux/config
  3. # This file controls the state of SELinux on the system.
  4. # SELINUX= can take one of these three values:
  5. # enforcing - SELinux security policy is enforced.
  6. # permissive - SELinux prints warnings instead of enforcing.
  7. # disabled - No SELinux policy is loaded.
  8. SELINUX=disabled ##修改这一位置的内容
  9. # SELINUXTYPE= can take one of three two values:
  10. # targeted - Targeted processes are protected,
  11. # minimum - Modification of targeted policy. Only selected processes are protected.
  12. # mls - Multi Level Security protection.
  13. SELINUXTYPE=targeted

修改完成后重启:

  1. [root@kvm ~]# getenforce
  2. Disabled

5.安装KVM

安装KVM以及一些插件

  1. [root@kvm ~]# yum install -y virt-* libvirt bridge-utils qemu-img
  2. net——tools vim bash-c*
  3. …………
  4. Complete!

二、启动KVM

1.配置网卡

增加桥接网卡ifcfg-br0:

  1. [root@kvm ~]# cd /etc/sysconfig/network-scripts/
  2. [root@kvm network-scripts]# cp ifcfg-eno16777736 ifcfg-br0

修改桥接网卡br0的内容:

  1. [root@kvm network-scripts]# vi ifcfg-br0
  2. [root@kvm network-scripts]# cat ifcfg-br0
  3. TYPE=Bridge
  4. BOOTPROTO=none
  5. NAME=br0
  6. DEVICE=br0
  7. ONBOOT=yes
  8. IPADDR=192.168.200.20
  9. NETMASK=255.255.255.0
  10. GATEWAY=192.168.200.2
  11. DNS1=114.114.114.114
  12. DNS2=8.8.8.8

修改NAT网卡ifcfg-eno16777736:

  1. [root@kvm network-scripts]# vi ifcfg-eno16777736
  2. [root@kvm network-scripts]# cat ifcfg-eno16777736
  3. TYPE=Ethernet
  4. BOOTPROTO=none
  5. NAME=eno16777736
  6. DEVICE=eno16777736
  7. ONBOOT=yes
  8. BRIDGE=br0

修改完网卡信息后,重启网卡服务并查看网卡信息:

  1. [root@kvm network-scripts]# systemctl restart network
  2. [root@kvm network-scripts]# service network restart
  3. Restarting network (via systemctl): [ OK ]
  4. [root@kvm network-scripts]# ifconfig
  5. br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  6. inet 192.168.16.5 netmask 255.255.255.0 broadcast
  7. 192.168.16.255
  8. inet6 fe80::20c:29ff:fe8d:1f90 prefixlen 64 scopeid 0x20<link>
  9. ether 00:0c:29:8d:1f:90 txqueuelen 0 (Ethernet)
  10. RX packets 90 bytes 7092 (6.9 KiB)
  11. RX errors 0 dropped 0 overruns 0 frame 0
  12. TX packets 38 bytes 3604 (3.5 KiB)
  13. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  14. eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
  15. mtu 1500
  16. ether 00:0c:29:8d:1f:90 txqueuelen 1000 (Ethernet)
  17. RX packets 126287 bytes 182444678 (173.9 MiB)
  18. RX errors 0 dropped 0 overruns 0 frame 0
  19. TX packets 32532 bytes 2953477 (2.8 MiB)
  20. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  21. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  22. inet 127.0.0.1 netmask 255.0.0.0
  23. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  24. loop txqueuelen 0 (Local Loopback)
  25. RX packets 0 bytes 0 (0.0 B)
  26. RX errors 0 dropped 0 overruns 0 frame 0
  27. TX packets 0 bytes 0 (0.0 B)
  28. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

可以看到br0网卡出现并带有IP,而以前的eno16777736网卡没有IP。网卡配置成功

2.启动libvirtdfuwu

首先检查KVM模块是否加载:

  1. [root@kvm network-scripts]# lsmod|grep kvm
  2. kvm_intel 162153 0
  3. kvm 525259 1 kvm_intel

启动libvirtd并检查是否启动成功:

  1. [root@kvm network-scripts]# systemctl start libvirtd
  2. [root@kvm network-scripts]# ps -ef |grep libvirt
  3. root 17270 1 1 04:36 ? 00:00:00 /usr/sbin/libvirtd
  4. nobody 17383 1 0 04:36 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
  5. root 17384 17383 0 04:36 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
  6. root 17416 2264 0 04:36 pts/0 00:00:00 grep --color=auto libvirt

启动成功后,使用brctl命令可以看到两个网卡:

  1. [root@kvm ~]# cd
  2. [root@kvm ~]# brctl show
  3. bridge name bridge id STP enabled interfaces
  4. br0 8000.000c298d1f90 no eno16777736
  5. virbr0 8000.525400c7e229 yes virbr0-nic

3.命令行安装CentOS 7

通过Xftp上传Centos1511镜像到/tmp目录下,上传后使用ll命令查看/tmp目录下是否存在:

  1. [root@kvm ~]# ll /tmp/
  2. total 4228096
  3. -rw-r--r-- 1 root root 4329570304 Apr 15 2016 CentOS-7-x86_64-DVD-1511.iso

接下来使用virt-install进行CentOS 1511的安装:

  1. [root@kvm ~]# yum install -y qemu-kvm
  2. [root@kvm ~]# 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"

—name:指定虚拟机的名称。
—memory:指定分配给虚拟机的内存资源大小。
maxmemory:指定可调节的最大内存资源大小,因为KVM支持热调整虚拟机的资源。
—vcpus:指定分配给虚拟机的CPU核心数量。
maxvcpus:指定可调节的最大CPU核心数量。
—os-type:指定虚拟机安装的操作系统类型。
—os-variant:指定系统的发行版本。
—location:指定ISO镜像文件所在的路径,支持使用网络资源路径,也就是说可以使用URL。
—disk path:指定虚拟硬盘所存放的路径及名称,size则是指定该硬盘的可用大小,单位是G。
—bridge:指定使用哪一个桥接网卡,也就是说使用桥接的网络模式。
—graphics:指定是否开启图形。
—console:定义终端的属性,target_type 则是定义终端的类型。
—extra-args:定义终端额外的参数。
等待一会儿就能看到以下界面:

  1. Starting installer, one moment...
  2. anaconda 21.48.22.56-1 for CentOS 7 started.
  3. * installation log files are stored in /tmp during the installation
  4. * shell is available on TTY2
  5. * when reporting a bug add logs from /tmp as separate text/plain attachments
  6. 17:01:51 Not asking for VNC because we don't have a network
  7. ================================================================================
  8. ================================================================================
  9. Installation
  10. 1) [x] Language settings 2) [!] Timezone settings
  11. (English (United States)) (Timezone is not set.)
  12. 3) [!] Installation source 4) [!] Software selection
  13. (Processing...) (Processing...)
  14. 5) [!] Installation Destination 6) [x] Kdump
  15. (No disks selected) (Kdump is enabled)
  16. 7) [ ] Network configuration 8) [!] Root password
  17. (Not connected) (Password is not set.)
  18. 9) [!] User creation
  19. (No user will be created)
  20. Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: 2
  21. =====================================================================

“Timezone settings”时区设置选择 5) Asia亚洲,再选择城市 62) Shanghai上海,命令如下:

  1. Timezone settings
  2. Available regions
  3. 1) Africa 6) Atlantic 10) Pacific
  4. 2) America 7) Australia 11) US
  5. 3) Antarctica 8) Europe 12) Etc
  6. 4) Arctic 9) Indian
  7. 5) Asia
  8. Please select the timezone.
  9. Use numbers or type names directly [b to region list, q to quit]: 5
  10. ================================================================================
  11. ================================================================================
  12. Timezone settings
  13. Available timezones in region Asia
  14. 1) Aden 28) Irkutsk 54) Pyongyang
  15. 2) Almaty 29) Jakarta 55) Qatar
  16. 3) Amman 30) Jayapura 56) Qyzylorda
  17. 4) Anadyr 31) Jerusalem 57) Rangoon
  18. 5) Aqtau 32) Kabul 58) Riyadh
  19. 6) Aqtobe 33) Kamchatka 59) Sakhalin
  20. 7) Ashgabat 34) Karachi 60) Samarkand
  21. 8) Baghdad 35) Kathmandu 61) Seoul
  22. 9) Bahrain 36) Khandyga 62) Shanghai
  23. 10) Baku 37) Kolkata 63) Singapore
  24. 11) Bangkok 38)Krasnoyarsk 64) Srednekolymsk
  25. 12) Beirut 39) Kuala_Lumpur 65) Taipei
  26. 13) Bishkek 40) Kuching 66) Tashkent
  27. 14) Brunei 41) Kuwait 67) Tbilisi
  28. 15) Chita 42) Macau 68) Tehran
  29. 16) Choibalsan 43) Magadan 69) Thimphu
  30. 17) Colombo 44) Makassar 70) Tokyo
  31. 18) Damascus 45) Manila 71) Ulaanbaatar
  32. 19) Dhaka 46) Muscat 72) Urumqi
  33. 20) Dili 47) Nicosia 73) Ust-Nera
  34. 21) Dubai 48) Novokuznetsk 74) Vientiane
  35. 22) Dushanbe 49) Novosibirsk 75) Vladivostok
  36. Press ENTER to continue
  37. 23) Gaza 50) Omsk 76) Yakutsk
  38. 24) Hebron 51) Oral 77) Yekaterinburg
  39. 25) Ho_Chi_Minh 52) Phnom_Penh 78) Yerevan
  40. 26) Hong_Kong 53) Pontianak
  41. 27) Hovd
  42. Please select the timezone.
  43. Use numbers or type names directly [b to region list, q to quit]: 62
  44. ================================================================================

输入完毕后,可以发现2的[!]变成了[x],证明配置完毕。
接下来我们配置“Software selection”,选择“4”,在选择“c”,因为默认就是minimalinstall,按Enter键:

  1. Installation
  2. 1) [x] Language settings 2) [x] Timezone settings
  3. (English (United States)) (Asia/Shanghai timezone)
  4. 3) [x] Installation source 4) [!] Software selection
  5. (Local media) (Minimal Install)
  6. 5) [!] Installation Destination 6) [x] Kdump
  7. (No disks selected) (Kdump is enabled)
  8. 7) [ ] Network configuration 8) [!] Root password
  9. (Not connected) (Password is not set.)
  10. 9) [!] User creation
  11. (No user will be created)
  12. Please make your choice from above ['q' to quit | 'b' to begin installation |
  13. 'r' to refresh]: 4
  14. ================================================================================
  15. ================================================================================
  16. Base environment
  17. Software selection
  18. Base environment
  19. 1) [x] Minimal Install 7) [ ] Server with GUI
  20. 2) [ ] Compute Node 8) [ ] GNOME Desktop
  21. 3) [ ] Infrastructure Server 9) [ ] KDE Plasma Workspaces
  22. 4) [ ] File and Print Server 10) [ ] Development and Creative Work
  23. 5) [ ] Basic Web Server station
  24. 6) [ ] Virtualization Host
  25. Please make your choice from above ['q' to quit | 'c' to continue |
  26. 'r' to refresh]: c

接下来我们配置“Installation Destination”,选择“5”,其余的依次选择“c”,按Enter键:

  1. Installation
  2. 1) [x] Language settings 2) [x] Timezone settings
  3. (English (United States)) (Asia/Shanghai timezone)
  4. 3) [!] Installation source 4) [!] Software selection
  5. (Processing...) (Processing...)
  6. 5) [!] Installation Destination 6) [x] Kdump
  7. (No disks selected) (Kdump is enabled)
  8. 7) [ ] Network configuration 8) [!] Root password
  9. (Not connected) (Password is not set.)
  10. 9) [!] User creation
  11. (No user will be created)
  12. Please make your choice from above ['q' to quit | 'b' to begin installation |
  13. 'r' to refresh]: 5
  14. ================================================================================
  15. ================================================================================
  16. Probing storage...
  17. Installation Destination
  18. [x] 1) : 10 GiB (vda)
  19. 1 disk selected; 10 GiB capacity; 10 GiB free ...
  20. Please make your choice from above ['q' to quit | 'c' to continue |
  21. 'r' to refresh]: c
  22. ================================================================================
  23. ================================================================================
  24. Autopartitioning Options
  25. [ ] 1) Replace Existing Linux system(s)
  26. [x] 2) Use All Space
  27. [ ] 3) Use Free Space
  28. Installation requires partitioning of your hard drive. Select what space to use
  29. for the install target.
  30. Please make your choice from above ['q' to quit | 'c' to continue |
  31. 'r' to refresh]: c
  32. ================================================================================
  33. ================================================================================
  34. Partition Scheme Options
  35. [ ] 1) Standard Partition
  36. [ ] 2) Btrfs
  37. [x] 3) LVM
  38. [ ] 4) LVM Thin Provisioning
  39. Select a partition scheme configuration.
  40. Please make your choice from above ['q' to quit | 'c' to continue |
  41. 'r' to refresh]: c
  42. Generating updated storage configuration
  43. Checking storage configuration...
  44. ================================================================================
  45. ================================================================================
  46. 接下来配置“Root password”,选择8,按Enter键,命令如下:
  47. Installation
  48. 1) [x] Language settings 2) [x] Timezone settings
  49. (English (United States)) (Asia/Shanghai timezone)
  50. 3) [x] Installation source 4) [x] Software selection
  51. (Local media) (Minimal Install)
  52. 5) [x] Installation Destination 6) [x] Kdump
  53. (Automatic partitioning selecte (Kdump is enabled)
  54. d) 8) [!] Root password
  55. 7) [ ] Network configuration (Password is not set.)
  56. (Not connected)
  57. 9) [!] User creation
  58. (No user will be created)
  59. Please make your choice from above ['q' to quit | 'b' to begin installation |
  60. 'r' to refresh]: 8
  61. ================================================================================
  62. ================================================================================
  63. Please select new root password. You will have to type it twice.

依次输入两次密码,密码相同,如123456:

  1. Password: 123456
  2. //密码为密文,不显示。
  3. Password (confirm): 123456
  4. ================================================================================
  5. ================================================================================
  6. Question
  7. You have provided a weak password: The password fails the dictionary check - it
  8. is too simplistic/systematic
  9. Would you like to use it anyway?
  10. //这句话的意思:你的密码太过简单,是否使用它,输入yes即可。
  11. Please respond 'yes' or 'no': yes
  12. =========================================================
  13. =========================================================

配置完成按“b”,按Enter键后,开始安装:

  1. Installation
  2. 1) [x] Language settings 2) [x] Timezone settings
  3. (English (United States)) (Asia/Shanghai timezone)
  4. 3) [x] Installation source 4) [x] Software selection
  5. (Local media) (Minimal Install)
  6. 5) [x] Installation Destination 6) [x] Kdump
  7. (Automatic partitioning selecte (Kdump is enabled)
  8. d) 8) [x] Root password
  9. 7) [ ] Network configuration (Password is set.)
  10. (Not connected)
  11. 9) [ ] User creation
  12. (No user will be created)
  13. Please make your choice from above ['q' to quit | 'b' to begin installation |
  14. 'r' to refresh]: b
  15. ================================================================================
  16. ================================================================================
  17. Progress
  18. Setting up the installation environment
  19. .
  20. Creating disklabel on /dev/vda
  21. .
  22. Creating xfs on /dev/vda1
  23. .
  24. Creating lvmpv on /dev/vda2
  25. .
  26. Creating swap on /dev/mapper/centos-swap
  27. .
  28. ……………………
  29. Installing readline (34/297)
  30. Installing gawk (35/297)
  31. Installing elfutils-libelf (36/297)
  32. Installing libgpg-error (37/297)
  33. Installing libffi (38/297)
  34. Installing libattr (39/297)
  35. Installing libacl (40/297)
  36. Installing libcap (41/297)
  37. Installing libgcrypt (42/297)
  38. Installing cpio (43/297)
  39. Installing libxml2 (44/297)
  40. Installing libnl3 (45/297)
  41. Installing expat (46/297)
  42. Installing p11-kit (47/297)
  43. ……………………
  44. .
  45. Performing post-installation setup tasks
  46. .
  47. Configuring installed system
  48. .
  49. Writing network configuration
  50. .
  51. Creating users
  52. .
  53. Configuring addons
  54. .
  55. Generating initramfs
  56. .
  57. Running post-installation scripts
  58. .
  59. Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA
  60. Installation complete. Press return to quit
  61. //按回车
  62. ……………………
  63. CentOS Linux 7 (Core)
  64. Kernel 3.10.0-327.el7.x86_64 on an x86_64
  65. localhost login: root
  66. Password: 123456
  67. [root@localhost ~]#
  68. //按“Ctrl+]”键,退出终端,回到宿主机。
  69. [root@kvm ~]#

CentOS 1511安装完成

三、虚拟机管理

1.KVM基本管理

完成虚拟机安装,已经退回到宿主机,接下来我们来通过宿主机virsh命令进行管理刚才安装的CentOS 7虚拟机。
查看虚拟机列表:

  1. [root@kvm ~]# virsh list
  2. //查看虚拟机列表,只能看到正在运行的虚拟机
  3. Id Name State
  4. ----------------------------------------------------
  5. 2 test running
  6. [root@kvm ~]# virsh list --all
  7. //查看虚拟机列表,包括未运行的虚拟机
  8. Id Name State
  9. ----------------------------------------------------
  10. 2 test running

进入指定虚拟:

  1. [root@kvm ~]# virsh console test
  2. Connected to domain test
  3. Escape character is ^]
  4. //如果迟迟未动敲下回车就ok,下面就是正常登录时需要输入的用户名密码
  5. CentOS Linux 7 (Core)
  6. Kernel 3.10.0-327.el7.x86_64 on an x86_64
  7. localhost login:

vrish常用命令:

  1. virsh shutdown test
  2. //关闭虚拟机
  3. virsh start test
  4. //开启虚拟机
  5. virsh destroy test
  6. //类似stop,这个是强制停止
  7. virsh undefine test
  8. //彻底销毁虚拟机,会删除虚拟机配置文件,virsh list --all就看不到
  9. virsh autostart test
  10. //宿主机开机该虚拟机也开机
  11. virsh autostart --disable test
  12. //解除开机启动
  13. virsh suspend test
  14. //挂起
  15. virsh resume test
  16. //恢复

2.克隆虚拟机

克隆虚拟机之前要先关闭虚拟机:

  1. [root@kvm ~]# virsh shutdown test
  2. Domain test is being shutdown
  3. [root@kvm ~]# virt-clone --original test --name test02 --file
  4. /kvm_data/test02.img
  5. Allocating 'test02.img' 3%
  6. [= ] 20 MB/s | 365 MB 00:08:23 ETA
  7. //开始克隆
  8. [root@kvm ~]# virt-clone --original test --name test02 --file
  9. /kvm_data/test02.img
  10. Allocating 'test02.img' | 10 GB 00:00:35
  11. Clone 'test02' created successfully.
  12. //克隆完成且成功

查看虚拟机配置文件/etc/libvirt/qemu,查看是否增加test02.xml文件:

  1. [root@kvm ~]# ls /etc/libvirt/qemu/
  2. networks test02.xml test.xml

接下来启动克隆的test02虚拟机,首先查看虚拟机列表,然后启动test02:

  1. [root@kvm ~]# virsh list --all
  2. Id Name State
  3. ----------------------------------------------------
  4. - test shut off
  5. - test02 shut off
  6. [root@kvm ~]# virsh start test02
  7. Domain test02 started

3.快照管理

创建快照:

  1. [root@kvm ~]# virsh snapshot-create test
  2. Domain snapshot 1588485687 created

查看test.img信息,同时会查看到快照列表:

  1. [root@kvm ~]# qemu-img info /kvm_data/test.img
  2. image: /kvm_data/test.img
  3. file format: qcow2
  4. virtual size: 10G (10737418240 bytes)
  5. disk size: 1.1G
  6. cluster_size: 65536
  7. Snapshot list:
  8. ID TAG VM SIZE DATE VM CLOCK
  9. 1 1588485687 0 2020-05-03 14:01:27 00:00:00.000
  10. Format specific information:
  11. compat: 1.1
  12. lazy refcounts: true

列出所有快照:

  1. [root@kvm ~]# virsh snapshot-list test
  2. Name Creation Time State
  3. ------------------------------------------------------------
  4. 1588485687 2020-05-03 14:01:27 +0800 shutoff

查看当前快照版本:

  1. [root@kvm ~]# virsh snapshot-current test
  2. <domainsnapshot>
  3. <name>1588485687</name>
  4. <state>shutoff</state>
  5. <creationTime>1588485687</creationTime>
  6. <memory snapshot='no'/>
  7. <disks>
  8. <disk name='vda' snapshot='internal'/>
  9. <disk name='hda' snapshot='no'/>
  10. </disks>
  11. <domain type='kvm'>
  12. <name>test</name>
  13. <uuid>49d7cb9c-20dc-42dd-a260-01532b5132e5</uuid>
  14. <memory unit='KiB'>1048576</memory>
  15. <currentMemory unit='KiB'>524288</currentMemory>
  16. <vcpu placement='static' current='1'>2</vcpu>
  17. <os>
  18. <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
  19. <boot dev='hd'/>
  20. </os>
  21. <features>
  22. <acpi/>
  23. <apic/>
  24. </features>
  25. <cpu mode='custom' match='exact' check='partial'>
  26. <model fallback='allow'>Broadwell-noTSX-IBRS</model>
  27. <feature policy='require' name='md-clear'/>
  28. <feature policy='require' name='spec-ctrl'/>
  29. <feature policy='require' name='ssbd'/>
  30. </cpu>
  31. <clock offset='utc'>
  32. <timer name='rtc' tickpolicy='catchup'/>
  33. <timer name='pit' tickpolicy='delay'/>
  34. <timer name='hpet' present='no'/>
  35. </clock>
  36. <on_poweroff>destroy</on_poweroff>
  37. <on_reboot>restart</on_reboot>
  38. <on_crash>destroy</on_crash>
  39. <pm>
  40. <suspend-to-mem enabled='no'/>
  41. <suspend-to-disk enabled='no'/>
  42. </pm>
  43. <devices>
  44. <emulator>/usr/libexec/qemu-kvm</emulator>
  45. <disk type='file' device='disk'>
  46. <driver name='qemu' type='qcow2'/>
  47. <source file='/kvm_data/test.img'/>
  48. <target dev='vda' bus='virtio'/>
  49. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  50. </disk>
  51. <disk type='file' device='cdrom'>
  52. <driver name='qemu' type='raw'/>
  53. <target dev='hda' bus='ide'/>
  54. <readonly/>
  55. <address type='drive' controller='0' bus='0' target='0' unit='0'/>
  56. </disk>
  57. <controller type='usb' index='0' model='ich9-ehci1'>
  58. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
  59. </controller>
  60. <controller type='usb' index='0' model='ich9-uhci1'>
  61. <master startport='0'/>
  62. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
  63. </controller>
  64. <controller type='usb' index='0' model='ich9-uhci2'>
  65. <master startport='2'/>
  66. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
  67. </controller>
  68. <controller type='usb' index='0' model='ich9-uhci3'>
  69. <master startport='4'/>
  70. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
  71. </controller>
  72. <controller type='ide' index='0'>
  73. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
  74. </controller>
  75. <controller type='virtio-serial' index='0'>
  76. <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
  77. </controller>
  78. <controller type='pci' index='0' model='pci-root'/>
  79. <interface type='bridge'>
  80. <mac address='52:54:00:93:bf:07'/>
  81. <source bridge='br0'/>
  82. <model type='virtio'/>
  83. <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  84. </interface>
  85. <serial type='pty'>
  86. <target type='isa-serial' port='0'>
  87. <model name='isa-serial'/>
  88. </target>
  89. </serial>
  90. <console type='pty'>
  91. <target type='serial' port='0'/>
  92. </console>
  93. <channel type='unix'>
  94. <target type='virtio' name='org.qemu.guest_agent.0'/>
  95. <address type='virtio-serial' controller='0' bus='0' port='1'/>
  96. </channel>
  97. <input type='mouse' bus='ps2'/>
  98. <input type='keyboard' bus='ps2'/>
  99. <memballoon model='virtio'>
  100. <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
  101. </memballoon>
  102. <rng model='virtio'>
  103. <backend model='random'>/dev/urandom</backend>
  104. <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
  105. </rng>
  106. </devices>
  107. </domain>
  108. </domainsnapshot>

查看所有快照配置文件:

  1. [root@kvm ~]# ls /var/lib/libvirt/qemu/snapshot/test/
  2. 1588485687.xml

恢复指定快照:

  1. [root@kvm ~]# virsh snapshot-revert test 1588485687

删除指定快照:

  1. [root@kvm ~]# virsh snapshot-delete test 1588485687
  2. Domain snapshot 1588485687 deleted

4.磁盘格式化

查看虚拟磁盘格式:

  1. [root@kvm ~]# qemu-img info /kvm_data/test.img
  2. image: /kvm_data/test.img
  3. file format: qcow2
  4. virtual size: 10G (10737418240 bytes)
  5. disk size: 1.1G
  6. cluster_size: 65536
  7. Format specific information:
  8. compat: 1.1
  9. lazy refcounts: true

创建2GB的RAW格式磁盘:

  1. [root@kvm ~]# qemu-img create -f raw /kvm_data/test_1.img 2G
  2. Formatting '/kvm_data/test_1.img', fmt=raw size=2147483648

RAW格式的磁盘转换为qcow2格式:

  1. [root@kvm ~]# qemu-img convert -O qcow2 /kvm_data/test_1.img /kvm_data/test_1.qcow2

查看test1.img大小:

  1. [root@kvm ~]# ls -lh /kvm_data/test_1.img
  2. -rw-r--r-- 1 root root 2.0G May 3 15:11 /kvm_data/test_1.img
  3. [root@kvm ~]# ls -lh /kvm_data/test_1.qcow2
  4. -rw-r--r-- 1 root root 193K May 3 15:38 /kvm_data/test_1.qcow2
  5. ##可以看到qcow2文件比较小,raw文件大小和我们指定空间大小一样是2G
  6. ##raw格式的磁盘性能比qcow2要好,但是raw格式的磁盘无法做快照

将test02转成raw格式:

  1. [root@kvm ~]# qemu-img convert -O raw /kvm_data/test02.img /kvm_data/test02_2.raw

更改磁盘格式和文件路径,然后启动test02虚拟机:

  1. [root@kvm ~]# virsh edit test02
  2. …………
  3. <disk type='file' device='disk'>
  4. <driver name='qemu' type='qcow'/>
  5. <source file='/kvm_data/test02.img'/>
  6. <target dev='vda' bus='virtio'/>
  7. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  8. </disk>
  9. //找到上面这部分将qcow修改为raw,/kvm_data/test02.img改为/kvm_data/test02_2.raw
  10. <disk type='file' device='disk'>
  11. <driver name='qemu' type='raw'/>
  12. <source file='/kvm_data/test02_2.raw'/>
  13. <target dev='vda' bus='virtio'/>
  14. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  15. </disk>
  16. ………………

查看磁盘所属用户和组:

  1. [root@kvm ~]# ls -l /kvm_data/
  2. total 3310628
  3. drwx------ 2 root root 16384 Apr 29 00:20 lost+found
  4. -rw-r--r-- 1 root root 10737418240 May 3 16:14 test02_2.raw
  5. -rw------- 1 root root 1133772800 May 3 16:02 test02.img
  6. -rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img
  7. -rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2
  8. -rw------- 1 root root 10739384832 May 3 14:14 test.img

重启虚拟机test02,然后再次查看磁盘所属用户和组:

  1. [root@kvm ~]# virsh start test02
  2. Domain test02 started
  3. [root@kvm ~]# ls -l /kvm_data/
  4. total 3310628
  5. drwx------ 2 root root 16384 Apr 29 00:20 lost+found
  6. -rw-r--r-- 1 qemu qemu 10737418240 May 3 16:14 test02_2.raw
  7. -rw------- 1 root root 1133772800 May 3 16:02 test02.img
  8. -rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img
  9. -rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2
  10. -rw------- 1 root root 10739384832 May 3 14:14 test.img
  11. //启动后所属组变化成qemu的是test02_2.raw,证明这个磁盘正在被使用

5.磁盘扩容

在这里磁盘一共有两种格式,一种是RAW格式,一种是qcow2格式,接下来我们分别给这两种磁盘格式进行扩容。、
首先扩容RAW格式:

  1. [root@kvm ~]# qemu-img resize /kvm_data/test02_2.raw +2G
  2. Image resized.

查看test02_2.raw信息:

  1. [root@kvm ~]# qemu-img info /kvm_data/test02_2.raw
  2. image: /kvm_data/test02_2.raw
  3. file format: raw
  4. virtual size: 12G (12884901888 bytes)
  5. disk size: 1.0G

进入虚拟机test02,使用fdisk -l查看磁盘:

  1. [root@kvm ~]# virsh console test02
  2. [root@localhost ~]# fdisk -l
  3. Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
  4. Units = sectors of 1 * 512 = 512 bytes
  5. Sector size (logical/physical): 512 bytes / 512 bytes
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes
  7. Disk label type: dos
  8. Disk identifier: 0x0005b865
  9. Device Boot Start End Blocks Id System
  10. /dev/vda1 * 2048 1026047 512000 83 Linux
  11. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  12. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  13. Units = sectors of 1 * 512 = 512 bytes
  14. Sector size (logical/physical): 512 bytes / 512 bytes
  15. I/O size (minimum/optimal): 512 bytes / 512 bytes
  16. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  17. Units = sectors of 1 * 512 = 512 bytes
  18. Sector size (logical/physical): 512 bytes / 512 bytes
  19. I/O size (minimum/optimal): 512 bytes / 512 bytes
  20. //可以看到磁盘还是10 GB

退出终端,关闭test02虚拟机,重新启动,然后再次进入虚拟机test02,使用fdisk -l查看磁盘:

  1. [root@kvm ~]# virsh destroy test02
  2. Domain test02 destroyed
  3. [root@kvm ~]# virsh start test02
  4. Domain test02 started
  5. [root@kvm ~]# virsh console test02
  6. [root@localhost ~]# fdisk -l
  7. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  8. Units = sectors of 1 * 512 = 512 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/O size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk label type: dos
  12. Disk identifier: 0x0005b865
  13. Device Boot Start End Blocks Id System
  14. /dev/vda1 * 2048 1026047 512000 83 Linux
  15. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  16. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  17. Units = sectors of 1 * 512 = 512 bytes
  18. Sector size (logical/physical): 512 bytes / 512 bytes
  19. I/O size (minimum/optimal): 512 bytes / 512 bytes
  20. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  21. Units = sectors of 1 * 512 = 512 bytes
  22. Sector size (logical/physical): 512 bytes / 512 bytes
  23. I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到已经变成12GB
接下来,对新增的2GB进行分区:

  1. [root@localhost ~]# fdisk /dev/vda
  2. Welcome to fdisk (util-linux 2.23.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. Command (m for help): n
  6. //新建分区
  7. Partition type:
  8. p primary (2 primary, 0 extended, 2 free)
  9. e extended
  10. Select (default p): p
  11. Partition number (3,4, default 3):
  12. //按Enter键
  13. First sector (20971520-25165823, default 20971520):
  14. //按Enter键
  15. Using default value 20971520
  16. Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823):
  17. //按Enter键
  18. Using default value 25165823
  19. Partition 3 of type Linux and of size 2 GiB is set
  20. Command (m for help): p
  21. //查看分区列表
  22. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  23. Units = sectors of 1 * 512 = 512 bytes
  24. Sector size (logical/physical): 512 bytes / 512 bytes
  25. I/O size (minimum/optimal): 512 bytes / 512 bytes
  26. Disk label type: dos
  27. Disk identifier: 0x0005b865
  28. Device Boot Start End Blocks Id System
  29. /dev/vda1 * 2048 1026047 512000 83 Linux
  30. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  31. /dev/vda3 20971520 25165823 2097152 83 Linux
  32. Command (m for help): w
  33. //保存退出并退出终端

除了对已有磁盘扩容外,还可以额外增加磁盘:

  1. [root@kvm ~]# qemu-img create -f raw /kvm_data/test02_3.raw 5G
  2. Formatting '/kvm_data/test02_3.raw', fmt=raw size=5368709120

使用virsh edit编辑test02虚拟机,将新磁盘增加到test02虚拟机:

  1. [root@kvm ~]# virsh edit test02
  2. ………………
  3. <disk type='file' device='disk'>
  4. <driver name='qemu' type='raw'/>
  5. <source file='/kvm_data/test02_2.raw'/>
  6. <target dev='vda' bus='virtio'/>
  7. <address type='pci' domain='0x0000' bus='0x00' slot='0x06'
  8. function='0x0'/>
  9. </disk>
  10. //找到这一部分内容,在下面增加以下内容
  11. <disk type='file' device='disk'>
  12. <driver name='qemu' type='raw'/>
  13. <source file='/kvm_data/test02_3.raw'/>
  14. <target dev='vdb' bus='virtio'/>
  15. <address type='pci' domain='0x0000' bus='0x00' slot='0x09'
  16. function='0x0'/>
  17. </disk>
  18. ………………

关闭test02虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘

  1. [root@kvm ~]# virsh destroy test02
  2. Domain test02 destroyed
  3. [root@kvm ~]# virsh start test02
  4. Domain test02 started
  5. [root@kvm ~]# virsh console test02
  6. [root@localhost ~]# fdisk -l
  7. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  8. Units = sectors of 1 * 512 = 512 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/O size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk label type: dos
  12. Disk identifier: 0x0005b865
  13. Device Boot Start End Blocks Id System
  14. /dev/vda1 * 2048 1026047 512000 83 Linux
  15. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  16. /dev/vda3 20971520 25165823 2097152 83 Linux
  17. Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
  18. Units = sectors of 1 * 512 = 512 bytes
  19. Sector size (logical/physical): 512 bytes / 512 bytes
  20. I/O size (minimum/optimal): 512 bytes / 512 bytes
  21. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  22. Units = sectors of 1 * 512 = 512 bytes
  23. Sector size (logical/physical): 512 bytes / 512 bytes
  24. I/O size (minimum/optimal): 512 bytes / 512 bytes
  25. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  26. Units = sectors of 1 * 512 = 512 bytes
  27. Sector size (logical/physical): 512 bytes / 512 bytes
  28. I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到多了一块5GB的磁盘,退出终端
接下来,扩容qcow2格式的磁盘:

  1. [root@kvm ~]# qemu-img resize /kvm_data/test.img +2G
  2. Image resized.
  3. //若提示qemu-img: Can't resize an image which has snapshots,需要删除快照

查看test.img信息:

  1. [root@kvm ~]# qemu-img info /kvm_data/test.img
  2. image: /kvm_data/test.img
  3. file format: qcow2
  4. virtual size: 12G (12884901888 bytes)
  5. disk size: 1.1G
  6. cluster_size: 65536
  7. Format specific information:
  8. compat: 1.1
  9. lazy refcounts: true

关闭test虚拟机,重新启动,然后再次进入test02,使用fdisk -l查看磁盘:

  1. [root@kvm ~]# virsh destroy test
  2. Domain test destroyed
  3. [root@kvm ~]# virsh start test
  4. Domain test started
  5. [root@kvm ~]# virsh console test
  6. [root@localhost ~]# fdisk -l
  7. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  8. Units = sectors of 1 * 512 = 512 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/O size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk label type: dos
  12. Disk identifier: 0x0005b865
  13. Device Boot Start End Blocks Id System
  14. /dev/vda1 * 2048 1026047 512000 83 Linux
  15. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  16. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  17. Units = sectors of 1 * 512 = 512 bytes
  18. Sector size (logical/physical): 512 bytes / 512 bytes
  19. I/O size (minimum/optimal): 512 bytes / 512 bytes
  20. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  21. Units = sectors of 1 * 512 = 512 bytes
  22. Sector size (logical/physical): 512 bytes / 512 bytes
  23. I/O size (minimum/optimal): 512 bytes / 512 bytes

分区新增2GB的磁盘:

  1. [root@localhost ~]# fdisk /dev/vda
  2. Welcome to fdisk (util-linux 2.23.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. Command (m for help): n
  6. Partition type:
  7. p primary (2 primary, 0 extended, 2 free)
  8. e extended
  9. Select (default p): p
  10. Partition number (3,4, default 3):
  11. First sector (20971520-25165823, default 20971520):
  12. Using default value 20971520
  13. Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823):
  14. Using default value 25165823
  15. Partition 3 of type Linux and of size 2 GiB is set
  16. Command (m for help): w
  17. /保存并退出终端

新增一块qcow2格式的磁盘:

  1. [root@kvm ~]# qemu-img create -f qcow2 /kvm_data/test_2.img 5G
  2. Formatting '/kvm_data/test_2.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off

使用virsh edit编辑test虚拟机,将新磁盘增加到test虚拟机:

  1. [root@kvm ~]# virsh edit test
  2. ………………
  3. <disk type='file' device='disk'>
  4. <driver name='qemu' type='qcow2'/>
  5. <source file='/kvm_data/test.img'/>
  6. <target dev='vda' bus='virtio'/>
  7. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  8. </disk>
  9. //找到这一部分内容,在下面增加以下内容
  10. <disk type='file' device='disk'>
  11. <driver name='qemu' type='qcow2'/>
  12. <source file='/kvm_data/test_2.img'/>
  13. <target dev='vdb' bus='virtio'/>
  14. <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
  15. </disk>
  16. ………………

关闭test虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘:

  1. [root@kvm ~]# virsh destroy test
  2. Domain test destroyed
  3. [root@kvm ~]# virsh start test
  4. Domain test started
  5. [root@kvm ~]# virsh console test
  6. [root@localhost ~]# fdisk -l
  7. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  8. Units = sectors of 1 * 512 = 512 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/O size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk label type: dos
  12. Disk identifier: 0x0005b865
  13. Device Boot Start End Blocks Id System
  14. /dev/vda1 * 2048 1026047 512000 83 Linux
  15. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  16. /dev/vda3 20971520 25165823 2097152 83 Linux
  17. Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
  18. Units = sectors of 1 * 512 = 512 bytes
  19. Sector size (logical/physical): 512 bytes / 512 bytes
  20. I/O size (minimum/optimal): 512 bytes / 512 bytes
  21. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  22. Units = sectors of 1 * 512 = 512 bytes
  23. Sector size (logical/physical): 512 bytes / 512 bytes
  24. I/O size (minimum/optimal): 512 bytes / 512 bytes
  25. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  26. Units = sectors of 1 * 512 = 512 bytes
  27. Sector size (logical/physical): 512 bytes / 512 bytes
  28. I/O size (minimum/optimal): 512 bytes / 512 bytes
  29. 退出终端

6.调整CPU内存、网卡

查看虚拟机配置信息:

  1. [root@kvm ~]# virsh dominfo test
  2. Id: 8
  3. Name: test
  4. UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5
  5. OS Type: hvm
  6. State: running
  7. CPU(s): 1
  8. CPU time: 20.8s
  9. Max memory: 1048576 KiB
  10. Used memory: 524288 KiB
  11. Persistent: yes
  12. Autostart: disable
  13. Managed save: no
  14. Security model: none
  15. Security DOI: 0

编辑虚拟机内存:

  1. [root@kvm ~]# virsh edit test
  2. …………
  3. <memory unit='KiB'>1048576</memory>
  4. //最大内存
  5. <currentMemory unit='KiB'>524288</currentMemory>
  6. //可用内存
  7. <vcpu placement='static' current='1'>2</vcpu>
  8. //最大cpu
  9. ………………
  10. //将以上内容修改为如下内容
  11. …………
  12. <memory unit='KiB'>1048576</memory>
  13. <currentMemory unit='KiB'>624288</currentMemory>
  14. <vcpu placement='static' current='1'>2</vcpu>
  15. …………

关闭虚拟机,重启虚拟机:

  1. [root@kvm ~]# virsh destroy test
  2. Domain test destroyed
  3. [root@kvm ~]# virsh start test
  4. Domain test started

查看虚拟机配置信息,查看是否修改成功:

  1. [root@kvm ~]# virsh dominfo test
  2. Id: 10
  3. Name: test
  4. UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5
  5. OS Type: hvm
  6. State: running
  7. CPU(s): 1
  8. CPU time: 21.4s
  9. Max memory: 1048576 KiB
  10. Used memory: 624288 KiB
  11. Persistent: yes
  12. Autostart: disable
  13. Managed save: no
  14. Security model: none
  15. Security DOI: 0

还可以使用动态修改的方式来进行修改:

  1. [root@kvm ~]# virsh setmem test 800m
  2. //动态修改内存
  3. [root@kvm ~]# virsh setvcpus test 2
  4. //动态修改cpu,只可以增加不可以减少

查看配置信息,看是否修改成功:

  1. [root@kvm ~]# virsh dominfo test
  2. Id: 10
  3. Name: test
  4. UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5
  5. OS Type: hvm
  6. State: running
  7. CPU(s): 2
  8. CPU time: 22.8s
  9. Max memory: 1048576 KiB
  10. Used memory: 819200 KiB
  11. Persistent: yes
  12. Autostart: disable
  13. Managed save: no
  14. Security model: none
  15. Security DOI: 0
  16. [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml
  17. //需要把配置写入到配置文件里

查看网卡列表:

  1. [root@kvm ~]# virsh domiflist test
  2. Interface Type Source Model MAC
  3. -------------------------------------------------------
  4. vnet1 bridge br0 virtio 52:54:00:93:bf:07

增加一块新的网卡,并设置为NAT网络模式(virbr0类似VMware的VMnet8),这里如果写—source br0,则网络模式为桥接:

  1. [root@kvm ~]# virsh attach-interface test --type bridge --source virbr0
  2. Interface attached successfully
  3. [root@kvm ~]# virsh domiflist test
  4. Interface Type Source Model MAC
  5. -------------------------------------------------------
  6. vnet1 bridge br0 virtio 52:54:00:93:bf:07
  7. vnet2 bridge virbr0 rtl8139 52:54:00:29:ed:8b
  8. [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml

进入test虚拟机,查看网卡信息:

  1. [root@kvm ~]# virsh console test
  2. [root@localhost ~]# ifconfig
  3. ens10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  4. inet 192.168.122.8 netmask 255.255.255.0 broadcast 192.168.122.255
  5. inet6 fe80::5054:ff:fe29:ed8b prefixlen 64 scopeid 0x20<link>
  6. ether 52:54:00:29:ed:8b txqueuelen 1000 (Ethernet)
  7. RX packets 5186 bytes 7413267 (7.0 MiB)
  8. RX errors 0 dropped 0 overruns 0 frame 0
  9. TX packets 1480 bytes 84889 (82.8 KiB)
  10. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  11. ens11: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  12. inet 192.168.16.129 netmask 255.255.255.0 broadcast 192.168.16.255
  13. inet6 fe80::5054:ff:fe15:abbd prefixlen 64 scopeid 0x20<link>
  14. ether 52:54:00:15:ab:bd txqueuelen 1000 (Ethernet)
  15. RX packets 13 bytes 1328 (1.2 KiB)
  16. RX errors 0 dropped 0 overruns 0 frame 0
  17. TX packets 16 bytes 1668 (1.6 KiB)
  18. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  19. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  20. ether 52:54:00:93:bf:07 txqueuelen 1000 (Ethernet)
  21. RX packets 55 bytes 8520 (8.3 KiB)
  22. RX errors 0 dropped 0 overruns 0 frame 0
  23. TX packets 0 bytes 0 (0.0 B)
  24. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  25. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  26. inet 127.0.0.1 netmask 255.0.0.0
  27. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  28. loop txqueuelen 0 (Local Loopback)
  29. RX packets 0 bytes 0 (0.0 B)
  30. RX errors 0 dropped 0 overruns 0 frame 0
  31. TX packets 0 bytes 0 (0.0 B)
  32. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

7.迁移虚拟机

关闭虚拟机:

  1. [root@kvm ~]# virsh shutdown test
  2. Domain test is being shutdown

查看虚拟机磁盘所在目录:

  1. [root@kvm ~]# virsh domblklist test
  2. Target Source
  3. ------------------------------------------------
  4. vda /kvm_data/test.img
  5. vdb /kvm_data/test_2.img
  6. hda -
  7. [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test03.xml
  8. //如果是远程机器,需要把该配置文件拷贝到远程机器上
  9. [root@kvm ~]# rsync -av /kvm_data/test.img /kvm_data/test03.img
  10. //-bash: rsync: command not found则yum install -y rsync
  11. //如果是迁移到远程,则需要把该磁盘文件拷贝到远程机器上
  12. sending incremental file list
  13. test.img
  14. sent 10,742,006,844 bytes received 35 bytes 60,179,310.25 bytes/sec
  15. total size is 10,739,384,832 speedup is 1.00

因为是迁移到本机,配置文件用的是test子机的配置,不改会有冲突,所以需要修改该文件。如果是远程机器不用修改:

  1. [root@kvm ~]# vi /etc/libvirt/qemu/test03.xml
  2. <name>test03</name>
  3. //修改domname:
  4. //修改uuid(随便改一下数字,位数不要变)
  5. <disk type='file' device='disk'>
  6. <driver name='qemu' type='qcow2'/>
  7. <source file='/kvm_data/test03.img'/>
  8. <target dev='vda' bus='virtio'/>
  9. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  10. </disk>
  11. //修改磁盘路径

定义新虚拟机:

  1. [root@kvm ~]# virsh define /etc/libvirt/qemu/test03.xml
  2. Domain test03 defined from /etc/libvirt/qemu/test03.xml

查看虚拟机列表,会发现新迁移的虚拟机test03:

  1. [root@kvm ~]# virsh list --all
  2. Id Name State
  3. ----------------------------------------------------
  4. 6 test02 running
  5. - test shut off
  6. - test03 shut off