Share, , Google Plus, Pinterest,

Print

Posted in:

Resize Partition and Filesystem with fdisk & resize2fs

There was a question in my post on “Linux partitioning with fdisk on CentOS 6“. Our reader asked if it is possible to extend an existing linux non LVM partition without loosing it’s data … here is a post on how to resize partition and filesystem with fdisk and resize2fs.

If you want to increase your root partition size you must follow my guide on “Increase Root Partition Size – LVM – CentOS“!

IMPORTANT: In order to resize partition (extend), enough disk space must be available! We can not extend a partition if there are no free sectors/cylinders at the end of the partition to extend!

PLEASE BACKUP YOUR DATA BEFORE RESIZING ANY PARTITION! GeekPeek.Net is not responsible for any data loss!

Resize Partition and Filesystem with fdisk and resize2fs
Resize Partition and Filesystem with fdisk and resize2fs

 

Let’s Resize Partition and Filesystem with fdisk and resize2fs!

Our system has two disks:

  • /dev/sda – 16GB system disk with LVM partitions (root and swap)
  • /dev/sdb – 1GB clean disk for tutorial purpose

For the start of this tutorial tutorial we have created one partition on /dev/sdb disk. Partition size (/dev/sdb1) is 500MB. We created an ext4 filesystem on this partition and put some dummy data on it. We will be modifying our partitions with fdisk and filesystem with resize2fs.

For more information on how to create and modify partitions with fdisk read “Linux partitioning with fdisk on CentOS 6“.

Extend Partition and Filesystem

Before we start, let’s check the current disk configuration. Geekpeek mount point is the partition we want to extend. We can see that the partition has 494MB of available space and 401MB is already used by dummy data. We want to extend the partition to 1GB:

[root@foo1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_foo-LogVol01
                       13G  4.6G  7.7G  38% /
tmpfs                 376M     0  376M   0% /dev/shm
/dev/sda1             485M  105M  355M  23% /boot
/dev/sdb1             494M  402M   67M  86% /geekpeek

1. Unmount the partition

[root@foo1 ~]# umount /geekpeek/
[root@foo1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_foo-LogVol01
                       13G  4.6G  7.7G  38% /
tmpfs                 376M     0  376M   0% /dev/shm
/dev/sda1             485M  105M  355M  23% /boot

2. Delete the partition

[root@foo1 ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2dbb9f13

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

3. Create a new (larger) partition

[root@foo1 ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-2097151, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): 
Using default value 2097151

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2dbb9f13

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2097151     1047552   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

 

We are able to mount the partition at this point, but the filesystem on this partition is only 500MB large. We need to resize the filesystem using resize2fs command in the following steps.

4. Run fsck on your filesystem

[root@foo1 ~]# e2fsck -f /dev/sdb1
e2fsck 1.41.12 (17-May-2010)
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/sdb1: 16/130560 files (0.0% non-contiguous), 426988/522080 blocks

5. Resize your filesystem with resize2fs

[root@foo1 ~]# resize2fs /dev/sdb1 
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdb1 to 1044192 (1k) blocks.
The filesystem on /dev/sdb1 is now 1044192 blocks long.

6. Re-mount extended partition

[root@foo1 ~]# mount /dev/sdb1 /geekpeek/
[root@foo1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_foo-LogVol01
                       13G  4.6G  7.7G  38% /
tmpfs                 376M     0  376M   0% /dev/shm
/dev/sda1             485M  105M  355M  23% /boot
/dev/sdb1             988M  402M  536M  43% /geekpeek

Voila! The partition was successfully extended, as we can see all of the data survived! 🙂 Always remember to resize the filesystem with resize2fs.

Reduce a Partition and Filesystem

Reviewing the current disk configuration: Geekpeek mount point is the partition we want to reduce. We can see that the partition has 988MB of available space and 324MB is used by dummy data. We want to reduce the partition to 400MB without loosing data:

[root@foo1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_foo-LogVol01
                       13G  4.6G  7.7G  38% /
tmpfs                 376M     0  376M   0% /dev/shm
/dev/sda1             485M  105M  355M  23% /boot
/dev/sdb1             988M  324M  614M  35% /geekpeek

1. Unmount the partition

[root@foo1 ~]# umount /geekpeek/
[root@foo1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_foo-LogVol01
                       13G  4.6G  7.7G  38% /
tmpfs                 376M     0  376M   0% /dev/shm
/dev/sda1             485M  105M  355M  23% /boot

2. Run fsck on your filesystem

[root@foo1 ~]# e2fsck -f /dev/sdb1
e2fsck 1.41.12 (17-May-2010)
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/sdb1: 15/261120 files (0.0% non-contiguous), 363953/1044192 blocks

 3. Resize the filesystem with resize2fs

[root@foo1 ~]# resize2fs /dev/sdb1 400M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdb1 to 409600 (1k) blocks.
The filesystem on /dev/sdb1 is now 409600 blocks long.

4. Delete the partition

[root@foo1 ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x2dbb9f13

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

4. Create a new (smaller) partition

[root@foo1 ~]# fdisk /dev/sdb 

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +400M 

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x2dbb9f13

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          52      417658+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

5. Mount the new partition and filesystem

[root@foo1 ~]# mount /dev/sdb1 /geekpeek/
[root@foo1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_foo-LogVol01
                       13G  4.6G  7.7G  38% /
tmpfs                 376M     0  376M   0% /dev/shm
/dev/sda1             485M  105M  355M  23% /boot
/dev/sdb1             388M  323M   45M  88% /geekpeek

Voila! The partition was successfully reduced to 400MB, as we can see all of the data survived! 🙂 Always remember to resize the filesystem with resize2fs.

  • dan

    Hi Mitch,
    Nice comprehensive, precise guide for onine-resizing!
    This is exactly what i was searching for.
    Have your site bookmarked now.

    I am deeply open-source interested as well, but it’s more a hobby of mine.
    Do you have a tipp for a good start into linux server administration perhaps? (and a recommendation for a suitable distro as well)

    Thanks in advance, dan

    • Mitch

      Hi Dan! Nice to hear from another open source enthusiast 😉 Where to start? It all depends what you want from it. To work with linux professionally red hat classess and certifications are the best. You learn everything from fundamentals up and it is really intense! As far as distributions go, i prefer red hat and centos over ubuntu. It is in the end your choice to make. Regards, Mitch

  • aliosha

    Hello Mtch:
    Thanks so much for the tutorial.
    I have a question; keeps complaining “Bad magic number in super-block while trying to open /dev/sdb1. The superblock could not be read or does not describe a correct ext2 filesystem. ….. ” after creates the new partition. We tried with the alternative superblock provided by mke2fs -n /dev/sdb1 with the same luck.
    Do you have any idea of what may be wrong?
    -aliosha

  • Matthieu

    This guide is simple and awesome.
    I wanted to reduce my /home partition (no GUI because i had to unmount it) but didn’t want to use live USB disk with Gparted or a Linux distro so I tried this. I must admit that deleting the partition with fdisk is really scary, but this worked perfectly.

    Thank you, you saved my night!

  • I want to extend my linux lvm root partition…how can i extend it, i have installed fedora 19 on it and while upgrading to fedora 20 the root partition get out of space…now how it solve…plz reply me?

    • Mitch

      Hi CyberExpert. you need to boot a live linux CD and resize root partition from there. I will write a how to about this soon – i see there is a lot of interest in it. Regards, Mitja

      • Thanks for your response, Mitch ….i have tried to extend my root partition with a new unallocated partition with lvmextend2 but it didn’t works properly, i have lost access to my swap partition and now the swap partition is deactive….please give me a solution …because my / partition get out of space…i am unable to install or download anything directly from fedora.

  • Kenny

    Thank you so much for this tutorial. Can I still follow the same steps if I want to extend dev/sda1 mounted on /boot ?
    I ran out of space for my dev/sda1 mounted on /boot

    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/servermain–vg-root 11G 5.4G 5.0G 52% /
    udev 3.7G 4.0K 3.7G 1% /dev
    tmpfs 745M 340K 745M 1% /run
    none 5.0M 0 5.0M 0% /run/lock
    none 3.7G 0 3.7G 0% /run/shm
    /dev/sda1 228M 219M 0 100% /boot

    As you can see, I have used all the allocated space for /dev/sda1. My sda1 starts from 1 – 32, sda2 starts from 32 – 19458.

    Thanks for your help, please let me know if you need more information about my disk configuration.

    • Mitch

      Hi Kenny, there is an option to create a new /boot partition at the end of the disk (if you have no space left, try to shrink your last partition), copy all of the contents from existing /boot to the new one and re-configure your /etc/fstab. Regards, Mitja

      • Kenny

        Wow Mitch, that was so fast. Thank you for the prompt response. What is this option to create a new /boot partition? Please tell me how I can do this. Can I follow the same steps as above to shrink my /dev/sda2 first before I create a new /boot partition?

  • Martin Manscher

    Hi Mitch, I’ve followed the tutorial, and it worked fine for “downsizsing”. After some intermediate steps I got to expanding the partition and filesystem. The partition expansion went smoothly, from 200G to 250G. However, resize2fs (step 5 in your first section) gives me

    # resize2fs /dev/sda4
    resize2fs 1.42.5 (29-Jul-2012)
    The filesystem is already 52428800 blocks long. Nothing to do!

    gdisk reports a size of 250G for the partition, but df -h gives 197G for the mounted filesystem. What am I doing wrong?

    • Martin Manscher

      Seems to have helped to reboot the box, Although I accidentally did an on-line resize because I forgot to umount.

  • Pingback: Linux Partitioning | greenstack()

  • df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/xvda2 15G 5.7G 8.4G 41% /
    tmpfs 7.7G 0 7.7G 0% /dev/shm
    /dev/xvda3 199G 188M 189G 1% /data

    I want to delete /dev/xvda3 then add space to /dev/xvda2

    But I was stuck here:

    fdisk /dev/xvda3
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
    Building a new DOS disklabel with disk identifier 0xe40d9f2b.
    Changes will remain in memory only, until you decide to write them.
    After that, of course, the previous content won’t be recoverable.

    Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

    WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
    switch off the mode (command ‘c’) and change display units to
    sectors (command ‘u’).

    Command (m for help): d
    No partition is defined yet!

    but no partition is defined?

    • Mitch

      Hi Youhui, device /dev/xvda3 is a partition. You should fdisk /dev/xvda and then delete number 3. To resize your root partition you should read my guide about it! You will not be able to do in on the fly. Regards, Mitch

  • I think you mean this post: Increase Root Partition Size – LVM – CentOS

    But my server is a VPS on cloud, how do I use USB or CD/DVD? Any optional way?

    • Mitch

      How do you see the disks in your VPS management? Can you remove the second disk and “grow” the first? The only other way i see it is by using LVM, which is a bit late to do now and you would loose your data :/ Regards, Mitch

  • I’ve just verified that extending the partition also works if it’s a root partition. Tested on Debian 7 “Wheezy”. You do need to change the procedure slightly.

    1. Remove the root partition.
    2. Create the new partition.
    3. Make sure that the ‘boot’ flag is set. If so, an asterisk (*) should appear in the 2nd column. If not, you can toggle it with ‘a’. The asterisk should now be visible in the 2nd (boot) column. If you forget this option, the system might not boot. If that would be the case, simply boot from a linux live cd and use fdisk on there to toggle the ‘boot’ flag.
    Correct:
    Device Boot Start End Blocks Id System
    /dev/sda1 * 2048 251658240 125828096+ 83 Linux
    Incorrect:
    Device Boot Start End Blocks Id System
    /dev/sda1 2048 251658240 125828096+ 83 Linux
    4. Enter ‘w’ to save the changes. It should give you an error message that the device is busy, and offers you to perform it at the next boot. Answer with ‘y’ (yes) and reboot.
    5. After the reboot has been completed, you can use ‘resize2fs’ to resize the partition.

    • Mitch

      Hi Roy! Thanks for posting your results, it seems logical that it would work, yes. I will try it myself and write a how to, with your permission of course 😉 Regards, Mitch

  • efimvo

    You are big !
    TNX

  • RAMAKANT IRRANGI

    is there is any remedy/other way of bind mount as i have done bind mount in a cluster environment and my datbase is crashed .please suggest

  • Pingback: Don't I need to move a shrunk filesystem before resizing the partition? | DL-UAT()

  • PRAGEETH KARUNADHEERA

    Fantastic and useful post! Keep up the good work!!

  • Kalai

    I have 7 partitions in my disk and i want to extend the first and second partition alone with affecting/losing data in any of the partitions. Is it possible?

    • Mitch

      Hi Kalai, It is possible if you can shrink or move the third partition for the size you want to increase the first and second partition.

      • Kalai

        I don’t want to shrink or move the third partition. But i will be happy to shrink the last partition.

        • Mitch

          Are you using LVM?

          • Kalai

            No. I am using sfdisk.

          • Mitch

            Then you must shrink the third partition to make room for first and second partition increase.

          • Kalai

            Will I be able to do that without affecting any-of the content?

          • Mitch

            if done right, sure. it’s doable.

          • Kalai

            Can you explain it please?

          • Mitch

            As you can see i wrote this post to resize only one existing root partition. In your case, having more partitions, you should firstly move the existing data away from the beginning of the third partition. Next you should shrink the third partition and make room to enlarge the second partition. Again to increase the first partition you should move the data on the second partition away from the beginning to be able to shrink it without loosing data. Once shrinked you can enlarge the first partition. This is a whole new how to post, to explain. Not something i could fully explain in comments.

  • Pingback: Installing Openhab on Pi | technpol()

  • Pingback: Extending lvm mounts in oracle linux on vmware, part 1: a new disk device | Bitbach's Blog()

  • Pingback: Resize partition on Linux VMWARE | Information Technology Knowledgebase()

  • Pingback: Raspberry Pi Image der SD-Karte verkleinern ohne Datenverlust – TechnikBlog()

  • Jonny Cache

    This is not about resizing partition. This is about recreating partition with different size. Resize is when you change size of the existing partition.