其他分享
首页 > 其他分享> > LVM 逻辑卷管理与磁盘配额

LVM 逻辑卷管理与磁盘配额

作者:互联网

目录


1. 添加磁盘

虚拟机关机状态添加


启动查看磁盘

fdisk -l

未识别执行以下命令:其实这条命令会重新扫描scsi主机并更新信息,在不关机的情况下让系统识别新加的磁盘。

echo "- - -" > /sys/class/scsi_host/host0/scan

再次查看

[root@c7-1 ~]#fdisk -l

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

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   106956799    52428800   83  Linux
/dev/sda3       106956800   115345407     4194304   82  Linux swap / Solaris
/dev/sda4       115345408   209715199    47184896    5  Extended
/dev/sda5       115347456   207622143    46137344   83  Linux

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

2. 对 /dev/sdb 进行快速分区

echo -e "n\np\n1\n\n\nt\n1\n8e\nw\n" |fdisk /dev/sdb
partprobe /dev/sdb

# ------------------------------------------------------
[root@c7-1 ~]#fdisk -l
......
磁盘 /dev/sdb:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x117f3034

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   104857599    52427776    1  FAT12

3. 使用 sdb1 创建 pv

pvcreate /dev/sdb1

#----------------------------------
[root@c7-1 ~]#pvscan
  PV /dev/sdb1                      lvm2 [<50.00 GiB]
  Total: 1 [<50.00 GiB] / in use: 0 [0   ] / in no VG: 1 [<50.00 GiB]

4. 使用 PV sdb1 创建 VG

vgcreate zcm /dev/sdb1

#------------------------------------
[root@c7-1 ~]#vgs
  VG  #PV #LV #SN Attr   VSize   VFree  
  zcm   1   0   0 wz--n- <50.00g <50.00g
[root@c7-1 ~]#vgdisplay
  --- Volume group ---
  VG Name               zcm
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <50.00 GiB
  PE Size               4.00 MiB
  Total PE              12799
  Alloc PE / Size       0 / 0   
  Free  PE / Size       12799 / <50.00 GiB
  VG UUID               9KSu1l-Syr4-QB0a-zoDN-qMrA-90mo-jBymo5

5. 使用 VG zcm 创建 LV

lvcreate -L [逻辑卷大小] -n [逻辑卷名] [卷组名]
lvcreate -L 20G -n zcm9 zcm

# --------------------------------
[root@c7-1 ~]#lvcreate -L 20G -n zcm9 zcm
  Logical volume "zcm9" created.
[root@c7-1 ~]#lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  zcm9 zcm -wi-a----- 20.00g    
[root@c7-1 ~]#lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda            8:0    0  100G  0 disk 
├─sda1         8:1    0    1G  0 part /boot
├─sda2         8:2    0   50G  0 part /
├─sda3         8:3    0    4G  0 part [SWAP]
├─sda4         8:4    0    1K  0 part 
└─sda5         8:5    0   44G  0 part /data
sdb            8:16   0   50G  0 disk 
└─sdb1         8:17   0   50G  0 part 
  └─zcm-zcm9 253:0    0   20G  0 lvm  
sr0           11:0    1  4.4G  0 rom     

6. 格式化文件系统

[root@c7-1 ~]#ll /dev/mapper/zcm-zcm9 
lrwxrwxrwx 1 root root 7 7月  25 20:53 /dev/mapper/zcm-zcm9 -> ../dm-0
[root@c7-1 ~]#mkfs.xfs /dev/mapper/zcm-zcm9 
meta-data=/dev/mapper/zcm-zcm9   isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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

7. 创建目录并挂载

[root@c7-1 ~]#mkdir /nfsdata
[root@c7-1 ~]#mount /dev/mapper/zcm-zcm9 /nfsdata/
[root@c7-1 ~]#df -h
文件系统              容量  已用  可用 已用% 挂载点
devtmpfs              1.9G     0  1.9G    0% /dev
tmpfs                 1.9G     0  1.9G    0% /dev/shm
tmpfs                 1.9G   13M  1.9G    1% /run
tmpfs                 1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda2              50G  6.6G   44G   14% /
/dev/sda5              44G   40M   44G    1% /data
/dev/sda1            1014M  179M  836M   18% /boot
tmpfs                 378M   12K  378M    1% /run/user/42
tmpfs                 378M     0  378M    0% /run/user/0
/dev/mapper/zcm-zcm9   20G   33M   20G    1% /nfsdata

写进 /etc/fstab

[root@c7-1 ~]#umount /nfsdata/
[root@c7-1 ~]#echo "/dev/mapper/zcm-zcm9 /nfsdata xfs defaults,usrquota,grpquota 0 0" >> /etc/fstab
[root@c7-1 ~]#mount -a

8. 扩展挂载目录

lvextend -L +20G /dev/mapper/zcm-zcm9  # 扩展 20G
lvextend -l +100%FREE /dev/mapper/zcm-zcm9  # 扩展剩余所有

# --------------------------------------------------------
[root@c7-1 ~]#lvextend -L +20G /dev/mapper/zcm-zcm9
  Size of logical volume zcm/zcm9 changed from 20.00 GiB (5120 extents) to 40.00 GiB (10240 extents).
  Logical volume zcm/zcm9 successfully resized.
[root@c7-1 ~]#lvextend -l +100%FREE /dev/mapper/zcm-zcm9
  Size of logical volume zcm/zcm9 changed from 40.00 GiB (10240 extents) to <50.00 GiB (12799 extents).
  Logical volume zcm/zcm9 successfully resized.

查看是否生效:此时应该还没有生效

[root@c7-1 ~]#lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda            8:0    0  100G  0 disk 
├─sda1         8:1    0    1G  0 part /boot
├─sda2         8:2    0   50G  0 part /
├─sda3         8:3    0    4G  0 part [SWAP]
├─sda4         8:4    0    1K  0 part 
└─sda5         8:5    0   44G  0 part /data
sdb            8:16   0   50G  0 disk 
└─sdb1         8:17   0   50G  0 part 
  └─zcm-zcm9 253:0    0   50G  0 lvm  /nfsdata
sr0           11:0    1  4.4G  0 rom  
[root@c7-1 ~]#df -h
文件系统              容量  已用  可用 已用% 挂载点
devtmpfs              1.9G     0  1.9G    0% /dev
tmpfs                 1.9G     0  1.9G    0% /dev/shm
tmpfs                 1.9G   13M  1.9G    1% /run
tmpfs                 1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda2              50G  6.6G   44G   14% /
/dev/sda5              44G   40M   44G    1% /data
/dev/sda1            1014M  179M  836M   18% /boot
tmpfs                 378M   12K  378M    1% /run/user/42
tmpfs                 378M     0  378M    0% /run/user/0
/dev/mapper/zcm-zcm9   20G   33M   20G    1% /nfsdata

使其生效

# xfs 文件系统
[root@c7-1 ~]#xfs_growfs /dev/mapper/zcm-zcm9
meta-data=/dev/mapper/zcm-zcm9   isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 5242880 to 13106176
[root@c7-1 ~]#df -h
文件系统              容量  已用  可用 已用% 挂载点
devtmpfs              1.9G     0  1.9G    0% /dev
tmpfs                 1.9G     0  1.9G    0% /dev/shm
tmpfs                 1.9G   13M  1.9G    1% /run
tmpfs                 1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda2              50G  6.6G   44G   14% /
/dev/sda5              44G   40M   44G    1% /data
/dev/sda1            1014M  179M  836M   18% /boot
tmpfs                 378M   12K  378M    1% /run/user/42
tmpfs                 378M     0  378M    0% /run/user/0
/dev/mapper/zcm-zcm9   50G   33M   50G    1% /nfsdata

9. 设置磁盘配额

创建用户,针对用户对文件目录设置配额。

yum -y install xfsprogs quota
useradd gby
echo "123456" |passwd --stdin gby
xfs_quota -x -c 'limit -u bsoft=80M bhard=100M isoft=8 ihard=10 gby' /nfsdata/  # 报错重启机器
xfs_quota -x -c 'report -aibh'

#-----------------------------------
[root@c7-1 ~]#xfs_quota -x -c 'report -aibh'
User quota on /nfsdata (/dev/mapper/zcm-zcm9)
                        Blocks                            Inodes              
User ID      Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace  
---------- --------------------------------- --------------------------------- 
root            0      0      0  00 [------]      3      0      0  00 [------]
gby             0    80M   100M  00 [------]      0      8     10  00 [------]

Group quota on /nfsdata (/dev/mapper/zcm-zcm9)
                        Blocks                            Inodes              
Group ID     Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace  
---------- --------------------------------- --------------------------------- 
root            0      0      0  00 [------]      3      0      0  00 [------]

验证

chmod 777 /nfsdata
su - gby
cd /nfsdata
dd if=/dev/zero of=/nfsdata/test.txt bs=10M count=12      #超出磁盘配额,只复制了100M
touch {1..6}.txt        #不能创建,超出磁盘限额

10. 容器和宿主机通过 nfs 共享目录

宿主机配置 nfs

yum -y install nfs-utils rpcbind
chmod 777 /nfsdata

cat >> /etc/exports << EOF
/nfsdata 192.168.10.0/24(rw,sync,no_root_squash)
EOF

systemctl start rpcbind && systemctl enable rpcbind
systemctl start nfs && systemctl enable nfs
exportfs -rv
showmount -e

运行一个容器



标签:zcm9,zcm,1.9,dev,配额,LVM,磁盘,512,root
来源: https://www.cnblogs.com/shenyuanhaojie/p/16518692.html