This article will guide to expand disk with LVM. My scenario is I install linux in virtual machine VMWare. And I use VMWare to expand disk (sdb) from 80G to 160G. So now, I need to setup linux machine to use expanded disk.
At the first, check disks with fdisk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$ sudo fdisk -l Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 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 Disklabel type: dos Disk identifier: 0x5c9902c7 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 499711 497664 243M 83 Linux /dev/sda2 501758 167770111 167268354 79.8G 5 Extended /dev/sda5 501760 167770111 167268352 79.8G 8e Linux LVM Disk /dev/sdb: 160 GiB, 171798691840 bytes, 335544320 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 Disklabel type: dos Disk identifier: 0x3236c046 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 167772159 167770112 80G 8e Linux LVM Disk /dev/mapper/sonnst--vg2-lvol0: 80 GiB, 85895151616 bytes, 167763968 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 /dev/mapper/sonnst--vg-root: 77.7 GiB, 83454066688 bytes, 162996224 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 /dev/mapper/sonnst--vg-swap_1: 2 GiB, 2143289344 bytes, 4186112 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 |
You can see in the output that /dev/sdb is 160 Gb. However, we just create a partition sdb1 (80G).
So use fdisk to add a new partition sdb2 (type Linux LVM)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
$ sudo fdisk /dev/sdb Welcome to fdisk (util-linux 2.25.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): m Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table Misc m print this menu u change display/entry units x extra functionality (experts only) Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): First sector (167772160-335544319, default 167772160): Last sector, +sectors or +size{K,M,G,T,P} (167772160-335544319, default 335544319): Created a new partition 2 of type 'Linux' and of size 80 GiB. Command (m for help): t Partition number (1,2, default 2): 2 Hex code (type L to list all 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 all codes): 8e Changed type of partition 'Linux' to 'Linux LVM'. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: 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). |
Step 2, create physical volume with pvcreate:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
thanhson1085@sonnst:~$ sudo pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created thanhson1085@sonnst:~$ sudo pvdisplay --- Physical volume --- PV Name /dev/sdb1 VG Name sonnst-vg2 PV Size 80.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 20479 Free PE 0 Allocated PE 20479 PV UUID lyjbbo-VZxn-PZd0-wyBQ-iLFv-H9De-iwi0NT --- Physical volume --- PV Name /dev/sda5 VG Name sonnst-vg PV Size 79.76 GiB / not usable 2.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 20418 Free PE 10 Allocated PE 20408 PV UUID NtHuvl-Agge-5Kmo-MLHq-lTvs-u6Q1-eCgTbD "/dev/sdb2" is a new physical volume of "80.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 80.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 5V8VSW-XYhc-l7YK-Xgom-S0ya-FxFu-111gNW |
Step 3, extend volume group:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
thanhson1085@sonnst:~$ sudo vgdisplay --- Volume group --- VG Name sonnst-vg2 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size 80.00 GiB PE Size 4.00 MiB Total PE 20479 Alloc PE / Size 20479 / 80.00 GiB Free PE / Size 0 / 0 VG UUID pjea3V-vhMI-omeF-EvF0-QxfC-FIUy-DvvmwX --- Volume group --- VG Name sonnst-vg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 79.76 GiB PE Size 4.00 MiB Total PE 20418 Alloc PE / Size 20408 / 79.72 GiB Free PE / Size 10 / 40.00 MiB VG UUID vZwRqI-Nx1j-WzmV-tShM-PsMG-KHeH-dFNf1u $ sudo vgextend sonnst-vg2 /dev/sdb2 Volume group "sonnst-vg2" successfully extended thanhson1085@sonnst:~$ sudo lvdisplay --- Logical volume --- LV Path /dev/sonnst-vg2/lvol0 LV Name lvol0 VG Name sonnst-vg2 LV UUID Bwnffm-61lW-PaP4-dcli-4e6J-BDuP-6V6b9L LV Write Access read/write LV Creation host, time sonnst, 2014-12-04 20:56:24 +0700 LV Status available # open 1 LV Size 80.00 GiB Current LE 20479 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:0 --- Logical volume --- LV Path /dev/sonnst-vg/root LV Name root VG Name sonnst-vg LV UUID UAgPC2-0lT0-qf6E-Gaa1-o5D9-kudc-aX43M9 LV Write Access read/write LV Creation host, time sonnst, 2014-12-05 10:22:10 +0700 LV Status available # open 1 LV Size 77.72 GiB Current LE 19897 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:1 |
Extend logical volume
1 2 3 |
$ sudo lvextend -L +79G /dev/sonnst-vg2/lvol0 Extending logical volume lvol0 to 159.00 GiB Logical volume lvol0 successfully resized |
Finally, re-size file system:
1 2 3 4 5 |
thanhson1085@sonnst:~$ sudo resize2fs /dev/sonnst-vg2/lvol0 resize2fs 1.42.10 (18-May-2014) Filesystem at /dev/sonnst-vg2/lvol0 is mounted on /var/data; on-line resizing required old_desc_blocks = 5, new_desc_blocks = 10 The filesystem on /dev/sonnst-vg2/lvol0 is now 41679872 blocks long. |