YangYouji's WebSite

[树莓派扩容]Extend root filesystem using CLI parted & resize2fs

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.

退出移动版