KVM虚拟化基本管理

简介:

上篇文章详细介绍了KVM虚拟化的安装配置http://msiyuetian.blog.51cto.com/8637744/1707539

接下来这篇文章主要介绍KVM虚拟化的基本管理:

一、virsh 命令常用操作

二、克隆虚拟机

三、磁盘镜像格式转换

四、快照管理

五、磁盘扩容

六、调整cpu和内存大小

七、增加网卡

八、虚拟机迁移


一、virsh 命令常用操作


1、开启并登入子机

[root@centos ~]# virsh start tpp1

[root@centos ~]# ssh 192.168.0.10    

也可以在开启的同时进入子机:
[root@centos ~]# virsh start tpp1 --console

2、关闭子机
[root@centos ~]# virsh shutdown tpp1                 //关闭

注意:默认我们没有办法在宿主机直接shutdown自己,我们需要借助于子机上的acpid服务,这个服务是让宿主机可以去调用子机的电源关闭的接口。所以,子机上需要安装并启动acpid服务,若子机没开启acpid服务,虽然显示关闭成功,但查看仍为running状态。

[root@centos ~]# virsh console tpp1                    

[root@localhost ~]# yum install -y acpid

[root@localhost ~]# /etc/init.d/acpid start

然后Ctrl+] 退出子机

[root@centos ~]# virsh shutdown tpp1

[root@centos ~]# virsh destroy tpp1                      //另一种方法是直接从列表中删除


3、子机随宿主机开机自启
[root@centos ~]# virsh autostart tpp1                   //自动启动
[root@centos ~]# virsh autostart --disable tpp1     //解除自动启动

4、列出子机

[root@centos ~]# virsh list                                     //只能列出启动的子机
[root@centos ~]# virsh list --all                              //把所有子机都列出来

5、删除子机
[root@centos ~]# virsh destroy tpp1
[root@centos ~]# virsh undefine tpp1
[root@centos ~]# rm -f /data/tpp1.img                 //若为qcow2格式的,删除tpp2.qcow2

6、挂起和恢复子机
[root@centos ~]# virsh suspend tpp1                     //挂起
[root@centos ~]# virsh resume tpp1                       //恢复


7、退出子机

按ctrl+] 可以返回到宿主机。



二、克隆虚拟机


[root@centos ~]# virt-clone --original tpp1 --name tpp2 --file /data/tpp2.img

说明:

1)克隆之前先关闭待克隆的机器,不然会报错(ERROR    必须暂停或者关闭有要克隆设备的域。

2)--original tpp1:指定待克隆的机器

3)--name tpp2:指定克隆后机器名

4)--file /data/tpp2.img :指定存放路径(命名可由样本的磁盘镜像格式决定是.qcow2或者.img)

[root@centos ~]# virsh list --all           //可以看到克隆的子机


三、磁盘镜像格式转换


1)查看子机磁盘镜像格式

[root@centos ~]# qemu-img info /data/tpp1.img

image: /data/tpp1.img

file format: raw

virtual size: 10G (10739318784 bytes)

disk size: 1.5G

2)raw格式转换为qcow2格式

[root@centos ~]# qemu-img convert -f raw -O qcow2 /data/tpp1.img /data/tpp1.qcow2

说明:其实就是相当于复制了一份,会生成一个qcow2格式的文件(tpp1.qcow2)

3)再次查看子机磁盘镜像格式

[root@centos ~]# qemu-img info /data/tpp1.qcow2

image: /data/tpp1.qcow2

file format: qcow2

virtual size: 10G (10739318784 bytes)

disk size: 392K

cluster_size: 65536

4)编辑子机配置文件

[root@centos ~]# virsh edit tpp1  

找到:
      <driver name='qemu' type='raw' cache='none'/>
      <source file='/data/tpp1.img'/>
改为:
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/data/tpp1.qcow2'/>

说明:编辑该子机配置文件,让它使用新格式的虚拟磁盘镜像文件。命令:virsh edit tpp1 就是进入了该子机的配置文件(/etc/libvirt/qemu/tpp1.xml),跟用vim编辑这个文件一样的用法。



四、快照管理


1、创建快照

[root@centos ~]# virsh snapshot-create tpp1                  //提示以下信息则成功

Domain snapshot 1447276326 created

注意:若磁盘镜像格式为raw格式则会报下面错误:

unsupported configuration: internal snapshot for disk vda unsupported for storage type raw


2、列出快照

[root@centos ~]# virsh snapshot-list tpp1

 名称                        Creation Time                      状态

------------------------------------------------------------

 1447276326           2015-11-12 05:12:06 +0800   shutoff


3、查看当前子机的快照版本

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

说明:tpp1子机的快照文件在  /var/lib/libvirt/qemu/snapshot/tpp1/  目录下

[root@centos ~]# ls /var/lib/libvirt/qemu/snapshot/tpp1/

1447276326.xml


4、恢复快照

1)首先需要关闭子机
[root@centos ~]# virsh destroy tpp1

2)确认子机是否关闭

[root@centos ~]# virsh domstate tpp1

关闭

3) 列出快照

[root@centos ~]# virsh snapshot-list tpp1

 名称                     Creation Time                       状态

------------------------------------------------------------

 1447276326         2015-11-12 05:12:06 +0800   shutoff

4)恢复快照

[root@centos ~]# virsh snapshot-revert tpp1 1447276326


5、删除快照

[root@centos ~]# virsh snapshot-delete tpp1 1447276326

Domain snapshot 1447276326 deleted



五、磁盘扩容


磁盘扩容有两种方式,一种是扩充容量,另一种是直接增加磁盘。raw格式和qcow2格式的虚拟磁盘扩容步骤是一样的,下面我们对raw格式的虚拟磁盘进行扩容。

方法1:扩充容量

1)查看虚拟机信息(大小为10G)

[root@centos ~]# qemu-img info /data/tpp1.qcow2

image: /data/tpp1.img

file format: raw

virtual size: 10G (10739318784 bytes)

disk size: 400K

cluster_size: 65536

2)增加5G

[root@centos ~]# qemu-img resize /data/tpp1.img +5G

Image resized.

[root@centos ~]# qemu-img info /data/tpp1.img

image: /data/tpp1.img

file format: raw

virtual size: 15G (16108027904 bytes)

disk size: 400K

cluster_size: 65536

3)使其生效

[root@centos ~]# virsh destroy tpp1               

[root@centos ~]# virsh start tpp1

[root@centos ~]# virsh console tpp1               //进入子机

[root@localhost ~]# fdisk -l                              //查看磁盘分区已经增加到 16 GB

Disk /dev/vda: 16.1 GB, 16108027904 bytes

16 heads, 63 sectors/track, 31211 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x0003aadd


   Device Boot      Start         End      Blocks   Id  System

/dev/vda1   *           3        1018      512000   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/vda2            1018       20808     9973760   8e  Linux LVM

Partition 2 does not end on cylinder boundary.

.......

[root@localhost ~]# df -h                             //但磁盘挂载的空间并没有增加

Filesystem                                     Size     Used     Avail    Use%   Mounted on

/dev/mapper/VolGroup-lv_root   8.5G    766M     7.3G    10%     /

tmpfs                                            246M     0         246M   0%       /dev/shm

/dev/vda1                                     485M   33M      427M   8%       /boot

4)分区(新增加的空间还没有划分使用,故要继续分区)

[root@localhost ~]# fdisk /dev/vda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

         switch off the mode (command 'c') and change display units to

         sectors (command 'u').


Command (m for help): p


Disk /dev/vda: 16.1 GB, 16108027904 bytes

16 heads, 63 sectors/track, 31211 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x0003aadd


   Device Boot      Start         End      Blocks   Id  System

/dev/vda1   *           3        1018      512000   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/vda2            1018       20808     9973760   8e  Linux LVM

Partition 2 does not end on cylinder boundary.


Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 3

First cylinder (1-31211, default 1): 20809

Last cylinder, +cylinders or +size{K,M,G} (20809-31211, default 31211): 31211


Command (m for help): p


Disk /dev/vda: 16.1 GB, 16108027904 bytes

16 heads, 63 sectors/track, 31211 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x0003aadd


   Device Boot      Start         End      Blocks   Id  System

/dev/vda1   *           3        1018      512000   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/vda2            1018       20808     9973760   8e  Linux LVM

Partition 2 does not end on cylinder boundary.

/dev/vda3           20809       31211     5243112   83  Linux


Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.


WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

[root@localhost ~]# reboot      //重启生效     

5)把/dev/vda3 加入到lvm里面去

[root@localhost ~]# pvcreate /dev/vda3                     //创建物理卷

  Physical volume "/dev/vda3" successfully created

[root@localhost ~]# pvs                                             //列出物理卷

  PV             VG            Fmt     Attr  PSize     PFree

  /dev/vda2  VolGroup  lvm2   a--    9.51g    0

  /dev/vda3                   lvm2   a--   5.00g     5.00g

[root@localhost ~]# vgextend VolGroup /dev/vda3    //物理卷加入卷组

  Volume group "VolGroup" successfully extended

[root@localhost ~]# vgs                                             //查看卷组,可看到5G空余

  VG             #PV  #LV  #SN    Attr         VSize      VFree

  VolGroup       2      2       0    wz--n-    14.50g     5.00g

[root@localhost ~]# lvs                                             //查看逻辑卷            

  LV         VG               Attr            LSize      Pool Origin Data%  Move Log Cpy%Sync Convert

  lv_root   VolGroup    -wi-ao----   8.54g

  lv_swap  VolGroup    -wi-ao----   992.00m

[root@localhost ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root   //卷组空余加入到lv_root

  Extending logical volume lv_root to 13.54 GiB

  Logical volume lv_root successfully resized

[root@localhost ~]# resize2fs /dev/VolGroup/lv_root                        //刷新下

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 1

Performing an on-line resize of /dev/VolGroup/lv_root to 3548160 (4k) blocks.

The filesystem on /dev/VolGroup/lv_root is now 3548160 blocks long.

[root@localhost ~]# df -h                     //可查看到增加到了15G

Filesystem                                   Size     Used     Avail    Use%   Mounted on

/dev/mapper/VolGroup-lv_root   14G     769M   12G       6%      /

tmpfs                                          246M   0         246M    0%      /dev/shm

/dev/vda1                                   485M   33M     427M    8%      /boot

以上步骤成功则表示扩充容量成功,下面我们进行第二种方式



方法2增加磁盘

1)创建磁盘

[root@centos ~]# qemu-img create -f raw /data/tpp1_1.img 5G

Formatting '/data/tpp1_1.img', fmt=raw size=5368709120

[root@centos ~]# virsh destroy tpp1         //关闭虚拟机

2)编辑配置文件

[root@centos ~]# virsh edit tpp1             //在disk模块后面再增加一个disk模块

    <disk type='file' device='disk'>

      <driver name='qemu' type='raw' cache='none'/>

      <source file='/data/tpp1_1.img'/>

      <target dev='vdb' bus='virtio'/>

    </disk>

[root@centos ~]# virsh start tpp1

[root@centos ~]# virsh console tpp1           //进入虚拟机

3)格式化

[root@localhost ~]# fdisk -l                         //可以看到sdb

.......

Disk /dev/vdb: 5368 MB, 5368709120 bytes

.......

[root@localhost ~]# mkfs.ext4 /dev/vdb     //格式化成ext4格式

4)挂载

[root@localhost ~]# vi /etc/fstab                //增加一行      

/dev/vdb      /mnt      ext4    defaults     0 0

[root@localhost ~]# mount -a

[root@localhost ~]# df -h                           //可看到vdb已挂载成功

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   14G  769M   12G   6% /

tmpfs                         246M     0  246M   0% /dev/shm

/dev/vda1                     485M   33M  427M   8% /boot

/dev/vdb                      5.0G  138M  4.6G   3% /mnt

当然也可以按照方法1思路进行分区,再把 /dev/vdb1 加入到 lvm里面去。



六、调整cpu和内存大小


需求:内存增加300M,同时增加一个cpu

[root@centos ~]# virsh edit tpp1        

找到:

  <memory unit='KiB'>524288</memory>

  <currentMemory unit='KiB'>524288</currentMemory>

  <vcpu placement='static'>1</vcpu>

改为:

  <memory unit='KiB'>824288</memory>

  <currentMemory unit='KiB'>824288</currentMemory>

  <vcpu placement='static'>2</vcpu>

[root@centos ~]# virsh destroy tpp1

[root@centos ~]# virsh start tpp1

[root@centos ~]# virsh console tpp1

[root@localhost ~]# free

             total       used       free     shared    buffers     cached

Mem:        795932     128264     667668          0       7064      36684

-/+ buffers/cache:      84516     711416

Swap:      1015800          0    1015800

[root@localhost ~]# cat /proc/cpuinfo |grep processor

processor       : 0

processor       : 1



七、增加网卡


当虚拟子机tpp1处于关闭状态,在宿主机上进行下面操作

[root@centos ~]# virsh domiflist tpp1                     //列出网卡

Interface  Type       Source     Model       MAC

-------------------------------------------------------

vnet1      bridge     br0        virtio      52:54:00:2b:7c:ab

[root@centos ~]# virsh edit tpp1                 //在interface模块下面再增加一个接口

    <interface type='bridge'>

      <mac address='52:54:00:26:80:cb'/>

      <source bridge='br0'/>

      <model type='virtio'/>

    </interface>

[root@centos ~]# virsh start tpp1 

[root@centos ~]# virsh domiflist tpp1          //发现增加了一块网卡

Interface  Type       Source     Model       MAC

-------------------------------------------------------

vnet1      bridge     br0        virtio      52:54:00:2b:7c:ab

vnet2      bridge     br0        -            52:54:00:26:80:cb

[root@centos ~]# virsh console tpp1

[root@localhost ~]# ifconfig -a                          //可看到增加了一块网卡

eth1      Link encap:Ethernet  HWaddr 52:54:00:2B:7C:AB

          BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)


eth2      Link encap:Ethernet  HWaddr 52:54:00:26:80:CB

          BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

          Interrupt:11 Base address:0x4000



八、虚拟机迁移


首先要确保虚拟子机是处于关机状态。

1)查看子机状态

[root@centos ~]# virsh list --all

 Id    名称                         状态

----------------------------------------------------

 3     tpp1                           running

2)关闭子机

[root@centos ~]# virsh destroy tpp1

3)导出文件

[root@centos ~]# virsh dumpxml tpp1 > /etc/libvirt/qemu/tpp5.xml

注意:如果是远程机器,需要把该配置文件拷贝到远程机器上

4)查看磁盘所在目录

[root@centos ~]# virsh domblklist tpp1

Target     Source

-------------------------------

vda        /data/tpp1.img

5)拷贝磁盘文件

[root@centos ~]# rsync -avP /data/tpp1.img /data/tpp5.img

6)修改配置文件

[root@centos ~]# vim /etc/libvirt/qemu/tpp5.xml

<name>tpp5</name>                                  //修改domname

<uuid>7737aeb1-cad1-4eb6-f187-56cdbft57ew0</uuid>  //修改uuid(随便修改,但位数不要变)

<source file='/data/tpp5.img'/>                    //修改磁盘路径

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

7)定义域

[root@centos ~]# virsh define /etc/libvirt/qemu/tpp5.xml

8)查看子机

[root@centos ~]# virsh list --all               //可查看到该迁移的子机

 Id    名称                   状态

--------------------------------------

 -     tpp1                   关闭

 -     tpp5                   关闭

 



      本文转自 M四月天 51CTO博客,原文链接:http://blog.51cto.com/msiyuetian/1711927 ,如需转载请自行联系原作者





相关文章
|
机器学习/深度学习 Linux KVM
Debian11安装KVM虚拟化并安装Centos
grep --color --perl-regexp 'vmx | svm' /proc/cpuinfo 首先检查其是否支持虚拟化. 分别用以下条命令查看
1111 0
Debian11安装KVM虚拟化并安装Centos
|
机器学习/深度学习 存储 Linux
CentOS 7 部署 KVM 虚拟化
CentOS 7 部署 KVM 虚拟化
620 0
|
安全 IDE KVM
[ 云计算相关 ] KVM虚拟化平台windows虚拟机迁移到openstack虚拟化平台(KVM虚拟化环境中Windows虚拟机安装Virtio驱动程序)
KVM虚拟化平台windows虚拟机迁移到openstack虚拟化平台(KVM虚拟化环境中Windows虚拟机安装Virtio驱动程序) 因为这也不是我的本职工作,只是这次恰巧碰到了,帮着解决了以下,我就没深究原理了,问我原理可能我也不知道,只是这个操作步骤吧,亲测有效。
450 0
[ 云计算相关 ] KVM虚拟化平台windows虚拟机迁移到openstack虚拟化平台(KVM虚拟化环境中Windows虚拟机安装Virtio驱动程序)
|
10月前
|
存储 监控 网络安全
【KVM虚拟化】· 虚拟机的冷迁移和热迁移
【KVM虚拟化】· 虚拟机的冷迁移和热迁移
832 0
|
10月前
|
KVM 虚拟化 Windows
【KVM虚拟化】· KVM中的网络
【KVM虚拟化】· KVM中的网络
194 0
|
10月前
|
存储 KVM 文件存储
【KVM虚拟化】· 存储池、存储卷
【KVM虚拟化】· 存储池、存储卷
366 0
|
10月前
|
存储 监控 安全
【KVM虚拟化】· virsh文件管理
【KVM虚拟化】· virsh文件管理
303 0
|
10月前
|
XML Shell API
【KVM虚拟化】· virsh管理命令
【KVM虚拟化】· virsh管理命令
233 0
|
10月前
|
XML 存储 安全
【KVM虚拟化】· 命令行KVM安装linux
【KVM虚拟化】· 命令行KVM安装linux
125 0
|
10月前
|
存储 安全 Linux
【KVM虚拟化】· 图形化KVM安装linux
【KVM虚拟化】· 图形化KVM安装linux
192 0