This may be useful in some cases of change your mind of use of hibernation or missing swap partition during OS setup ocassionally or deliberately (e.g. in the case of Ubuntu LTS Server installation the mounting of swap partition leads to installer crash).
It’s fine if the storage device has unallocated disk space or additonal non-root partition, which may be shrinked to fit the swap one. We need usually 2xRAM size if hibernation mode supposed or some less if not.
Use resize2fs -p /dev/<partition name> <new size e.g. 128G>
command to shrink the filesysem. It may require additional e2fsck
command to be run (check the disk for errors), so follow the instruction.
It’s easy then:
fdisk /dev/<disk to process (e.g. sda)>
Step by step:
p
– check the partition to resize, note Device and Start valued
– delete partition, number is the index after disk name (e.g. sda1 – partition number is 1)n
– new partition with the same first sector (Start) and the new size in accordance to new filesystem size defined duringresize2fs
command execution (very useful format is+128G
, no need to calculate blocks and so on).t
– change the partition type if needed (if it doesn’t match initial type)n
– create new swap partitiont
– change the partition type from default Linux to Linux swap / Solaris – hex code82
p
– shows new partitions layout, double check if it’s all rightw
– write out the changes
The reboot may be needed to make the system to read the new disk layout.
It sounds worse if here is only the root-volume, which you have to bite off from. Simple way – LiveCD (or any installation USB-flash). The hard way – online root-volume resizing.
Check what’s happening with patritions: blkid
– look for the storage device records you’ve just modified. Initiate swap on that partition.
mkswap <partition name (e.g. /dev/sda3)>
blkid | grep <partition name (short, e.g. sda3 is enough> >> /etc/fstab
– it adds the UUID containing string to the end of fstab file (it responsible for default partitions mounting points) to prevent boring UUID typing. Then edit fstab:nano /etc/fstab
Remove the line with swap.img completely and bring the last string to the following order, deleting all unnecessary garbage:
/dev/disk/by-uuid/<the symbols group following "UUID="> swap sw 0 0
Exit (Ctrl+X
, save as prompted – y
, confirm file name ‘fstab‘ <Enter>
).
Refresh swap configuration:
swapon --all --verbose
– it has to output some strings including mention of our swap partition.
Check if swap partition is OK: cat /proc/swaps
– the swap file may be still in, but it disappears after reboot.
At last – delete swap.img from / directory.