Expand filesystem of linux vm

So we have a test server running ubuntu 12.04. We realized we will be needing more space on this server for testing different versions of our application.

First I have allocated more disk space from VM properties of virtual machine. Increased size under Disk provisioning section.

root@dagobah:~# fdisk -l /dev/sda
 
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: 0x000556a5
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 25165823 12581888 83 Linux
/dev/sda2 25167870 41940991 8386561 5 Extended
/dev/sda5 25167872 41940991 8386560 82 Linux swap / Solaris

To delete and move swap we need to turn swap off

root@dagobah:~# free -m
             total       used       free     shared    buffers     cached
Mem:          7986        649       7337          0         30        124
-/+ buffers/cache:        493       7492
Swap:         8189          0       8189
root@dagobah:~# swapoff -a
root@dagobah:~# free -m
             total       used       free     shared    buffers     cached
Mem:          7986        643       7343          0         30        124
-/+ buffers/cache:        487       7498
Swap:            0          0          0
root@dagobah:~#

Using fdisk for editing partition table. Removed old partition tables and created as I needed after that edited type for swap space and reboot server.

root@dagobah:~# fdisk /dev/sda
 
Command (m for help): p
 
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: 0x000556a5
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    25165823    12581888   83  Linux
/dev/sda2        25167870    41940991     8386561    5  Extended
/dev/sda5        25167872    41940991     8386560   82  Linux swap / Solaris
 
Command (m for help): d
Partition number (1-5): 1
 
Command (m for help): d
Partition number (1-5): 2
 
Command (m for help): p
 
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: 0x000556a5
 
   Device Boot      Start         End      Blocks   Id  System
 
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-83886079, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079): +38G
 
Command (m for help): p
 
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: 0x000556a5
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    79693823    39845888   83  Linux
 
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 (79693824-83886079, default 79693824): 
Using default value 79693824
Last sector, +sectors or +size{K,M,G} (79693824-83886079, default 83886079): 
Using default value 83886079
 
Command (m for help): p
 
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: 0x000556a5
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    79693823    39845888   83  Linux
/dev/sda2        79693824    83886079     2096128   83  Linux
 
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)
 
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.
root@dagobah:~# shutdown -r now

After reboot check disk usage and get OS to use new UUID for swap. I filled /dev/sda2 with zeros. Activate swap and then resize the file system.

root@dagobah:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        12G  3.2G  8.1G  29% /
udev            3.9G   12K  3.9G   1% /dev
tmpfs           1.6G  1.4M  1.6G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            3.9G     0  3.9G   0% /run/shm
root@dagobah:~# awk '/swap/ { print $1 }' /etc/fstab
#
UUID=cfe5c5f6-933a-4e2d-92dc-355d7949cbc7
root@dagobah:~# swapoff -a
root@dagobah:~# free -m
             total       used       free     shared    buffers     cached
Mem:          7986       2487       5498          0         30        123
-/+ buffers/cache:       2333       5653
Swap:            0          0          0
root@dagobah:~# dd if=/dev/sda
sda   sda1  sda2  
root@dagobah:~# dd if=/dev/zero of=/dev/sda2
dd: writing to `/dev/sda2': No space left on device
4192257+0 records in
4192256+0 records out
2146435072 bytes (2.1 GB) copied, 273.709 s, 7.8 MB/s
root@dagobah:~# mkswap -U cfe5c5f6-933a-4e2d-92dc-355d7949cbc7 /dev/sda2
Setting up swapspace version 1, size = 2096124 KiB
no label, UUID=cfe5c5f6-933a-4e2d-92dc-355d7949cbc7
root@dagobah:~# swapon -a
root@dagobah:~# free -m
             total       used       free     shared    buffers     cached
Mem:          7986       2661       5324          0         31        145
-/+ buffers/cache:       2485       5501
Swap:         2046          0       2046
root@dagobah:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        12G  3.2G  8.1G  29% /
udev            3.9G   12K  3.9G   1% /dev
tmpfs           1.6G  1.4M  1.6G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            3.9G     0  3.9G   0% /run/shm
root@dagobah:~# resize2fs /dev/sda
sda   sda1  sda2  
root@dagobah:~# resize2fs /dev/sda1
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/sda1 to 9961472 (4k) blocks.
The filesystem on /dev/sda1 is now 9961472 blocks long.
 
root@dagobah:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        38G  3.2G   33G   9% /
udev            3.9G   12K  3.9G   1% /dev
tmpfs           1.6G  1.4M  1.6G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            3.9G     0  3.9G   0% /run/shm