• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Life always offers you a second chance ... Its called tomorrow !!!

    Friday, March 21, 2014

    How to extend/resize Logical Volume and Volume Group in Linux

    As of now you can see my root partition is on LVM with 5.8Gb size. I want to extend my root filesystem with +1GB. Now here there can be two scenarios where
    # df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-root
                         
    5.8G  4.1G  1.4G  76% /
    tmpfs                 504M  260K  504M   1% /dev/shm
    /dev/sda1             194M   26M  158M  15% /boot

    1. You want to extend size using extra partition
    2. You want to extend size with space in existing Volume Group

    I will show you steps required to perform the same using both the scenarios

    Scenario 1

    Extend LVM using additional partition

    # fdisk -l
    
    Disk /dev/sda: 10.7 GB, 10737418240 bytes
    255 heads, 63 sectors/track, 1305 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: 0x000cf049
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1          26      204800   83  Linux
    Partition 1 does not end on cylinder boundary.
    /dev/sda2              26        1306    10279936   8e  Linux LVM
    
    Disk /dev/sdb: 2147 MB, 2147483648 bytes
    255 heads, 63 sectors/track, 261 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/VolGroup-root: 6278 MB, 6278873088 bytes
    255 heads, 63 sectors/track, 763 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/VolGroup-swap: 2147 MB, 2147483648 bytes
    255 heads, 63 sectors/track, 261 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
    So as you see I have added extra partition hard disk /dev/sdb with 2GB space. Let us use it to extend our root file system.

    Steps required to perform the same
    1. Create partition for /dev/sdb with 1 GB storage
    2. Create Physical Volume
    3. Extend existing Volume Group
    4. Extend LVM
    5. Verify the changes

    Create partition with /dev/sdb

    # 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 0x55693454.
    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)
    
    WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
             switch off the mode (command 'c') and change display units to
             sectors (command 'u').
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 1
    First cylinder (1-261, default 1): 1
    Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): +1G
    
    Command (m for help): p
    
    Disk /dev/sdb: 2147 MB, 2147483648 bytes
    255 heads, 63 sectors/track, 261 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: 0x55693454
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1         132     1060258+  83  Linux
    
    Command (m for help): t
    Selected partition 1
    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 1 to 8e (Linux LVM)
    
    Command (m for help): p
    
    Disk /dev/sdb: 2147 MB, 2147483648 bytes
    255 heads, 63 sectors/track, 261 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: 0x55693454
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1         132     1060258+  8e  Linux LVM
    
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    # partprobe /dev/sdb1 Now at this stage our partition is created as /dev/sdb1 with LVM type.

    Create Physical Volume

    # pvcreate /dev/sdb1
     
    Physical volume "/dev/sdb1" successfully created


    Extend Volume Group

    Before extending the size of our existing Volume group let us verify the size of the same
    # vgdisplay
      --- Volume group ---
      VG Name              
    VolGroup
      System ID
      Format                lvm2
      Metadata Areas        1
      Metadata Sequence No  5
      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               9.80 GiB
      PE Size               4.00 MiB
      Total PE              2509
      Alloc PE / Size       2009 / 7.85 GiB
      Free  PE / Size       500 / 1.95 GiB
      VG UUID               uH5AP5-b24E-92h7-nL8b-7Bio-fXe3-pstWIW

    So our Volume Group contains 9.80 GB

    Next extend the Volume Group using the below command
    # vgextend VolGroup /dev/sdb1
     
    Volume group "VolGroup" successfully extended

    Verify the change
    # vgdisplay
      --- Volume group ---
      VG Name               VolGroup
      System ID
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  6
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                2
      Open LV               2
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size              
    10.81 GiB
      PE Size               4.00 MiB
      Total PE              2767
      Alloc PE / Size       2009 / 7.85 GiB
      Free  PE / Size       758 / 2.96 GiB
      VG UUID               uH5AP5-b24E-92h7-nL8b-7Bio-fXe3-pstWIW

    As you can verify the change i.e. extra 1 GB has been added to our Volume Group which we can use to extend out root filesystem

    Extend LVM

    Before extending the root fs let us verify the existing size
    # lvdisplay /dev/VolGroup/root
      --- Logical volume ---
      LV Path                /dev/VolGroup/root
      LV Name                root
      VG Name                VolGroup
      LV UUID                Qn8TnI-TLNm-rl4Y-ORnd-zU3p-2Kj1-ALSLAg
      LV Write Access        read/write
      LV Creation host, time ,
      LV Status              available
      # open                 1
     
    LV Size                5.85 GiB
      Current LE             1497
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0

    So our existing LVM size for root is 5.85GB

    Let us extend the size with +1GB
    # lvextend -L +1G /dev/VolGroup/root
      Extending logical volume root to 6.85 GiB
      Logical volume root successfully resized

    Verify the changes
    # lvdisplay /dev/VolGroup/root
      --- Logical volume ---
      LV Path                /dev/VolGroup/root
      LV Name                root
      VG Name                VolGroup
      LV UUID                Qn8TnI-TLNm-rl4Y-ORnd-zU3p-2Kj1-ALSLAg
      LV Write Access        read/write
      LV Creation host, time ,
      LV Status              available
      # open                 1
      LV Size                6.85 GiB
      Current LE             1753
      Segments               2
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0

    Let us see if the changes are reflected using df
    # df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-root
                         
    5.8G  4.1G  1.4G  76% /
    tmpfs                 504M  260K  504M   1% /dev/shm
    /dev/sda1             194M   26M  158M  15% /boot

    But it still shows old size for root. The reason is because we need to update the partition table with the new value of LVM which can be done using the below command
    # resize2fs /dev/VolGroup/root
    resize2fs 1.41.12 (17-May-2010)
    Filesystem at /dev/VolGroup/root is mounted on /; on-line resizing required
    old desc_blocks = 1, new_desc_blocks = 1
    Performing an on-line resize of /dev/VolGroup/root to 1795072 (4k) blocks.
    The filesystem on /dev/VolGroup/root is now 1795072 blocks long.

    In case you notice above initially the command throws out an error as we were attempting to resize on a mounted partition but since our file system is ext4, we get an additional feature of online resizing where we do not need to unmount the file system for extending its size

    NOTE: We can resize the file system online if following conditions are met

    • 2.6.x kernel sereis
    • Must be formatted with ext3 fs or above

    Re-verify the change now

    # df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-root
                          6.8G  4.2G  2.3G  65% /
    tmpfs                 504M  260K  504M   1% /dev/shm
    /dev/sda1             194M   26M  158M  15% /boot

    So everything worked as expected.

    Scenario 2

    In case you already have enough space in your volume group for extending the root file system then you don't need to create a new physical volume.

    You can just follow the steps of extending LVM as shown above

    Related Articles
    How to remove logical and physical volume from Volume Group in Linux
    How to rename Logical Volume and Volume Group in Linux

    Follow the below links for more tutorials

    Configure Red Hat Cluster using VMware, Quorum Disk, GFS2, Openfiler
    Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
    How to configure Samba 4 Secondary Domain Controller
    How to secure Apache web server in Linux using password (.htaccess)
    How to register Red Hat Linux with RHN (Red Hat Network )
    Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
    15 tips to enhance security of your Linux machine
    Why is Linux more secure than windows and any other OS
    What is the difference between "su" and "su -" in Linux?
    What is kernel-PAE in Linux?
    What is swappiness and how do we change its value?
    How to log iptables messages in different log file
    What are the s and k scripts in the etc rcx.d directories
    How to check all the currently running services in Linux
    How to auto start service after reboot in Linux
    What is virtual memory, paging and swap space?

    10 comments:

    1. do not use this, i had followed everything, after restarting i had this error:
      unable to resume vg group centos rescue mode install exited abnormally. DO NOT USE IT.

      thanks

      ReplyDelete
      Replies
      1. I personally verify all the steps on my machine prior posting it on my website.

        Can you please brief me out about your scenario to help me figure out what exactly went wrong?

        Delete
    2. It works perfectly. I have searched several tutorials over the Internet about how to do this and this is the best one for me. It is clearly explained and you shall not have any problem following the steps. Highlighting the options in blue helped a lot. Just one question: what we would have to do if we had to do it in a multipath disk such as a LUN mapped in FC?

      ReplyDelete
    3. In case of multipath also I use the same process, While using SAN either the existing LUN is extended or a new LUN is added. In either case the storage will be mapped on your machine on existing partition or a new partition will be created depending upon your action.

      Further which you can use the same process of extending your VG and LV

      ReplyDelete
    4. thank you for this article
      Perfect and very understandable
      thank 's

      ReplyDelete
    5. Thank you for this article. Good tutorial!

      ReplyDelete
    6. Thanks for article , it's very helpful.. :)

      ReplyDelete
    7. Very nice, How would you do the same for software raid though?

      Thanks

      ReplyDelete
    8. Very good, How would you set this up for software raid

      ReplyDelete