Extend Partition and File system on Raspberry Pi
Flashed an openwrt image to a 8GB sdcard, it only used 256M, the other 7.2GB are wasted.
Disk /dev/sda: 7.5 GiB, 8017412096 bytes, 15659008 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: 0x5452574fDevice Boot Start End Sectors Size Id Type
/dev/sda1 * 8192 49151 40960 20M c W95 FAT32 (LBA)
/dev/sda2 57344 581631 524288 256M 83 LinuxStart End Sectors Size
2048 8191 6144 3M
49152 57343 8192 4M
581632 15659007 15077376 7.2G
To utilize the unallocated 7.2GB, we can use fdisk and to extend the existing partition.
1. Unmount the partition
pi@raspberrypi:~ $ umount /media/pi/06F7–6E7D
pi@raspberrypi:~ $ umount /media/pi/57f8f4bc-abf4–655f-bf67–946fc0f9f25b
2. Delete the partition
pi@raspberrypi:~ $ sudo fdisk /dev/sdaWelcome to fdisk (util-linux 2.29.2).
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/sda: 7.5 GiB, 8017412096 bytes, 15659008 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: 0x5452574fDevice Boot Start End Sectors Size Id Type
/dev/sda1 * 8192 49151 40960 20M c W95 FAT32 (LBA)
/dev/sda2 57344 581631 524288 256M 83 Linux
F to list free unpartitioned space:
Command (m for help): F
Unpartitioned space /dev/sda: 7.2 GiB, 7726956544 bytes, 15091712 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytesStart End Sectors Size
2048 8191 6144 3M
49152 57343 8192 4M
581632 15659007 15077376 7.2GCommand (m for help): d
Partition number (1,2, default 2): 2Partition 2 has been deleted.Command (m for help): p
Disk /dev/sda: 7.5 GiB, 8017412096 bytes, 15659008 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: 0x5452574fDevice Boot Start End Sectors Size Id Type
/dev/sda1 * 8192 49151 40960 20M c W95 FAT32 (LBA)
3. Create a new (larger) partition
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):Using default response p.
Partition number (2-4, default 2): 2
First sector (2048-15659007, default 2048): 57344
Last sector, +sectors or +size{K,M,G,T,P} (57344-15659007, default 15659007):Created a new partition 2 of type 'Linux' and of size 7.5 GiB.
Partition #2 contains a ext4 signature.Do you want to remove the signature? [Y]es/[N]o: NCommand (m for help): pDisk /dev/sda: 7.5 GiB, 8017412096 bytes, 15659008 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: 0x5452574fDevice Boot Start End Sectors Size Id Type
/dev/sda1 * 8192 49151 40960 20M c W95 FAT32 (LBA)
/dev/sda2 57344 15659007 15601664 7.5G 83 LinuxCommand (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
4. Run fsck on your filesystem
pi@raspberrypi:~ $ sudo e2fsck -f /dev/sda2
e2fsck 1.43.4 (31-Jan-2017)
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/sda2: 1263/16384 files (0.0% non-contiguous), 4275/65536 blocks
5. Resize your filesystem with resize2fs
pi@raspberrypi:~ $ sudo resize2fs /dev/sda2
resize2fs 1.43.4 (31-Jan-2017)
Resizing the filesystem on /dev/sda2 to 1950208 (4k) blocks.
The filesystem on /dev/sda2 is now 1950208 (4k) blocks long.
6. Re-mount extended partition
pi@raspberrypi:/media/pi $ sudo mkdir sdcard
pi@raspberrypi:/media/pi $ sudo mount /dev/sda2 /media/pi/sdcardpi@raspberrypi:/media/pi/sdcard $ df -h .
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.4G 14M 7.3G 1% /media/pi/sdcard