1. 调整虚拟机

(基本配置就是这样)
image.png
(做虚拟化最主要的就是虚拟引擎的两处勾选)
image.png

2. 检查CPU参数

  1. 启动虚拟机,我们查看以下,内存是否更改成功,命令如下:
  2. [root@kvm ~]# free
  3. total used free shared buff/cache available
  4. Mem: 1868688 125360 1582372 8752 160956 1584208
  5. Swap: 097148 0 2097148
  6. 检查CPU是否开启虚拟化支持,命令如下:
  7. [root@kvm ~]# grep -Ei 'vmx|svm' /proc/cpuinfo
  8. //vmx为英特尔的cpu;svm为AMD的cpu。
  9. 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
  10. 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
  11. 如果显示为空,就要检查你虚拟机设置,是否打“√”

3. 挂载新磁盘

  1. //使用lsblk命令,检查虚拟机是否新增一块50 GB的磁盘,命令如下:
  2. [root@kvm ~]# lsblk
  3. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  4. sda 8:0 0 20G 0 disk
  5. ├─sda1 8:1 0 500M 0 part /boot
  6. └─sda2 8:2 0 19.5G 0 part
  7. ├─centos-root 253:0 0 17.5G 0 lvm /
  8. └─centos-swap 253:1 0 2G 0 lvm [SWAP]
  9. sdb 8:16 0 50G 0 disk
  10. sr0 11:0 1 4G 0 rom
  11. //我们可以看到确实新增了一块磁盘名为sdb。
  12. //接下来格式化这块磁盘,格式化成ext4格式,命令如下:
  13. [root@kvm ~]# mkfs.ext4 /dev/sdb
  14. mke2fs 1.42.9 (28-Dec-2013)
  15. /dev/sdb is entire device, not just one partition!
  16. Proceed anyway? (y,n) y
  17. Filesystem label=
  18. OS type: Linux
  19. Block size=4096 (log=2)
  20. Fragment size=4096 (log=2)
  21. Stride=0 blocks, Stripe width=0 blocks
  22. 3276800 inodes, 13107200 blocks
  23. 655360 blocks (5.00%) reserved for the super user
  24. First data block=0
  25. Maximum filesystem blocks=2162163712
  26. 400 block groups
  27. 32768 blocks per group, 32768 fragments per group
  28. 8192 inodes per group
  29. Superblock backups stored on blocks:
  30. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  31. 4096000, 7962624, 11239424
  32. Allocating group tables: done
  33. Writing inode tables: done
  34. Creating journal (32768 blocks): done
  35. Writing superblocks and filesystem accounting information: done
  36. [root@kvm ~]# blkid /dev/sdb
  37. //blkid+磁盘:查看磁盘信息
  38. /dev/sdb: UUID="dab6d659-e334-41ed-a15a-96ce05b48c1a" TYPE="ext4"
  39. 格式化完成后,我们挂载磁盘,先创建挂载目录/kvm_data,然后进行挂载,命令如下:
  40. [root@kvm ~]# mkdir /kvm_data
  41. [root@kvm ~]# mount /dev/sdb /kvm_data/
  42. //这种方式挂载,重启虚拟机后,需要再次重新挂载,我们为了方便开机后可以自动挂载,编写/etc/fstab文件,添加文件最末一行,命令如下:
  43. [root@kvm ~]# vi /etc/fstab
  44. [root@kvm ~]# cat /etc/fstab
  45. #
  46. # /etc/fstab
  47. # Created by anaconda on Thu Aug 22 22:51:46 2019
  48. #
  49. # Accessible filesystems, by reference, are maintained under '/dev/disk'
  50. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  51. #
  52. /dev/mapper/centos-root / xfs defaults 0 0
  53. UUID=34f3cd91-b7f0-44dd-9334-2bb66e939898 /boot xfs defaults 0 0
  54. /dev/mapper/centos-swap swap swap defaults 0 0
  55. /dev/sdb /kvm_data ext4 defaults 0 0

4. 关闭防火墙和selinux

  1. //关闭防火墙并设置开机不自启,命令如下:
  2. [root@kvm ~]# systemctl stop firewalld
  3. [root@kvm ~]# systemctl disable firewalld
  4. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  5. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
  6. //关闭SELinux,编辑/etc/selinux/config,将enforcing修改为disabled,命令如下:
  7. [root@kvm ~]# vi /etc/selinux/config
  8. [root@kvm ~]# cat /etc/selinux/config
  9. # This file controls the state of SELinux on the system.
  10. # SELINUX= can take one of these three values:
  11. # enforcing - SELinux security policy is enforced.
  12. # permissive - SELinux prints warnings instead of enforcing.
  13. # disabled - No SELinux policy is loaded.
  14. SELINUX=disabled
  15. //修改这一位置的内容
  16. # SELINUXTYPE= can take one of three two values:
  17. # targeted - Targeted processes are protected,
  18. # minimum - Modification of targeted policy. Only selected processes are protected.
  19. # mls - Multi Level Security protection.
  20. SELINUXTYPE=targeted
  21. //修改完成后,重启虚拟机,使其生效,使用getenforce查看,命令如下:
  22. [root@kvm ~]# getenforce
  23. Disabled

5. 安装KVM

  1. 上述操作完成后,我们使用Yum进行安装KVM,命令如下:
  2. [root@kvm ~]# yum install -y virt-* libvirt bridge-utils qemu-img
  3. …………
  4. Complete!

正式配置(启动kvm)

1. 配置网卡

  1. //增加桥接网卡ifcfg-br0,命令如下:
  2. [root@kvm ~]# cd /etc/sysconfig/network-scripts/
  3. [root@kvm network-scripts]# cp ifcfg-eno16777736 ifcfg-br0
  4. //ifcfg-eno16777736为配置IP的网卡,你的网卡可能与我的不同
  5. 修改桥接网卡ifcfg-br0的内容,命令如下:
  6. [root@kvm network-scripts]# vi ifcfg-br0
  7. [root@kvm network-scripts]# cat ifcfg-br0
  8. TYPE=Bridge
  9. BOOTPROTO=none
  10. NAME=br0
  11. DEVICE=br0
  12. ONBOOT=yes
  13. IPADDR=192.168.16.5
  14. NETMASK=255.255.255.0
  15. GATEWAY=192.168.16.2
  16. DNS1=114.114.114.114
  17. DNS2=8.8.8.8
  18. 修改NAT网卡ifcfg-eno16777736,内容如下:
  19. [root@kvm network-scripts]# vi ifcfg-eno16777736
  20. [root@kvm network-scripts]# cat ifcfg-eno16777736
  21. TYPE=Ethernet
  22. BOOTPROTO=none
  23. NAME=eno16777736
  24. DEVICE=eno16777736
  25. ONBOOT=yes
  26. BRIDGE=br0
  27. 修改完成网卡内容后,重新启动网卡服务并查看网卡信息,命令如下:
  28. [root@kvm network-scripts]# service network restart
  29. Restarting network (via systemctl): [ OK ]
  30. [root@kvm network-scripts]# ifconfig
  31. br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  32. inet 192.168.16.5 netmask 255.255.255.0 broadcast
  33. 192.168.16.255
  34. inet6 fe80::20c:29ff:fe8d:1f90 prefixlen 64 scopeid 0x20<link>
  35. ether 00:0c:29:8d:1f:90 txqueuelen 0 (Ethernet)
  36. RX packets 90 bytes 7092 (6.9 KiB)
  37. RX errors 0 dropped 0 overruns 0 frame 0
  38. TX packets 38 bytes 3604 (3.5 KiB)
  39. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  40. eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
  41. mtu 1500
  42. ether 00:0c:29:8d:1f:90 txqueuelen 1000 (Ethernet)
  43. RX packets 126287 bytes 182444678 (173.9 MiB)
  44. RX errors 0 dropped 0 overruns 0 frame 0
  45. TX packets 32532 bytes 2953477 (2.8 MiB)
  46. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  47. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  48. inet 127.0.0.1 netmask 255.0.0.0
  49. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  50. loop txqueuelen 0 (Local Loopback)
  51. RX packets 0 bytes 0 (0.0 B)
  52. RX errors 0 dropped 0 overruns 0 frame 0
  53. TX packets 0 bytes 0 (0.0 B)
  54. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  55. 可以清楚的看到br0网卡出现并且带有IP,而以前的eno16777736网卡则没有IP。网卡配置完成

2. 启动libvirtd服务

  1. 首先检查KVM模块是否加载,命令如下:
  2. [root@kvm network-scripts]# lsmod|grep kvm
  3. kvm_intel 162153 0
  4. kvm 525259 1 kvm_intel
  5. 启动libvirtd并检查是否成功启动,命令如下:
  6. [root@kvm network-scripts]# systemctl start libvirtd
  7. [root@kvm network-scripts]# ps -ef |grep libvirt
  8. root 17270 1 1 04:36 ? 00:00:00 /usr/sbin/libvirtd
  9. 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
  10. 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
  11. root 17416 2264 0 04:36 pts/0 00:00:00 grep --color=auto libvirt
  12. 启动成功后,使用brctl命令可以看到两个网卡,如下代码所示:
  13. [root@kvm ~]# cd
  14. [root@kvm ~]# brctl show
  15. bridge name bridge id STP enabled interfaces
  16. br0 8000.000c298d1f90 no eno16777736
  17. virbr0 8000.525400c7e229 yes virbr0-nic

3. 命令行安装CentOS 7

我用的是CentOS-7-x86_64-DVD-1511.iso的镜像 通过SCRT上传CentOS 7镜像到/tmp目录下,如果没有镜像的可以自行下载。上传后,使用ll命令查看/tmp目录下是否存在CentOS 7镜像文件,

  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
  4. 镜像存在,接下来使用virt-install进行CentOS 7的安装,命令如下:
  5. [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"

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

  1. 打完上面那条命令后,等一会,就可以看见下面这段命令,这是系统基础设置,带[!]基本都是需要配置的,接下来我们开始配置“Timezone settings”,输入“2”,按Enter键,命令如下:
  2. Starting installer, one moment...
  3. anaconda 21.48.22.56-1 for CentOS 7 started.
  4. * installation log files are stored in /tmp during the installation
  5. * shell is available on TTY2
  6. * when reporting a bug add logs from /tmp as separate text/plain attachments
  7. 17:01:51 Not asking for VNC because we don't have a network
  8. ================================================================================
  9. ================================================================================
  10. Installation
  11. 1) [x] Language settings 2) [!] Timezone settings
  12. (English (United States)) (Timezone is not set.)
  13. 3) [!] Installation source 4) [!] Software selection
  14. (Processing...) (Processing...)
  15. 5) [!] Installation Destination 6) [x] Kdump
  16. (No disks selected) (Kdump is enabled)
  17. 7) [ ] Network configuration 8) [!] Root password
  18. (Not connected) (Password is not set.)
  19. 9) [!] User creation
  20. (No user will be created)
  21. Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: 2
  22. =====================================================================
  23. “Timezone settings”时区设置选择 5) Asia亚洲,再选择城市 62) Shanghai上海,命令如下:
  24. Timezone settings
  25. Available regions
  26. 1) Africa 6) Atlantic 10) Pacific
  27. 2) America 7) Australia 11) US
  28. 3) Antarctica 8) Europe 12) Etc
  29. 4) Arctic 9) Indian
  30. 5) Asia
  31. Please select the timezone.
  32. Use numbers or type names directly [b to region list, q to quit]: 5
  33. ================================================================================
  34. ================================================================================
  35. Timezone settings
  36. Available timezones in region Asia
  37. 1) Aden 28) Irkutsk 54) Pyongyang
  38. 2) Almaty 29) Jakarta 55) Qatar
  39. 3) Amman 30) Jayapura 56) Qyzylorda
  40. 4) Anadyr 31) Jerusalem 57) Rangoon
  41. 5) Aqtau 32) Kabul 58) Riyadh
  42. 6) Aqtobe 33) Kamchatka 59) Sakhalin
  43. 7) Ashgabat 34) Karachi 60) Samarkand
  44. 8) Baghdad 35) Kathmandu 61) Seoul
  45. 9) Bahrain 36) Khandyga 62) Shanghai
  46. 10) Baku 37) Kolkata 63) Singapore
  47. 11) Bangkok 38)Krasnoyarsk 64) Srednekolymsk
  48. 12) Beirut 39) Kuala_Lumpur 65) Taipei
  49. 13) Bishkek 40) Kuching 66) Tashkent
  50. 14) Brunei 41) Kuwait 67) Tbilisi
  51. 15) Chita 42) Macau 68) Tehran
  52. 16) Choibalsan 43) Magadan 69) Thimphu
  53. 17) Colombo 44) Makassar 70) Tokyo
  54. 18) Damascus 45) Manila 71) Ulaanbaatar
  55. 19) Dhaka 46) Muscat 72) Urumqi
  56. 20) Dili 47) Nicosia 73) Ust-Nera
  57. 21) Dubai 48) Novokuznetsk 74) Vientiane
  58. 22) Dushanbe 49) Novosibirsk 75) Vladivostok
  59. Press ENTER to continue
  60. 23) Gaza 50) Omsk 76) Yakutsk
  61. 24) Hebron 51) Oral 77) Yekaterinburg
  62. 25) Ho_Chi_Minh 52) Phnom_Penh 78) Yerevan
  63. 26) Hong_Kong 53) Pontianak
  64. 27) Hovd
  65. Please select the timezone.
  66. Use numbers or type names directly [b to region list, q to quit]: 62
  67. ================================================================================
  68. 输入完毕后,可以发现2的[!]变成了[x],证明配置完毕。
  69. 接下来我们配置“Software selection”,选择“4”,在选择“c”,因为默认就是minimalinstall,按Enter键,命令如下:
  70. Installation
  71. 1) [x] Language settings 2) [x] Timezone settings
  72. (English (United States)) (Asia/Shanghai timezone)
  73. 3) [x] Installation source 4) [!] Software selection
  74. (Local media) (Minimal Install)
  75. 5) [!] Installation Destination 6) [x] Kdump
  76. (No disks selected) (Kdump is enabled)
  77. 7) [ ] Network configuration 8) [!] Root password
  78. (Not connected) (Password is not set.)
  79. 9) [!] User creation
  80. (No user will be created)
  81. Please make your choice from above ['q' to quit | 'b' to begin installation |
  82. 'r' to refresh]: 4
  83. ================================================================================
  84. ================================================================================
  85. Base environment
  86. Software selection
  87. Base environment
  88. 1) [x] Minimal Install 7) [ ] Server with GUI
  89. 2) [ ] Compute Node 8) [ ] GNOME Desktop
  90. 3) [ ] Infrastructure Server 9) [ ] KDE Plasma Workspaces
  91. 4) [ ] File and Print Server 10) [ ] Development and Creative Work
  92. 5) [ ] Basic Web Server station
  93. 6) [ ] Virtualization Host
  94. Please make your choice from above ['q' to quit | 'c' to continue |
  95. 'r' to refresh]: c
  96. 接下来我们配置“Installation Destination”,选择“5”,其余的依次选择“c”,按Enter键,命令如下:
  97. Installation
  98. 1) [x] Language settings 2) [x] Timezone settings
  99. (English (United States)) (Asia/Shanghai timezone)
  100. 3) [!] Installation source 4) [!] Software selection
  101. (Processing...) (Processing...)
  102. 5) [!] Installation Destination 6) [x] Kdump
  103. (No disks selected) (Kdump is enabled)
  104. 7) [ ] Network configuration 8) [!] Root password
  105. (Not connected) (Password is not set.)
  106. 9) [!] User creation
  107. (No user will be created)
  108. Please make your choice from above ['q' to quit | 'b' to begin installation |
  109. 'r' to refresh]: 5
  110. ================================================================================
  111. ================================================================================
  112. Probing storage...
  113. Installation Destination
  114. [x] 1) : 10 GiB (vda)
  115. 1 disk selected; 10 GiB capacity; 10 GiB free ...
  116. Please make your choice from above ['q' to quit | 'c' to continue |
  117. 'r' to refresh]: c
  118. ================================================================================
  119. ================================================================================
  120. Autopartitioning Options
  121. [ ] 1) Replace Existing Linux system(s)
  122. [x] 2) Use All Space
  123. [ ] 3) Use Free Space
  124. Installation requires partitioning of your hard drive. Select what space to use
  125. for the install target.
  126. Please make your choice from above ['q' to quit | 'c' to continue |
  127. 'r' to refresh]: c
  128. ================================================================================
  129. ================================================================================
  130. Partition Scheme Options
  131. [ ] 1) Standard Partition
  132. [ ] 2) Btrfs
  133. [x] 3) LVM
  134. [ ] 4) LVM Thin Provisioning
  135. Select a partition scheme configuration.
  136. Please make your choice from above ['q' to quit | 'c' to continue |
  137. 'r' to refresh]: c
  138. Generating updated storage configuration
  139. Checking storage configuration...
  140. ================================================================================
  141. ================================================================================
  142. 接下来配置“Root password”,选择8,按Enter键,命令如下:
  143. Installation
  144. 1) [x] Language settings 2) [x] Timezone settings
  145. (English (United States)) (Asia/Shanghai timezone)
  146. 3) [x] Installation source 4) [x] Software selection
  147. (Local media) (Minimal Install)
  148. 5) [x] Installation Destination 6) [x] Kdump
  149. (Automatic partitioning selecte (Kdump is enabled)
  150. d) 8) [!] Root password
  151. 7) [ ] Network configuration (Password is not set.)
  152. (Not connected)
  153. 9) [!] User creation
  154. (No user will be created)
  155. Please make your choice from above ['q' to quit | 'b' to begin installation |
  156. 'r' to refresh]: 8
  157. ================================================================================
  158. ================================================================================
  159. Please select new root password. You will have to type it twice.
  160. 依次输入两次密码,密码相同,我设置的为123456,命令如下:
  161. Password: 123456
  162. //密码为密文,不显示。
  163. Password (confirm): 123456
  164. ================================================================================
  165. ================================================================================
  166. Question
  167. You have provided a weak password: The password fails the dictionary check - it
  168. is too simplistic/systematic
  169. Would you like to use it anyway?
  170. //这句话的意思:你的密码太过简单,是否使用它,输入yes即可。
  171. Please respond 'yes' or 'no': yes
  172. =========================================================
  173. =========================================================
  174. 配置完成,选择“b”,按Enter键后,开始安装,命令如下:
  175. Installation
  176. 1) [x] Language settings 2) [x] Timezone settings
  177. (English (United States)) (Asia/Shanghai timezone)
  178. 3) [x] Installation source 4) [x] Software selection
  179. (Local media) (Minimal Install)
  180. 5) [x] Installation Destination 6) [x] Kdump
  181. (Automatic partitioning selecte (Kdump is enabled)
  182. d) 8) [x] Root password
  183. 7) [ ] Network configuration (Password is set.)
  184. (Not connected)
  185. 9) [ ] User creation
  186. (No user will be created)
  187. Please make your choice from above ['q' to quit | 'b' to begin installation |
  188. 'r' to refresh]: b
  189. ================================================================================
  190. ================================================================================
  191. Progress
  192. Setting up the installation environment
  193. .
  194. Creating disklabel on /dev/vda
  195. .
  196. Creating xfs on /dev/vda1
  197. .
  198. Creating lvmpv on /dev/vda2
  199. .
  200. Creating swap on /dev/mapper/centos-swap
  201. .
  202. ……………………
  203. Installing readline (34/297)
  204. Installing gawk (35/297)
  205. Installing elfutils-libelf (36/297)
  206. Installing libgpg-error (37/297)
  207. Installing libffi (38/297)
  208. Installing libattr (39/297)
  209. Installing libacl (40/297)
  210. Installing libcap (41/297)
  211. Installing libgcrypt (42/297)
  212. Installing cpio (43/297)
  213. Installing libxml2 (44/297)
  214. Installing libnl3 (45/297)
  215. Installing expat (46/297)
  216. Installing p11-kit (47/297)
  217. ……………………
  218. .
  219. Performing post-installation setup tasks
  220. .
  221. Configuring installed system
  222. .
  223. Writing network configuration
  224. .
  225. Creating users
  226. .
  227. Configuring addons
  228. .
  229. Generating initramfs
  230. .
  231. Running post-installation scripts
  232. .
  233. Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA
  234. Installation complete. Press return to quit
  235. //按回车
  236. ……………………
  237. CentOS Linux 7 (Core)
  238. Kernel 3.10.0-327.el7.x86_64 on an x86_64
  239. localhost login: root
  240. Password: 123456
  241. [root@localhost ~]#
  242. //按“Ctrl+]”键,退出终端,回到宿主机。
  243. [root@kvm ~]#
  244. CentOS 7安装完成。

虚拟机管理

1. KVM基本管理

  1. 完成虚拟机安装,已经退回到宿主机,接下来我们来通过宿主机virsh命令进行管理刚才安装的CentOS 7虚拟机。
  2. 查看虚拟机列表,命令如下:
  3. [root@kvm ~]# virsh list
  4. //查看虚拟机列表,只能看到正在运行的虚拟机
  5. Id Name State
  6. ----------------------------------------------------
  7. 2 test running
  8. [root@kvm ~]# virsh list --all
  9. //查看虚拟机列表,包括未运行的虚拟机
  10. Id Name State
  11. ----------------------------------------------------
  12. 2 test running
  13. 进入指定的虚拟,命令如下:
  14. [root@kvm ~]# virsh console test
  15. Connected to domain test
  16. Escape character is ^]
  17. //如果迟迟未动敲下回车就ok,下面就是正常登录时需要输入的用户名密码
  18. CentOS Linux 7 (Core)
  19. Kernel 3.10.0-327.el7.x86_64 on an x86_64
  20. localhost login:
  21. 若发生以下报错信息,命令如下:
  22. [root@kvm ~]# virsh console test
  23. Connected to domain test
  24. Escape character is ^]
  25. error: operation failed: Active console session exists for this domain
  26. 解决方式,命令如下:
  27. [root@kvm ~]# ps -ef |grep console
  28. root 11167 10987 0 01:55 tty1 00:00:00 virsh console test
  29. root 16017 15750 0 10:01 pts/1 00:00:00 grep --color=auto console
  30. [root@kvm ~]# kill -9 11167
  31. vrish常用命令,命令如下:
  32. virsh shutdown test
  33. //关闭虚拟机
  34. virsh start test
  35. //开启虚拟机
  36. virsh destroy test
  37. //类似stop,这个是强制停止
  38. virsh undefine test
  39. //彻底销毁虚拟机,会删除虚拟机配置文件,virsh list --all就看不到
  40. virsh autostart test
  41. //宿主机开机该虚拟机也开机
  42. virsh autostart --disable test
  43. //解除开机启动
  44. virsh suspend test
  45. //挂起
  46. virsh resume test
  47. //恢复

2.克隆虚拟机

  1. 克隆虚拟机之前,要先关闭虚拟机,不然会提示这种错误,命令如下:
  2. [root@kvm ~]# virt-clone --original test --name test02 --file
  3. /kvm_data/test02.img
  4. ERROR Domain with devices to clone must be paused or shutoff.
  5. 关闭虚拟机后,再次进行克隆,命令如下:
  6. [root@kvm ~]# virsh shutdown test
  7. Domain test is being shutdown
  8. [root@kvm ~]# virt-clone --original test --name test02 --file
  9. /kvm_data/test02.img
  10. Allocating 'test02.img' 3%
  11. [= ] 20 MB/s | 365 MB 00:08:23 ETA
  12. //开始克隆
  13. [root@kvm ~]# virt-clone --original test --name test02 --file
  14. /kvm_data/test02.img
  15. Allocating 'test02.img' | 10 GB 00:00:35
  16. Clone 'test02' created successfully.
  17. //克隆完成且成功
  18. 命令解释:
  19. --original:指定克隆源虚拟机。
  20. --name:指定克隆后的虚拟机名字。
  21. --file:指定目标虚拟机的虚拟磁盘文件。
  22. 查看虚拟机配置文件/etc/libvirt/qemu/,看是否增加test02.xml文件,命令如下:
  23. [root@kvm ~]# ls /etc/libvirt/qemu/
  24. networks test02.xml test.xml
  25. 接下来,我们启动刚刚克隆的虚拟机test02,首先查看虚拟机列表,然后启动克隆的虚拟机test02,命令如下:
  26. [root@kvm ~]# virsh list --all
  27. Id Name State
  28. ----------------------------------------------------
  29. - test shut off
  30. - test02 shut off
  31. [root@kvm ~]# virsh start test02
  32. Domain test02 started

3. 快照管理

  1. //快照是在我们使用Vmware的时候,很常用的一个功能。它可以回到之前的某一状态。在KVM中RAW格式的虚拟磁盘不支持做快照,qcow2支持。
  2. 创建快照,命令如下:
  3. [root@kvm ~]# virsh snapshot-create test
  4. Domain snapshot 1588485687 created
  5. 查看test.img信息,同时会查看到快照列表,命令如下:
  6. [root@kvm ~]# qemu-img info /kvm_data/test.img
  7. image: /kvm_data/test.img
  8. file format: qcow2
  9. virtual size: 10G (10737418240 bytes)
  10. disk size: 1.1G
  11. cluster_size: 65536
  12. Snapshot list:
  13. ID TAG VM SIZE DATE VM CLOCK
  14. 1 1588485687 0 2020-05-03 14:01:27 00:00:00.000
  15. Format specific information:
  16. compat: 1.1
  17. lazy refcounts: true
  18. 列出所有快照,命令如下:
  19. [root@kvm ~]# virsh snapshot-list test
  20. Name Creation Time State
  21. ------------------------------------------------------------
  22. 1588485687 2020-05-03 14:01:27 +0800 shutoff
  23. 查看当前快照版本,命令如下:
  24. [root@kvm ~]# virsh snapshot-current test
  25. <domainsnapshot>
  26. <name>1588485687</name>
  27. <state>shutoff</state>
  28. <creationTime>1588485687</creationTime>
  29. <memory snapshot='no'/>
  30. <disks>
  31. <disk name='vda' snapshot='internal'/>
  32. <disk name='hda' snapshot='no'/>
  33. </disks>
  34. <domain type='kvm'>
  35. <name>test</name>
  36. <uuid>49d7cb9c-20dc-42dd-a260-01532b5132e5</uuid>
  37. <memory unit='KiB'>1048576</memory>
  38. <currentMemory unit='KiB'>524288</currentMemory>
  39. <vcpu placement='static' current='1'>2</vcpu>
  40. <os>
  41. <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
  42. <boot dev='hd'/>
  43. </os>
  44. <features>
  45. <acpi/>
  46. <apic/>
  47. </features>
  48. <cpu mode='custom' match='exact' check='partial'>
  49. <model fallback='allow'>Broadwell-noTSX-IBRS</model>
  50. <feature policy='require' name='md-clear'/>
  51. <feature policy='require' name='spec-ctrl'/>
  52. <feature policy='require' name='ssbd'/>
  53. </cpu>
  54. <clock offset='utc'>
  55. <timer name='rtc' tickpolicy='catchup'/>
  56. <timer name='pit' tickpolicy='delay'/>
  57. <timer name='hpet' present='no'/>
  58. </clock>
  59. <on_poweroff>destroy</on_poweroff>
  60. <on_reboot>restart</on_reboot>
  61. <on_crash>destroy</on_crash>
  62. <pm>
  63. <suspend-to-mem enabled='no'/>
  64. <suspend-to-disk enabled='no'/>
  65. </pm>
  66. <devices>
  67. <emulator>/usr/libexec/qemu-kvm</emulator>
  68. <disk type='file' device='disk'>
  69. <driver name='qemu' type='qcow2'/>
  70. <source file='/kvm_data/test.img'/>
  71. <target dev='vda' bus='virtio'/>
  72. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  73. </disk>
  74. <disk type='file' device='cdrom'>
  75. <driver name='qemu' type='raw'/>
  76. <target dev='hda' bus='ide'/>
  77. <readonly/>
  78. <address type='drive' controller='0' bus='0' target='0' unit='0'/>
  79. </disk>
  80. <controller type='usb' index='0' model='ich9-ehci1'>
  81. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
  82. </controller>
  83. <controller type='usb' index='0' model='ich9-uhci1'>
  84. <master startport='0'/>
  85. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
  86. </controller>
  87. <controller type='usb' index='0' model='ich9-uhci2'>
  88. <master startport='2'/>
  89. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
  90. </controller>
  91. <controller type='usb' index='0' model='ich9-uhci3'>
  92. <master startport='4'/>
  93. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
  94. </controller>
  95. <controller type='ide' index='0'>
  96. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
  97. </controller>
  98. <controller type='virtio-serial' index='0'>
  99. <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
  100. </controller>
  101. <controller type='pci' index='0' model='pci-root'/>
  102. <interface type='bridge'>
  103. <mac address='52:54:00:93:bf:07'/>
  104. <source bridge='br0'/>
  105. <model type='virtio'/>
  106. <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  107. </interface>
  108. <serial type='pty'>
  109. <target type='isa-serial' port='0'>
  110. <model name='isa-serial'/>
  111. </target>
  112. </serial>
  113. <console type='pty'>
  114. <target type='serial' port='0'/>
  115. </console>
  116. <channel type='unix'>
  117. <target type='virtio' name='org.qemu.guest_agent.0'/>
  118. <address type='virtio-serial' controller='0' bus='0' port='1'/>
  119. </channel>
  120. <input type='mouse' bus='ps2'/>
  121. <input type='keyboard' bus='ps2'/>
  122. <memballoon model='virtio'>
  123. <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
  124. </memballoon>
  125. <rng model='virtio'>
  126. <backend model='random'>/dev/urandom</backend>
  127. <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
  128. </rng>
  129. </devices>
  130. </domain>
  131. </domainsnapshot>
  132. 查看所有快照配置文件,命令如下:
  133. [root@kvm ~]# ls /var/lib/libvirt/qemu/snapshot/test/
  134. 1588485687.xml
  135. 恢复指定快照,命令如下:
  136. [root@kvm ~]# virsh snapshot-revert test 1588485687
  137. 删除指定快照,命令如下:
  138. [root@kvm ~]# virsh snapshot-delete test 1588485687
  139. Domain snapshot 1588485687 deleted

4. 磁盘格式

  1. 查看虚拟磁盘格式,命令如下:
  2. [root@kvm ~]# qemu-img info /kvm_data/test.img
  3. image: /kvm_data/test.img
  4. file format: qcow2
  5. virtual size: 10G (10737418240 bytes)
  6. disk size: 1.1G
  7. cluster_size: 65536
  8. Format specific information:
  9. compat: 1.1
  10. lazy refcounts: true
  11. 创建2 GBRAW格式磁盘,命令如下:
  12. [root@kvm ~]# qemu-img create -f raw /kvm_data/test_1.img 2G
  13. Formatting '/kvm_data/test_1.img', fmt=raw size=2147483648
  14. RAW格式的磁盘转换为qcow2格式,命令如下:
  15. [root@kvm ~]# qemu-img convert -O qcow2 /kvm_data/test_1.img
  16. /kvm_data/test_1.qcow2
  17. 查看test1.img大小,命令如下:
  18. [root@kvm ~]# ls -lh /kvm_data/test_1.img
  19. -rw-r--r-- 1 root root 2.0G May 3 15:11 /kvm_data/test_1.img
  20. [root@kvm ~]# ls -lh /kvm_data/test_1.qcow2
  21. -rw-r--r-- 1 root root 193K May 3 15:38 /kvm_data/test_1.qcow2
  22. //可以看到qcow2文件比较小,raw文件大小和我们指定空间大小一样是2G
  23. //raw格式的磁盘性能比qcow2要好,但是raw格式的磁盘无法做快照
  24. test02转成raw格式,命令如下:
  25. [root@kvm ~]# qemu-img convert -O raw
  26. /kvm_data/test02.img /kvm_data/test02_2.raw
  27. 更改磁盘格式和文件路径,然后启动test02虚拟机,命令如下:
  28. [root@kvm ~]# virsh edit test02
  29. …………
  30. <disk type='file' device='disk'>
  31. <driver name='qemu' type='qcow'/>
  32. <source file='/kvm_data/test02.img'/>
  33. <target dev='vda' bus='virtio'/>
  34. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  35. </disk>
  36. //找到上面这部分将qcow修改为raw,/kvm_data/test02.img改为/kvm_data/test02_2.raw
  37. <disk type='file' device='disk'>
  38. <driver name='qemu' type='raw'/>
  39. <source file='/kvm_data/test02_2.raw'/>
  40. <target dev='vda' bus='virtio'/>
  41. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  42. </disk>
  43. ………………
  44. 查看磁盘所属用户和组,命令如下:
  45. [root@kvm ~]# ls -l /kvm_data/
  46. total 3310628
  47. drwx------ 2 root root 16384 Apr 29 00:20 lost+found
  48. -rw-r--r-- 1 root root 10737418240 May 3 16:14 test02_2.raw
  49. -rw------- 1 root root 1133772800 May 3 16:02 test02.img
  50. -rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img
  51. -rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2
  52. -rw------- 1 root root 10739384832 May 3 14:14 test.img
  53. 启动虚拟机test02,然后再次查看磁盘所属用户和组,命令如下:
  54. [root@kvm ~]# virsh start test02
  55. Domain test02 started
  56. [root@kvm ~]# ls -l /kvm_data/
  57. total 3310628
  58. drwx------ 2 root root 16384 Apr 29 00:20 lost+found
  59. -rw-r--r-- 1 qemu qemu 10737418240 May 3 16:14 test02_2.raw
  60. -rw------- 1 root root 1133772800 May 3 16:02 test02.img
  61. -rw-r--r-- 1 root root 2147483648 May 3 15:11 test_1.img
  62. -rw-r--r-- 1 root root 197120 May 3 16:05 test_1.qcow2
  63. -rw------- 1 root root 10739384832 May 3 14:14 test.img
  64. //启动后所属组变化成qemu的是test02_2.raw,证明这个磁盘正在被使用

5. 磁盘扩容

  1. 我们知道,在这里磁盘一共有两种格式,一种是RAW格式,一种是qcow2格式,接下来我们分别给这两种磁盘格式进行扩容。
  2. 首先扩容RAW格式,命令如下:
  3. [root@kvm ~]# qemu-img resize /kvm_data/test02_2.raw +2G
  4. Image resized.
  5. 查看test02_2.raw信息,命令如下:
  6. [root@kvm ~]# qemu-img info /kvm_data/test02_2.raw
  7. image: /kvm_data/test02_2.raw
  8. file format: raw
  9. virtual size: 12G (12884901888 bytes)
  10. disk size: 1.0G
  11. 进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:
  12. [root@kvm ~]# virsh console test02
  13. [root@localhost ~]# fdisk -l
  14. Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
  15. Units = sectors of 1 * 512 = 512 bytes
  16. Sector size (logical/physical): 512 bytes / 512 bytes
  17. I/O size (minimum/optimal): 512 bytes / 512 bytes
  18. Disk label type: dos
  19. Disk identifier: 0x0005b865
  20. Device Boot Start End Blocks Id System
  21. /dev/vda1 * 2048 1026047 512000 83 Linux
  22. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  23. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  24. Units = sectors of 1 * 512 = 512 bytes
  25. Sector size (logical/physical): 512 bytes / 512 bytes
  26. I/O size (minimum/optimal): 512 bytes / 512 bytes
  27. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  28. Units = sectors of 1 * 512 = 512 bytes
  29. Sector size (logical/physical): 512 bytes / 512 bytes
  30. I/O size (minimum/optimal): 512 bytes / 512 bytes
  31. //可以看到磁盘还是10 GB
  32. 退出终端,关闭test02虚拟机,重新启动,然后再次进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:
  33. [root@kvm ~]# virsh destroy test02
  34. Domain test02 destroyed
  35. [root@kvm ~]# virsh start test02
  36. Domain test02 started
  37. [root@kvm ~]# virsh console test02
  38. [root@localhost ~]# fdisk -l
  39. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  40. Units = sectors of 1 * 512 = 512 bytes
  41. Sector size (logical/physical): 512 bytes / 512 bytes
  42. I/O size (minimum/optimal): 512 bytes / 512 bytes
  43. Disk label type: dos
  44. Disk identifier: 0x0005b865
  45. Device Boot Start End Blocks Id System
  46. /dev/vda1 * 2048 1026047 512000 83 Linux
  47. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  48. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  49. Units = sectors of 1 * 512 = 512 bytes
  50. Sector size (logical/physical): 512 bytes / 512 bytes
  51. I/O size (minimum/optimal): 512 bytes / 512 bytes
  52. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  53. Units = sectors of 1 * 512 = 512 bytes
  54. Sector size (logical/physical): 512 bytes / 512 bytes
  55. I/O size (minimum/optimal): 512 bytes / 512 bytes
  56. 可以看到已经变成12 GB了。
  57. 接下来,我们怎么使用新增加的这2 GB磁盘,那就是分区,命令如下:
  58. [root@localhost ~]# fdisk /dev/vda
  59. Welcome to fdisk (util-linux 2.23.2).
  60. Changes will remain in memory only, until you decide to write them.
  61. Be careful before using the write command.
  62. Command (m for help): n
  63. //新建分区
  64. Partition type:
  65. p primary (2 primary, 0 extended, 2 free)
  66. e extended
  67. Select (default p): p
  68. Partition number (3,4, default 3):
  69. //按Enter键
  70. First sector (20971520-25165823, default 20971520):
  71. //按Enter键
  72. Using default value 20971520
  73. Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823):
  74. //按Enter键
  75. Using default value 25165823
  76. Partition 3 of type Linux and of size 2 GiB is set
  77. Command (m for help): p
  78. //查看分区列表
  79. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  80. Units = sectors of 1 * 512 = 512 bytes
  81. Sector size (logical/physical): 512 bytes / 512 bytes
  82. I/O size (minimum/optimal): 512 bytes / 512 bytes
  83. Disk label type: dos
  84. Disk identifier: 0x0005b865
  85. Device Boot Start End Blocks Id System
  86. /dev/vda1 * 2048 1026047 512000 83 Linux
  87. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  88. /dev/vda3 20971520 25165823 2097152 83 Linux
  89. Command (m for help): w
  90. //保存退出并退出终端
  91. 除了对已有磁盘扩容外,还可以额外增加磁盘,命令如下:
  92. [root@kvm ~]# qemu-img create -f raw /kvm_data/test02_3.raw 5G
  93. Formatting '/kvm_data/test02_3.raw', fmt=raw size=5368709120
  94. 使用virsh edit编辑test02虚拟机,将新磁盘增加到test02虚拟机,命令如下
  95. [root@kvm ~]# virsh edit test02
  96. ………………
  97. <disk type='file' device='disk'>
  98. <driver name='qemu' type='raw'/>
  99. <source file='/kvm_data/test02_2.raw'/>
  100. <target dev='vda' bus='virtio'/>
  101. <address type='pci' domain='0x0000' bus='0x00' slot='0x06'
  102. function='0x0'/>
  103. </disk>
  104. //找到这一部分内容,在下面增加以下内容
  105. <disk type='file' device='disk'>
  106. <driver name='qemu' type='raw'/>
  107. <source file='/kvm_data/test02_3.raw'/>
  108. <target dev='vdb' bus='virtio'/>
  109. <address type='pci' domain='0x0000' bus='0x00' slot='0x09'
  110. function='0x0'/>
  111. </disk>
  112. ………………
  113. 关闭test02虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘,命令如下:
  114. [root@kvm ~]# virsh destroy test02
  115. Domain test02 destroyed
  116. [root@kvm ~]# virsh start test02
  117. Domain test02 started
  118. [root@kvm ~]# virsh console test02
  119. [root@localhost ~]# fdisk -l
  120. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  121. Units = sectors of 1 * 512 = 512 bytes
  122. Sector size (logical/physical): 512 bytes / 512 bytes
  123. I/O size (minimum/optimal): 512 bytes / 512 bytes
  124. Disk label type: dos
  125. Disk identifier: 0x0005b865
  126. Device Boot Start End Blocks Id System
  127. /dev/vda1 * 2048 1026047 512000 83 Linux
  128. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  129. /dev/vda3 20971520 25165823 2097152 83 Linux
  130. Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
  131. Units = sectors of 1 * 512 = 512 bytes
  132. Sector size (logical/physical): 512 bytes / 512 bytes
  133. I/O size (minimum/optimal): 512 bytes / 512 bytes
  134. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  135. Units = sectors of 1 * 512 = 512 bytes
  136. Sector size (logical/physical): 512 bytes / 512 bytes
  137. I/O size (minimum/optimal): 512 bytes / 512 bytes
  138. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  139. Units = sectors of 1 * 512 = 512 bytes
  140. Sector size (logical/physical): 512 bytes / 512 bytes
  141. I/O size (minimum/optimal): 512 bytes / 512 bytes
  142. 可以看到多了一块5 GB的磁盘,退出终端。
  143. 接下来,我们扩容qcow2格式的磁盘,命令如下:
  144. [root@kvm ~]# qemu-img resize /kvm_data/test.img +2G
  145. Image resized.
  146. //若提示qemu-img: Can't resize an image which has snapshots,需要删除快照
  147. 查看test.img信息,命令如下:
  148. [root@kvm ~]# qemu-img info /kvm_data/test.img
  149. image: /kvm_data/test.img
  150. file format: qcow2
  151. virtual size: 12G (12884901888 bytes)
  152. disk size: 1.1G
  153. cluster_size: 65536
  154. Format specific information:
  155. compat: 1.1
  156. lazy refcounts: true
  157. 关闭test虚拟机,重新启动,然后再次进入虚拟机test02,使用fdisk -l查看磁盘,命令如下:
  158. [root@kvm ~]# virsh destroy test
  159. Domain test destroyed
  160. [root@kvm ~]# virsh start test
  161. Domain test started
  162. [root@kvm ~]# virsh console test
  163. [root@localhost ~]# fdisk -l
  164. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  165. Units = sectors of 1 * 512 = 512 bytes
  166. Sector size (logical/physical): 512 bytes / 512 bytes
  167. I/O size (minimum/optimal): 512 bytes / 512 bytes
  168. Disk label type: dos
  169. Disk identifier: 0x0005b865
  170. Device Boot Start End Blocks Id System
  171. /dev/vda1 * 2048 1026047 512000 83 Linux
  172. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  173. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  174. Units = sectors of 1 * 512 = 512 bytes
  175. Sector size (logical/physical): 512 bytes / 512 bytes
  176. I/O size (minimum/optimal): 512 bytes / 512 bytes
  177. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  178. Units = sectors of 1 * 512 = 512 bytes
  179. Sector size (logical/physical): 512 bytes / 512 bytes
  180. I/O size (minimum/optimal): 512 bytes / 512 bytes
  181. 分区新增加的2 GB磁盘,命令如下:
  182. [root@localhost ~]# fdisk /dev/vda
  183. Welcome to fdisk (util-linux 2.23.2).
  184. Changes will remain in memory only, until you decide to write them.
  185. Be careful before using the write command.
  186. Command (m for help): n
  187. Partition type:
  188. p primary (2 primary, 0 extended, 2 free)
  189. e extended
  190. Select (default p): p
  191. Partition number (3,4, default 3):
  192. First sector (20971520-25165823, default 20971520):
  193. Using default value 20971520
  194. Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823):
  195. Using default value 25165823
  196. Partition 3 of type Linux and of size 2 GiB is set
  197. Command (m for help): w
  198. /保存并退出终端
  199. 新增一块qcow2格式的磁盘,命令如下:
  200. [root@kvm ~]# qemu-img create -f qcow2 /kvm_data/test_2.img 5G
  201. Formatting '/kvm_data/test_2.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off
  202. 使用virsh edit编辑test虚拟机,将新磁盘增加到test虚拟机,命令如下:
  203. [root@kvm ~]# virsh edit test
  204. ………………
  205. <disk type='file' device='disk'>
  206. <driver name='qemu' type='qcow2'/>
  207. <source file='/kvm_data/test.img'/>
  208. <target dev='vda' bus='virtio'/>
  209. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  210. </disk>
  211. //找到这一部分内容,在下面增加以下内容
  212. <disk type='file' device='disk'>
  213. <driver name='qemu' type='qcow2'/>
  214. <source file='/kvm_data/test_2.img'/>
  215. <target dev='vdb' bus='virtio'/>
  216. <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
  217. </disk>
  218. ………………
  219. 关闭test虚拟机,重新启动,然后进入该虚拟机,使用fdisk -l查看磁盘,命令如下:
  220. [root@kvm ~]# virsh destroy test
  221. Domain test destroyed
  222. [root@kvm ~]# virsh start test
  223. Domain test started
  224. [root@kvm ~]# virsh console test
  225. [root@localhost ~]# fdisk -l
  226. Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
  227. Units = sectors of 1 * 512 = 512 bytes
  228. Sector size (logical/physical): 512 bytes / 512 bytes
  229. I/O size (minimum/optimal): 512 bytes / 512 bytes
  230. Disk label type: dos
  231. Disk identifier: 0x0005b865
  232. Device Boot Start End Blocks Id System
  233. /dev/vda1 * 2048 1026047 512000 83 Linux
  234. /dev/vda2 1026048 20971519 9972736 8e Linux LVM
  235. /dev/vda3 20971520 25165823 2097152 83 Linux
  236. Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
  237. Units = sectors of 1 * 512 = 512 bytes
  238. Sector size (logical/physical): 512 bytes / 512 bytes
  239. I/O size (minimum/optimal): 512 bytes / 512 bytes
  240. Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
  241. Units = sectors of 1 * 512 = 512 bytes
  242. Sector size (logical/physical): 512 bytes / 512 bytes
  243. I/O size (minimum/optimal): 512 bytes / 512 bytes
  244. Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
  245. Units = sectors of 1 * 512 = 512 bytes
  246. Sector size (logical/physical): 512 bytes / 512 bytes
  247. I/O size (minimum/optimal): 512 bytes / 512 bytes
  248. 退出终端

6. 调整CPU内存、网卡

  1. 查看虚拟机配置信息,命令如下:
  2. [root@kvm ~]# virsh dominfo test
  3. Id: 8
  4. Name: test
  5. UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5
  6. OS Type: hvm
  7. State: running
  8. CPU(s): 1
  9. CPU time: 20.8s
  10. Max memory: 1048576 KiB
  11. Used memory: 524288 KiB
  12. Persistent: yes
  13. Autostart: disable
  14. Managed save: no
  15. Security model: none
  16. Security DOI: 0
  17. 编辑虚拟机内存,命令如下:
  18. [root@kvm ~]# virsh edit test
  19. …………
  20. <memory unit='KiB'>1048576</memory>
  21. //最大内存
  22. <currentMemory unit='KiB'>524288</currentMemory>
  23. //可用内存
  24. <vcpu placement='static' current='1'>2</vcpu>
  25. //最大cpu
  26. ………………
  27. //将以上内容修改为如下内容
  28. …………
  29. <memory unit='KiB'>1048576</memory>
  30. <currentMemory unit='KiB'>624288</currentMemory>
  31. <vcpu placement='static' current='1'>2</vcpu>
  32. …………
  33. 关闭虚拟机,重启动虚拟机,命令如下:
  34. [root@kvm ~]# virsh destroy test
  35. Domain test destroyed
  36. [root@kvm ~]# virsh start test
  37. Domain test started
  38. 查看虚拟机配置信息,看是否修改成功,命令如下:
  39. [root@kvm ~]# virsh dominfo test
  40. Id: 10
  41. Name: test
  42. UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5
  43. OS Type: hvm
  44. State: running
  45. CPU(s): 1
  46. CPU time: 21.4s
  47. Max memory: 1048576 KiB
  48. Used memory: 624288 KiB
  49. Persistent: yes
  50. Autostart: disable
  51. Managed save: no
  52. Security model: none
  53. Security DOI: 0
  54. 除了这种修改方式之外,还有一种动态修改,命令如下:
  55. [root@kvm ~]# virsh setmem test 800m
  56. //动态修改内存
  57. [root@kvm ~]# virsh setvcpus test 2
  58. //动态修改cpu,只可以增加不可以减少
  59. 查看配置信息,看是否修改成功,命令如下:
  60. [root@kvm ~]# virsh dominfo test
  61. Id: 10
  62. Name: test
  63. UUID: 49d7cb9c-20dc-42dd-a260-01532b5132e5
  64. OS Type: hvm
  65. State: running
  66. CPU(s): 2
  67. CPU time: 22.8s
  68. Max memory: 1048576 KiB
  69. Used memory: 819200 KiB
  70. Persistent: yes
  71. Autostart: disable
  72. Managed save: no
  73. Security model: none
  74. Security DOI: 0
  75. [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml
  76. //需要把配置写入到配置文件里
  77. 查看网卡列表,命令如下:
  78. [root@kvm ~]# virsh domiflist test
  79. Interface Type Source Model MAC
  80. -------------------------------------------------------
  81. vnet1 bridge br0 virtio 52:54:00:93:bf:07
  82. 增加一块新的网卡,并设置为NAT网络模式(virbr0类似VMwareVMnet8),这里如果写--source br0,则网络模式为桥接,命令如下:
  83. [root@kvm ~]# virsh attach-interface test --type bridge --source virbr0
  84. Interface attached successfully
  85. [root@kvm ~]# virsh domiflist test
  86. Interface Type Source Model MAC
  87. -------------------------------------------------------
  88. vnet1 bridge br0 virtio 52:54:00:93:bf:07
  89. vnet2 bridge virbr0 rtl8139 52:54:00:29:ed:8b
  90. [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml
  91. 进入test虚拟机,查看网卡信息,命令如下:
  92. [root@kvm ~]# virsh console test
  93. [root@localhost ~]# ifconfig
  94. ens10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  95. inet 192.168.122.8 netmask 255.255.255.0 broadcast 192.168.122.255
  96. inet6 fe80::5054:ff:fe29:ed8b prefixlen 64 scopeid 0x20<link>
  97. ether 52:54:00:29:ed:8b txqueuelen 1000 (Ethernet)
  98. RX packets 5186 bytes 7413267 (7.0 MiB)
  99. RX errors 0 dropped 0 overruns 0 frame 0
  100. TX packets 1480 bytes 84889 (82.8 KiB)
  101. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  102. ens11: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  103. inet 192.168.16.129 netmask 255.255.255.0 broadcast 192.168.16.255
  104. inet6 fe80::5054:ff:fe15:abbd prefixlen 64 scopeid 0x20<link>
  105. ether 52:54:00:15:ab:bd txqueuelen 1000 (Ethernet)
  106. RX packets 13 bytes 1328 (1.2 KiB)
  107. RX errors 0 dropped 0 overruns 0 frame 0
  108. TX packets 16 bytes 1668 (1.6 KiB)
  109. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  110. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  111. ether 52:54:00:93:bf:07 txqueuelen 1000 (Ethernet)
  112. RX packets 55 bytes 8520 (8.3 KiB)
  113. RX errors 0 dropped 0 overruns 0 frame 0
  114. TX packets 0 bytes 0 (0.0 B)
  115. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  116. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  117. inet 127.0.0.1 netmask 255.0.0.0
  118. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  119. loop txqueuelen 0 (Local Loopback)
  120. RX packets 0 bytes 0 (0.0 B)
  121. RX errors 0 dropped 0 overruns 0 frame 0
  122. TX packets 0 bytes 0 (0.0 B)
  123. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

7. 迁移虚拟机

  1. 首先关闭虚拟机,命令如下:
  2. [root@kvm ~]# virsh shutdown test
  3. Domain test is being shutdown
  4. 查看虚拟机磁盘所在目录,命令如下:
  5. [root@kvm ~]# virsh domblklist test
  6. Target Source
  7. ------------------------------------------------
  8. vda /kvm_data/test.img
  9. vdb /kvm_data/test_2.img
  10. hda -
  11. [root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test03.xml
  12. //如果是远程机器,需要把该配置文件拷贝到远程机器上
  13. [root@kvm ~]# rsync -av /kvm_data/test.img /kvm_data/test03.img
  14. //-bash: rsync: command not found则yum install -y rsync
  15. //如果是迁移到远程,则需要把该磁盘文件拷贝到远程机器上
  16. sending incremental file list
  17. test.img
  18. sent 10,742,006,844 bytes received 35 bytes 60,179,310.25 bytes/sec
  19. total size is 10,739,384,832 speedup is 1.00
  20. 因为是迁移到本机,配置文件用的是test子机的配置,不改会有冲突,所以需要修改该文件。如果是远程机器不用修改,命令如下:
  21. [root@kvm ~]# vi /etc/libvirt/qemu/test03.xml
  22. <name>test03</name>
  23. //修改domname:
  24. //修改uuid(随便改一下数字,位数不要变)
  25. <disk type='file' device='disk'>
  26. <driver name='qemu' type='qcow2'/>
  27. <source file='/kvm_data/test03.img'/>
  28. <target dev='vda' bus='virtio'/>
  29. <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
  30. </disk>
  31. //修改磁盘路径
  32. 定义新虚拟机,命令如下:
  33. [root@kvm ~]# virsh define /etc/libvirt/qemu/test03.xml
  34. Domain test03 defined from /etc/libvirt/qemu/test03.xml
  35. 查看虚拟机列表,会发现新迁移的虚拟机test03,命令如下:
  36. [root@kvm ~]# virsh list --all
  37. Id Name State
  38. ----------------------------------------------------
  39. 6 test02 running
  40. - test shut off
  41. - test03 shut off