22

OK , I know this sounds a bit silly and solid state drives like a USB flash stick are probably the last thing on earth you want to use as a swap disk since they could go bad pretty quick from all the read/writes.

Regardless of the bad idea aspect of this I would like to know how to make one of my old 4 gig USB sticks my designated swap disk so that when my computer running Ubuntu Precise starts up, it will use the USB stick as swap.

Couple of twists to add to the challenge:

  1. the computer I'm using is my wife's laptop core2 duo and has 4gigs of ram

  2. she likes Ubuntu but doesn't trust me to backup her hd so I have her running Ubuntu 12.04 on a 16gb highspeed USB 2 flash memory stick with persistency.

  3. the second USB stick will be my swap disk and when the live USB starts up I want it to do all the heavy swap writing etc on the second USB stick

Anyone up to the challenge. Trivial maybe but please indulge me :D

wjandrea
  • 14,236
  • 4
  • 48
  • 98

1 Answers1

27

1) Make sure your USB stick is connected. In a terminal type:

sudo blkid

It will list all your connected drives. Search for your USB stick you want to use as swap and copy the UUID (everything inside these quotes UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX").

This represents the individual name of your device.

Also note the /dev/XXX point of your device.

2) Now unmount your device by typing:

sudo umount /dev/XXX (where XXX represents your device name)

3) Format your USB stick as swap, e.g. by terminal

sudo mkswap /dev/XXX (<-- be sure to use the correct device name here or you'll probably end up formatting the wrong drive!)

or simply use GParted.

4) Now you have to edit your /etc/fstab file, so type

gksudo gedit /etc/fstab

and enter the following

UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX none swap sw,pri=5 0 0

(for all the Xs, use the UUID number you got by typing sudo blkid)

5) Now type

sudo swapon -a

That's it, this should already work.

FuzzyQ
  • 2,328
  • Awesome. I'm trying this right when I get home! I had a feeling there was a little bit more involved than simply formatting a USB stick as swap and plopping it in the slot and booting up . I'll be back if I have any issues to award you with the correct answer my Linux brother. – Joshua Robison Aug 09 '12 at 09:01
  • 1
    To see whether your changes have been applied, type cat /proc/swaps. In the following list shown there should be your usb drive mounted as swap. Good luck to you, bro! :) – FuzzyQ Aug 09 '12 at 09:41
  • well sir @FuzzyQ it worked like a charm. Also, though I did do the fstab work, I was worried about that because even on persistent live usb sticks/ hybrid usb sticks... the fstab is always reset on reboot... but something funny happened when I rebooted and it turns out though I am thankful for your fstab suggestion I didn't even need to use that magic and Ubuntu recognized the swap disk and started using it right away. – Joshua Robison Aug 09 '12 at 13:18
  • Alright, I'm glad it worked. Congratulations! – FuzzyQ Aug 09 '12 at 13:22
  • 1
    Getting good results too. The wife's computer definitely pauses and dims from USB writes much less – Joshua Robison Aug 09 '12 at 15:26
  • Which is the proper way to reverse this? swapoff -a then editing the /ertc/fstab to remove the USB device line and then swapon -a again? – Drubio Mar 29 '20 at 09:47