Sunday, October 12, 2008

Resizing the swap partition size after install

I just increased the amount of RAM in an Ubuntu web server from 256 MB to 1 GB. I wanted to increase the size of the swap file accordingly. There are various schools of thought as to how big the swap file should be, I went with 2 GB which is almost certainly overkill, but there you go.

My procedure was as follows:
  • Boot to using the Ubuntu8.04 live CD.
  • Use the partition editor to shrink the primary partition, and grow the the swap file partition and volume.
  • Write the changes to the disk.
  • Reboot to the server command line.
  • Determine the swap volume's new UUID using:
ls -l /dev/disk/by-uuid
  • Edit /etc/fstab and change the UUID for the swap partition to the correct value
  • reboot
  • Check that the partition is correctly mounted with the "free" command. Don't mind that you may not be using any of the swap at present, but you can at least tell that it's there for when you do need it.

Text editing with multiple files on the command line

To make my life easier, and considering that my web server doesn't have a GUI I first saved the output of the ls command to a text file, and edited that so that it contained just a single line containing the UUID
I then opened fstab
sudo vim /etc/fstab
and then, at the end of fstab, I inserted the file I'd created with the command:
:r /home/john/uuid.txt
I copied the UUID to the appropriate spot, deleted the line at the end and saved the file.

There are plenty of good tutorials and command summaries for vi / vim around on the web if you need them. (here's one, and another). I admit I'm somewhat conditioned to using graphical editors, but one doesn't always have a choice.