1

I have a SD card that has two partitions /dev/sdb1 that holds the boot sector, and /dev/sdb2 that holds the OS. The card still has unpartitioned free space.

How can I take all the unpartitioned free space and add it to /dev/sdb2 without formatting the drive? I want to do this in on command line only

4
  • See man resize2fs.
    – goldilocks
    Sep 23, 2015 at 19:04
  • The sequence of steps is: 1. resize the partition, using any appropriate tool (fdisk, cfdisk, gdisk, &c.), and 2. resize the filesystem, using resize2fs (most likely; there are different tools for different FSs). If you want, you can use gparted to do it automatically.
    – Tom Hunt
    Sep 23, 2015 at 20:17
  • @goldilocks, I did use resize2fs but it says I am already at full block. When I run parted /dev/sdd/ print free, I have a whole chunk of Free Space. I want to allocate that to my parition
    – Kousha
    Sep 23, 2015 at 20:48
  • Whoops! You have to increase the partition first w/ fdisk. Resize2fs resizes the filesystem inside. I don't have time to write a complete answer right now, but the fdisk thing means "deleting" the partition then creating it again with the exact same starting block, but then a greater final size. It's pretty straightforward, but you may want to look for an intro to fdisk or something if you haven't used it before.
    – goldilocks
    Sep 23, 2015 at 21:14

1 Answer 1

2

Ended up using the following. Assuming the disk I want to expand is /dev/sdd and has two partitions /dev/sdd1 and /dev/sdd2, and I want to increase /dev/sdd2.

umount /dev/sdd2
sudo parted /dev/sdd resizepart 2 -- -1
sudo e2fsck -f /dev/sdd2
sudo resize2fs /dev/sdd2

In this example, I wanted to increase to the maximum volume (hence -- -1 as argument of resizepart. If you want to resize to a specific size, I suggest you first run

sudo parted /dev/sdd unit s print free

This should print something similar to

Number  Start     End        Size       Type     File system  Flags
        63s       2047s      1985s               Free Space
 1      2048s     32767s     30720s     primary  fat16        boot, lba
 2      32768s    5119999s   5087232s   primary  ext2
        5120000s  15728639s  10608640s           Free Space

Say you want to expand to sector 10120000s (that's somewhere between the start/end of my free space 5120000s to 15728639s. In that case you run

umount /dev/sdd2
sudo parted /dev/sdd resizepart 2 10120000s
sudo e2fsck -f /dev/sdd2
sudo resize2fs /dev/sdd2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.