Linux基础指令磁盘管理002

       LVM(Logical Volume Manager)是Linux系统中一种灵活的磁盘管理和存储解决方案,它允许用户在物理卷(Physical Volumes, PV)上创建卷组(Volume Groups, VG),然后在卷组上创建逻辑卷(Logical Volumes, LV)。LVM提供了比传统分区更高级的功能,如动态调整存储容量、方便的备份与恢复、以及跨越多个磁盘的卷管理等。今天我们讲一下lvm分区。

系统环境:

CentOS Stream

分区过程:

首先添加一块新的磁盘

启动查看磁盘是开机否添加成功 

[root@localhost ~]# fdisk -l | grep sd
Disk /dev/sda:120 GiB,128849018880 字节,251658240 个扇区
/dev/sda1  *        2048 10487807 10485760   5G 83 Linux
/dev/sda2       10487808 31475711 20987904  10G 8e Linux LVM
Disk /dev/sdb:5 GiB,5368709120 字节,10485760 个扇区
Disk /dev/sdc:20 GiB,21474836480 字节,41943040 个扇区

 

新建非交互式分区

[root@localhost ~]# cat  fdisk_10G.txt
n
p
1

+5G
n
p


+5G
w
[root@localhost ~]# 
fdisk为 fdisk_10G.txt 的文本文件中的指令来分区磁盘 /dev/sdc。
[root@localhost ~]# fdisk /dev/sdc < fdisk_10G.txt

欢迎使用 fdisk (util-linux 2.37.4)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

设备不包含可识别的分区表。
创建了一个磁盘标识符为 0x6b93d7c1 的新 DOS 磁盘标签。

命令(输入 m 获取帮助):分区类型
   p   主分区 (0 primary, 0 extended, 4 free)
   e   扩展分区 (逻辑分区容器)
选择 (默认 p):分区号 (1-4, 默认  1): 第一个扇区 (2048-41943039, 默认 2048): 最后一个扇区,+/-sectors 或 +size{K,M,G,T,P} (2048-41943039, 默认 41943039): 
创建了一个新分区 1,类型为“Linux”,大小为 5 GiB。

命令(输入 m 获取帮助):分区类型
   p   主分区 (1 primary, 0 extended, 3 free)
   e   扩展分区 (逻辑分区容器)
选择 (默认 p):分区号 (2-4, 默认  2): 第一个扇区 (10487808-41943039, 默认 10487808): 最后一个扇区,+/-sectors 或 +size{K,M,G,T,P} (10487808-41943039, 默认 41943039): 
创建了一个新分区 2,类型为“Linux”,大小为 5 GiB。

命令(输入 m 获取帮助):分区表已调整。
将调用 ioctl() 来重新读分区表。
正在同步磁盘。

点击查看

[root@localhost ~]# fdisk -l /dev/sdc
Disk /dev/sdc:20 GiB,21474836480 字节,41943040 个扇区
磁盘型号:VMware Virtual S
单元:扇区 / 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x6b93d7c1

设备       启动     起点     末尾     扇区 大小 Id 类型
/dev/sdc1           2048 10487807 10485760   5G 83 Linux
/dev/sdc2       10487808 20973567 10485760   5G 83 Linux

 

创建pv

[root@localhost ~]# pvs #先查看pv
  PV         VG Fmt  Attr PSize  PFree
  /dev/sda2  cs lvm2 a--  10.00g    0 
  /dev/sdb   cs lvm2 a--  <5.00g    0 
[root@localhost ~]# pvcreate /dev/sdc{1,2}  #创建pv
  Physical volume "/dev/sdc1" successfully created.
  Physical volume "/dev/sdc2" successfully created.
[root@localhost ~]# pvs #再次查看
  PV         VG Fmt  Attr PSize  PFree
  /dev/sda2  cs lvm2 a--  10.00g    0 
  /dev/sdb   cs lvm2 a--  <5.00g    0 
  /dev/sdc1     lvm2 ---   5.00g 5.00g
  /dev/sdc2     lvm2 ---   5.00g 5.00g
[root@localhost ~]# 

创建vg

[root@localhost ~]# vgs   #同样先查看vg
  VG #PV #LV #SN Attr   VSize  VFree
  cs   2   1   0 wz--n- 15.00g    0 
[root@localhost ~]# vgcreate lgb /dev/sdc{1,2}  #创建vg
  Volume group "lgb" successfully created
[root@localhost ~]# vgs   #再次查看
  VG  #PV #LV #SN Attr   VSize  VFree
  cs    2   1   0 wz--n- 15.00g    0 
  lgb   2   0   0 wz--n-  9.99g 9.99g
[root@localhost ~]# 

 创建lv

[root@localhost ~]#  lvcreate -L 100m -n nginx_lvm lgb #在一个名为lgb的卷组中创建一个大小为100MB、名为nginx_lvm的逻辑卷
  Logical volume "nginx_lvm" created.
[root@localhost ~]# lvs   #查看lv
  LV        VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root      cs  -wi-ao----  15.00g                                                    
  nginx_lvm lgb -wi-a----- 100.00m                                                    
[root@localhost ~]# vgs查看vg卷组
  VG  #PV #LV #SN Attr   VSize  VFree
  cs    2   1   0 wz--n- 15.00g    0 
  lgb   2   1   0 wz--n-  9.99g 9.89g
[root@localhost ~]#

 

格式化

[root@localhost ~]# lvs   #先查看信息
  LV        VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root      cs  -wi-ao----  15.00g                                                    
  nginx_lvm lgb -wi-a----- 100.00m                                                    
[root@localhost ~]# lvdisplay    #详细信息
  --- Logical volume ---
  LV Path                /dev/lgb/nginx_lvm
  LV Name                nginx_lvm
  VG Name                lgb
  LV UUID                ivzUhH-s3uZ-rbO6-XfHo-fjoT-eWZi-jd41ll
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2024-06-05 18:46:37 +0800
  LV Status              available
  # open                 0
  LV Size                100.00 MiB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/cs/root
  LV Name                root
  VG Name                cs
  LV UUID                xbdeiT-PCxR-ss1w-RTkq-35Uu-gJtX-NQa4oy
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2024-06-05 15:35:41 +0800
  LV Status              available
  # open                 1
  LV Size                15.00 GiB
  Current LE             3840
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[root@localhost ~]# ^C
[root@localhost ~]# mkfs.ext4 /dev/lgb/nginx_lvm     #格式化
mke2fs 1.46.5 (30-Dec-2021)
创建含有 102400 个块(每块 1k)和 25584 个inode的文件系统
文件系统UUID:9d518877-493f-459d-b628-7001978ccd49
超级块的备份存储于下列块: 
	8193, 24577, 40961, 57345, 73729

正在分配组表: 完成                            
正在写入inode表: 完成                            
创建日志(4096 个块)完成
写入超级块和文件系统账户统计信息: 已完成

挂载lv

[root@localhost ~]# mkdir -pv /home/nginx_100m       # 创建挂载目录
mkdir: 已创建目录 '/home/nginx_100m'
[root@localhost ~]# vim /etc/fstab          编辑信息
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# tail -l /etc/fstab
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cs-root     /                       xfs     defaults        0 0
UUID=fcfd7195-966b-42d6-96c3-ab6ef2e3bf93 /boot                   xfs     defaults        0 0
UUID=9d518877-493f-459d-b628-7001978ccd49  /home/nginx_100m    ext4    defaults          0 0
[root@localhost ~]# 

[root@localhost ~]# mount -a    #挂载
mount: (hint) your fstab has been modified, but systemd still uses
       the old version; use 'systemctl daemon-reload' to reload.
[root@localhost ~]# 

 查看挂载分区

[root@localhost ~]# df
文件系统                     1K-块    已用    可用 已用% 挂载点
devtmpfs                      4096       0    4096    0% /dev
tmpfs                      1988404       0 1988404    0% /dev/shm
tmpfs                       795364    9224  786140    2% /run
/dev/mapper/cs-root       15663104 9814784 5848320   63% /
/dev/sda1                  5177344  226756 4950588    5% /boot
tmpfs                       397680       0  397680    0% /run/user/0
/dev/mapper/lgb-nginx_lvm    90333      14   83151    1% /home/nginx_100m
[root@localhost ~]# df -Th
文件系统                  类型      容量  已用  可用 已用% 挂载点
devtmpfs                  devtmpfs  4.0M     0  4.0M    0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs                     tmpfs     777M  9.1M  768M    2% /run
/dev/mapper/cs-root       xfs        15G  9.4G  5.6G   63% /
/dev/sda1                 xfs       5.0G  222M  4.8G    5% /boot
tmpfs                     tmpfs     389M     0  389M    0% /run/user/0
/dev/mapper/lgb-nginx_lvm ext4       89M   14K   82M    1% /home/nginx_100m
[root@localhost ~]# 

创建测试数据

[root@localhost ~]# for i in `seq 3` ; do echo "${i}-${RANDOM}" > /home/nginx_100m/${i}.txt ; done
[root@localhost ~]#  ls -lhrt /home/nginx_100m/
总用量 15K
drwx------ 2 root root 12K  6月  5 18:55 lost+found
-rw-r--r-- 1 root root   8  6月  5 19:15 3.txt
-rw-r--r-- 1 root root   7  6月  5 19:15 2.txt
-rw-r--r-- 1 root root   8  6月  5 19:15 1.txt
[root@localhost ~]#

重启并验证:

[root@localhost ~]# df -Th
文件系统                  类型      容量  已用  可用 已用% 挂载点
devtmpfs                  devtmpfs  4.0M     0  4.0M    0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs                     tmpfs     777M  9.1M  768M    2% /run
/dev/mapper/cs-root       xfs        15G  9.4G  5.6G   63% /
/dev/sda1                 xfs       5.0G  222M  4.8G    5% /boot
/dev/mapper/lgb-nginx_lvm ext4       89M   17K   82M    1% /home/nginx_100m
tmpfs                     tmpfs     389M     0  389M    0% /run/user/0
[root@localhost ~]#  ls -lhrt /home/nginx_100m/
总用量 15K
drwx------ 2 root root 12K  6月  5 18:55 lost+found
-rw-r--r-- 1 root root   8  6月  5 19:15 3.txt
-rw-r--r-- 1 root root   7  6月  5 19:15 2.txt
-rw-r--r-- 1 root root   8  6月  5 19:15 1.txt
[root@localhost ~]# cat /home/nginx_100m/1.txt
1-23127
[root@localhost ~]# cat /home/nginx_100m/2.txt
2-9275
[root@localhost ~]# cat /home/nginx_100m/3.txt
3-19715
[root@localhost ~]# 

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值