How To Resize ext3 Partitions Without Losing Data
How To Resize ext3 Partitions Without Losing DataVersion 1.0 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 Ubuntu Edgy Eft desktop system that has all files in one large partition (around 10 GB, device /dev/sda1). The partitioning looks like this: df -h Filesystem Size Used Avail Use% Mounted on 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/sda1 in this tutorial. If your partition is named differently, please replace /dev/sda1 with your own device (e.g. /dev/hda5, /dev/sdb3, etc.).
2 Shrinking An ext3 PartitionThis 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 df -B 4k Filesystem 4K-blocks Used Available Use% Mounted on df -h Filesystem Size Used Avail Use% Mounted on fdisk -l
fdisk -s /dev/sda1 10016496 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/sda1 to go sure. Then run fsck -n /dev/sda1 The output looks like this: fsck 1.38 (30-Jun-2005) Next we remove the journal from /dev/sda1, thus turning it into an ext2 partition: tune2fs -O ^has_journal /dev/sda1 The output looks like this: tune2fs 1.38 (30-Jun-2005) Then run e2fsck -f /dev/sda1 e2fsck 1.38 (30-Jun-2005) 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/sda1 to ext2. Currently, 4.1GB are used on /dev/sda1 (see the df -h output above), So it's safe to shrink it from 10GB to about 6GB (if you make it smaller than 4.1GB, you will lose data!). Therefore we run resize2fs /dev/sda1 6000M The output is as follows: resize2fs 1.38 (30-Jun-2005) Please take note of the amount of blocks (1536000) and their size (4k). We need that soon. Now we delete our /dev/sda1 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.) The number of cylinders for this disk is set to 1305. Type m to get a list of all commands: Command (m for help): m Now we delete partition no. 1 (/dev/sda1): Command (m for help): d Next we create a new /dev/sda1 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 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-1305, 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 (1536000) by the size of a block (4k), and to go 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%): 1536000 * 4k * 1.03 = 6328320k 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): +6328320K 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 Now let's write our new partition table and exit fdisk: Command (m for help): w Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 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.38 (30-Jun-2005) Then we create the journal on our new /dev/sda1, thus turning it into an ext3 partition again: tune2fs -j /dev/sda1 tune2fs 1.38 (30-Jun-2005) 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 df -B 4k Filesystem 4K-blocks Used Available Use% Mounted on df -h Filesystem Size Used Avail Use% Mounted on fdisk -l
fdisk -s /dev/sda1 6337611
|
Join the discussion.
www.seamlessenterprise.com
IP Convergence
Integrate your wireless and wireline networks.
Learn how from the experts at Sprint.
www.seamlessenterprise.com
Wireless & Wireline Integration
Thoughts, strategies and solutions: join the discussion
www.seamlessenterprise.com
Unified Communications 2009
Join the Discussion. Now.
www.seamlessenterprise.com
AT&T Synaptic Compute as a Service. Boost your power on demand.
Trial: IBM Cognos Express Reporting, Analysis & Planning
Recent comments
1 day 45 min ago
1 day 2 hours ago
1 day 3 hours ago
1 day 8 hours ago
1 day 15 hours ago
1 day 16 hours ago
1 day 21 hours ago
1 day 23 hours ago
2 days 3 hours ago
2 days 8 hours ago