Moving space from swap to /

I have a 4GB sd card but what i’d like to be able to do is have more free space on the / partition I don’t actually need a swap partition either so how would i resize/move partitions for example using fdisk?

Disk /dev/mmcblk0: 3.7 GiB

Device         Boot   Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *       2048 3844095 3842048  1.9G 83 Linux
/dev/mmcblk0p2      3846142 7772159 3926018  1.9G  5 Extended
/dev/mmcblk0p5      3846144 7772159 3926016  1.9G 82 Linux swap / Solaris

Filesystem      Size  Used Avail Use% Mounted on
udev            920M     0  920M   0% /dev
tmpfs           187M   20M  168M  11% /run
/dev/mmcblk0p1  1.8G  1.3G  417M  76% /
tmpfs           935M     0  935M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           935M     0  935M   0% /sys/fs/cgroup
tmpfs           187M     0  187M   0% /run/user/1001
Asked By: Lurch

  1. Stop the swap using swapoff -a
  2. Remove the swap (/dev/mmcblk0p5) and the extended (/dev/mmcblk0p2) partitions. To remove a partition with fdisk just run fdisk /dev/mmcblk0 and use d to delete a partition (it will ask which one).
  3. Don’t forget to remove swap entry from /etc/fstab and GRUB config.
  4. Resize the / partition. Resizing a partition with fdisk means deleting it and then creating a new one with same start sector and different end sector. You can follow for example this answer.
  5. Don’t forget to resize the filesystem on /dev/mmcblk0p1 after you resize it. Use tool for your filesystem resize2fs /dev/mmcblk0p1 for Ext4 or xfs_growfs /dev/mmcblk0p1 for XFS.

As always with storage, make sure to make a backup first.

Answered By: Vojtech Trefny
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.