How can I expand ext4 partition size on debian

  • I have a drive with this configuration:

    fdisk -l
    
    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disk identifier: 0x000f1b8b
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1        2612    20971520   83  Linux
    /dev/sda3           60736       60801      525312   82  Linux swap / Solaris
    

    There is 478GB unallocated space, how would I go about adding this space to /dev/sda1 without losing the data that is currently on /dev/sda1? (the filesystem is ext4).

    NOTE: it is a server with only SSH, no GUI for running stuff like gparted.

    I can netboot the server and use it while the drive is unmounted.

    Also see https://askubuntu.com/a/937351/621883 especially the answer containing growpart and resize2fs was useful for me.

  • Enlarge the partition: fdisk -u /dev/sda.

    p to print the partition table, take note of the number, start, end, type of sda1.

    Delete it: d:

    Recreate it with same number (1), start and type but with a bigger end (taking care not to overlap with other partitions). Try to align things on a megabyte boundary that is for end, make it a multiple of 2048 minus 1. Change the type if needed with t (for partitions holding an extX or btrfs filesystem, the default of 83 is fine).

    Then w to write and q to quit.

    The partition table will have been modified but the kernel will not be able to take that into account as some partitions are mounted.

    However, if in-use partitions were only enlarged, you should be able to force the kernel to take the new layout with:

    partx /dev/sda
    

    If that fails, you'll need to reboot. The system should boot just fine.

    Then, resize the filesystem so it spreads to the extent of the enlarged partition:

    resize2fs /dev/sda1
    

    Which for ext4 will work just fine even on a live FS.

    I know this is old, but will this keep the data if you delete the partition?

    @CanadianLuke, yes, deleting the partition in fdisk only modifies the partition table. If you recreate it the same size, there will be no change at all. The data is never altered unless you shrink a partition and create another logical or extended partition in the middle of what your partition used to be, in which case the partition record for that extended/logical partition will be written over some data there.

    This also works great for ntfs partitions (with an existing volume, with data).

    That sounds reasonable, but what if i have Extended, Linux LVM and Linux swap/Solaris partitions which starts on the end of Linux partition? Should I delete these partitions>? I am worried that in that case, filesystem won't start, becuase may be using Linux LVM for example in same start and end. And i don't want to loose data too.

    @cyan resizing a LVM logical volume with a ext4 FS on it, or resizing a partition used as a physical volume for LVM would be different questions with different procedures.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM