Skip to content

Instantly share code, notes, and snippets.

@mylesmegyesi
Last active August 29, 2015 14:09
Show Gist options
  • Save mylesmegyesi/ca622bb33e0680a5b5dd to your computer and use it in GitHub Desktop.
Save mylesmegyesi/ca622bb33e0680a5b5dd to your computer and use it in GitHub Desktop.
Increasing virtual machine hard disk size
  1. Shutdown vm and resize hard disk with VMWare to the desired size.

  2. Figure out which partition to resize:

    1. sudo lvdisplay

       % sudo lvdisplay
       --- Logical volume ---
       LV Path                /dev/packer-ubuntu-14-vg/root
       LV Name                root
       VG Name                packer-ubuntu-14-vg
       LV UUID                JdYIsh-wJZx-pOla-jpw3-T1Fm-XY21-fQCTPT
       LV Write Access        read/write
       LV Creation host, time packer-ubuntu-14, 2014-08-14 09:34:29 -0500
       LV Status              available
       # open                 1
       LV Size                7.43 GiB
       Current LE             1901
       Segments               1
       Allocation             inherit
       Read ahead sectors     auto
       - currently set to     256
       Block device           252:0
       
       --- Logical volume ---
       LV Path                /dev/packer-ubuntu-14-vg/swap_1
       LV Name                swap_1
       VG Name                packer-ubuntu-14-vg
       LV UUID                1RkA1C-Dadv-t2t6-omX6-iqAu-YW6q-yxLUq6
       LV Write Access        read/write
       LV Creation host, time packer-ubuntu-14, 2014-08-14 09:34:29 -0500
       LV Status              available
       # open                 2
       LV Size                12.00 GiB
       Current LE             3072
       Segments               1
       Allocation             inherit
       Read ahead sectors     auto
       - currently set to     256
       Block device           252:1
      
    2. Find the non swap volume and note VG Name value. - packer-ubuntu-14-vg

    3. sudo pvdisplay

       % sudo pvdisplay
       --- Physical volume ---
       PV Name               /dev/sda5
       VG Name               packer-ubuntu-14-vg
       PV Size               19.43 GiB / not usable 2.00 MiB
       Allocatable           yes (but full)
       PE Size               4.00 MiB
       Total PE              4973
       Free PE               0
       Allocated PE          4973
       PV UUID               jtpnCo-wiA7-SQK9-9arL-hAYs-dh7r-8IaDE0
      
    4. Find the VG Name from the previous step and note the PV Name -- /dev/sda5 this means partition 5 of volume /dev/sda

  3. Resize the partition:

    This entails first deleting the extended volume which owns the logical volume, recreating it, then recreating the logical volume.

     % sudo fdisk /dev/sda
     
     Command (m for help): p
     
     Disk /dev/sda: 53.7 GB, 53687091200 bytes
     255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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: 0x000dedfa
     
        Device Boot      Start         End      Blocks   Id  System
     /dev/sda1   *        2048      499711      248832   83  Linux
     /dev/sda2          499712   104857599    52178944    5  Extended
     /dev/sda5          501760   104857599    52177920   8e  Linux LVM
     
     
     Command (m for help): d
     Partition number (1-5): 2
     
     Command (m for help): p
     
     Disk /dev/sda: 53.7 GB, 53687091200 bytes
     255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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: 0x000dedfa
     
        Device Boot      Start         End      Blocks   Id  System
     /dev/sda1   *        2048      499711      248832   83  Linux
     
     Command (m for help): n
     Partition type:
        p   primary (1 primary, 0 extended, 3 free)
        e   extended
     Select (default p): e
     Partition number (1-4, default 2): 2
     First sector (499712-104857599, default 499712): 
     Using default value 499712
     Last sector, +sectors or +size{K,M,G} (499712-104857599, default 104857599): 
     Using default value 104857599
     
     Command (m for help): p
     
     Disk /dev/sda: 53.7 GB, 53687091200 bytes
     255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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: 0x000dedfa
     
        Device Boot      Start         End      Blocks   Id  System
     /dev/sda1   *        2048      499711      248832   83  Linux
     /dev/sda2          499712   104857599    52178944    5  Extended
     
     Command (m for help): n
     Partition type:
        p   primary (1 primary, 1 extended, 2 free)
        l   logical (numbered from 5)
     Select (default p): l
     Adding logical partition 5
     First sector (501760-104857599, default 501760): 
     Using default value 501760
     Last sector, +sectors or +size{K,M,G} (501760-104857599, default 104857599): 
     Using default value 104857599
     
     Command (m for help): p
     
     Disk /dev/sda: 53.7 GB, 53687091200 bytes
     255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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: 0x000dedfa
     
        Device Boot      Start         End      Blocks   Id  System
     /dev/sda1   *        2048      499711      248832   83  Linux
     /dev/sda2          499712   104857599    52178944    5  Extended
     /dev/sda5          501760   104857599    52177920   83  Linux
     
     Command (m for help): t
     Partition number (1-5): 5
     Hex code (type L to list codes): 8e
     Changed system type of partition 5 to 8e (Linux LVM)
     
     Command (m for help): p
     
     Disk /dev/sda: 53.7 GB, 53687091200 bytes
     255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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: 0x000dedfa
     
        Device Boot      Start         End      Blocks   Id  System
     /dev/sda1   *        2048      499711      248832   83  Linux
     /dev/sda2          499712   104857599    52178944    5  Extended
     /dev/sda5          501760   104857599    52177920   8e  Linux LVM
    
  4. Reboot

  5. Resize the physical volume:

     sudo pvresize /dev/sda5
    
  6. Resize logical volume:

     sudo lvresize -l +100%FREE /dev/packer-ubuntu-14-vg/root
    
  7. Resize the filesystem on the logical volume:

     sudo resize2fs /dev/packer-ubuntu-14-vg/root
    
  8. See your handy work:

     sudo df -h
    

Legend

List disks:

sudo fdisk -l

Show physical volumes:

sudo pvdisplay

Show volume groups:

sudo vgdisplay

Show logical volumes:

sudo lvdisplay

To show disk usage by mount:

sudo df -h

To show disk usage by directory:

du -hs /path/to/directory

-h is to get the numbers "human readable", e.g. get 140M instead of 143260 (size in KBytes)
-s is for summary (otherwise you'll get not only the size of the folder but also for everything in the folder separately)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment