0

new to this and I am trying to figure what will be the best way to go on about this. I have a system that I need to increase its swap file, I realized that the swap partition is not lvm so that is kind of out. However, my current swap partition is 8Gb but needs to be at 16GB.

#  swapon -s
Filename      Type    Size      Used     Priority
/swapfile     file    8191996   6341008  -1

I guess my issue is that it is a swap file and not a partition (at least is an issue for me :(...) I read online I can use dd to create a swapfile, but I'm not sure I understand how to actually increase the size or if I need to create a new partition.

/etc/fstab looks like this:

/swapfile        swap           swap    defaults        0 0

Any recommendations on how to proceed?

user3311890
  • 181
  • 2
  • 7

2 Answers2

1

As mentioned above, you can resize the swapfile to gain the desired effect. But I would recommend adding another swapfile, with the same priority, so you will not need to swap out the GB's of swapped data.

# Create another swapfile, mind the filename!
sudo dd if=/dev/zero of=/swapfile2 bs=1M count=8192
# Make the new file a swapfile
sudo mkswap /swapfile2
# Enable it
sudo swapon /swapfile2
# Change its priority
sudo swapon /swapfile2 -p -1 # Or anything you want

Then add /swapfile2 swap swap defaults 0 0 to /etc/fstab.

lkp111138
  • 236
  • 1
  • 5
  • This has the benefit of not being without swap over my solution. – mzhaase Jan 04 '17 at 15:18
  • Thanks! so is there any pros vs. cons for either deleting the existing swap and creating a new one? or to just add a second swap partition? like in your example. I've been asked by a support team to increase it, so Im not sure if by creating another one will actually suffice – user3311890 Jan 04 '17 at 15:20
  • Pro for deleting the existing swap: - No need to edit system files, less risk Con for deleting the existing swap: - May take a performance hit (swapping out) or even system instabilities if the system doesn't have enough RAM Pro for creating a new one: - No need to swap out, impact on performance is minimal Con for creating a new one: - Failure of editing a system file will screw your system on reboot – lkp111138 Jan 04 '17 at 15:30
  • ok, so I added a /swapfile2 swap but I see two issues. First one is that even though I put to have the same preiority as the other swap file (-1), it does not take it and shows a priority of -2 `# swapon -s Filename Type Size Used Priority /swapfile file 8191996 6238112 -1 /swapfile2 file 8188 0 -2 ` Also, when I run the `free -h` it only shows that swap has the previous 7.8G and not 16 as it's supposed to be after adding /swapfile2 – user3311890 Jan 04 '17 at 16:20
  • It won't take negative values as its priority after an experiment. and /swapfile2 has size of 8188K (as opposed to /swapfile's 8191996K). `sudo dd if=/dev/zero of=/swapfile2 bs=1M count=8192` should create a 8192M file. – lkp111138 Jan 05 '17 at 04:29
0

A swapfile is really just that, an actual file used as swap. There are a couple of ways to go about this, best is to probably delete it and create a new one.

# this disables ALL swap, you can target with swapoff /swapfile
sudo swapoff -a
# delete old swap file
sudo rm /swapfile
# create new swap file. dd takes /dev/zero which always returns.. 0 then it
# writes that data into the file under 'of'. So it is just a file full of 0s
# bs is blocksize and count is number of blocks, so bs=1M and count=16284 
# will create a 16284 MByte file. Adjust count to make a bigger file.
sudo dd if=/dev/zero of=/swapfile bs=1M count=16384
# make file a swapfile
sudo mkswap /swapfile
# enable swapfile
sudo swapon /swapfile
# you already have swapfile of the same name in fstab, so no need to edit it
mzhaase
  • 3,778
  • 2
  • 19
  • 32