linux自学随笔-基于raid的lvm逻辑卷部署(扩容、缩小、删除)

2021/5/20 7:30:52

本文主要是介绍linux自学随笔-基于raid的lvm逻辑卷部署(扩容、缩小、删除),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、创建raid磁盘阵列,此处我使用raid0。raid创建方法请参考之前的文章。

2、让/dev/md0支持Lvm技术

pvcreate /dev/md0

3、将硬盘加入storage卷组,查看卷组状态。

[root@cat ~]# vgcreate storage /dev/md0    #蓝色字体为卷组名称
  Volume group "storage" successfully created
[root@cat ~]# vgdisplay           #查看卷组状态
  --- Volume group ---
  VG Name               storage
  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               40.00 GiB
  PE Size               4.00 MiB
  Total PE              10239
  Alloc PE / Size       0 / 0   
  Free  PE / Size       10239 / 40.00 GiB
  VG UUID               nD9xXT-2h4U-LtM2-Pnij-FWsK-4PlK-i1Gefd
   
  --- Volume group ---
  VG Name               rhel
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.51 GiB
  PE Size               4.00 MiB
  Total PE              4994
  Alloc PE / Size       4994 / 19.51 GiB        #卷组状态
  Free  PE / Size       0 / 0   
  VG UUID               2RrMTz-JyCJ-kt5v-8pak-YoOu-MnRU-bYgxNu

4、切割逻辑卷设备(建议用-l切割;基本单元为4M)-L为容量

[root@cat ~]# lvcreate -n v0 -l 37 storage  #4*37M #逻辑卷名称
  Logical volume "v0" created
[root@cat ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/storage/v0
  LV Name                v0
  VG Name                storage
  LV UUID                0Cqg9l-3I8w-kwdd-Gu3r-R5wp-fAqV-tGY2yS
  LV Write Access        read/write
  LV Creation host, time cat, 2021-05-19 05:19:51 -0400
  LV Status              available
  # open                 0
  LV Size                148.00 MiB              #逻辑卷的大小
  Current LE             37
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     4096
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                ZING2N-KJL9-aACt-kjNT-dLtV-iqSG-4EfuON
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-05-07 21:33:24 -0400
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                T12Xzg-q6Zq-qKCh-O7Hz-7FBH-S29C-hKnmUY
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-05-07 21:33:24 -0400
  LV Status              available
  # open                 1
  LV Size                17.51 GiB
  Current LE             4482
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

5、将逻辑卷格式化,并挂载

[root@cat home]# mkfs.ext4 /dev/storage/v0 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=512 blocks, Stripe width=1024 blocks
38000 inodes, 151552 blocks
7577 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
19 block groups
8192 blocks per group, 8192 fragments per group
2000 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@cat home]# mkdir haha
[root@cat home]# mount /dev/storage/v0 /home/haha
[root@cat home]# mkfs.ext4 /dev/storage/v0 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=512 blocks, Stripe width=1024 blocks
38000 inodes, 151552 blocks
7577 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
19 block groups
8192 blocks per group, 8192 fragments per group
2000 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@cat home]# mkdir haha
[root@cat home]# mount /dev/storage/v0 /home/haha
[root@cat home]# echo "/dev/storage/v0 /home/haha ext4 defaults 0 0" >> /etc/fstab
[root@cat home]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat May  8 01:33:26 2021
#
# 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/rhel-root   /                       xfs     defaults        1 1
UUID=4de5a662-4bfe-41f9-8385-ef09b83c0e54 /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/storage/v0 /home/haha ext4 defaults 0 0

以上就将lvm部署介绍完成。

以下扩容逻辑卷:

1、卸载设备(卸载挂载目录)

[root@cat ~]# umount /home/haha

2、扩展逻辑卷(暂时以290M)为例

[root@cat ~]# lvextend -L 290M /dev/storage/v0          #扩展到290M
  Rounding size to boundary between physical extents: 292.00 MiB
  Extending logical volume v0 to 292.00 MiB
  Logical volume v0 successfully resized

3、检查硬盘的完整性,确认目录结构、内容和文件没有丢失。

[root@cat ~]# e2fsck -f /dev/storage/v0
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/storage/v0: 11/38000 files (0.0% non-contiguous), 10453/151552 blocks

4、重置设备在系统中的容量。(注:对lv逻辑卷进行扩容操作,但系统内核未同步,需手动同步)

[root@cat ~]# resize2fs /dev/storage/v0
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage/v0 to 299008 (1k) blocks.
The filesystem on /dev/storage/v0 is now 299008 blocks long.

5、重新挂载

[root@cat ~]# mount -a
[root@cat ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root    18G  3.1G   15G  18% /
devtmpfs                905M     0  905M   0% /dev
tmpfs                   914M   80K  914M   1% /dev/shm
tmpfs                   914M  8.9M  905M   1% /run
tmpfs                   914M     0  914M   0% /sys/fs/cgroup
/dev/sda1               497M  119M  379M  24% /boot
/dev/mapper/storage-v0  279M  2.1M  259M   1% /home/haha

以上为扩容逻辑卷操作

以下为缩小逻辑卷操作:

1、卸载设备(卸载挂载目录)

[root@cat ~]# umount /home/haha

2、检查文件完整性(注:与扩容步骤不一样)

[root@cat ~]# e2fsck -f /dev/storage/v0
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/storage/v0: 11/74000 files (0.0% non-contiguous), 15507/299008 blocks

3、通知内核将逻辑卷容量减少120M

[root@cat ~]# resize2fs /dev/storage/v0 120M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage/v0 to 122880 (1k) blocks.
The filesystem on /dev/storage/v0 is now 122880 blocks long                   #要求满足

4、将逻辑卷修改为120M。

[root@cat ~]# lvreduce -L 120M /dev/storage/v0
  WARNING: Reducing active logical volume to 120.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce v0? [y/n]: y
  Reducing logical volume v0 to 120.00 MiB
  Logical volume v0 successfully resized

5、重新挂载

[root@cat ~]# mount -a
[root@cat ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root    18G  3.1G   15G  18% /
devtmpfs                905M     0  905M   0% /dev
tmpfs                   914M   80K  914M   1% /dev/shm
tmpfs                   914M  8.9M  905M   1% /run
tmpfs                   914M     0  914M   0% /sys/fs/cgroup
/dev/sda1               497M  119M  379M  24% /boot
/dev/mapper/storage-v0  113M  1.6M  103M   2% /home/haha             #减少到120M

以上为缩小逻辑卷操作

以下为删除逻辑卷

1、取消挂载及删除永久挂载文件(/etc/fstab)的挂载内容

2、删除逻辑卷

[root@cat ~]# lvremove /dev/storage/v0
Do you really want to remove active logical volume v0? [y/n]: y
  Logical volume "v0" successfully removed

2、删除卷组

[root@cat ~]# vgremove storage
  Volume group "storage" successfully removed

3、删除物理卷

[root@cat ~]# pvremove /dev/md0
  Labels on physical volume "/dev/md0" successfully wiped

 



这篇关于linux自学随笔-基于raid的lvm逻辑卷部署(扩容、缩小、删除)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程