Thursday, February 26, 2015

Increase/Extend Disk Space of Linux VM without using LVM without losing data


Scenario: We have Linux VM in which administrator have created physical partition as below  


root@localhost ~]# fdisk -l


Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000273b1


  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1306     9972736   83  Linux
/dev/sda3            1306        1958     5241875   83  Linux


[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       9.3G  3.7G  5.2G  42% /
tmpfs           931M     0  931M   0% /dev/shm
/dev/sda1       477M   25M  427M   6% /boot
/dev/sda3       5.0G  4.2G  545M  89% /PROD


Now the /PROD(/dev/sda3) is being utlized upto 89% and the users need more space in this partition.


Solution:


Step 1 : Shutdown Linux VM
Step 2 : Take a snapshot of Linux VM
Step 3 : Increase/Extend the required diskspace of linux VM thru Vmware vCenter Portal.
Step 4 : Unmount the /PROD(/dev/sda3) partition  
umount /dev/sda3
  
verify the Partition has been unmounted or not
df -h


Then run
fsck -n /dev/sda3
[root@localhost ~]# fsck -n /dev/sda3
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sda3: clean, 16/327680 files, 1105539/1310468 blocks
[root@localhost ~]#
Next we remove the journaling from /dev/sda3 so turning it into ext2.
[root@localhost ~]# tune2fs -O ^has_journal /dev/sda3
tune2fs 1.41.12 (17-May-2010)
[root@localhost ~]#
  
Now we use fdisk to delete our current /dev/sda3 partition and create a bigger one (don't be afraid, no data will be lost):
fdisk /dev/sda
(Yes, it's /dev/sda, not /dev/sda3.)
The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
  (e.g., DOS FDISK, OS/2 FDISK)
Type m to get a list of all commands:
Command (m for help): m
Command action
  a   toggle a bootable flag
  b   edit bsd disklabel
  c   toggle the dos compatibility flag
  d   delete a partition
  l   list known partition types
  m   print this menu
  n   add a new partition
  o   create a new empty DOS partition table
  p   print the partition table
  q   quit without saving changes
  s   create a new empty Sun disklabel
  t   change a partition's system id
  u   change display/entry units
  v   verify the partition table
  w   write table to disk and exit
  x   extra functionality (experts only)
Let's print out the partition table:
Command (m for help): p


Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000273b1


  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1306     9972736   83  Linux
/dev/sda3            1306        1958     5241875   83  Linux


Now we delete partition no. 3 (/dev/sda3):
Command (m for help): d
Partition number (1-4): 3
Next we create a new /dev/sda3 partition. It was a primary partition before, so we choose p again, and again it is our partition no. 3:
Command (m for help): n
Command action
  l   logical (5 or over)
  p   primary partition (1-4)
p
Partition number (1-4): 3
Now we must specify the first and the last cylinder of our new /dev/sda3 partition. We know the first cylinder, can take it from the fdisk -l output before:
First cylinder (1-1305, default 1): 1
Now fdisk tells us the highest possible cylinder of our new partition (1247 in this example), so we simply enter this number:
Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): 1247
Let's print out our new partition table:
Command (m for help): p


Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


  Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        1247    10016496   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
Our original /dev/sda1 had the bootable flag (see the fdisk -l output from the beginning of this chapter), so we must add it to our new /dev/sda1 again:
Command (m for help): a
Partition number (1-5): 1
Now let's write our new partition table and exit fdisk:
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.
Syncing disks.
Now reread the partition table with partprob command
[root@localhost ~]# partprobe /dev/sda
Then run
e2fsck -f /dev/sda3
Now we must resize the file system in our /dev/sda3 partition. If we don't specify a size for the resize2fs command, it will assume the biggest possible size so we don't have to calculate. So we run
resize2fs /dev/sda3
The output looks like this:
resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda3 to 2504124 (4k) blocks.
The filesystem on /dev/sda3 is now 2504124 blocks long.
Next we run
fsck -n /dev/sda3
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/1254176 files, 1062544/2504124 blocks
and create the journal on /dev/sda3, thus turning it into an ext3 partition again:
tune2fs -j /dev/sda3
tune2fs 1.38 (30-Jun-2005)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.


Now we are done.

Step 5. Verification
mount the partition and Verify the partition with below commands
mount /dev/sda3 /PROD or mount -a
[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       9.3G  616M  8.2G   7% /
tmpfs           931M     0  931M   0% /dev/shm
/dev/sda1       477M   25M  427M   6% /boot
/dev/sda3       9.9G  7.2G  2.2G  77% /PROD