Extend Raspberry Pi main partition a little bit longer

Take 2018-10-09-raspbian-stretch-lite.img and push to SD card.

After booting into operatation system you can look at the situation by runing:
df -h
This will report:
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.7G  1.1G  521M  67% /
devtmpfs        460M     0  460M   0% /dev
tmpfs           464M     0  464M   0% /dev/shm
tmpfs           464M   12M  452M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           464M     0  464M   0% /sys/fs/cgroup
/dev/mmcblk0p1   44M   22M   22M  51% /boot
tmpfs            93M     0   93M   0% /run/user/1000
As you can see the main partition is just about 1.3Gb.

What if we let it to be a little, little bit longer?

This is possible!

Let's open SD cards master boot record for editing:
sudo fdisk /dev/mmcblk0
This will output:
Welcome 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):
Lets print the situation of master boot record:
p
This will output:
Disk /dev/mmcblk0: 7.5 GiB, 8026849280 bytes, 15677440 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: 0xee25660b

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk0p1       8192   97889   89698 43.8M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      98304 3645439 3547136  1.7G 83 Linux
write down the start sector of second partition which in this case is:
98304
Lets delete the information about how long should be second partition:

Press d to delete partition

Write 2 to delete second partition

Press n to create new partition

Write p for primary

Partition number set 2

Enter 98304 as the first block of second partition and press enter

In next step we will say that the size of partition will be 2560M which stands for 1.5Gb.
Write down +2560M and hit enter.

Press w to write changes

So the output of all this will be:
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-15677439, default 2048): 98304
Last sector, +sectors or +size{K,M,G,T,P} (98304-15677439, default 15677439): +2560M

Created a new partition 2 of type 'Linux' and of size 2.5 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
Now restart system:
sudo reboot
After restart lets check again the situation:
df -h
This will output pretty much the same. WTFloppy?
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.7G  1.1G  470M  71% /
devtmpfs        460M     0  460M   0% /dev
tmpfs           464M     0  464M   0% /dev/shm
tmpfs           464M   12M  452M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           464M     0  464M   0% /sys/fs/cgroup
/dev/mmcblk0p1   44M   22M   22M  51% /boot
tmpfs            93M     0   93M   0% /run/user/1000
Well actually you have to make the system know that there is a better place to live:
sudo resize2fs /dev/mmcblk0p2
This will output:
resize2fs 1.42.12 (29-Aug-2014)
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 393216 (4k) blocks long.
Lets check the situation:
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       2.5G  1.1G  1.3G  47% /
devtmpfs        460M     0  460M   0% /dev
tmpfs           464M     0  464M   0% /dev/shm
tmpfs           464M   12M  452M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           464M     0  464M   0% /sys/fs/cgroup
/dev/mmcblk0p1   44M   22M   22M  51% /boot
tmpfs            93M     0   93M   0% /run/user/1000
Yeah! 2.5Gb! And the dream comes true :)

No comments: