Linux中将多块新硬盘合并成一个,挂载到/data目录下

本文详细介绍了如何通过LVM(逻辑卷管理)将两块未分配的硬盘/dev/sdb和/dev/sdc合并为一个逻辑卷,并挂载到/data目录下。步骤包括创建物理卷(PV),创建卷组(VG),扩展VG,创建逻辑卷(LV),格式化分区,挂载分区以及设置开机加载。此外,还展示了如何使用LVM的相关命令进行管理。
摘要由CSDN通过智能技术生成

需求:
将两块空硬盘合并为“一块”,挂载到指定目录(/data)下,达到在一个目录使用2块硬盘所有空间的效果。

使用 fdisk -l 命令查看当前系统中的硬盘,如下图:
在这里插入图片描述
系统中存在两块未分配的硬盘:/dev/sdb 和 /dev/sdc,共13G

现将两块硬盘合并成一块,挂载到/data目录下,具体方法如下:

1.创建pv

pvcreate /dev/sdb  # 硬盘1
pvcreate /dev/sdc  # 硬盘2

在这里插入图片描述

2.创建vg

# vgcreate [自定义LVM名称] [硬盘]
# 先使用硬盘1创建vg:lvm_data

vgcreate lvm_data /dev/sdb

在这里插入图片描述

3.扩展vg

# vgextend [自定义vg名称] [硬盘]   
# 使用硬盘2扩展vg

vgextend lvm_data /dev/sdc

在这里插入图片描述

4.创建lv

# lvcreate -l[自定义分区大小] -n[自定义分区名称] [vg名称]
# 分区大小不能超过硬盘容量总和
如创建一个10G的分区:
lvcreate -l10.0G -n vg_data lvm_data

如果将两个盘全部空间分区,可以使用以下方式:
lvcreate -l 100%VG -n vg_data lvm_data

5.格式化分区

# mkfs -t [文件系统] [分区位置]

mkfs -t ext4 /dev/lvm_data/vg_data

在这里插入图片描述

6.挂载分区

# mount [分区位置] [目录地址]

mount /dev/lvm_data/vg_data /data

在这里插入图片描述

7.设置开机加载

在/etc/fstab文件末尾添加如下行:

/dev/lvm_data/vg_data	/data 	ext4	defaults	0 0

8.总结

一.pv lv vg 介绍

  • PV(physical volume)即物理卷,就是物理磁盘,可以通过 fdisk -l 查看操作系统有几块硬盘

  • VG(volume group)即卷组,就是一组物理磁盘的组合,里面可以有一块硬盘也可以有多块硬盘

  • LV(logical volume)及逻辑卷,就是在VG(指定的物理磁盘组)里面划分出来的

可以说成是PV就是硬盘,而VG就是管理硬盘的操作系统,而LV就是操作系统分出来的各个分区.

PV->VG->LV-> 文件系统使用(挂载到某个目录)

硬盘或分区做成pv,然后将pv或多个pv建立vg,vg上建立lv
在这里插入图片描述

二.命令集合

1.PV

pvcreate :根据物理盘,创建pv
pvscan :查询目前系统里的pv
pvdisplay:显示pv的状态
pvremove:将pv属性移除

2.VG

vgcreate:创建vg
vgscan:查找当前系统里面的vg
vgdisplay:显示当前系统vg的状态
vgextend:给vg添加额外的pv
vgreduce:在vg内删除pv
vgchange:设置vg是否是启动状态(active)
vgremove:删除一个vg

3.LV

lvcreate:创建lv
lvscan:查询当前系统的lv
lvdisplay:显示lv的属性
lvextend:给lv添加容量
lvredurce:给lv减少容量
lvremove:删除一个lv
lvresize:对lv大小的容量进行调整

三.实际操作

下面实际操作下,来看看

1.案例描述

从下看到只是把sda这块硬盘用了,做了vg(vg_dsgdata),并且在vg(vg_dsgdata)下建了/dev/vg_dsgdata/lv_root, /dev/vg_dsgdata/lv_swap, /dev/vg_dsgdata/lv_home三个lv, 通过df -h看到/dev/mapper/vg_dsgdata-lv_root挂载在根目录/下,/dev/mapper/vg_dsgdata-lv_home挂载在/home目录下.从上述看,还剩余sdb,sdc,sdd三块硬盘没有使用

[root@dsgdata ~]# fdisk -l

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/sdc: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/sdd: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a2d9e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64      121602   976248832   8e  Linux LVM

Disk /dev/mapper/vg_dsgdata-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_dsgdata-lv_swap: 8355 MB, 8355053568 bytes
255 heads, 63 sectors/track, 1015 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_dsgdata-lv_home: 937.6 GB, 937632464896 bytes
255 heads, 63 sectors/track, 113993 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@dsgdata ~]# 


[root@dsgdata ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_dsgdata
  PV Size               931.02 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              238341
  Free PE               0
  Allocated PE          238341
  PV UUID               lPOKfO-HZ6y-4ubK-5TPk-FuB0-ypCU-y0mK1R
   
[root@dsgdata ~]# 
[root@dsgdata ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_dsgdata
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               931.02 GiB
  PE Size               4.00 MiB
  Total PE              238341
  Alloc PE / Size       238341 / 931.02 GiB
  Free  PE / Size       0 / 0   
  VG UUID               zQcZh0-2t6e-9LuM-zHnv-R0tG-sQDi-Ba3wLK
   
[root@dsgdata ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_dsgdata/lv_root
  LV Name                lv_root
  VG Name                vg_dsgdata
  LV UUID                D7Z7jC-v0aK-fYJ5-AbcN-LmjX-nMEJ-dwpcm0
  LV Write Access        read/write
  LV Creation host, time dsgdata, 2014-03-05 17:59:43 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg_dsgdata/lv_swap
  LV Name                lv_swap
  VG Name                vg_dsgdata
  LV UUID                0HrVlq-IS9g-hNH0-R1Sr-hvcd-BUI2-hkjfLh
  LV Write Access        read/write
  LV Creation host, time dsgdata, 2014-03-05 18:00:00 +0800
  LV Status              available
  # open                 1
  LV Size                7.78 GiB
  Current LE             1992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/vg_dsgdata/lv_home
  LV Name                lv_home
  VG Name                vg_dsgdata
  LV UUID                PC4IMN-Nvm3-UpaB-Em5I-NCvq-fzPF-MKcqOs
  LV Write Access        read/write
  LV Creation host, time dsgdata, 2014-03-05 18:00:01 +0800
  LV Status              available
  # open                 1
  LV Size                873.24 GiB
  Current LE             223549
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
   
[root@dsgdata ~]#
[root@dsgdata ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/vg_dsgdata-lv_root   50G  4.4G   43G  10% /
tmpfs                           3.9G  228K  3.9G   1% /dev/shm
/dev/sda1                       485M   40M  420M   9% /boot
/dev/mapper/vg_dsgdata-lv_home  860G  200M  816G   1% /home
[root@dsgdata ~]# 

2.下面手工操作,创建增加减少删除关于pv,vg,lv的操作

创建pv

[root@dsgdata /]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created
[root@dsgdata /]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_dsgdata
  PV Size               931.02 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              238341
  Free PE               0
  Allocated PE          238341
  PV UUID               lPOKfO-HZ6y-4ubK-5TPk-FuB0-ypCU-y0mK1R
   
  "/dev/sdb" is a new physical volume of "931.51 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               931.51 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               MO29Sj-3lEd-Licp-BNxD-9qPH-v5y4-zLUNHI
   
[root@dsgdata /]# pvscan
  PV /dev/sda2   VG vg_dsgdata      lvm2 [931.02 GiB / 0    free]
  PV /dev/sdb                       lvm2 [931.51 GiB]
  Total: 2 [1.82 TiB] / in use: 1 [931.02 GiB] / in no VG: 1 [931.51 GiB]
[root@dsgdata /]# 
[root@dsgdata /]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped
[root@dsgdata /]# pvscan
  PV /dev/sda2   VG vg_dsgdata   lvm2 [931.02 GiB / 0    free]
  Total: 1 [931.02 GiB] / in use: 1 [931.02 GiB] / in no VG: 0 [0   ]<pre code_snippet_id="220533" snippet_file_name="blog_20140306_6_5401133" class="html" name="code">[root@dsgdata /]# pvcreate /dev/sdb 
Physical volume "/dev/sdb" successfully created

3.创建VG

[root@dsgdata /]# vgcreate DATA /dev/sdb  

Volume group "DATA" successfully created

[root@dsgdata /]#

[root@dsgdata /]# vgdisplay DATA  

--- Volume group ---  VG Name               DATA  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               931.51 GiB  PE Size               4.00 MiB  Total PE              238467  Alloc PE / Size       0 / 0     Free  PE / Size       238467 / 931.51 GiB  VG UUID               HhP6O7-sjLT-xzA7-YqLl-QsOJ-lQSM-3sN5VS   

[root@dsgdata /]#

给VG添加或者减少容量:

[root@dsgdata /]# pvcreate /dev/sdc  Physical volume "/dev/sdc" 

successfully created

[root@dsgdata /]# vgextend DATA /dev/sdc  

Volume group "DATA" successfully extended

[root@dsgdata /]# vgdisplay DATA  

--- Volume group ---  VG Name               DATA  System ID               Format                lvm2  Metadata Areas        2  Metadata Sequence No  2  VG Access             read/write  VG Status             resizable  MAX LV                0  Cur LV                0  Open LV               0  Max PV                0  Cur PV                2  Act PV                2  VG Size               1.82 TiB  PE Size               4.00 MiB  Total PE              476934  Alloc PE / Size       0 / 0     Free  PE / Size       476934 / 1.82 TiB  VG UUID               HhP6O7-sjLT-xzA7-YqLl-QsOJ-lQSM-3sN5VS  

[root@dsgdata /]# vgreduce DATA /dev/sdc  

Removed "/dev/sdc" from volume group "DATA"

[root@dsgdata /]# vgdisplay DATA  

--- Volume group ---  VG Name               DATA  System ID               Format                lvm2  Metadata Areas        1  Metadata Sequence No  3  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               931.51 GiB  PE Size               4.00 MiB  Total PE              238467  Alloc PE / Size       0 / 0     Free  PE / Size       238467 / 931.51 GiB  VG UUID               HhP6O7-sjLT-xzA7-YqLl-QsOJ-lQSM-3sN5VS   

[root@dsgdata /]#

4.创建LV,并挂载

[root@dsgdata /]# lvscan  

ACTIVE            '/dev/vg_dsgdata/lv_root' [50.00 GiB] inherit  
ACTIVE            '/dev/vg_dsgdata/lv_swap' [7.78 GiB] inherit  
ACTIVE            '/dev/vg_dsgdata/lv_home' [873.24 GiB] inherit

[root@dsgdata /]# lvcreate -n test -L 100G DATA  

Logical volume "test" created

[root@dsgdata /]# lvscan  ACTIVE            

'/dev/DATA/test' [100.00 GiB] inherit  ACTIVE           
'/dev/vg_dsgdata/lv_root' [50.00 GiB] inherit  ACTIVE           
'/dev/vg_dsgdata/lv_swap' [7.78 GiB] inherit  ACTIVE            
'/dev/vg_dsgdata/lv_home' [873.24 GiB] inherit

[root@dsgdata /]#

在根目录下创建目录CXF
[root@dsgdata /]# mkdir /cxf
对逻辑卷进行格式化:

[root@dsgdata /]# mkfs -t ext4 /dev/DATA/test
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872</p><p>Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: 
done</p><p>This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@dsgdata /]#

将逻辑卷/dev/DATA/test挂载到/cxf目录下:

[root@dsgdata /]# mount /dev/DATA/test /cxf
[root@dsgdata /]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/vg_dsgdata-lv_root   50G  4.4G   43G  10% /
tmpfs                           3.9G  228K  3.9G   1% /dev/shm
/dev/sda1                       485M   40M  420M   9% /boot
/dev/mapper/vg_dsgdata-lv_home  860G  200M  816G   1% /home
/dev/mapper/DATA-test            99G  188M   94G   1% /cxf
[root@dsgdata /]#
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值