KVM虚拟化技术的-热添加硬盘和存储池

一、热添加硬盘

方法一:

[root@node3 ~]# virsh list   #列出虚拟机
 Id    Name                           State
----------------------------------------------------
 15    centos7.2                      running

[root@node3 ~]# virsh --help |grep list 
    domblklist                     list all domain blocks
    domiflist                      list all domain virtual interfaces
    list                           list domains
    iface-list                     list physical host interfaces
    nwfilter-list                  list network filters
    net-list                       list networks
    nodedev-list                   enumerate devices on this host
    secret-list                    list secrets
    snapshot-list                  List snapshots for a domain
    pool-list                      list pools
    vol-list                       list vols
[root@node3 ~]# virsh domblklist centos7.2  #显示虚拟机硬盘列表
Target     Source
------------------------------------------------
vda        /var/lib/libvirt/images/centos7.2.qcow2
hda        -
#使用qemu-img 创建格式为qcow2的磁盘 
[root@node3 ~]# qemu-img create -f qcow2 /disk10.img 2G 
Formatting '/disk10.img', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 lazy_refcounts=off 
#添加硬盘给centos7.2虚拟机,未写到配置文件中,只写到内存中;
#--subdriver qcow2  未配置此选项默认使用:raw格式
[root@node3 ~]# virsh attach-disk centos7.2 /disk10.img  vdb --cache writeback --subdriver qcow2 
Disk attached successfully
#查询虚拟配置文件
[root@node3 ~]# virsh edit centos7.2
#在虚拟机中查询
[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom  
vda             252:0    0   20G  0 disk 
├─vda1          252:1    0  500M  0 part /boot
└─vda2          252:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.7G  0 lvm  /
  └─centos-swap 253:1    0  1.8G  0 lvm  [SWAP]
vdb             252:16   0    2G  0 disk  #已经添加硬盘 

#添加并保存配置文件和立即使用;
[root@node3 ~]# virsh attach-disk centos7.2 /disk10.img  vdb --cache writeback --subdriver qcow2  --config --live 
[root@node3 ~]# virsh attach-disk centos7.2 /disk10.img  vdb --cache writeback --subdriver qcow2  --persistent


#使用qemu-img 调整硬盘信息,只能增加不能缩小(qcow2格式)
[root@node3 ~]# qemu-img resize /var/lib/libvirt/images/centos7.2.qcow2 +1G 
Image resized.

#查看信息
[root@node3 ~]# qemu-img info /var/lib/libvirt/images/centos7.2.qcow2
image: /var/lib/libvirt/images/centos7.2.qcow2
file format: qcow2
virtual size: 21G (22548578304 bytes)
disk size: 1.1G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

方法二:

[root@node3 ~~]# qemu-img create -f qcow2 /disk20.img 1G
Formatting '/disk20.img', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 
#新建xml文件 
[root@node3 ~~]# vim temp.xml
<disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='writeback'/>
      <source file='/disk20.img'/>
      <target dev='vdc' bus='virtio'/>
 </disk>
#加载xml文件
[root@tianyun ~]# virsh attach-device centos7.2  temp.xml --persistent
Device attached successfully 
#查看硬盘信息
[root@node3 ~]# virsh domblklist centos7.2 

二、删除硬盘

[root@node3 ~]# virsh detach-disk  centos7.2  vdc  --persistent
Disk detached successfully

[root@node3 ~]# virsh  domblklist centos7.2
Target     Source
------------------------------------------------
vda        /var/lib/libvirt/images/centos7.2.qcow2
hda        -

三、KVM存储池管理:
KVM平台以存储池的形式对存储进行统一管理,所谓存储池可以理解为本地目录、通过远端磁盘阵列(iSCSI、NFS)分配过来磁盘或目录,当然也支持各类分布式文件系统 ,通过virsh中pool命令能够查看、创建、激活、注册、删除存储池。

(1)查看存储池信息

#查看存储池
[root@node3 ~]# virsh pool-list --all
 Name                 State      Autostart 
-------------------------------------------
 dd                   inactive   yes       
 default              active     yes       
 root                 active     yes  

#存储池路径:
[root@node3 storage]# pwd 
/etc/libvirt/storage
[root@node3 storage]# ll
total 8
drwxr-xr-x 2 root root  39 Aug 21 06:37 autostart
-rw------- 1 root root 538 Aug 16 02:29 default.xml
-rw------- 1 root root 514 Aug 16 21:12 root.xml
#查询default存储池信息
[root@node3 ~]# virsh pool-info default
Name:           default
UUID:           f94a053e-c6a5-43f5-973d-3825a04b9635
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       45.57 GiB
Allocation:     3.96 GiB
Available:      41.61 GiB

(2)创建一个新的存储池
仿照default的XML文件,编写新创建的的XML文档

#default 存储池配置文件:
[root@node3 ~]# virsh pool-dumpxml default 
<pool type='dir'>
  <name>default</name>
  <uuid>f94a053e-c6a5-43f5-973d-3825a04b9635</uuid>
  <capacity unit='bytes'>48927821824</capacity>
  <allocation unit='bytes'>4251684864</allocation>
  <available unit='bytes'>44676136960</available>
  <source>
  </source>
  <target>
    <path>/var/lib/libvirt/images</path>
    <permissions>
      <mode>0711</mode>
      <owner>0</owner>
      <group>0</group>
    </permissions>
  </target>
</pool>


[root@node3 ~]# cat /home/my-pools/pool.xml 
<pool type='dir'>
  <name>pool</name>
  <uuid>6e52c3da-d6ae-4016-a97a-32be87e9f8fc</uuid>
  <capacity unit='bytes'>48927821824</capacity>
  <allocation unit='bytes'>4251623424</allocation>
  <available unit='bytes'>44676198400</available>
  <source>
  </source>
  <target>
    <path>/home/img</path>
    <permissions>
      <mode>0755</mode>
      <owner>0</owner>
      <group>0</group>
    </permissions>
  </target>
</pool>
#创建存储池
[root@node3 my-pools]# virsh pool-define pool.xml 
Pool pool defined from pool.xml

[root@node3 my-pools]# virsh pool-list 
 Name                 State      Autostart 
-------------------------------------------
 default              active     yes       
 pool                 active     no        
 root                 active     yes       
#设置或取消存储池开机自动启动:--disable  
[root@node3 my-pools]# virsh pool-autostart  pool
Pool pool marked as autostarted
#启用已经定义的存储池:
[root@node3 my-pools]# virsh pool-start  pool
Pool pool marked as autostarted

[root@node3 my-pools]# virsh pool-list 
 Name                 State      Autostart 
-------------------------------------------
 default              active     yes       
 pool                 active     yes       
 root                 active     yes       

#virsh pool-destroy vmdisk #取消激活存储池,数据不做删除
#virsh pool-delete  vmdisk  #删除存储池定义的目录和数据
[root@node3 my-pools]# virsh pool-destroy pool
Pool pool destroyed

四、网络存储池;
常用的网络存储池:NFS-based 、GlusterFS-based、Ceph-based、iSCSI-based;GFS-based、SAN-based;
用NFS-based实现存储池

存储端:

[root@install ~]# yum -y install nfs-utils
[root@install ~]# mkdir /var/nfs-storage
[root@install ~]# vim /etc/exports
/var/nfs-storage    172.16.30.0/24(rw,sync,no_root_squash)

KVM端:

[root@node3 ~]# yum -y install nfs-utils
[root@node3 ~]# showmount -e 172.16.8.100
Export list for 172.16.8.100:
/var/nfs-storage 172.16.30.0/24

[root@tianyun ~]# virsh pool-list --all
Name                 State      Autostart 
-----------------------------------------
default                active     yes       
nfs-storage-pool       active     yes  
#仿照default配置文件在 <source>  </source>添加配置文件
[root@node3 autostart]# virsh pool-dumpxml default
<pool type='dir'>
  <name>default</name>
  <uuid>f94a053e-c6a5-43f5-973d-3825a04b9635</uuid>
  <capacity unit='bytes'>48927821824</capacity>
  <allocation unit='bytes'>4251856896</allocation>
  <available unit='bytes'>44675964928</available>
  <source>
  </source>
  <target>
    <path>/var/lib/libvirt/images</path>
    <permissions>
      <mode>0711</mode>
      <owner>0</owner>
      <group>0</group>
    </permissions>
  </target>
</pool>
例如:
[root@node3 ~]# virsh pool-edit nfs-storage-pool
<pool type='netfs'>
  <name>nfs-storage-pool</name>
  <uuid>83d872db-154a-f721-a6ae-9d883583a739</uuid>
  <capacity unit='bytes'>61793632256</capacity>
  <allocation unit='bytes'>7562330112</allocation>
  <available unit='bytes'>54231302144</available>
  <source>  #添加内容
    <host name='172.16.8.100'/>     #配置服务器
    <dir path='/var/nfs-storage'/>  #存储池路径
    <format type='nfs'/>            #存储池网络类型
  </source>
  <target>
    <path>/var/lib/libvirt/images/nfs-storage-pool</path>
    <permissions>
      <mode>0755</mode>
      <owner>0</owner>
      <group>0</group>
    </permissions>
  </target>
</pool>
#通过pool-defined 创建存储池
[root@node3 my-pools]# virsh pool-define nfs-storage-pool.xml 


#方法二:virsh pool-define-as
# pool-create-as name --print-xml type [source-host] [source-path] [source-dev] [source-name] [<target>] [--source-format format]

[root@node3 ~]# mkdir /var/kvm-pool2          #target挂载点
#my-pool2   存储池名 --target  存储池挂载的位置
[root@node3 ~]# virsh pool-define-as my-pool2 --source-format nfs --type netfs --source-host 172.16.8.100 --source-path /var/nfs-storage --target /var/kvm-pool2
[root@node3 ~]# virsh pool-autostart my-pool2
Pool my-pool2 marked as autostarted
[root@node3 ~]# virsh pool-start my-pool2
Pool my-pool2 started


安装虚拟机到存储池nfs-storage-pool:
[root@node3 ~]# /usr/sbin/virt-install \
        --vnc \
        --name=allen\
        --ram=512 \
        --arch=x86_64 \
        --vcpus=1 \
        --os-type=linux \
        --os-variant=rhel6 \
        --hvm \
        --accelerate \
        --disk path=/var/pool2/allen.img,size=8,format=qcow2 \
        --bridge=virbr0 \
        --location=ftp://172.16.8.100/rhel6.4 \
        --extra-args="ks=ftp://172.16.8.100/centos7.2.ks" #网络安装系统 
# --extra-args=EXTRA:根据--location指定的方式安装GuestOS时,
#用于传递给内核的额外选项,例如指定kickstart文件的位置,      


#查看快对齐查看
[root@node3 images]# virt-alignment-scan -a centos7.2.qcow2 
/dev/sda1      1048576         1024K   ok
/dev/sda2    525336576         1024K   ok

参考文档:
http://docs.redhat.com
https://docs.openstack.org/queens/
http://cloud.centos.org
https://www.centos.org/ #

猜你喜欢

转载自blog.csdn.net/genglei1022/article/details/81911143