2010-01-06 23:41

Before doing anything: backup your sensible data!

To extend a partition and it’s filesystem, you have to:

  1. Extend the partition
  2. Extend the filesystem

To shrink a partition and it’s filesystem, you have to:

  1. Shrink the filesystem
  2. Shrink the partition

For an ext3 partition, simply use parted:

parted /dev/sdx
print
resize N

Parted doesn’t support ext4 (yet?). For an ext4 partition or if parted refuses to resize your ext3 partition (Error: File system has an incompatible feature enabled.), use resize2fs:

To extend:

cfdisk /dev/sdx
# delete the partition and create it again with the desired size
resize2fs /dev/sdxY

Without giving any size, resize2fs extends the filesystem to the partition’s size.

To shrink, it’s almost as simple:

# example if you want a 10G partition
# resize filesystem with a size smaller than the desired size
resize2fs /dev/sdxY 9G
cfdisk /dev/sdx
# delete the partition and create it again with the desired size
# (a little bigger than the filesystem!!)
# then launch resize2fs again
resize2fs /dev/sdxY

Doing so we get the good partition size without loosing any space.

Notes:

  • If your partition is over LVM, you can use the lvresize or lvextend or lvreduce commands to resize the partition, instead of deleting/creating the partition with cfdisk.
  • The method also works for other filesystems like NTFS. For NTFS, you will use the ntfsresize command, or parted if it works.

Links :

2010-01-06 23:41 · Tags: , , , , , , , ,

11 Comments

  1. Thanks for the article, it shed some light much better than other posts I found on the net. I was able to resize my partition with no error ! I will be back for more…

    Reply

  2. dear sir,

    in ubuntu server, to reduce the ext4 filesytem size from 1T, i run the “resize2fs /dev/mapper/ispconfig-root 500G” and got the below output to the console:

    Filesystem at /dev/mapper/ispconfig-root is mounted on /; on-line resizing required
    On-line shrinking from 267809792 to 131072000 not supported.

    it is much appreciated if you can help to email the resolution to my email address qiubosu@gmail.com.

    thanks.

    Reply

  3. Dear David su, asking for a PM just means leaving others with your same problem in the dark. Since this is not a private matter (nothing confidential is passed on) wouldn’t it be better to let everyone know?

    What the system is saying is that you have to do the resize on an unmounted partition. Filesystem operations like resizing, checking and so on should always be done offline.

    You just have to keep a live cd at hand.

    Reply

  4. Will this LOOSE my data ? I need to resise an ext4 /home partition (/dev/sda9) to make space for something else but I don’t want to loose the data I have inside it.
    I can boot and go directly to console as root so I can unmount /home, but then how can I resize it without loosing data inside? For some reasons I can’t go to the gui with no /home so I can’t use gparted.

    Reply

  5. to Antonello,

    It should not loose your data. Of course, it’s *always* a smart idea to have a current backup before you start! (-;

    Rick

    Reply

  6. Tim Miller Dyck

    Hi, a note to check the starting sector before recreating the partition and ensure the new partition starts at the same sector. When I used cfdisk, it created the partition starting at sector 34. I had used fdisk to create the original partition and it created the partition starting at sector 2048. Starting the partition at a different disk location than before, of course, resulted in a partition with no valid file system. I used fdisk again to create the new (larger) partition and all was fine.

    Reply

  7. Hi Tim,
    Yes, you’re right : it’s better to use fdisk to be sure that the partition keeps the same start.

    Reply

  8. Pingback: Linux boot with broken partition | Question and Answer

  9. Pingback: Unix:How to extend CentOS 5 partition on VMware? – Unix Questions

  10. I just added additional 100gb to /dev/sda on vSphere client. How would you suggest I add the new space to /dev/sda2 shown below?

    # fdisk -l /dev/sda

    Disk /dev/sda: 221.2 GB, 113816633344 bytes
    255 heads, 63 sectors/track, 13837 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: 0x0009c915

    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 26 204800 83 Linux
    Partition 1 does not end on cylinder boundary.
    /dev/sda2 26 5690 45493248 83 Linux
    /dev/sda3 5690 9229 28432384 83 Linux
    /dev/sda4 9229 13838 37017600 5 Extended
    /dev/sda5 9230 10645 11372544 83 Linux
    /dev/sda6 10646 11495 6823936 83 Linux
    /dev/sda7 11495 12203 5686272 83 Linux
    /dev/sda8 12203 13272 8584192 82 Linux swap / Solaris
    /dev/sda9 13272 13555 2274304 83 Linux
    /dev/sda10 13555 13838 2270208 83 Linux

    Reply

  11. Shrinking ext4 worked exactly as described, using resize2fs, cfdisk, resize2fs again.

    Thanks a lot for posting, cheers!

    Reply

Leave a Reply to Antonello Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>