0

Given

$ cat /etc/fstab
...
/dev/mapper/lubuntu--vg-swap none            swap    sw              0       0

why do we still need:

$ sudo swapoff /dev/mapper/lubuntu--vg-swap
$ sudo mkswap /dev/mapper/lubuntu--vg-swap
$ sudo swapon /dev/mapper/lubuntu--vg-swap

?

In other wors, why do we need both mkswap and /etc/fstab to set up swap? What do mkswap and /etc/fstab do differently?

My question comes from https://unix.stackexchange.com/a/502591/674

Thanks.

  • 2
    mkswap sets up the swap space. fstab just saws to use the swap space (eliminating the need to manually use swapon on boot). – jordanm Feb 24 at 6:52
1

As already said by jordanm, fstab is just a file containing the information of your filesystems to be mounted and the swap devices to be enabled on boot. You don't wan't to do that manually.

Have a look at the man pages of mount and swapon:

mount (8)

 -a, --all
              Mount all filesystems (of the given types) mentioned in fstab (except for those whose line contains the
              noauto keyword). The filesystems are mounted following their order in fstab.

swapon (8)

 -a, --all
              All devices marked as ``swap'' in /etc/fstab are made available, except for those with the ``noauto''
              option. Devices that are already being used as swap are silently skipped.

The command mkswap creates the swap device, just as mkfs.ext4 creates an ext4 filesystem on a device.

0

If you want swap to used post reboot also

Then entry of the swap partition details should be in /etc/fstab

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.