[ale] fun fun changing Linux swap partition to a swap file

Ron Frazier atllinuxenthinfo at c3energy.com
Thu Jan 20 01:17:05 EST 2011


Hi all,

As I've mentioned in other posts, I've been experimenting with using
cloning as my main backup method.  I was testing a hard drive that I'd
cloned, and I suddenly found that I no longer had a hibernate option on
the menu.  After investigating, I found that the swap partition was not
functional on the cloned drive, which apparently disables hibernation.
I have the machine set to hibernate if the battery becomes critically
low or if I press the suspend button.  I will occasionally manually
hibernate as well.  In other posts, there was discussion of using a swap
file instead of a swap partition.  I decided to change my swap
partition, which is sandwiched between an NTFS partition and a EXT4
partition, into a swap file.  Here is the story of how I did it, for
those who might like to try.  You can benefit from my pain!

I used this link, which someone else here posted, as my baseline:

http://www.linux.com/archive/feature/113956

Before I detail the steps, I just have to say this.  I don't intend to
start a flame war, but most of what I did here, I could have done on the
Windows side of my dual boot fence within 15 minutes from the graphical,
easy to use, utility built into Vista.  Doing it on Linux took me over
an hour the first time and involved numerous trips to the command line
and a dance with a live boot disk.  I think it could and should be much
easier.

OK, here are the exact steps I used to accomplish this transition from
swap partition to swap file.  I'm using Ubuntu 10.04.

WARNING, this involves editing your disk partitions.  It's a good idea
to backup your data first in case something goes wrong.

01) Turn off the existing swap.  Shut down other apps first, especially
on a low memory machine.  From a terminal:
     sudo swapoff -a

02) Verify that the swap is off.  The following should not print any
active lines.  From a terminal:
     sudo swapon -s

03) Create a file to be used for swap.  Enter exactly as shown, but
change the count=8192 to the number of MB you want in your swap file.
Change /swapfile to the path and name you want to use for your swap
file.  Note that the bs option has a capital "M" in it.  This command
took about 2 minutes to run on my laptop.  Wait for the prompt to
reappear.  From a terminal:
     sudo dd if=/dev/zero of=/swapfile bs=1M count=8192

04) Turn this empty file into a swap file.  From a terminal:
     sudo mkswap /swapfile

05) Turn on the new swap file.  From a terminal:
     sudo swapon /swapfile

06) Verify that the swap is active.  From a terminal:
     sudo swapon -s

You should get a printout similar to this.  The type should say file:

Filename				Type		Size	Used	Priority
/swapfile                               file		8388600	0	-1

07) The /etc/fstab file must be edited to activate the swap file on
boot.  First, make a backup copy of the file.  From a terminal:
     sudo cp /etc/fstab /etc/fstab.mybackup

08) Edit the /etc/fstab file.  From a terminal:
     sudo gedit /etc/fstab

09) Comment out the original line with the word "swap" in it by placing
a # symbol at the front of it.

10) Add a few more comment lines with # symbols at the front and note
that you are adding a new line to activate the swap file.  Blank lines
should have # in front too.

11) Add the following line, correcting for the path and name of your
file:
     /swapfile     none     swap     sw     0     0

Make the spacing so that each element lines up with the original line
(not totally sure if this matters or not), accounting for the fact that
you commented out the original line.  Here are my lines:

# swap was on /dev/sda5 during installation
# UUID=6ef4c386-4dbc-4837-ba85-c21f7d94744a none            swap    sw
0       0
#
# Commented out original swap line above and add swap FILE below.  RWF
2011-01-19
#
/swapfile                                 none            swap    sw
0       0

12) Save the file and exit the editor.

13) Reboot the system.

14) Start system - administration - System Monitor from the menu and
click the resources tab.  It should show (in my case) 8 GB of swap.

15) Verify that the correct swap is on.  From a terminal:
     sudo swapon -s

     You should get a printout similar to this:

Filename Type Size Used Priority
/swapfile                               file 8388600 0 -1
     
There should be only a file, not a partition.

16) Now it's time to eliminate the old swap partition and use the space.
If needed, start Synaptic Package Manager and search for and install
GParted.

17) Start GParted from the system - administration - GParted menu.

18) Select the linux-swap partition, which is probably within an
extended partition.  Note the location of the extended partition (in my
case /dev/sda4).

19) Click the trash button to delete the linux-swap partition.

20) Select and delete the extended partition that the linux-swap
partition was in.

21) Verify that the changes are correct, then apply them by clicking the
"check" button.  Wait for the changes to finish.

22) Shut down GParted.

23) Put a Ubuntu 10.10 live CD in the drive.  Reboot the computer and
boot into the CD.  You may have to enable CD booting in the BIOS, and
probably will have to press a special function key on boot.  This is
necessary to modify the root partition to use the newly available space.
As far as I can tell, no swap is active when the CD is running, so don't
run any extra apps on low memory machines.

24) When the menu comes up, select your language and click Try Ubuntu.
Wait for the main screen to come up.

25) Run GParted again from the system - administration - GParted menu.
Ubuntu 10.10 includes this program.  If necessary, you could run the
package manager and install it.

26) In my case, I want to expand the root partition to use up the 8 GB
freed by removing the swap.  Select the EXT4 (or whatever your root is)
partition.

27) Click the resize button in the upper toolbar.

28) If possible, select the new size by dragging the graphical border
indicator (in my case, to the right) to fill all available space.  If
necessary, you can edit the numbers to maximize your root partition and
leave no space before and after it.

29) Click the resize / move button in the sub window, verify that you
want to make the change, then click the "check" to apply.  Wait for the
changes to finish.

30) Shut down any windows that are open, and shut down GParted.

31) Reboot the system, which should eject the CD.

32) Start up GParted or Disk Utility and verify that the partitions are
what you wanted.  There should be no swap partition.  Your root
partition should be larger, and the others should be as they were.

33) Start System Monitor, click the resources tab.  It should show that
your swap is active, in my case, 8 GB.

34) Verify that your swap file is the only active swap.  From a
terminal:
     sudo swapon -s

35) If you use hibernate, as I do, click the start / stop / power on
screen button, and verify that hibernate is an option, along with
restart and shutdown.  Cancel out of this.

36) If you use hibernate, open up the power preferences and verify that
the hibernate settings you want are there.

I presume that, since Linux apparently uses swap for hibernate, unlike
the Windows hibernate file, that you should have slightly more swap than
you do RAM.

That's it.  Now you have a swap file which you can delete, resize, and
recreate at will without worrying with partitions.

Hope this is helpful.  Feel free to ask any questions about the
procedures.

Sincerely,

Ron

-- 

(PS - If you email me and don't get a quick response, you might want to
call on the phone.  I get about 300 emails per day from alternate energy
mailing lists and such.  I don't always see new messages very quickly.)

Ron Frazier

770-205-9422 (O)   Leave a message.
linuxdude AT c3energy.com




More information about the Ale mailing list