2014年4月1日火曜日

How to use zram

By default, Raspbian uses swap file in sdcard for its easy setup.
Swap is generally helpful when system memory is getting less.
But, swap in sdcard partition is a problem since it shortens life of sdcard hardware by frequent data write for swap out. Also, it has slow performance problem.

Therefore, 'zram' is one solution to solve the issues.
It is a kernel feature to create compressible swap partition on ram.

Following instruction explains how to enable it for your raspberry pi.

Disable default swap setting

# sudo apt-get install chkconfig
# sudo chkconfig dphys-swapfile off


Enable swap on zram

Load zram module.

# modprobe zram

Set 128MB zram.

# echo $((128*1024*1024)) > /sys/block/zram0/disksize

Create swap on the zram.

# mkswap /dev/block/zram0

Enable swap.

# swapon -p 10 /dev/block/zram0

How to move rootfs to external usb storage

This instruction is valid for linux OS.

Prepare raspbian image
Download wheezy-raspbian image from official site.

http://www.raspberrypi.org/downloads

Unzip downloaded file and put img file under working directory.
Also create following directory for mount target.

  • mnt_src_rootfs
  • mnt_dist_rootfs

Flash image to sdcard
We don't need rootfs on sdcard finally. But it is useful to check device entry for your external usb storage in this instruction.

# sudo dd bs=1M if=2014-01-07-wheezy-raspbian.img of=/dev/sdc


Mount partitions in image
Check wheezy-raspbian image file with fdisk.

# fdisk -l -u 2014-01-07-wheezy-raspbian.img

Disk 2014-01-07-wheezy-raspbian.img: 2962 MB, 2962227200 bytes
255 heads, 63 sectors/track, 360 cylinders, total 5785600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

 
Device Boot Start End Blocks Id System
2014-01-07-wheezy-raspbian.img1 8192 122879 57344 c W95 FAT32 (LBA)
2014-01-07-wheezy-raspbian.img2 122880 5785599 2831360 83 Linux

 

Mount 2nd partition with offset.

# sudo mount -o loop,offset=$((122880*512)) 2014-01-07-wheezy-raspbian.img mnt_src_rootfs


Prepare external usb storage

Create ext4 partitions on your extarnal usb storage by fdisk and mkfs.
And mount it as 'mnt_dist_rootfs'.

Copy rootfs

Copy all rootfs files from img file to extarnal usb storage.

# sudo cp -ax mnt_src_rootfs/. mnt_dist_rootfs

Boot Raspberry pi

Insert sdcard and bootup. (Here, rootfs on sdcard is used.)
On raspbian,
  1. login
  2. connect usb extarnal usb storage.
  3. execute dmesg
  4. Get device entry for the ext4 partition on extarnal usb storage
I will assume your device entry is '/dev/sdc1'

Modify cmdline.txt


On raspbian,

# nano /boot/cmdline.txt

Modify following path to device entry of rootfs partition on extarnal usb storage.

root=/dev/sdc1


Sync and reboot rasbian.


# sync
# reboot 0

That's it.