Sepero
11-30-2006, 04:56 AM
:
(At the time of this writing, I had tried gparted and parted. They didn't work, and qtparted was (is?) still quite dangerous.)
I've been struggling with this for a couple hours now and I finally figured it out, Again. I say it like that because I figured out the math once before on resizing, but I never wrote it down (so I friggin forgot). I figured that this is as good a place to record my notes as any.
Anyway, here is my goal:
To resize (shrink) an ext3 partition from 50Gb to... as small as possible!
(These instructions should work for ext2 also.)
By the way: I am actually doing this to my system as I type this. Hopefully it will make for a more interesting HowTo. (Hopefully, I won't destroy my system. ;) )
First thing to do:
FORCE a file system check on the partition (Make sure you put YOUR correct partition!).
# fsck.ext2 -f /dev/hda3
After the filesystem check, it will print some info like this:
/dev/hda3: 25218/6099296 files (1.6% non-contiguous), 8860236/12199359 blocks
I should be able to shrink it to at least "8860236" (the second number from the right).
So, resize the filesystem:
# resize2fs /dev/hda3 8860236
Now run file system check again (force isn't needed unless you mount it):
# fsck.ext2 /dev/hda3
And I see something different:
/dev/hda3: clean, 25218/4431392 files, 8807906/8860236 blocks
Ah, now I can shrink even more, so we do it again:
# resize2fs /dev/hda3 8807906
To shrink your filesystem more, you might be able to repeat the above processes another time or two. Additionally, you might be able to save a little time by trying to resize with a smaller number on the first resize operation.
OK, FILESYSTEM HAS BEEN RESIZED. NOW TO FIND OUT HOW THE PARTITION SHOULD BE RESIZED.
Run file system check to see the size of our filesystem:
# fsck.ext2 /dev/hda3
e2fsck 1.38 (30-Jun-2005)
/dev/hda3: clean, 25218/4398688 files, 8806880/8806880 blocks
Ok, take the far right number "8806880". This is the current size of our filesystem in 4k units (4096 byte units). Multiply this by 4096 to give us the actual number of bytes used: 36072980480. Record this number for later use, it is exactly how many bytes the filesystem uses.
:
Your filesystem might Not be in 4K units! To be certain run this command:
tune2fs -l /dev/hda3 | grep "Block size"
It will output your units in bytes. If it shows:
Block size: 1024
-or-
Block size: 2048
Then your filesystem uses 1K or 2K block size. Be sure to make appropriate changes when following this HowTo.
Now run fdisk to see our current partitions
# fdisk -l /dev/hda
Disk /dev/hda: 100.0 GB, 100030242816 bytes
255 heads, 63 sectors/track, 12161 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 1 486 3903763+ 83 Linux
/dev/hda2 * 487 1220 5895855 83 Linux
/dev/hda3 1221 7295 48797437+ 83 Linux
Ok, now there are only 3 areas of useful information here for us. The "Start", "End", and "Units" information. I can see that my partition (hda3) starts at 1221 and ends at 7295. There is a bit of a catch here though, the partition INCLUDES 1221. So in counting, it is actually from 1220 to 7295. So to find out how big the current partition is, we subtract: 7295 - 1220. Giving us 6075.
The Units are 8225280 bytes each. If we multiply 6075 by 8225280, it gives us the size of our current partition in bytes 49968576000. We want to change this to the lowest possible number of bytes, but it Must be larger than our filesystem.
Now take the number from earlier (bytes used by the filesystem 36072980480). Divide this number by the Units used in fdisk (for me 8225280). And we get "4385.623404917522565". Ok, this will be the size (roughly) we need to change our partition to.
First we round it UP from the decimal to make sure there is room for our filesystem, changing it to 4386. Now we need to calculate: If the partition starts at 1220, and is 4386 long, then it must end at: 1220 + 4386 = 5606. So that's it, we 've done the math. My new partition must start at the same place as my old partition (1221), and it will now end at 5606.
:
In recap, for resizing the partition, the only things you really need to know are the "current partition start point" and the "new partition end point". The formula for finding the new partition end point for fdisk is this:
partition_start = 1221
partition_unit_size = 8225280
filesystem_blocks = 8806880
filesystem_block_size = 4096
filesystem_bytes = filesystem_blocks * filesystem_block_size
required_partition_units = rounded_up( filesystem_bytes / partition_unit_size )
new_partition_end = required_partition_units + partition_start - 1When using fdisk, you will keep 'partition_start' the same, and 'new_partition_end' will be the new ending point.
The filesystem has been resized. The new partition end location is known. It is now time to take out the scalpel and recarve the harddrive. :)
CAUTION: Make BACKUPS. Partition resizing can potentially erase large parts (or all) of your system. Be careful!
I will walk you through how I ran fdisk on my system.
I start up fdisk with hda (not hda3), like so:# fdisk /dev/hda
(NOTE: You may enter "q" before step 11 to have fdisk forget your changes and not write them to disk.)
1. Press "m" then enter, to see my options.
2. Enter "p" to see my current partitions.
3. Enter "d" to delete a partition.
4. It asks for the partition number. I enter "3".
5. Enter "n" to create a new partition.
6. Enter "p" to create a primary (hda1-hda4) partition.
7. Enter "3" to (re)create the partition I deleted (hda3).
8. It asks me for the first cylinder. I enter "1221". Same as the original partition!
9. It asks me for the last cylinder. I enter "5606". This is the new End calculated earlier.
10. Enter "p" to make sure everything looks correct.
11. Enter "w" to write the modified partition table.
Reboot (or use the program "partprobe") so that the Kernel (Linux) can read your new partitions.
After running partprobe. I ran "fsck.ext3/dev/hda3", just to make sure everything was good. After mounting, I now see a 34Gb partition where a 50Gb partition used to be.
# df -h /dev/hda3
Filesystem Size Used Avail Use% Mounted on
/dev/hda3 34G 34G 0 100% /home/
/me takes a deep sigh of relief, then takes a bow...
(At the time of this writing, I had tried gparted and parted. They didn't work, and qtparted was (is?) still quite dangerous.)
I've been struggling with this for a couple hours now and I finally figured it out, Again. I say it like that because I figured out the math once before on resizing, but I never wrote it down (so I friggin forgot). I figured that this is as good a place to record my notes as any.
Anyway, here is my goal:
To resize (shrink) an ext3 partition from 50Gb to... as small as possible!
(These instructions should work for ext2 also.)
By the way: I am actually doing this to my system as I type this. Hopefully it will make for a more interesting HowTo. (Hopefully, I won't destroy my system. ;) )
First thing to do:
FORCE a file system check on the partition (Make sure you put YOUR correct partition!).
# fsck.ext2 -f /dev/hda3
After the filesystem check, it will print some info like this:
/dev/hda3: 25218/6099296 files (1.6% non-contiguous), 8860236/12199359 blocks
I should be able to shrink it to at least "8860236" (the second number from the right).
So, resize the filesystem:
# resize2fs /dev/hda3 8860236
Now run file system check again (force isn't needed unless you mount it):
# fsck.ext2 /dev/hda3
And I see something different:
/dev/hda3: clean, 25218/4431392 files, 8807906/8860236 blocks
Ah, now I can shrink even more, so we do it again:
# resize2fs /dev/hda3 8807906
To shrink your filesystem more, you might be able to repeat the above processes another time or two. Additionally, you might be able to save a little time by trying to resize with a smaller number on the first resize operation.
OK, FILESYSTEM HAS BEEN RESIZED. NOW TO FIND OUT HOW THE PARTITION SHOULD BE RESIZED.
Run file system check to see the size of our filesystem:
# fsck.ext2 /dev/hda3
e2fsck 1.38 (30-Jun-2005)
/dev/hda3: clean, 25218/4398688 files, 8806880/8806880 blocks
Ok, take the far right number "8806880". This is the current size of our filesystem in 4k units (4096 byte units). Multiply this by 4096 to give us the actual number of bytes used: 36072980480. Record this number for later use, it is exactly how many bytes the filesystem uses.
:
Your filesystem might Not be in 4K units! To be certain run this command:
tune2fs -l /dev/hda3 | grep "Block size"
It will output your units in bytes. If it shows:
Block size: 1024
-or-
Block size: 2048
Then your filesystem uses 1K or 2K block size. Be sure to make appropriate changes when following this HowTo.
Now run fdisk to see our current partitions
# fdisk -l /dev/hda
Disk /dev/hda: 100.0 GB, 100030242816 bytes
255 heads, 63 sectors/track, 12161 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 1 486 3903763+ 83 Linux
/dev/hda2 * 487 1220 5895855 83 Linux
/dev/hda3 1221 7295 48797437+ 83 Linux
Ok, now there are only 3 areas of useful information here for us. The "Start", "End", and "Units" information. I can see that my partition (hda3) starts at 1221 and ends at 7295. There is a bit of a catch here though, the partition INCLUDES 1221. So in counting, it is actually from 1220 to 7295. So to find out how big the current partition is, we subtract: 7295 - 1220. Giving us 6075.
The Units are 8225280 bytes each. If we multiply 6075 by 8225280, it gives us the size of our current partition in bytes 49968576000. We want to change this to the lowest possible number of bytes, but it Must be larger than our filesystem.
Now take the number from earlier (bytes used by the filesystem 36072980480). Divide this number by the Units used in fdisk (for me 8225280). And we get "4385.623404917522565". Ok, this will be the size (roughly) we need to change our partition to.
First we round it UP from the decimal to make sure there is room for our filesystem, changing it to 4386. Now we need to calculate: If the partition starts at 1220, and is 4386 long, then it must end at: 1220 + 4386 = 5606. So that's it, we 've done the math. My new partition must start at the same place as my old partition (1221), and it will now end at 5606.
:
In recap, for resizing the partition, the only things you really need to know are the "current partition start point" and the "new partition end point". The formula for finding the new partition end point for fdisk is this:
partition_start = 1221
partition_unit_size = 8225280
filesystem_blocks = 8806880
filesystem_block_size = 4096
filesystem_bytes = filesystem_blocks * filesystem_block_size
required_partition_units = rounded_up( filesystem_bytes / partition_unit_size )
new_partition_end = required_partition_units + partition_start - 1When using fdisk, you will keep 'partition_start' the same, and 'new_partition_end' will be the new ending point.
The filesystem has been resized. The new partition end location is known. It is now time to take out the scalpel and recarve the harddrive. :)
CAUTION: Make BACKUPS. Partition resizing can potentially erase large parts (or all) of your system. Be careful!
I will walk you through how I ran fdisk on my system.
I start up fdisk with hda (not hda3), like so:# fdisk /dev/hda
(NOTE: You may enter "q" before step 11 to have fdisk forget your changes and not write them to disk.)
1. Press "m" then enter, to see my options.
2. Enter "p" to see my current partitions.
3. Enter "d" to delete a partition.
4. It asks for the partition number. I enter "3".
5. Enter "n" to create a new partition.
6. Enter "p" to create a primary (hda1-hda4) partition.
7. Enter "3" to (re)create the partition I deleted (hda3).
8. It asks me for the first cylinder. I enter "1221". Same as the original partition!
9. It asks me for the last cylinder. I enter "5606". This is the new End calculated earlier.
10. Enter "p" to make sure everything looks correct.
11. Enter "w" to write the modified partition table.
Reboot (or use the program "partprobe") so that the Kernel (Linux) can read your new partitions.
After running partprobe. I ran "fsck.ext3/dev/hda3", just to make sure everything was good. After mounting, I now see a 34Gb partition where a 50Gb partition used to be.
# df -h /dev/hda3
Filesystem Size Used Avail Use% Mounted on
/dev/hda3 34G 34G 0 100% /home/
/me takes a deep sigh of relief, then takes a bow...