lvm扩张与收缩小结

lvm结构:

磁盘 => 分区 => PV => VG => LV => 文件系统


扩张:

先扩LV再扩文件系统

lvextend -L +100M /dev/vg1/lv1 逻辑卷的大小增加100M

resize2fs /dev/vg1/lv1 文件系统增到逻辑卷的大小


收缩:

先减文件系统再减LV(注意顺序)

umount /dev/vg1/lv1

e2fsck -f /dev/vg1/lv1

resize2fs /dev/vg1/lv1 100M 减小文件系统到100M

lvreduce -L 100M /dev/vg1/lv1 减小逻辑卷到100M

mount -a



创建PV所需分区:

[root@rhel6-client ~]# fdisk -cul /dev/sda


Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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: 0x000cb393

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41371647    20480000   83  Linux
/dev/sda3        41371648    45467647     2048000   82  Linux swap / Solaris
[root@rhel6-client ~]# fdisk -cu /dev/sda

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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: 0x000cb393

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41371647    20480000   83  Linux
/dev/sda3        41371648    45467647     2048000   82  Linux swap / Solaris

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First sector (45467648-209715199, default 45467648):
Using default value 45467648
Last sector, +sectors or +size{K,M,G} (45467648-209715199, default 209715199):
Using default value 209715199

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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: 0x000cb393

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41371647    20480000   83  Linux
/dev/sda3        41371648    45467647     2048000   82  Linux swap / Solaris
/dev/sda4        45467648   209715199    82123776    5  Extended

Command (m for help): n
First sector (45469696-209715199, default 45469696):
Using default value 45469696
Last sector, +sectors or +size{K,M,G} (45469696-209715199, default 209715199): +100M

Command (m for help): n
First sector (45676544-209715199, default 45676544): +100
Value out of range.
First sector (45676544-209715199, default 45676544):
Using default value 45676544
Last sector, +sectors or +size{K,M,G} (45676544-209715199, default 209715199): +100M

Command (m for help): n
First sector (45883392-209715199, default 45883392):
Using default value 45883392
Last sector, +sectors or +size{K,M,G} (45883392-209715199, default 209715199): +100M

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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: 0x000cb393

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41371647    20480000   83  Linux
/dev/sda3        41371648    45467647     2048000   82  Linux swap / Solaris
/dev/sda4        45467648   209715199    82123776    5  Extended
/dev/sda5        45469696    45674495      102400   83  Linux
/dev/sda6        45676544    45881343      102400   83  Linux
/dev/sda7        45883392    46088191      102400   83  Linux

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

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

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

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

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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: 0x000cb393

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41371647    20480000   83  Linux
/dev/sda3        41371648    45467647     2048000   82  Linux swap / Solaris
/dev/sda4        45467648   209715199    82123776    5  Extended
/dev/sda5        45469696    45674495      102400   8e  Linux LVM
/dev/sda6        45676544    45881343      102400   8e  Linux LVM
/dev/sda7        45883392    46088191      102400   8e  Linux LVM

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.


reboot/partx -a /dev/sda


[root@rhel6-client ~]# cat /proc/partitions
major minor  #blocks  name

   8        0  104857600 sda
   8        1     204800 sda1
   8        2   20480000 sda2
   8        3    2048000 sda3
   8        4          1 sda4
   8        5     102400 sda5
   8        6     102400 sda6
   8        7     102400 sda7


创建PV:
[root@rhel6-client ~]# pvcreate /dev/sda5 /dev/sda6 /dev/sda7
  Physical volume "/dev/sda5" successfully created
  Physical volume "/dev/sda6" successfully created
  Physical volume "/dev/sda7" successfully created


创建VG:

[root@rhel6-client ~]# vgcreate -s 8M /dev/sda5 /dev/sda6 /dev/sda7
  /dev/sda5: already exists in filesystem
  New volume group name "sda5" is invalid
  Run `vgcreate --help' for more information.
[root@rhel6-client ~]# vgcreate -s 8M vg1 /dev/sda5 /dev/sda6 /dev/sda7
  Volume group "vg1" successfully created
[root@rhel6-client ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree 
  vg1    3   0   0 wz--n- 288.00m 288.00m


创建LV:

[root@rhel6-client ~]# lvcreate vg1 -L 100M -n lv1
  Rounding up size to full physical extent 104.00 MiB
  Logical volume "lv1" created
[root@rhel6-client ~]# lvs
  LV   VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv1  vg1  -wi-a- 104.00m                                      
[root@rhel6-client ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G  2.6G   16G  15% /
tmpfs                 244M   88K  244M   1% /dev/shm

/dev/sda1             194M   26M  159M  14% /boot


创建文件系统:

[root@rhel6-client ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
26624 inodes, 106496 blocks
5324 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729

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

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


挂载文件系统:

[root@rhel6-client ~]# mkdir /lvm
[root@rhel6-client ~]# mount /dev/vg1/lv1 /lvm
[root@rhel6-client ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G  2.6G   16G  15% /
tmpfs                 244M   88K  244M   1% /dev/shm
/dev/sda1             194M   26M  159M  14% /boot
/dev/mapper/vg1-lv1   101M  5.6M   90M   6% /lvm

扩张:先扩lvm再扩文件系统
[root@rhel6-client ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree  
  vg1    3   1   0 wz--n- 288.00m 184.00m
[root@rhel6-client ~]# lvextend -L +100M /dev/vg1/lv1
  Rounding up size to full physical extent 104.00 MiB
  Extending logical volume lv1 to 208.00 MiB
  Logical volume lv1 successfully resized
[root@rhel6-client ~]# lvs
  LV   VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv1  vg1  -wi-ao 208.00m                                      
[root@rhel6-client ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G  2.6G   16G  15% /
tmpfs                 244M   88K  244M   1% /dev/shm
/dev/sda1             194M   26M  159M  14% /boot
/dev/mapper/vg1-lv1   101M  5.6M   90M   6% /lvm
[root@rhel6-client ~]# resize2fs /dev/vg1/lv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg1/lv1 is mounted on /lvm; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg1/lv1 to 212992 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 212992 blocks long.

[root@rhel6-client ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G  2.6G   16G  15% /
tmpfs                 244M   88K  244M   1% /dev/shm
/dev/sda1             194M   26M  159M  14% /boot
/dev/mapper/vg1-lv1   202M  5.8M  186M   4% /lvm

收缩:先件文件系统再减lvm
[root@rhel6-client ~]# resize2fs /dev/vg1/lv1 150M
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg1/lv1 is mounted on /lvm; on-line resizing required
On-line shrinking from 212992 to 153600 not supported.
[root@rhel6-client ~]# umount /lvm
[root@rhel6-client ~]# resize2fs /dev/vg1/lv1 150M
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/vg1/lv1' first.

[root@rhel6-client ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.41.12 (17-May-2010)
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/vg1/lv1: 11/53248 files (9.1% non-contiguous), 12625/212992 blocks
[root@rhel6-client ~]# resize2fs /dev/vg1/lv1 150M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg1/lv1 to 153600 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 153600 blocks long.

[root@rhel6-client ~]# lvs
  LV   VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv1  vg1  -wi-a- 208.00m                                     
[root@rhel6-client ~]# lvreduce -L 150M /dev/vg1/lv1
  Rounding up size to full physical extent 152.00 MiB
  WARNING: Reducing active logical volume to 152.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
  Reducing logical volume lv1 to 152.00 MiB
  Logical volume lv1 successfully resized
[root@rhel6-client ~]# lvs
  LV   VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv1  vg1  -wi-a- 152.00m                                      
[root@rhel6-client ~]# mount /dev/vg1/lv1 /lvm
[root@rhel6-client ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G  2.6G   16G  15% /
tmpfs                 244M   88K  244M   1% /dev/shm
/dev/sda1             194M   26M  159M  14% /boot
/dev/mapper/vg1-lv1   146M  5.6M  133M   5% /lvm
[root@rhel6-client ~]#
[root@rhel6-client ~]# lvs
  LV   VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv1  vg1  -wi-ao 152.00m                                      
[root@rhel6-client ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree  
  vg1    3   1   0 wz--n- 288.00m 136.00m


你可能感兴趣的:(lvm扩张与收缩小结)