Saturday, April 28, 2007

Partition SD card and create swap file

If you are gonna compile software on your zaurus, you should set up a swap file.

Swap file must be on a ext3 partition, if you don't want the whole card to be formatted as ext3, then you need to partition the card.

1. Make sure the SD card is unmounted.
#umount /mnt/card

2. Partition the SD card by cfdisk.
#cfdisk /dev/mmcd/disc0/disc
Set both partitions to primary, first partition type is 06 (FAT), second partition type is 83 (Linux). Then write and exit.


3. Format the partitions.
#mkfs.vfat /dev/mmcd/disc0/part1
#mkfs.ext3 /dev/mmcd/disc0/part2

4. Create swap file.
#mkdir /mnt/swap
#mount -t ext3 /dev/mmcd/disc0/part2 /mnt/swap
#cd /mnt/swap
#dd if=/dev/zero of=swapfile bs=1M count=64
#mkswap swapfile
A 64MB swap file will be created, changing the count value will change the swap file size.


5. Testing the swap file.
#free
#swapon swapfile
#free
#swapoff swapfile
free will show you the current memory usage, and you should be able to see the difference with the swap on/off.


6. Unmounting the swap file
#cd
#umount /mnt/swap

Now, what we need to do is to edit some system files so that swap will be turned on/off when SD card is inserted/removed.

Edit /etc/fstab and add the following line
...
/dev/mmcd/disc0/part2 /mnt/swap auto noauto,owner,noatime 0 0
...

Edit /etc/sdcontrol as followed
...
DEVICE=/dev/mmcd/disc0/part1
DEVICE2=/dev/mmcd/disc0/part2
MOUNT_POINT=/mnt/card
MOUNT_SWAP=/mnt/swap
SWAP_FILE=/mnt/swap/swapfile
...
'insert')
...
mount $FSTYPE $DEVICE $MOUNT_POINT
mount $DEVICE2 $MOUNT_SWAP
...
swapon $SWAP_FILE
;;
'eject')
swapoff $SWAP_FILE
...
umount $MOUNT_POINT
umount $MOUNT_SWAP
...
;;
'comeject')
...
umount $MOUNT_POINT
umount $MOUNT_SWAP
...
usleep 500000
umount $MOUNT_POINT
umount $MOUNT_SWAP
...
;;

If you have followed the commands above, the SD card should be unmounted. Remove the SD card and reinsert it. Swap should be mount automatically (use free to check that). And if you unmount the SD card by applet or by command /etc/sdcontrol stop, swap should be unmounted.