linux-LVM(逻辑卷管理),动态管理存储空间

LVM是逻辑盘卷管理(LogicalVolumeManager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和 分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。

1.添加一个swap分区
在系统中添加一个新的swap分区,并且满足以下要求:
Swap的系统容量为512MB
当系统启动时,swap分区可以自动挂载
不要移除或者修改其他已经存在于您系统中的swap分区
2.创建一个逻辑卷
创建一个名为datastore的卷组,卷组的大小为4G
逻辑卷的名字为database,所属卷组为datastore,该逻辑卷的大小为3G
将新建的逻辑卷格式化为xfs文件系统,要求系统启动时,该逻辑卷能被自动挂载
3.扩大卷组
将上面database逻辑卷的大小扩大为5G
4.删除所有逻辑卷,恢复到系统最初状态

创建分区

[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

命令(输入 m 获取帮助):m
命令操作
   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)
   
命令(输入 m 获取帮助):n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e
已选择分区 4
起始 扇区 (46548992-83886079,默认为 46548992):
将使用默认值 46548992
Last 扇区, +扇区 or +size{K,M,G} (46548992-83886079,默认为 83886079):
将使用默认值 83886079
分区 4 已设置为 Extended 类型,大小设为 17.8 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (46551040-83886079,默认为 46551040):
将使用默认值 46551040
Last 扇区, +扇区 or +size{K,M,G} (46551040-83886079,默认为 83886079):+512M
分区 5 已设置为 Linux 类型,大小设为 512 MiB

修改id号

命令(输入 m 获取帮助):t
分区号 (1-5,默认 5):5
Hex 代码(输入 L 列出所有代码):82
已将分区“Linux”的类型更改为“Linux swap / Solaris”

命令(输入 m 获取帮助):w
The partition table has been altered!

同步磁盘

[root@localhost ~]# partprobe 
Error: Partition(s) 5 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。

创建3个分区,大小为2G:

[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 6
起始 扇区 (47601664-83886079,默认为 47601664):
将使用默认值 47601664
Last 扇区, +扇区 or +size{K,M,G} (47601664-83886079,默认为 83886079):+2G
分区 6 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 7
起始 扇区 (51798016-83886079,默认为 51798016):
将使用默认值 51798016
Last 扇区, +扇区 or +size{K,M,G} (51798016-83886079,默认为 83886079):+2G
分区 7 已设置为 Linux 类型,大小设为 2 GiB

修改id号

命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 8
起始 扇区 (55994368-83886079,默认为 55994368):
将使用默认值 55994368
Last 扇区, +扇区 or +size{K,M,G} (55994368-83886079,默认为 83886079):+2G
分区 8 已设置为 Linux 类型,大小设为 2 GiB
修改id:
命令(输入 m 获取帮助):T
分区号 (1-8,默认 8):6
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):t
分区号 (1-8,默认 8):7
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):t
分区号 (1-8,默认 8):8
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。

同步磁盘:

[root@localhost ~]# partprobe 
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@localhost ~]# pvcreate /dev/sda6 /dev/sda7 /dev/sda8
  Physical volume "/dev/sda6" successfully created
  Physical volume "/dev/sda7" successfully created
  Physical volume "/dev/sda8" successfully created
[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sda6       lvm2 ---  2.00g 2.00g
  /dev/sda7       lvm2 ---  2.00g 2.00g
  /dev/sda8       lvm2 ---  2.00g 2.00g

创建卷组datastore

[root@localhost ~]# vgcreate datastore /dev/sda6 /dev/sda7
  Volume group "datastore" successfully created
[root@localhost ~]# vgs
  VG        #PV #LV #SN Attr   VSize VFree
  datastore   2   0   0 wz--n- 3.99g 3.99g
[root@localhost ~]# pvs
  PV         VG        Fmt  Attr PSize PFree
  /dev/sda6  datastore lvm2 a--  2.00g 2.00g
  /dev/sda7  datastore lvm2 a--  2.00g 2.00g
  /dev/sda8            lvm2 ---  2.00g 2.00g

逻辑卷的名字为 database 逻辑卷大小为3G

[root@localhost ~]# lvcreate -L 3G -n database datastore
  Logical volume "database" created.
[root@localhost ~]# lvs
  LV       VG        Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  database datastore -wi-a----- 3.00g   

逻辑卷格式化为 xfs 文件系统

[root@localhost ~]# mkfs.xfs /dev/datastore/database
[root@localhost ~]# blkid
/dev/sr0: UUID="2015-10-30-11-11-49-00" LABEL="RHEL-7.2 Server.x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="91afe3d7-0581-4b9e-a1cd-94418167ee44" TYPE="xfs" 
/dev/sda2: UUID="ae750221-7377-489a-a543-9d32b93ae14b" TYPE="xfs" 
/dev/sda3: UUID="97c208ce-9abd-4016-b6da-6eb4f2d9b283" TYPE="swap" 
/dev/sda5: UUID="e6f4d495-d62e-4327-a95a-d7ee16653967" TYPE="swap" 
/dev/sda6: UUID="gPGp8t-OJaK-KZaI-oBC3-YXmP-U1Fz-Z1uoqR" TYPE="LVM2_member" 
/dev/sda7: UUID="GTHr8A-bzbV-cYuM-mRoA-qq10-BRtT-ckXbCk" TYPE="LVM2_member" 
/dev/sda8: UUID="GxKSoK-wwSQ-EKM4-NpZ3-q80l-Prvf-9MlsDC" TYPE="LVM2_member" 
/dev/mapper/datastore-database: UUID="6abdcc41-344c-419c-8c10-2f0e0095b01f" TYPE="xfs" 

[root@localhost ~]# vim /etc/fstab
UUID=6abdcc41-344c-419c-8c10-2f0e0095b01f /mnt                    xfs     defaults        0 0

扩大卷组

[root@localhost ~]# vgextend datastore /dev/sda8
  Volume group "datastore" successfully extended
[root@localhost ~]# pvs
  PV         VG        Fmt  Attr PSize PFree   
  /dev/sda6  datastore lvm2 a--  2.00g       0 
  /dev/sda7  datastore lvm2 a--  2.00g 1016.00m
  /dev/sda8  datastore lvm2 a--  2.00g    2.00g
database逻辑卷的大小扩大2G
[root@localhost ~]# lvextend -L +2G /dev/datastore/database
  Size of logical volume datastore/database changed from 3.00 GiB (768 extents) to 5.00 GiB (1280 extents).
  Logical volume database successfully resized.
[root@localhost ~]# lvs
  LV       VG        Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  database datastore -wi-a----- 5.00g

卸载

[root@localhost ~]# umount /dev/mapper/datastore-database 

删除逻辑卷

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

删除卷组

[root@localhost ~]# vgremove datastore
  Volume group "datastore" successfully removed

删除恢复原始状态

[root@localhost ~]# pvremove /dev/sda6 /dev/sda7 /dev/sda8
  Labels on physical volume "/dev/sda6" successfully wiped
  Labels on physical volume "/dev/sda7" successfully wiped
  Labels on physical volume "/dev/sda8" successfully wiped

[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):d
分区号 (1-8,默认 8):8
分区 8 已删除

命令(输入 m 获取帮助):d
分区号 (1-7,默认 7):
分区 7 已删除

命令(输入 m 获取帮助):d
分区号 (1-6,默认 6):
分区 6 已删除

命令(输入 m 获取帮助):d
分区号 (1-5,默认 5):
分区 5 已删除

命令(输入 m 获取帮助):d
分区号 (1-4,默认 4):
分区 4 已删除

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
[root@localhost ~]# partprobe 
Error: Partition(s) 5 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读

关注我的微信公众号:hello521day ,大量网络,渗透、编程资料以及ppt等模板等你来取!

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值