Linux中通过逻辑卷(LV,Logic Volumn)轻松实现文件系统扩容

Linux中如果直接使用物理磁盘的分区为作为文件系统,那么一旦磁盘空间满了,就不易扩容,而使用逻辑卷的话,可以先往逻辑卷所在卷组(VG, Volumn Group)增加物理卷(PV, Physical Volumn),增加之后,再对逻辑卷括容。


本文中,先是创建了一个含有两个PV的VG: mqsVG1, 然后在这个VG上创建了两个LV: mqsLV1和mqsLV2,后期发现mqsLV1空间不足,于是往VG上增加了一个新的PV,并对LV进行了护容。

//有一个空的磁盘/dev/sdb,还没有分区
root@db2a:~# fdisk -l

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000062fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    81788927    40893440   83  Linux
/dev/sda2        81790974    83884031     1046529    5  Extended
/dev/sda5        81790976    83884031     1046528   82  Linux swap / Solaris

创建两个分区 /dev/sdb1和/dev/sdb2 :
root@db2a:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9ea8b60b.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +400M

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

root@db2a:~# fdisk /dev/sdb

Command (m for help): n  
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (821248-2097151, default 821248): 
Using default value 821248
Last sector, +sectors or +size{K,M,G} (821248-2097151, default 2097151): 
Using default value 2097151

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

修改分区类型为8e:
root@db2a:~# fdisk /dev/sdb

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list codes): 8e   
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


root@db2a:~# fdisk /dev/sdb

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@db2a:~# fdisk -l

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9ea8b60b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      821247      409600   8e  Linux LVM
/dev/sdb2          821248     2097151      637952   8e  Linux LVM


Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000062fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    81788927    40893440   83  Linux
/dev/sda2        81790974    83884031     1046529    5  Extended
/dev/sda5        81790976    83884031     1046528   82  Linux swap / Solaris

//在两个分区上创建两个物理卷,并查询
root@db2a:~# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
root@db2a:~# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created
  
root@db2a:~# pvdisplay
  "/dev/sdb1" is a new physical volume of "400.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               400.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               na4fa1-hFRU-9EtX-3ZAe-NDEf-5g05-PqEcgO
   
  "/dev/sdb2" is a new physical volume of "623.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               623.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               bGYCTM-kyGW-MV5i-O8FL-SBww-poo8-rzRON8
  
//创建卷组,并查询:
root@db2a:~# vgcreate mqsVG1 /dev/sdb1 /dev/sdb2
  Volume group "mqsVG1" successfully created
root@db2a:~# vgdisplay
  --- Volume group ---
  VG Name               mqsVG1
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               1016.00 MiB
  PE Size               4.00 MiB
  Total PE              254
  Alloc PE / Size       0 / 0   
  Free  PE / Size       254 / 1016.00 MiB
  VG UUID               6KT7xw-6k0Q-yhpf-pVRA-9iTY-wCG4-QgfiXh

//创建两个LV:一个500MB,另一个516MB:
   root@db2a:~# lvcreate -L 500M -n mqsLV1 mqsVG1
  Logical volume "mqsLV1" created
root@db2a:~# lvcreate -L 516M -n mqsLV2 mqsVG1
  Logical volume "mqsLV2" created
root@db2a:~# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/mqsVG1/mqsLV1
  LV Name                mqsLV1
  VG Name                mqsVG1
  LV UUID                y3KmXN-U5Jd-xEvX-JoTl-YZEe-BxQK-7Wxk8H
  LV Write Access        read/write
  LV Creation host, time db2a, 2017-06-29 03:03:31 -0700
  LV Status              available
  # open                 0
  LV Size                500.00 MiB
  Current LE             125
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0
   
  --- Logical volume ---
  LV Path                /dev/mqsVG1/mqsLV2
  LV Name                mqsLV2
  VG Name                mqsVG1
  LV UUID                mQxR0S-tBYg-T1Vd-Gclo-4cfV-CZj2-qzGjYw
  LV Write Access        read/write
  LV Creation host, time db2a, 2017-06-29 03:03:47 -0700
  LV Status              available
  # open                 0
  LV Size                516.00 MiB
  Current LE             129
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1
     
//创建文件系统
root@db2a:~# mkfs.ext3 /dev/mqsVG1/mqsLV1
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

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

root@db2a:~# mkfs.ext3 /dev/mqsVG1/mqsLV2
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
33040 inodes, 132096 blocks
6604 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=138412032
5 block groups
32768 blocks per group, 32768 fragments per group
6608 inodes per group
Superblock backups stored on blocks: 
        32768, 98304

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


//创建挂载点,并挂载:
root@db2a:~# mkdir /mqslv1mountpoint
root@db2a:~# mkdir /mqslv2mountpoint
root@db2a:~# mount /dev/mqsVG1/mqsLV1 /mqslv1mountpoint
root@db2a:~# mount /dev/mqsVG1/mqsLV2 /mqslv2mountpoint
root@db2a:~# df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       482M  4.0K  482M   1% /dev
tmpfs                       99M 1008K   98M   2% /run
/dev/sda1                   39G  7.8G   29G  22% /
none                       4.0K     0  4.0K   0% /sys/fs/cgroup
none                       5.0M     0  5.0M   0% /run/lock
none                       492M     0  492M   0% /run/shm
none                       100M     0  100M   0% /run/user
/dev/mapper/mqsVG1-mqsLV1  477M  2.3M  449M   1% /mqslv1mountpoint
/dev/mapper/mqsVG1-mqsLV2  492M  428K  466M   1% /mqslv2mountpoint


//假设使用一段时间之后,mqsLV1空间不足了,如下:
root@db2a:~# df
Filesystem                1K-blocks    Used Available Use% Mounted on
udev                         492228       4    492224   1% /dev
tmpfs                        100760    1012     99748   2% /run
/dev/sda1                  40120704 8121980  29937668  22% /
none                              4       0         4   0% /sys/fs/cgroup
none                           5120       0      5120   0% /run/lock
none                         503780       0    503780   0% /run/shm
none                         102400       0    102400   0% /run/user
/dev/mapper/mqsVG1-mqsLV1    487652  487650         0 100% /mqslv1mountpoint
/dev/mapper/mqsVG1-mqsLV2    503676     428    476832   1% /mqslv2mountpoint

//新添加一块2G的磁盘,并在上面创建了一个PV,名为/dev/sdc1:
root@db2a:~# pvdisplay /dev/sdc1
  "/dev/sdc1" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               JS58Bm-Sh0D-u6tk-ZTmg-DGWt-ans5-5z5FwO

//扩充卷组
   root@db2a:~# vgextend mqsVG1 /dev/sdc1
  Volume group "mqsVG1" successfully extended
root@db2a:~# vgdisplay
  --- Volume group ---
  VG Name               mqsVG1
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       254 / 1016.00 MiB
  Free  PE / Size       511 / 2.00 GiB
  VG UUID               6KT7xw-6k0Q-yhpf-pVRA-9iTY-wCG4-QgfiXh  
 
//括容LV
root@db2a:~# lvextend -L +1024M /dev/mapper/mqsVG1-mqsLV1 
  Extending logical volume mqsLV1 to 1.49 GiB
  Logical volume mqsLV1 successfully resized
  
root@db2a:~#  resize2fs  /dev/mapper/mqsVG1-mqsLV1 
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/mapper/mqsVG1-mqsLV1 is mounted on /mqslv1mountpoint; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 6
The filesystem on /dev/mapper/mqsVG1-mqsLV1 is now 1560576 blocks long.

//可以看到,已经增加到1.5G
root@db2a:~# df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       481M  4.0K  481M   1% /dev
tmpfs                       99M 1016K   98M   2% /run
/dev/sda1                   39G  7.8G   29G  22% /
none                       4.0K     0  4.0K   0% /sys/fs/cgroup
none                       5.0M     0  5.0M   0% /run/lock
none                       492M     0  492M   0% /run/shm
none                       100M     0  100M   0% /run/user
/dev/mapper/mqsVG1-mqsLV1  1.5G  477M  926M  34% /mqslv1mountpoint
/dev/mapper/mqsVG1-mqsLV2  492M  428K  466M   1% /mqslv2mountpoint

参考资料:
LINUX系统命令及Shell脚本实践指南 王军

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值