extending a partition by resize2fs

19,603

You must tell apart the resizing of a block device (here: /dev/sdb4) from the resizing of a file system. A file system can be smaller but not bigger than the underlying block device.

You should make a backup of the partition table:

sfdisk -d /dev/sdb > ~/sfdisk_sdb.txt

Then you make a copy of that file and adapt the line that looks similar to this:

/dev/sdb4 : start=24260, size=3653948, Id= 83

You want that partition to end on the last sector of the device (i.e. 7744511; the first one is 0 not 1). The size is this number minus the start sector plus one (both the start and end sector count). Then you replace the partition table:

sfdisk /dev/sdb <~/sfdisk_sdb.mod.txt

After that you can use resize2fs without a size parameter. It will use the whole size of /dev/sdb4 then. You must run e2fsck -f /dev/sdb4 immediately before using resize2fs.

Share:
19,603

Related videos on Youtube

sven
Author by

sven

Updated on September 18, 2022

Comments

  • sven
    sven over 1 year

    I have a 4 GB SD card. Before the image load

    root@ubuntu# fdisk -l
    Disk /dev/sdb: 3965 MB, 3965190144 bytes
    49 heads, 48 sectors/track, 3292 cylinders, total 7744512 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: 0x00000000
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            8192     7744511     3868160    b  W95 FAT32
    

    I loaded a 2gb SD image to the card by dd if=2gbsd-noeclipse-latest.dd of=/dev/sdb bs=4M conv=fsync. The fdisk -l outputs:

    root@ubuntu# fdisk -l
    Disk /dev/sdb: 3965 MB, 3965190144 bytes
    122 heads, 62 sectors/track, 1023 cylinders, total 7744512 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: 0x00000000
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1       16063        8031+   b  W95 FAT32
    /dev/sdb2           16064       20158        2047+  da  Non-FS data
    /dev/sdb3           20162       24257        2048   da  Non-FS data
    /dev/sdb4           24260     3678207     1826974   83  Linux
    

    so I have 2GB that is not used. I want to extend sdb4 so that I can use the 2GB space that is not included.

    So I calculate the unused space as (7744512-3678207)*512= 2081948160 byte and 2081948160 / 1048576 = 1985.50048828 MB. So roughly I will extend 1900 MB. I use resize2fs to do that:

    resize2fs /dev/sdb4 1900M
    

    However, it outputs

    resize2fs 1.42.5 (29-Jul-2012)
    open: No such file or directory while opening /dev/sdb4
    

    Could anyone tell me how I should use the command above or how else I can extend the sdb4?

  • sven
    sven almost 11 years
    thank you very much for the solution. I still have little problem. Here is edited sfdisk_sdb.mod.txt and rest of the commands' execution pastebin.com/Z2gc5kSi could you please tell what I am still doing wrong?. Lastly an edit for the person who might need to use your helpful text < is missing in the first command between /dev/sdb and ~/sfdisk_sdb.txt
  • Hauke Laging
    Hauke Laging almost 11 years
    @sven I added the missing redirection. You did not what I told you: You took the last sector as size which is obviously wrong. You should subtract the start sector and add one resulting in 7744511-24260+1=7720252. You should pay more attention when playing with partition tables...
  • Hauke Laging
    Hauke Laging almost 11 years
    @sven Maybe it is not valid to use the last sector (whyever); I am not a partition table expert. You should decrease the size value. Either one by one until it works (so you get the whole capacity) or you don't care about a few sectors and just subtract 100.
  • sven
    sven almost 11 years
    thank you I left 10% of the image as unallocated, an instruction suggested that. Now it works fine. Thank you very much