facebooktwitterskypeRSS Feed
telephone: (920) 215-1842
facsimile: (866) 456-2115

How to Resize Partition without Data Loss – Part 1 of 3

email

This article is about resizing ext3 partitions without losing data. It shows how to shrink and enlarge existing ext3 partitions and how to merge two ext3 partitions. This can be quite useful if you do not use LVM and you realize that your existing partitioning does not meet your actual needs anymore.

There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I run all the commands in this tutorial as the root user, so make sure you’re logged in as root. If you are on a Ubuntu system, you can become root like this:

$ sudo su

I have tested this on a CentOS 5.4 system hosted on VMware Workstation 7, all files in one large disk (around 200 GB, device /dev/sda). The partitioning looks like this:

$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             4.8G  2.7G  1.9G  60% /
/dev/sda5              30G  396M   28G   2% /var
/dev/sda1              99M   28M   67M  30% /boot
tmpfs                 252M     0  252M   0% /dev/shm

The partition that is to be resized must be unmounted when we do the resizing; obviously this is not possible if this is the partition that holds all important system files like in this example. Therefore we download a Live Linux-CD such as Knoppix from which we boot later on (if you have physical access to the system). If it is a remote system that you don’t have physical access to, you need a rescue system on that system (a lot of hosting companies offer dedicated servers with rescue systems nowadays) that you can boot into (instead of Knoppix), and this rescue system must have the following tools: fdisk, umount, fsck, tune2fs, e2fsck, resize2fs.

If the partition that you want to resize doesn’t hold any system files (such as /home partitions, partitions for backups, etc.), you don’t need a Knoppix Live-CD or a rescue system, because all steps can be run from the original system.

If you want to resize partitions on production systems, please back up your data before, because it is possible you lose all your data if you don’t calculate the size of your new partition correctly (especially when shrinking a partition)! You have been warned! Tutorials about backups can be found here: http://www.howtoforge.com/taxonomy_menu/1/34

I’m going to resize /dev/sda5 in this tutorial. If your partition is named differently, please replace /dev/sda5 with your own device (e.g. /dev/hda5, /dev/sdb3, etc.).

2 Shrinking An ext3 Partition

This chapter is about shrinking an ext3 partition. I want to shrink /dev/sda1 in this example. First we gather some details on our original system:

$ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2              4956316   2776896   1923588  60% /
/dev/sda5             30475068    405328  29126024   2% /var
/dev/sda1               101086     27873     67994  30% /boot
tmpfs                   257664         0    257664   0% /dev/shm
$ df -B 4k
Filesystem           4K-blocks      Used Available Use% Mounted on
/dev/sda2              1239079    694224    480897  60% /
/dev/sda5              7618767    101332   7281506   2% /var
/dev/sda1                25272      6969     16999  30% /boot
tmpfs                    64416         0     64416   0% /dev/shm
$ df -h
Filesystem           4K-blocks      Used Available Use% Mounted on
/dev/sda2             4.8G  2.7G  1.9G  60% /
/dev/sda5              30G  396M   28G   2% /var
/dev/sda1              99M   28M   67M  30% /boot
tmpfs                 252M     0  252M   0% /dev/shm
$ fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14         650     5116702+  83  Linux
/dev/sda3             651         781     1052257+  82  Linux swap / Solaris
/dev/sda4             782        4816    32411137+   5  Extended
/dev/sda5             782        4816    32411106   83  Linux
$ fdisk -s /dev/sda5
32411106

Then we shut down the system and boot into our Knoppix Live-CD (or your rescue system) (if the partition you want to resize doesn’t hold any system files, you can do everything from the original system; the steps are the same, just omit booting into Knoppix/your rescue system).

$ shutdown -r now

After Knoppix has booted, open a terminal and become root by running

$ su

/dev/sda1 should be unmounted by default, but you can run

$ umount /dev/sda5

to be sure.

Then run

$ fsck -n /dev/sda5

The output looks like this:

fsck 1.40-WIP (14-Nov-2006)
e2fsck 1.40-WIP (14-Nov-2006)
/dev/sda1: clean, 1247/7841280 files, 346968/7864320 blocks

Next we remove the journal from /dev/sda5, thus turning it into an ext2 partition:

$ tune2fs -O ^has_journal /dev/sda5

The output looks like this:

tune2fs 1.40-WIP (14-Nov-2006)

Then run

$ e2fsck -f /dev/sda1
e2fsck 1.40 (14-Nov-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda1: 1247/7841280 files (7.5% non-contiguous), 314166/7864320  blocks

Now we resize our file system with resize2fs. resize2fs can resize ext2 file systems, but not ext3 file systems, that’s why we had to turn /dev/sda5 to ext2. Currently, 30GB are used on /dev/sda1 (see the df -h output above), So it’s safe to shrink it from 30GB to about 10GB (if you make it smaller than 400MB, you will lose data!). Therefore we run

$ resize2fs /dev/sda5 10000M

The output is as follows:

resize2fs 1.40-WIP (14-Nov-2006)
Resizing the filesystem on /dev/sda5 to 2560000 (4k) blocks.
The filesystem on /dev/sda5 is now 2560000 blocks long.

Please take note of the amount of blocks (2560000) and their size (4k). We need that soon.

Now we delete our /dev/sda5 partition (don’t be afraid, no data will be lost) and create a new, smaller one (but still big enough to hold our resized file system!). We can do this with fdisk:

$ fdisk /dev/sda

(Yes, it’s /dev/sda, not /dev/sda1.)

$ fdisk /dev/sda
The number of cylinders for this disk is set to 26108.
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)
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)

Command (m for help):

Now we delete partition no. 1 (/dev/sda5):

Command (m for help): d
Partition number (1-5): 5

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

Command (m for help): n
 Command action
 l   logical (5 or over)
 p   primary partition (1-4)
 p
 Partition number (1-4): 1

Now comes the crucial part – we are asked about the size of the new partition. The first cylinder is no problem, it is the one from the fdisk -l output at the beginning of this chapter (1).

First cylinder (1-4816, default 1): 1

But we don’t have a value for the last cylinder of our new partition. Fortunately, we can specify the size in kilobytes (K), so we calculate the size like this:

We multiply the amount of blocks from the resize2fs output (2560000) by the size of a block (4k), and to be sure the partition is big enough, we add 3 to 5% to it (3% was enough for me, but if you want to go sure take 5%):

2560000* 4k * 1.03 = 10547200k

So we prepend that value with a + sign and replace the small k with a capital one (K) and enter it:

Last cylinder or +size or +sizeM or +sizeK (1-1247,  default 1247): +10547200K

Our original /dev/sda5 did not have a bootable flag (see the fdisk -l output from the beginning of this chapter), so we don’t need add it to our new /dev/sda5 partition:

Command (m for help): a
Partition number (1-5): 5

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 we reboot the system, and again we boot into our Knoppix system (rescue system; original system if resized partition doesn’t hold system files):

$ shutdown -r now

Become root again (on Knoppix run)

$ su

and then run this:

$ fsck -n /dev/sda1

The output should look like this:

fsck 1.40-WIP (14-Nov-2006)
e2fsck 1.40-WIP (14-Nov-2006)
/dev/sda1: clean, 1247/2581088 files, 147413/2560000 blocks

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

$ tune2fs -j /dev/sda5
tune2fs 1.40-WIP (14-Nov-2006)
Creating journal inode: done
This filesystem will be automatically checked every -1 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.

Now we are done. Shut down the system and boot into the original system:

$ shutdown -r now

If everything goes well, the original system will boot up, and no data has been lost. Now we can gather some details about our new partitioning and compare them with the information we collected at the beginning of this chapter:

$ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2              4956316   2776884   1923600  60% /
/dev/sda5              9916660    395612   9316248   5% /var
/dev/sda1               101086     27873     67994  30% /boot
tmpfs                   257664         0    257664   0% /dev/shm
$ df -B 4k
Filesystem           4K-blocks      Used Available Use% Mounted on
/dev/sda2              1239079    694221    480900  60% /
/dev/sda5              2479165     98903   2329062   5% /var
/dev/sda1                25272      6969     16999  30% /boot
tmpfs                    64416         0     64416   0% /dev/shm
$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             4.8G  2.7G  1.9G  60% /
/dev/sda5             9.5G  387M  8.9G   5% /var
/dev/sda1              99M   28M   67M  30% /boot
tmpfs                 252M     0  252M   0% /dev/shm
$ fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14         650     5116702+  83  Linux
/dev/sda3             651         781     1052257+  82  Linux swap / Solaris
/dev/sda4             782        2095    10554705    5  Extended
/dev/sda5             782        2095    10554673+  83  Linux
$ fdisk -s /dev/sda1
10554673

Related posts:

  1. How to Resize Partition without Data Loss – Part 2 of 3
  2. How to Resize Partitions without Data Loss – Part 3 of 3
  3. Adding New Hard Drives in Linux
  4. Using Encryption

Post a Comment

Do you have a question or comment about this post. Please share! Your email address will not be published. Required fields are marked *

*