How to resize a linux filesytem without Data Loss

Today i’m going to show you how to resize a filesystem without data loss.This can be quite useful, if you do not use the LVM technology.

Note:- Before doing this in live enviornment, kindly test it in your testbox
Here i’m using my susebox .
Tools Used :- fdisk, umount, fsck, tune2fs, e2fsck, resize2fs.

I’m going to resize the /dev/sdb1 in this tutorial. ( if your partition is named diferently, kindly replace it) Read more

a

s

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In the above screenshot, you can able to see the harddisk size, mount point etc.

So /dev/sdb harddisk is of 16GB size

/dev/sdb1 is 8.7 GB . and it’s mounted to /testdir directory.

To extend the size of /dev/sdb1 , the filesystem should be unmounted. Here /dev/sdb1 is mounted on /testdir, run the umount command to unmount the same.

s

 

 

 

 

 

 

Now run fsck -n /dev/sdb1 for checking the filesystem.

a

 

 

 

 

Then we need to remove the journal from the filesystem , this will bring ext2 filesystem to /dev/sdb1

s

 

 

 

 

Now run e2fsck to check the filesystem,

t

 

 

 

 

 

 

 

Now we can resize the filesystem using resize2fs tool.

Resize2fs can resize ext2 file systems, but not ext3 file systems, that’s why we changed /dev/sdb1 to ext2 by removing journel from it.

8.7GB is the size of /dev/sdb1 (check the above df -h screenshot). Now we are going to extend  the /dev/sdb1 from 8.7 GB to 15 GB .

Note :- In case of shrinking data , don’t make it smaller than 1.9GB (space used as per df-kh output), you will lose data!

 


So we are going to run resize2fs /dev/sdb1 15000M

dd

 

 

 

 

 

Kindly note the number of blocks (384000) and their size (4k). We need it later..

Now we are going to delete the partition /dev/sdb1 using fdisk tool

” Dont worry about your data 🙂 ”

sss

 

 

 

 

 

 

Next we will create a new /dev/sdb1 partition. It was a primary partition earlier, so  choose p again, and again it is our partition no. 1:

sss

 

 

 

 

 

 

Then comes the important part,

Size of the partition:-

The  First Cylinder is not an issue , it’s default (1). But in the case of  Last Cylinder , we don’t have the value , So we can specify the size in kilobyes (K) .

We can calculate the size like this ,

Last Cylinder Size = The amount of blocks from the resize2fs output *  the size of block

// amount of blocks = 384000 .

// Size of block = 4k

Last Cylinder Size =    384000 * 4k =  1536000K

Apply this with +1536000K and write the partition using ‘w’

dddddThen run fsck -n /dev/sdb1.

 

Then  create the journal on our new /dev/sdb1, thus turning it into an ext3 partition again:

www

 

 

 

 

 

 

Check the size of filesystem,

q

 

 

 

 

Check with df -h and you are good to mount the filesystem.

Capture

 

 

 

 

 

 

Hope this helped.