3

I manage an Ubuntu server part-time in a scientific research laboratory, which has 512G memory and 8G swap space. Recently, we occasionally encounter low-frequency memory shortage, so I plan to expand the swap space. Currently, this server cannot accept any shutdown or accident. I find that they all recommend restarting the device after configuring the swap,

eg: Increase swap in 20.04

and I'm not sure if I can avoid this

My plan is:

First, find current swap file

$ sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file   8G 7.3G   -2

swap off and remove

sudo swapoff /swap.img 
sudo rm /swap.img

create new swap file

sudo dd if=/dev/zero of=/swap.img bs=1M count=92160
sudo chmod 600 /swap.img

create swap

sudo mkswap /swap.img

enable swap

sudo swapon /swap.img
# not reboot

Is it ok and safe to do?

zhang
  • 131
  • 1
    No need to remove "old" swapfile, create an additional one (not overwriting!). Linux can work with multiple swapfiles. https://askubuntu.com/questions/563430/how-to-create-a-swapfile – Marco Jun 03 '23 at 14:24
  • Hi, thanks for your help, sorry for more question, I am not sure is it necessary to add the new swapfiles to /etc/fastb for if I just add not instead the old swap, or the swapon will do it automatic – zhang Jun 03 '23 at 14:47
  • If you want to have the config over a reboot, you need to add it in /etc/fstab. – Marco Jun 03 '23 at 14:57
  • 1
    (however, yes, you can create a second swap file (I do this all the time on servers) and just enable it for that session, without modifying /etc/fstab) (to complete the answer) – popey Jun 03 '23 at 15:33
  • 1
    Creating a swap file with dd will create a discontinuous file, which won't work for swap. Read man mkswap swapon fstab. – waltinator Jun 03 '23 at 16:48
  • @waltinator at least for me (util-linux 2.37.2, Ubuntu jammy 22.04.03), man swapon explicitly recommends to create swapfiles with dd in the "Files with holes" section: "The most portable solution to create a swap file is to use dd(1) and /dev/zero." – Dietmar Feb 15 '24 at 07:58

0 Answers0