Monday 2 July 2007

Using a file as a Swap Partition for Ubuntu

I have one internal hard disk in my laptop Compaq Presario 1500 with 4 partitions
XP, Ubuntu, Solaris 10, Pc-BSD. So I can't create another partition to use as a swap for Ubuntu. But I have one large external hard disk connected by USB, so I used a file in this disk as a swap partition.
First I created a 1GB file in the disk (already mounted as /diskD/) :

sudo dd if=/dev/zero of=/diskD/swapubuntu bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 86.4074 seconds, 12.4 MB/s

Afterwards :
sudo mkswap /diskD/swapubuntu
Setting up swapspace version 1, size = 1073737 kB
no label, UUID=6da4119f-3329-4935-a921-10a5cbe521be

Then :
sudo swapon /diskD/swapubuntu

Then just to check I ran the following command :
free
total used free shared buffers cached
Mem: 515852 509052 6800 0 100460 170968
-/+ buffers/cache: 237624 278228
Swap: 1048568 0 1048568


In order to have the swap activated in every reboot I modified the file /etc/init.d/rc.local
sudo gedit /etc/init.d/rc.local
Adding the following line :
swapon /diskD/swapubuntu
Here my rc.local :

#! /bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin
[ -f /etc/default/rcS ] && . /etc/default/rcS
. /lib/lsb/init-functions

do_start() {
if [ -x /etc/rc.local ]; then
log_begin_msg "Running local boot scripts (/etc/rc.local)"
/etc/rc.local
log_end_msg $?
/home/ubuntu/unhide_ICH_SMBus
swapon /diskD/swapubuntu
fi
}

case "$1" in
start)
do_start
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac

No comments: