虚拟化

了解虚拟化

常见虚拟化软件

 虚拟化架构

kvm介绍

Centos7上安装KVM

 1.我们将我们的虚拟机模拟成一台配置比较高的物理服务器:Windows-》vmware-》centos7-》kvm-》centos7,相当于在虚拟机里安装虚拟机,

2.点击虚拟机右键点击设置,设置内存和cpu,增加硬盘,勾选cpu里面的容许虚拟化

3.配置完成之后开启虚拟机01

4.查看内存和cpu信息

[root@centos-01 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        357M        3.0G        9.1M        368M        3.1G
Swap:          2.0G          0B        2.0G
[root@centos-01 ~]# 
[root@centos-01 ~]# cat /proc/cpuinfo
[root@centos-01 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   18G  7.2G   11G   41% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G   84K  1.9G    1% /dev/shm
tmpfs                    1.9G  9.0M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                497M  157M  341M   32% /boot
tmpfs                    378M   16K  378M    1% /run/user/42
tmpfs                    378M     0  378M    0% /run/user/0
[root@centos-01 ~]# 

5.查看cpu参数是否支持虚拟化,有vmx说明支持

[root@centos-01 ~]# grep -Ei 'vmx|svm' /proc/cpuinfo 
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology
tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave avx f16c hypervisor lahf_lm arat epb pln pts dtherm tpr_shadow vnmi
ept vpid fsgsbase tsc_adjust smep flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology
tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave avx f16c hypervisor lahf_lm arat epb pln pts dtherm tpr_shadow vnmi
ept vpid fsgsbase tsc_adjust smep [root@centos-01 ~]#

6.将新增的硬盘格式化,并挂载

[root@centos-01 ~]# fdisk -l

磁盘 /dev/sdb:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b7e6b

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

磁盘 /dev/mapper/centos-root:18.8 GB, 18756927488 字节,36634624 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[root@centos-01 ~]# 

7.我们安装了一个新的硬盘,将它格式化成ext4

[root@centos-01 ~]# mkfs.ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107200 blocks
655360 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2162163712
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成   

[root@centos-01 ~]# 

8.查看硬盘有没有格式化,格式化成了什么格式

[root@centos-01 ~]# blkid /dev/sdb
/dev/sdb: UUID="5351fba5-75b2-4b5c-ae9e-2128e548f1a9" TYPE="ext4" 
[root@centos-01 ~]# 

9.创建kvm_data目录,把sdb磁盘挂载到kvm_data目录下

[root@centos-01 ~]# mkdir /kvm_data
[root@centos-01 ~]# mount /dev/sdb /kvm_data/
[root@centos-01 ~]# 

10.编辑下fstab让他开机的时候自动挂载新增加的硬盘

[root@centos-01 ~]# vim /etc/fstab
[root@centos-01 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Fri Feb 10 00:07:20 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=77a44c91-8dba-4915-b58b-0635a1c071a9 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb        /kvm_data       ext4    defaults        0 0 (新增)
[root@centos-01 ~]# 

11.停止iptables或Firewalls

[root@centos-01 ~]# systemctl stop firewalld
[root@centos-01 ~]# systemctl stop iptables
Failed to stop iptables.service: Unit iptables.service not loaded.
[root@centos-01 ~]# systemctl disable firewalld
[root@centos-01 ~]# systemctl disable iptables
Failed to execute operation: No such file or directory
[root@centos-01 ~]# yum -y install iptables-services
[root@centos-01 ~]# iptables -F
[root@centos-01 ~]# iptables -L               
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@centos-01 ~]# 

12.查看selinux是否是关闭的

[root@centos-01 ~]# getenforce 
Disabled
[root@centos-01 ~]# 

13.安装kvm

[root@centos-01 ~]# yum install -y virt-* libvirt bridge-utils qemu-img

配置网卡

1.桥接:直接使用和物理机一样的网段的ip,物理机和虚拟机是并列关系

   nat:相当于物理机提供了一个路由器

2.创建桥接网卡(提供虚拟机和物理机相互连接的桥梁)

[root@centos-01 ~]# cd /etc/sysconfig/network-scripts/
[root@centos-01 network-scripts]# ls
ifcfg-eno16777736  ifdown-ippp    ifdown-sit       ifup-bnep  ifup-plip    ifup-Team          network-functions-ipv6
ifcfg-lo           ifdown-ipv6    ifdown-Team      ifup-eth   ifup-plusb   ifup-TeamPort
ifdown             ifdown-isdn    ifdown-TeamPort  ifup-ib    ifup-post    ifup-tunnel
ifdown-bnep        ifdown-post    ifdown-tunnel    ifup-ippp  ifup-ppp     ifup-wireless
ifdown-eth         ifdown-ppp     ifup             ifup-ipv6  ifup-routes  init.ipv6-global
ifdown-ib          ifdown-routes  ifup-aliases     ifup-isdn  ifup-sit     network-functions
[root@centos-01 network-scripts]# cp ifcfg-eno16777736 ifcfg-br0
[root@centos-01 network-scripts]# 

3.编辑br0

[root@centos-01 network-scripts]# vim ifcfg-br0 
[root@centos-01 network-scripts]# cat ifcfg-br0 
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
DNS1=119.29.29.29
IPADDR=192.168.242.131
PREFIX=24
GATEWAY=192.168.242.2
[root@centos-01 network-scripts]# 

4.编辑eno16777736

[root@centos-01 network-scripts]# vim ifcfg-eno16777736
[root@centos-01 network-scripts]# cat ifcfg-eno16777736 
TYPE=Ethernet
BOOTPROTO=none
NAME=eno16777736
DEVICE=eno16777736
ONBOOT=yes
BRIDGE=br0

[root@centos-01 network-scripts]# 

5.启动网卡,查看ip

[root@centos-01 network-scripts]# systemctl restart network
[root@centos-01 network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.242.131  netmask 255.255.255.0  broadcast 192.168.242.255
        inet6 fe80::20c:29ff:fec1:d18d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c1:d1:8d  txqueuelen 0  (Ethernet)
        RX packets 30  bytes 4143 (4.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 5343 (5.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:c1:d1:8d  txqueuelen 1000  (Ethernet)
        RX packets 270556  bytes 403571382 (384.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 101776  bytes 6829373 (6.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:7c:76:ca  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@centos-01 network-scripts]#

6.检查kvm

[root@centos-01 network-scripts]# lsmod|grep kvm
kvm_intel             162153  0 
kvm                   525259  1 kvm_intel
[root@centos-01 network-scripts]# 

7.启动kvm相关的服务

[root@centos-01 network-scripts]# systemctl start libvirtd
[root@centos-01 network-scripts]# ps aux|grep libvirtd
root       5296  0.0  0.3 634288 15268 ?        Ssl  14:20   0:00 /usr/sbin/libvirtd
root       6425  0.0  0.0 112668   976 pts/0    S+   14:45   0:00 grep --color=auto libvirtd
[root@centos-01 network-scripts]# 

8.看下桥接网卡

[root@centos-01 network-scripts]# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c29c1d18d       no              eno16777736 (br0相当于桥接模式)
virbr0          8000.5254007c76ca       yes             virbr0-nic  (virbr0相对于nat模式)
[root@centos-01 network-scripts]# 

创建虚拟机安装CentOS7

1.下载centos7镜像,如果本地有镜像文件可直接传到服务器上

[root@centos-01 ~]# wget http://mirrors.sohu.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso

2.将镜像文件移到tmp下并安装

命令说明:

--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 定义终端额外的参数
[root@centos-01 ~]# mv CentOS-7-x86_64-DVD-1804.iso /tmp/
[root@centos-01 ~]# virt-install --name=centos-01 --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1804.iso --disk 
path=/kvm_data/centos-01.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"

3.出现了9个选项

Installation

 1) [x] Language settings                 2) [!] Time settings
        (English (United States))                (Timezone is not set.)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

4.第一项语言选择,按1,再按回车继续,当配置完成当前项前面会显示X,没有配置的会显示!

17)  Spanish               41)  Malayalam             64)  Turkish
18)  Estonian              42)  Marathi               65)  Ukrainian
19)  Basque                43)  Malay                 66)  Urdu
20)  Persian               44)  Norwegian Bokmål      67)  Vietnamese
21)  Finnish               45)  Nepali                68)  Chinese
22)  French                46)  Dutch                 69)  Zulu
Press ENTER to continue
23)  Galician              47)  Northern Sotho
24)  Gujarati
Please select language support to install.
[b to return to language list, c to continue, q to quit]: 68
================================================================================
================================================================================
Language settings

Available locales
 1)  Simplified Chinese     3)  Traditional Chinese    4)  Simplified Chinese
     (China)                    (Hong Kong)                (Singapore)
 2)  Traditional Chinese
     (Republic of China)
Please select language support to install.
[b to return to language list, c to continue, q to quit]: 1
================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [!] Time settings
        (Simplified Chinese (China))             (Timezone is not set.)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

5.设置时区

Installation

 1) [x] Language settings                 2) [!] Time settings
        (Simplified Chinese (China))             (Timezone is not set.)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 2
================================================================================
================================================================================
Time settings

Timezone: not set

NTP servers:not configured

 1)  Set timezone
 2)  Configure NTP servers
  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 1
================================================================================
================================================================================
Timezone settings

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

 1) [x] Language settings                 2) [x] Time settings
        (Simplified Chinese (China))             (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

6.配置磁盘

Installation

 1) [x] Language settings                 2) [x] Time settings
        (Simplified Chinese (China))             (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 5 
================================================================================
================================================================================
Probing storage...
Installation Destination

[x] 1) : 10 GiB (vda)

1 disk selected; 10 GiB capacity; 10 GiB free ...

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c
================================================================================
================================================================================
Autopartitioning Options

[ ] 1) Replace Existing Linux system(s)

[x] 2) Use All Space

[ ] 3) Use Free Space

Installation requires partitioning of your hard drive. Select what space to use
for the install target.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 2
================================================================================
================================================================================
Autopartitioning Options

[ ] 1) Replace Existing Linux system(s)

[x] 2) Use All Space

[ ] 3) Use Free Space

Installation requires partitioning of your hard drive. Select what space to use
for the install target.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c
================================================================================
================================================================================
Partition Scheme Options

[ ] 1) Standard Partition

[ ] 2) Btrfs

[x] 3) LVM

[ ] 4) LVM Thin Provisioning

Select a partition scheme configuration.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 1
================================================================================
================================================================================
Partition Scheme Options

[x] 1) Standard Partition

[ ] 2) Btrfs

[ ] 3) LVM

[ ] 4) LVM Thin Provisioning

Select a partition scheme configuration.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c
Generating updated storage configuration
Checking storage configuration...

================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [x] Time settings
        (Simplified Chinese (China))             (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [x] Installation Destination          6) [x] Kdump
        (Automatic partitioning                  (Kdump is enabled)
        selected)                         8) [!] Root password
 7) [ ] Network configuration                    (Password is not set.)
        (Not connected)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

7.设置root密码  

  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 8
================================================================================
================================================================================
Please select new root password. You will have to type it twice.

Password: 
Password (confirm): 
================================================================================
================================================================================
Question

       The password you have provided is weak: The password is too short.
       Would you like to use it anyway?

Please respond 'yes' or 'no': yes
================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [x] Time settings
        (Simplified Chinese (China))             (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [x] Installation Destination          6) [x] Kdump
        (Automatic partitioning                  (Kdump is enabled)
        selected)                         8) [x] Root password
 7) [ ] Network configuration                    (Password is set.)
        (Not connected)
 9) [ ] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

8.输入b,系统自动分区安装系统

  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: b
================================================================================
================================================================================
Progress
Setting up the installation environment
.
Creating disklabel on /dev/vda
.
Creating swap on /dev/vda2
.
Creating xfs on /dev/vda3
.
Creating xfs on /dev/vda1
.
Running pre-installation scripts
.
Starting package installation process

虚拟机管理  

http://blog.51cto.com/zero01/2083896

1,按ctr+】切出来,查看kvm_data是否挂载

域安装仍在进行。您可以重新连接
到控制台以便完成安装进程。
[root@centos-01 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   18G   12G  5.8G   68% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G   84K  1.9G    1% /dev/shm
tmpfs                    1.9G  9.0M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                497M  158M  339M   32% /boot
tmpfs                    378M   16K  378M    1% /run/user/42
tmpfs                    378M     0  378M    0% /run/user/0
/dev/sdb                  50G  1.4G   46G    3% /kvm_data
[root@centos-01 ~]# 
[root@centos-01 ~]# ls /kvm_data/
centos-01.img  lost+found
[root@centos-01 ~]# 

2.查看系统都有哪些虚拟机

[root@centos-01 ~]# virsh list
 Id    名称                         状态
----------------------------------------------------
 1     centos-01                      running

[root@centos-01 ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 1     centos-01                      running

[root@centos-01 ~]# 

3.启动虚拟机

[root@centos-01 ~]# virsh start centos-01 

4.重启虚拟机

[root@centos-01 ~]# virsh reboot centos-01 

5.链接到虚拟机,输入命令之后需要按回车进入登录

[root@centos-01 ~]# virsh console centos-01
连接到域 centos-01
换码符为 ^]


CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

localhost login: 

6.这样我们就进入虚拟机里面了,由于我们设置的是桥接模式,所以我们用dhclient获取到的ip和我们物理机的ip是一个网段并且可以联网了

localhost login: root
密码:
[root@localhost ~]# 
[root@localhost ~]# dhclient eth0
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:95:e5:93 brd ff:ff:ff:ff:ff:ff
    inet 192.168.242.128/24 brd 192.168.242.255 scope global dynamic eth0
       valid_lft 1769sec preferred_lft 1769sec
[root@localhost ~]# 
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=3.34 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=34.4 ms

--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 3.341/18.882/34.424/15.542 ms
[root@localhost ~]# 

7.下面我们安装net-tools包,这样我们就可以用ifconfig命令了

[root@localhost ~]# yum install -y net-tools
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.242.128  netmask 255.255.255.0  broadcast 192.168.242.255
        ether 52:54:00:95:e5:93  txqueuelen 1000  (Ethernet)
        RX packets 2805  bytes 10729240 (10.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2068  bytes 117243 (114.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 256  bytes 22272 (21.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 256  bytes 22272 (21.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# 
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           479M         89M        157M        6.6M        233M        227M
Swap:          1.0G          0B        1.0G
[root@localhost ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/vda3       8.0G  1.1G  7.0G   13% /
devtmpfs        486M     0  486M    0% /dev
tmpfs           496M     0  496M    0% /dev/shm
tmpfs           496M  6.7M  490M    2% /run
tmpfs           496M     0  496M    0% /sys/fs/cgroup
/dev/vda1      1014M  124M  891M   13% /boot
tmpfs            48M     0   48M    0% /run/user/0
[root@localhost ~]# 

1.虚拟机的配置文件为centos-01.xml

[root@centos-01 ~]# ls /etc/libvirt/qemu
centos-01.xml  networks
[root@centos-01 ~]# 

克隆虚拟机  

 1.首先将机器关机

[root@centos-01 ~]# virsh shutdown centos-01
域 centos-01 被关闭
[root@centos-01 ~]# virsh console centos-01
错误:域未运行

[root@centos-01 ~]# 

2.克隆,相当于将centos-01.img文件做一个拷贝,拷贝完成之后还会创建一个配置文件

[root@centos-01 ~]# virt-clone --original centos-01 --name centos-01-bak --file /kvm_data/centos-01-bak.img
 正在分配 'centos-01-bak.img'                            |  10 GB  04:00     

成功克隆 'centos-01-bak'。
[root@centos-01 ~]# ls /etc/libvirt/qemu
centos-01-bak.xml  centos-01.xml  networks
[root@centos-01 ~]# 

3.现在有两台虚拟机了

[root@centos-01 ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     centos-01                      关闭
 -     centos-01-bak                  关闭

[root@centos-01 ~]# 

快照管理

1.做快照

[root@centos-01 ~]# virsh snapshot-create centos-01
已生成域快照 1531209836
[root@centos-01 ~]# 

2.查看虚拟磁盘的信息(快照信息)

[root@centos-01 ~]# qemu-img info /kvm_data/centos-01.img 
image: /kvm_data/centos-01.img
file format: qcow2 (格式)
virtual size: 10G (10737418240 bytes)
disk size: 1.3G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         1531209836                0 2018-07-10 16:03:56   00:00:00.000
2         1531209940                0 2018-07-10 16:05:40   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: true
[root@centos-01 ~]# 

3.kvm分了50G,实际占用2.7G

[root@centos-01 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   18G   12G  5.8G   68% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G   84K  1.9G    1% /dev/shm
tmpfs                    1.9G  9.0M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sdb                  50G  2.7G   44G    6% /kvm_data
/dev/sda1                497M  159M  339M   32% /boot
tmpfs                    378M   16K  378M    1% /run/user/42
tmpfs                    378M     0  378M    0% /run/user/0
[root@centos-01 ~]# 
[root@centos-01 ~]# du -sh /kvm_data/*
1.3G    /kvm_data/centos-01-bak.img
1.4G    /kvm_data/centos-01.img
16K     /kvm_data/lost+found
[root@centos-01 ~]# 

4.列出所有快照

[root@centos-01 ~]# virsh snapshot-list centos-01
 名称               生成时间              状态
------------------------------------------------------------
 1531209836           2018-07-10 16:03:56 +0800 shutoff
 1531209940           2018-07-10 16:05:40 +0800 shutoff

[root@centos-01 ~]#

5.查看当前快照版本

[root@centos-01 ~]# virsh snapshot-current centos-01

6.查看所有快照配置文件

[root@centos-01 ~]# ls /var/lib/libvirt/qemu/snapshot/centos-01
1531209836.xml  1531209940.xml
[root@centos-01 ~]# 

7.删除指定的快照

[root@centos-01 ~]# virsh snapshot-delete centos-01 1531209940
已删除域快照 1531209940

[root@centos-01 ~]# 

磁盘格式

1.创建2G的raw格式磁盘

[root@centos-01 ~]# qemu-img create -f raw /kvm_data/centos-01_2.img 2G
Formatting '/kvm_data/centos-01_2.img', fmt=raw size=2147483648 
[root@centos-01 ~]# qemu-img info /kvm_data/centos-01_2.img 
image: /kvm_data/centos-01_2.img
file format: raw
virtual size: 2.0G (2147483648 bytes)
disk size: 0
[root@centos-01 ~]# 

2.把raw格式的磁盘转换为qcow2格式  

[root@centos-01 ~]# qemu-img convert -O qcow2 /kvm_data/centos-01_2.img /kvm_data/centos-01_2.qcow2
[root@centos-01 ~]# qemu-img info /kvm_data/centos-01_2.img 
image: /kvm_data/centos-01_2.img
file format: raw
virtual size: 2.0G (2147483648 bytes)
disk size: 0
[root@centos-01 ~]# qemu-img info /kvm_data/centos-01_2.qcow2 
image: /kvm_data/centos-01_2.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@centos-01 ~]# 

3.我们把centos-01-bak的qcow2格式转换成raw格式

[root@centos-01 ~]# qemu-img convert -O raw /kvm_data/centos-01-bak.img /kvm_data/centos-01-bak.raw

4.关闭centos-01-bak

[root@centos-01 kvm_data]# virsh shutdown centos-01-bak
域 centos-01-bak 被关闭

[root@centos-01 kvm_data]#   

5.编辑配置文件,修改红色部分,启动之后属组属主变了,这样我们就给centos-01-bak换完了raw的磁盘

[root@centos-01 kvm_data]# virsh edit centos-01-bak
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/kvm_data/centos-01-bak.raw'/>
[root@centos-01 kvm_data]# ls -l
总用量 3922352
-rw-r--r-- 1 root root  2147483648 7月  10 16:29 centos-01_2.img
-rw-r--r-- 1 root root      197120 7月  10 16:39 centos-01_2.qcow2
-rw------- 1 root root  1387659264 7月  10 15:54 centos-01-bak.img
-rw-r--r-- 1 root root 10737418240 7月  10 17:00 centos-01-bak.raw
-rw------- 1 root root 10739515904 7月  10 16:23 centos-01.img
drwx------ 2 root root       16384 7月   9 13:55 lost+found
[root@centos-01 kvm_data]# virsh start centos-01-bak
域 centos-01 已开始

[root@centos-01 kvm_data]# ls -l
总用量 3922352
-rw-r--r-- 1 root root  2147483648 7月  10 16:29 centos-01_2.img
-rw-r--r-- 1 root root      197120 7月  10 16:39 centos-01_2.qcow2
-rw------- 1 root root  1387659264 7月  10 15:54 centos-01-bak.img
-rw-r--r-- 1 qemu qemu 10737418240 7月  10 17:00 centos-01-bak.raw
-rw------- 1 root root 10739515904 7月  10 16:23 centos-01.img
drwx------ 2 root root       16384 7月   9 13:55 lost+found
[root@centos-01 kvm_data]# 
[root@centos-01 kvm_data]# virsh snapshot-create centos-01-bak
错误:不支持的配置:存储类型 vda 不支持磁盘 raw 的内部快照

[root@centos-01 kvm_data]# 

磁盘扩容(raw)

1.增加磁盘容量

[root@centos-01 kvm_data]# qemu-img info /kvm_data/centos-01-bak.raw 
image: /kvm_data/centos-01-bak.raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.1G
[root@centos-01 kvm_data]# qemu-img resize /kvm_data/centos-01-bak.raw +2G
Image resized.
[root@centos-01 kvm_data]# qemu-img info /kvm_data/centos-01-bak.raw      
image: /kvm_data/centos-01-bak.raw
file format: raw
virtual size: 12G (12884901888 bytes)
disk size: 1.1G
[root@centos-01 kvm_data]# 

2.现在我们到虚拟机查看并不是12G显示的还是10G,需要在物理机里面用virsh  (shutdown或者destroy) centos-01-bak关机,然后再用start重启

[root@centos-01 kvm_data]# virsh console centos-01-bak
连接到域 centos-01-bak
换码符为 ^]


CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

localhost login: root
密码:
Last login: Tue Jul 10 14:48:33 on ttyS0
[root@localhost ~]# fdisk -l

磁盘 /dev/vda:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000d2bc9

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    20971519     8387584   83  Linux
[root@localhost ~]# 
[root@centos-01 kvm_data]# virsh destroy centos-01-bak
域 centos-01-bak 被删除

[root@centos-01 kvm_data]# virsh start centos-01-bak  
域 centos-01-bak 已开始

[root@centos-01 kvm_data]# virsh console centos-01-bak
连接到域 centos-01-bak
换码符为 ^]


CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

localhost login: root
密码:
Last login: Wed Jul 11 11:22:12 on ttyS0
[root@localhost ~]# fdisk -l

磁盘 /dev/vda:12.9 GB, 12884901888 字节,25165824 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000d2bc9

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    20971519     8387584   83  Linux
[root@localhost ~]# 

3.我们怎么用这2G的磁盘呢,重新分区(不是破坏原来的分区表而是建立一个新的分区),这样vda4就是我们新的分区的2G空间(LVM可以用这种)

[root@localhost ~]# fdisk /dev/vda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):p

磁盘 /dev/vda:12.9 GB, 12884901888 字节,25165824 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000d2bc9

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    20971519     8387584   83  Linux

命令(输入 m 获取帮助):n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
已选择分区 4
起始 扇区 (20971520-25165823,默认为 20971520):
将使用默认值 20971520
Last 扇区, +扇区 or +size{K,M,G} (20971520-25165823,默认为 25165823):
将使用默认值 25165823
分区 4 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/vda:12.9 GB, 12884901888 字节,25165824 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000d2bc9

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    20971519     8387584   83  Linux
/dev/vda4        20971520    25165823     2097152   83  Linux

命令(输入 m 获取帮助):

4.标准分区情况,我们直接创建新的硬盘,假设5G

[root@centos-01 kvm_data]# qemu-img create -f raw /kvm_data/centos-01-bak_2.raw 5G
Formatting '/kvm_data/centos-01-bak_2.raw', fmt=raw size=5368709120 
[root@centos-01 kvm_data]# qemu-img info centos-01-bak_2.raw 
image: centos-01-bak_2.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
[root@centos-01 kvm_data]# 

5.怎么把我们的虚拟磁盘放到虚拟机里面去,让它加载,编辑配置文件找到disk部分复制一份,相当于两个disk  

[root@centos-01 kvm_data]# virsh edit centos-01-bak
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/kvm_data/centos-01-bak_2.raw'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </disk>

6.重启centos-01-bak,链接查看新增加了一个vdb,这样我们就可以对新磁盘进行分区、格式化、挂载操作了

[root@centos-01 kvm_data]# virsh destroy centos-01-bak
域 centos-01-bak 被删除

[root@centos-01 kvm_data]# virsh start centos-01-bak
域 centos-01-bak 已开始

[root@centos-01 kvm_data]# virsh console centos-01-bak                            
连接到域 centos-01-bak
换码符为 ^]


CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

localhost login: root
密码:
Last login: Wed Jul 11 11:26:55 on ttyS0
[root@localhost ~]# fdisk -l

磁盘 /dev/vda:12.9 GB, 12884901888 字节,25165824 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000d2bc9

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    20971519     8387584   83  Linux

磁盘 /dev/vdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[root@localhost ~]# 

磁盘扩容(qcow2)  

1.如果虚拟磁盘优快照需要先删除快照再扩容

[root@centos-01 kvm_data]# qemu-img resize /kvm_data/centos-01.img +2G
qemu-img: Can't resize an image which has snapshots
qemu-img: This image does not support resize
[root@centos-01 kvm_data]# 
[root@centos-01 kvm_data]# qemu-img info centos-01.img                  
image: centos-01.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.3G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         1531209836                0 2018-07-10 16:03:56   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: true
[root@centos-01 kvm_data]# virsh snapshot-delete centos-01 1531209836        
已删除域快照 1531209836

[root@centos-01 kvm_data]# 

2.再次resize,扩容成功,第二种扩容的方式和rwa是一样的增加新磁盘

[root@centos-01 kvm_data]# qemu-img resize /kvm_data/centos-01.img +2G
Image resized.
[root@centos-01 kvm_data]# 

调整cpu内存和网卡  

1.查看虚拟机的配置

[root@centos-01 kvm_data]# virsh dominfo centos-01
Id:             4
名称:       centos-01
UUID:           7d7a43ae-c75d-4d19-9284-4ba37876e514
OS 类型:    hvm
状态:       running
CPU:          1
CPU 时间:   49.7s
最大内存: 1048576 KiB
使用的内存: 524288 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: none
安全性 DOI: 0

[root@centos-01 kvm_data]# 

2.编辑配置文件修改下面两项,再次查看

[root@centos-01 kvm_data]# virsh shutdown centos-01
[root@centos-01 kvm_data]# virsh edit centos-01  
<currentMemory unit='KiB'>624288</currentMemory> <vcpu placement='static' current='2'>2</vcpu>
[root@centos-01 kvm_data]# virsh dominfo centos-01
Id:             -
名称:       centos-01
UUID:           7d7a43ae-c75d-4d19-9284-4ba37876e514
OS 类型:    hvm
状态:       关闭
CPU:          2
最大内存: 1048576 KiB
使用的内存: 624288 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: none
安全性 DOI: 0

[root@centos-01 kvm_data]# 

3.动态调整内存 

[root@centos-01 kvm_data]# virsh setmem centos-01 800m
错误:所需操作无效:域没有在运行

[root@centos-01 kvm_data]# virsh start centos-01 
域 centos-01 已开始

[root@centos-01 kvm_data]# virsh setmem centos-01 800m
[root@centos-01 kvm_data]# virsh dominfo centos-01
Id:             10
名称:       centos-01
UUID:           7d7a43ae-c75d-4d19-9284-4ba37876e514
OS 类型:    hvm
状态:       running
CPU:          2
CPU 时间:   22.9s
最大内存: 1048576 KiB
使用的内存: 819200 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: none
安全性 DOI: 0

[root@centos-01 kvm_data]# 

4.动态调整cpu数(只可以增加,内存调整可以增加和减小)

[root@centos-01 kvm_data]# virsh setvcpus centos-01 2

[root@centos-01 kvm_data]# 

5.这样只是暂时更改,想永久更改需要执行,重启

[root@centos-01 kvm_data]# virsh dumpxml centos-01 > /etc/libvirt/qemu/centos-01.xml 
[root@centos-01 kvm_data]# 

6.查看网卡

[root@centos-01 kvm_data]# virsh domiflist centos-01
接口     类型     源        型号      MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:95:e5:93

[root@centos-01 kvm_data]# 

7.增加网卡

[root@centos-01 kvm_data]# virsh attach-interface centos-01 --type bridge --source virbr0
成功附加接口

[root@centos-01 kvm_data]# virsh domiflist centos-01                                     
接口     类型     源        型号      MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:95:e5:93
vnet2      bridge     virbr0     rtl8139     52:54:00:75:77:97

[root@centos-01 kvm_data]# virsh attach-interface centos-01 --type bridge --source br0   
成功附加接口

[root@centos-01 kvm_data]# virsh domiflist centos-01                                  
接口     类型     源        型号      MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:95:e5:93
vnet2      bridge     virbr0     rtl8139     52:54:00:75:77:97
vnet3      bridge     br0        rtl8139     52:54:00:cd:a9:f4

[root@centos-01 kvm_data]# 

8.我们进到虚拟机看看网卡

[root@centos-01 kvm_data]# virsh console centos-01
连接到域 centos-01
换码符为 ^]


CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

localhost login: root
密码:
Last login: Wed Jul 11 10:15:29 on ttyS0
[root@localhost ~]# ifconfig
ens8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.47  netmask 255.255.255.0  broadcast 192.168.122.255
        inet6 fe80::4a44:28b1:71c8:585c  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:75:77:97  txqueuelen 1000  (Ethernet)
        RX packets 40  bytes 3811 (3.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 55  bytes 5346 (5.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.242.129  netmask 255.255.255.0  broadcast 192.168.242.255
        inet6 fe80::3d56:8800:8f44:1f24  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:cd:a9:f4  txqueuelen 1000  (Ethernet)
        RX packets 13  bytes 1255 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1844 (1.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:95:e5:93  txqueuelen 1000  (Ethernet)
        RX packets 91  bytes 14077 (13.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# 

9.想要保存刚刚的配置需要执行下面命令,重启服务器

[root@centos-01 kvm_data]# virsh dumpxml centos-01 > /etc/libvirt/qemu/centos-01.xml     
[root@centos-01 kvm_data]# 

10.总结:一句话其实就是编辑配置文件就行  

迁移虚拟机  

1.我们将centos-01迁移到另外一台机器上,拷贝xml配置文件和img镜像文件到远程机器

[root@centos-01 kvm_data]# virsh list
 Id    名称                         状态
----------------------------------------------------
 9     centos-01-bak                  running
 10    centos-01                      running

[root@centos-01 kvm_data]# virsh shutdown centos-01
域 centos-01 被关闭

2.查看虚拟磁盘所在目录

[root@centos-01 kvm_data]# virsh domblklist centos-01
目标     源
------------------------------------------------
vda        /kvm_data/centos-01.img
hda        -

[root@centos-01 kvm_data]# 

  

  

  

  

  

  

  

  

  

猜你喜欢

转载自www.cnblogs.com/sunyujun/p/9275728.html