KVM Storage Management - Learning Diary

KVM Storage Management - Learning Diary

The KVM storage pool is a large container, which mainly contains image files of virtual machines and other disk-related files.
When the disk space is not enough, you need to add and manage the KVM virtual machine disk. There are two ways to add a storage pool: one is to use the local partition as the storage pool, and the other is to use the share provided by the NFS server as the storage pool.
1. Use partitions to create a storage pool
Create a new hard disk in the virtual machine, and select SCSI as the hard disk type during the setup process. If you create a hard disk in the virtual machine after startup, you cannot directly use the newly added hard disk. You need to restart the virtual machine for the hard disk to take effect.
insert image description here
If you don't want to restart the virtual machine, you can use this method: execute "ls /sys/class/scsi_host" to re-scan the SCSI bus to add devices.

[root@kvm ~]# ls /sys/class/scsi_host/
host0  host1  host2
[root@kvm ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@kvm ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@kvm ~]# echo "- - -" > /sys/class/scsi_host/host2/scan

Use fdisk -l to view all partitions, the number of hard disks attached to the virtual machine and the partition status. You can see the newly added hard disk information. At this time, /dev/sdb can only display the partition size, which is currently unavailable. The details are as follows:

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

The size of this hard disk is 2147MB, 2147483648 bytes, 4194304 sectors. Each sector is 512 bytes in size.
blocks=(end-start)*sector size /1024
insert image description here

Use fdisk to partition the /dev/sdb disk.

[root@kvm ~]# fdisk /dev/sdb
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Command (m for help): n   --新建分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)  --主分区,目前4个可用
   e   extended --扩展分区
Select (default p):  --默认主分区
Using default response p
Partition number (1-4, default 1):   
First sector (2048-4194303, default 2048):  --默认分区开始的扇区 
Using default value 2048
Last sector, +sectors or +size{
    
    K,M,G} (2048-4194303, default 4194303):   --默认分区结束的扇区
Using default value 4194303  默认分区大小为4194303*512 bytes
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): w  --保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Format the created partition, that is, set the file system format of the partition.

[root@kvm ~]# lsblk -f  ---查看当前的文件系统格式有哪些
NAME         FSTYPE      LABEL           UUID                                   MOUNTPOINT
sda                                                                             
|-sda1       xfs                         6ec6fb2f-7fab-4c82-8e81-25d8dad17e74   /boot
`-sda2       LVM2_member                 OT8gtx-BwYm-bbG8-5g7K-qG5Y-zsUp-JZi7Di 
  |-centos-root
             xfs                         d016cbb9-e1bd-474f-be91-2f7e658afa1e   /
  `-centos-swap
             swap                        e4ff30b7-9041-496c-9128-91d322163bd9   [SWAP]
sdb                                                                             
`-sdb1                                                                          
sr0          iso9660     CentOS 7 x86_64 2015-12-09-23-14-10-00                 /mnt/cdrom
loop0        iso9660     CentOS 7 x86_64 2015-12-09-23-14-10-00                 /var/ftp/dvd
[root@kvm ~]# mkfs.xfs /dev/sdb1   ---将分区格式化为xfs类型
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131008 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=524032, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

After the partition is formatted, add the partition to the storage pool. Open virt-manager, and enter the connection details page when vm01 is powered off.
insert image description here
In the storage tab, there is a default storage pool by default, click the + sign to add a new storage pool, enter the name value, select fs (pre-formatted fast device) as the type, libvirt will automatically mount the partition, (by default, the storage pool The type is dir, which is a directory-based storage design, and the /var/lib/libvirt/images directory is the default storage pool). To create storage pools for different types, you can refer to this https://cloud.tencent.com/developer/news/258143 .
insert image description here
insert image description here
Added successfully.
insert image description here
View file system disk usage statistics on the host, df -h.

[root@kvm ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  5.4G   13G  31% /
devtmpfs                 903M     0  903M   0% /dev
tmpfs                    913M     0  913M   0% /dev/shm
tmpfs                    913M  8.9M  904M   1% /run
tmpfs                    913M     0  913M   0% /sys/fs/cgroup
/dev/sr0                 4.1G  4.1G     0 100% /mnt/cdrom
/dev/loop0               4.1G  4.1G     0 100% /var/ftp/dvd
/dev/sda1                497M  130M  368M  27% /boot
tmpfs                    183M  8.0K  183M   1% /run/user/0
/dev/sdb1                2.0G   33M  2.0G   2% /var/lib/libvirt/images/storage_pool
最后一条记录可以看到/dev/sdb1已经挂载到/var/lib/libvirt/images/storage_pool目录。

View the configuration file of the storage pool on the host.

[root@kvm ~]# cat /etc/libvirt/storage/autostart/   
default.xml       storage_pool.xml  
[root@kvm ~]# cat /etc/libvirt/storage/autostart/storage_pool.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh pool-edit storage_pool
or other application using the libvirt API.
-->

<pool type='fs'>
  <name>storage_pool</name>
  <uuid>1ed32488-55ea-4d68-ab60-09a392802c86</uuid>
  <capacity unit='bytes'>0</capacity>
  <allocation unit='bytes'>0</allocation>
  <available unit='bytes'>0</available>
  <source>
    <device path='/dev/sdb1'/>
    <format type='auto'/>
  </source>
  <target>
    <path>/var/lib/libvirt/images/storage_pool</path>
  </target>
</pool>

In the storage pool, create a new volume.
insert image description here
View the volumes in the storage pool and display the specific information of the volumes.
insert image description here
View the volumes of the storage pool through commands on the host.

[root@kvm ~]# ll -h /var/lib/libvirt/images/storage_pool/
total 952K
-rw-------. 1 root root 103M Feb  4 03:45 centos7-01.qcow2
-rw-------. 1 root root 206M Feb  4 03:41 centos7.qcow2

View the specific information of the volume through the command on the host.

[root@kvm ~]# qemu-img info /var/lib/libvirt/images/storage_pool/centos7-01.qcow2 
image: /var/lib/libvirt/images/storage_pool/centos7-01.qcow2
file format: qcow2
virtual size: 102M (107374592 bytes)
disk size: 488K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

View storage pools through commands on the host.

[root@kvm ~]# virsh pool-list 
 Name                 State      Autostart 
-------------------------------------------
 default              active     yes       
 storage_pool         active     yes  

Check the specific information of the storage pool on the host.

[root@kvm ~]# virsh pool-info storage_pool 
Name:           storage_pool
UUID:           1ed32488-55ea-4d68-ab60-09a392802c86
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       1.99 GiB
Allocation:     33.12 MiB
Available:      1.96 GiB

2. Use NFS partitions to create a storage pool
Build an NFS server on the host machine, create a shared file system, and then share it with the KVM virtual machine as a storage pool for the KVM virtual machine.
The steps to deploy NFS sharing are as follows:
Make sure that the host's firewall and selinux are turned off.
insert image description here
Install the NFS server software.

[root@kvm ~]# yum -y install nfs  ---tab键补齐
nfs-utils.x86_64       nfsometer.noarch       
nfs4-acl-tools.x86_64  nfstest.noarch         
[root@kvm ~]# yum -y install nfs-utils.x86_64 
Loaded plugins: fastestmirror, langpacks
Centos-Local                                                         | 3.6 kB  00:00:00     
base                                                                 | 3.6 kB  00:00:00     
extras                                                               | 2.9 kB  00:00:00     
updates                                                              | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                          | 5.6 MB  00:00:02    
.........

Create a shared directory /kvm/nfs/images, and modify its directory permissions to allow other users to have read and write permissions.

[root@kvm ~]# mkdir -p /kvm/nfs/images
[root@kvm ~]# ll -d /kvm/nfs/images/
drwxr-xr-x. 2 root root 6 Feb  6 03:33 /kvm/nfs/images/
[root@kvm ~]# chmod 757 /kvm/nfs/images/
[root@kvm ~]# ll -d /kvm/nfs/images/
drwxr-xrwx. 2 root root 6 Feb  6 03:33 /kvm/nfs/images/

Configure the NFS server, and set the directory /kvm/nfs/images just created to be shared with users on the network segment of the KVM virtual machine to read and write.
View the network segment of the kvm virtual machine. Introduce two methods to view the virtual machine ip address, but the premise is that the kvm virtual machine can only be viewed under the condition that the kvm virtual machine is turned on.
1. Directly open the KVM virtual machine to view the ip address
insert image description here2. View the virtual machine ip address in the host machine. First check the virtual host configuration file to find the mac address information of the virtual machine, and then use arp -a to determine the virtual machine IP address.
insert image description here
After knowing the network segment information of the virtual machine, configure the NFS server, find the nfs configuration file /etc/exports, and share the directory /kvm/nfs/images with users in the network segment of the KVM virtual machine to read and write.

[root@kvm ~]# vi /etc/exports
[root@kvm ~]# cat /etc/exports
/kvm/nfs/images	192.168.100.0/24(rw,no_root_squash)
/kvm/nfs/images 192.168.217.0/24(rw,no_root_squash)

Start the nfs service,

[root@kvm ~]# systemctl restart nfs-server.service 
[root@kvm ~]# systemctl restart rpcbind.service 

Find another host to check the sharing of the NFS server and see if you can see the shared folder.
insert image description here
After the preparations are complete, add the share provided by the NFS server to the KVM virtual machine storage pool. When adding a storage pool, the KVM virtual machine must be powered off.
insert image description here
insert image description here
After completion, you can view the storage pool information of the KVM virtual machine on the host machine.

[root@kvm ~]# virsh pool-list 
 Name                 State      Autostart 
-------------------------------------------
 default              active     yes       
 kvm_nfs_storage      active     yes       
 storage_pool         active     yes   
[root@kvm ~]# vi /kvm/nfs/images/1.txt
[root@kvm ~]# ll -h /var/lib/libvirt/images/kvm_nfs_storage/
total 4.0K
-rw-r--r--. 1 root root 51 Feb  6 04:56 1.txt

Add storage volumes to the storage pool.
insert image description here
insert image description here

[root@kvm ~]# qemu-img info /var/lib/libvirt/images/kvm_nfs_storage/nfs_share.qcow2    ---查看存储池中卷的详细信息
image: /var/lib/libvirt/images/kvm_nfs_storage/nfs_share.qcow2
file format: qcow2
virtual size: 102M (107374592 bytes)
disk size: 488K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
[root@kvm ~]# virsh pool-info kvm_nfs_storage  ---查看存储池的详细信息
Name:           kvm_nfs_storage
UUID:           a371060a-e8fe-49ae-abbc-6d67954dc0d5
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       17.46 GiB
Allocation:     5.45 GiB
Available:      12.01 GiB
[root@kvm ~]# virsh vol-list kvm_nfs_storage    ---查查看存储池中的卷信息
 Name                 Path                                    
------------------------------------------------------------------------------
 1.txt                /var/lib/libvirt/images/kvm_nfs_storage/1.txt
 nfs_share.qcow2      /var/lib/libvirt/images/kvm_nfs_storage/nfs_share.qcow2

Guess you like

Origin blog.csdn.net/qq_26350199/article/details/113631153