Extend root filesystem using CLI parted & resize2fs


15 posts
by tedsluis » Tue May 28, 2013 6:28 am
I opened this new topic to answer cclauss his question, posted in the "Pidora 18 (Raspberry Pi Fedora Remix) Release" topic: http://www.raspberrypi.org/phpBB3/viewtopic.php?p=358244#p358244:
cclauss wrote:In Pidora, is there a headless mode way to expand the rootfs to take up the entire SD card without doing remote Xwindows into the Pi?

'Parted' runs on the command line but I don't know how to expand the rootfs with it.
You could login using headless mode (headless mode means that you don't have a display connect and you login with ssh from any other system) and use these steps to expand your root partition and file system on a Raspberry Pi running Pedora (fedora remix):
- backup your system in case of a misstake!
- use "fdisk /dev/mmcblk0" to view your partitions.
- use "parted" to delete the partition and then recreate it but with a larger size. (don't worry, the data will remain)
- reboot to activate the partition changes.
- use "resize2fs /dev/mmclk0p2" to enlarge the root file system.
- use e2fsck -f /dev/mmcblk0p2 to perform a file system check.
- use "df -h" to check results.

Before you extend your root partition and filesystem you should know how big your rootfs is and how much space is available:
Code: Select all
[root@raspi ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.6G  1.5G   53M  97% /
/dev/mmcblk0p1   50M   18M   33M  35% /boot
[root@raspi ~]#
Determine the storage devices:
Code: Select all
[root@raspi ~]# ll /dev/mm*
brw-rw---- 1 root disk 179, 0 Jun  3 13:22 /dev/mmcblk0
brw-rw---- 1 root disk 179, 1 Jun  3 13:21 /dev/mmcblk0p1
brw-rw---- 1 root disk 179, 2 Jun  3 13:21 /dev/mmcblk0p2
[root@raspi ~]
Check the partition table:
Code: Select all
[root@raspi ~] fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.22.1).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/mmcblk0: 16.0 GB, 16012804096 bytes, 31275008 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: 0x000622ba

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      104447       51200    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          104448     3494304     1694928+  83  Linux

Command (m for help): q

[root@raspi ~]#
So the SD card has 31275008 (16GB) sectors and the last one in use is 3494304 (1.6GB).
Print the partition table with "parted":
Code: Select all
[root@raspi ~]# parted /dev/mmcblk0
GNU Parted 3.1
Using /dev/mmcblk0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit chs                                                         
(parted) print                                                           
Model: SD  (sd/mmc)
Disk /dev/mmcblk0: 1946,198,43
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 1946,255,63.  Each cylinder is 8225kB.
Partition Table: msdos
Disk Flags:

Number  Start     End        Type     File system  Flags
 1      0,32,32   6,127,56   primary  fat16        boot, lba
 2      6,127,57  217,130,9  primary  ext4
(parted)
So the disk ends at 1946,198,43 cylinder,head,sector and the current root partition ends at 217,130,9.

Note: "fdisk" displays the partition info in 512 bytes blocks and "parted" displays the cylinder,head,sector geometry. Each cylinder is 8225kB.

Now remove the second partition and recreate it larger.

Note: If you have a third swap or other partition that you don't need any longer, you can remove that one too and use the disk space to extend you.

Removing the partition will only change the partition table and not the data. Creating a new partition will write a new start and end point in the partition table.

Be careful: If you make a misstake, you lose you root partition data:
(Ignore the warning.)
Code: Select all
(parted) rm 2                                                             
Error: Partition(s) 2 on /dev/mmcblk0 have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will
remain in use.  You should reboot now before making further changes.
Ignore/Cancel? i                                                         
(parted)
And check whether the partition was removed:
Code: Select all
(parted) print                                                           
Model: SD  (sd/mmc)
Disk /dev/mmcblk0: 1946,198,43
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 1946,255,63.  Each cylinder is 8225kB.
Partition Table: msdos
Disk Flags:

Number  Start    End       Type     File system  Flags
 1      0,32,32  6,127,56  primary  fat16        boot, lba

(parted)
Now the second partition is removed. Do not reboot your system before you have created the new partition! Other wise you lose your root file system.

The new partition must start at the same position where the old root partition did start and it ends where you like. It must have at least the same size as current partition and it may not exceed the end of the disk (in my case 1946,198,43).
(Ignore the warning.)
Code: Select all
(parted) mkpart primary 6,127,57  1946,198,43
Error: Partition(s) 2 on /dev/mmcblk0 have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will
remain in use.  You should reboot now before making further changes.
Ignore/Cancel? i                                                         
(parted)
And check whether the partition was created:
Code: Select all
(parted) print                                                           
Model: SD  (sd/mmc)
Disk /dev/mmcblk0: 1946,198,43
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 1946,255,63.  Each cylinder is 8225kB.
Partition Table: msdos
Disk Flags:

Number  Start     End          Type     File system  Flags
 1      0,32,32   6,127,56     primary  fat16        boot, lba
 2      6,127,57  1946,198,43  primary  ext4

(parted) quit                                                             
Information: You may need to update /etc/fstab.

[root@raspi ~]#
Be carefull: The kernel is not aware yet of the new partition size. You must reboot your system before you do any thing else.
Code: Select all
[root@raspi ~]# reboot
Check the new partition size after the reboot:
Code: Select all
[root@raspi ~]# fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.22.1).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/mmcblk0: 16.0 GB, 16012804096 bytes, 31275008 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: 0x000622ba

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      104447       51200    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          104448    31275007    15585280   83  Linux

Command (m for help): quit
[root@raspi ~]#
Now the partition is larger, but the root file system has still the old size. Re-size the root filesystem:
Code: Select all
[root@raspi ~]# resize2fs /dev/mmcblk0p2
resize2fs 1.42.3 (14-May-2012)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p2 is now 3896320 blocks long.

[root@raspi ~]
The root file system is now extended.
Then check the file system for errors:
Code: Select all
[root@raspi ~]# e2fsck -f /dev/mmcblk0p2
e2fsck 1.42.3 (14-May-2012)
/dev/mmcblk0p2 is mounted. 


WARNING!!!  The filesystem is mounted.   If you continue you ***WILL***
cause ***SEVERE*** filesystem damage.


Do you really want to continue<n>? yes
rootfs: recovering journal
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
Free blocks count wrong (3453563, counted=3453559).
Fix<y>? yes

rootfs: ***** FILE SYSTEM WAS MODIFIED *****
rootfs: ***** REBOOT LINUX *****
rootfs: 63775/952000 files (0.1% non-contiguous), 442761/3896320 blocks
[root@raspi ~]#
The file system is free of errors.
Finaly check the file systems size and the available space:
Code: Select all
[root@raspi ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  1.5G   13G  11% /
/dev/mmcblk0p1   50M   18M   33M  35% /boot
[root@raspi ~]#
It has lots of free space available and it is ready to use.

I hope this helps you.

Ted Sluis
Last edited by tedsluis on Tue Feb 09, 2016 6:52 am, edited 1 time in total.
--- If you cannot divide then you cannot multiply. ---
User avatar
Posts: 107
Joined: Sun Mar 03, 2013 11:49 am
Location: The Netherlands
by PI_A » Fri Feb 28, 2014 12:52 am
This worked perfectly! Thanks a lot!
Posts: 1
Joined: Fri Feb 28, 2014 12:48 am
by tedsluis » Fri Feb 28, 2014 12:29 pm
you're welcome.
--- If you cannot divide then you cannot multiply. ---
User avatar
Posts: 107
Joined: Sun Mar 03, 2013 11:49 am
Location: The Netherlands
by JustThisGuy » Sat May 03, 2014 10:07 pm
Just used these instructions on a new 2014-R1 img that failed to resize on reboot and they worked great. Thanks.
Any conversation about a sufficiently complex subject is indistinguishable from babble.
Posts: 114
Joined: Thu Jan 05, 2012 11:22 pm
by oatley » Sun May 04, 2014 5:35 am
The best way to Resize the Rootfs on Pidora 2014 while using headless mode, if to use the Headless Mode function to resize it. This can be done by adding the option "RESIZE" to the headless file, see a detailed description in the link with examples for both DHCP and Static.

See here: http://zenit.senecac.on.ca/wiki/index.p ... figuration

The Headless Mode service, if configured to do so, will activate the rootfs resize service and reboot your computer. The computer may reboot a second time, but when complete, the rootfs should be at the max size.

[EDIT]

Though manually doing it the above way is pretty cool too! :p
Posts: 20
Joined: Thu May 16, 2013 8:00 pm
Location: Toronto
by gex » Sun Jul 19, 2015 2:03 am
Thanks so much! Consider reducing the article to just fdisk + resize2fs. This is a nice tour of useful commands but the task can be more easily accomplished with just those two.
Posts: 1
Joined: Sun Jul 19, 2015 12:12 am
by shamox » Sun Nov 01, 2015 4:15 pm
Thanks ! working well ;-)
Posts: 1
Joined: Sun Nov 01, 2015 4:14 pm
by iBRsbk » Sun Jan 17, 2016 5:20 pm
Worked perfect in Ubuntu MATE 15.10 on my RP2, thank you so much!
Posts: 1
Joined: Sun Jan 17, 2016 5:16 pm
by yangz » Tue Jan 26, 2016 7:23 am
I have solved my problem for extend root filesystem in Ubuntu mate 15.10 for Raspberry Pi 2 . Thank you very much! :D
Posts: 6
Joined: Mon Jan 25, 2016 4:23 am
by DanTup » Thu Mar 31, 2016 5:10 pm
FWIW, this seems possible with raspi-config now?

I have this at the start up my setup script:

sudo raspi-config --expand-rootfs

Not all of raspi-config seems to be scriptable, but there's a bunch of stuff that is. You can see it by examining the script (it's near the bottom) :)
Posts: 28
Joined: Sun Mar 27, 2016 7:29 pm
by gatorback » Thu Apr 07, 2016 1:22 am
Code: Select all
sudo raspi-config --expand-rootfs


then reboot.


Verifcation:

Code: Select all
sudo fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):
Code: Select all
 p

Disk /dev/mmcblk0: 7.4 GiB, 7946108928 bytes, 15519744 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
Disklabel type: dos
Disk identifier: 0x6f92008e

Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 131071 122880 60M c W95 FAT32 (LBA)
/dev/mmcblk0p2 131072 2658303 2527232 1.2G 83 Linux
Posts: 23
Joined: Wed Jan 16, 2013 2:53 pm
by thekotaksampah » Sun Apr 10, 2016 1:16 pm
Thank you so much, now I can increase my raspi storage :)
Posts: 1
Joined: Sun Apr 10, 2016 1:14 pm
by matthewh » Sun Sep 25, 2016 12:41 am
>> use e2fsck -f /dev/mmcblk0p2 to perform a file system check.

I am using this on Centos on RP2. rootfs-resize does not seem to work on SD cards 64GB in size.

When I get to this step:

# e2fsck -f /dev/mmcblk0p3
e2fsck 1.42.9 (28-Dec-2013)
/dev/mmcblk0p3 is mounted.
e2fsck: Cannot continue, aborting.

Is there a way to force it to run?
Posts: 15
Joined: Tue Dec 30, 2014 5:04 pm
by rpdom » Sun Sep 25, 2016 5:38 am
matthewh wrote:>> use e2fsck -f /dev/mmcblk0p2 to perform a file system check.

I am using this on Centos on RP2. rootfs-resize does not seem to work on SD cards 64GB in size.

When I get to this step:

# e2fsck -f /dev/mmcblk0p3
e2fsck 1.42.9 (28-Dec-2013)
/dev/mmcblk0p3 is mounted.
e2fsck: Cannot continue, aborting.

Is there a way to force it to run?

No, because it is a very bad idea to try and check a running filesystem for inconsistencies. You should be able to skip that step and run the next one without any problems.
User avatar
Posts: 10678
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by matthewh » Mon Sep 26, 2016 4:20 pm
No, because it is a very bad idea to try and check a running filesystem for inconsistencies. You should be able to skip that step and run the next one without any problems.


>>>e2fsck -f /dev/mmcblk0p3
>>> ....
>>>Free blocks count wrong (3453563, counted=3453559).

Will not correcting this with e2fsck create issues?
Posts: 15
Joined: Tue Dec 30, 2014 5:04 pm