External disc drive setup
I need to attach a decent size disc drive to my Pi to store radio recordings from my PVR. It will also allow me to create files, and use swap space, without worrying about wearing out the SD card. Given that I’m using a disc drive, it makes sense to put all the Pi’s software on it, relegating the SD card to a “bootloader”.
I found a useful guide to running a Pi from an external disc drive on raspberrypihobbyist.blogspot.co.uk. I’ve done things a bit differently, of course…
Firstly, I created four partitions: 2 of 8GB for “system” and “spare”, 2GB swap and the remainder of the disc (about 212GB) for “home”. Then I created and enabled the swap space, then formatted the other partitions:
fdisk /dev/sda
mkswap /dev/sda3
swapon /dev/sda3
mkfs.ext4 /dev/sda1
mkfs.ext4 /dev/sda2
mkfs.ext4 /dev/sda4
The next stage is to copy everything needed from the SD card, and create other directories and mount points:
mkdir /mnt/system
mount /dev/sda1 /mnt/system
cd /mnt/system
rsync -av /{bin,etc,lib,opt,root,sbin,selinux,srv,usr,var} .
mkdir boot dev home media mnt proc run spare sys tmp
mkdir /mnt/home
mount /dev/sda4 /mnt/home
cd /mnt/home
rsync -av /home/* .
Finally, edit /boot/cmdline.txt
and /mnt/system/etc/fstab
:
/boot/cmdline.txt:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait
/mnt/system/etc/fstab:
proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults,ro 0 2
/dev/sda1 / ext4 defaults,noatime 0 1
/dev/sda2 /spare ext4 defaults,noatime 0 1
/dev/sda3 none swap sw 0 0
/dev/sda4 /home ext4 defaults,noatime 0 1
On reboot, check that everything’s mounted as expected:
root@firefly:~# df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 7.9G 2.0G 5.6G 27% /
/dev/root 7.9G 2.0G 5.6G 27% /
devtmpfs 235M 0 235M 0% /dev
tmpfs 49M 264K 49M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 507M 0 507M 0% /run/shm
/dev/mmcblk0p1 56M 19M 38M 33% /boot
/dev/sda2 7.9G 146M 7.4G 2% /spare
/dev/sda4 212G 190M 201G 1% /home
root@firefly:~# free -h
total used free shared buffers cached
Mem: 485M 59M 426M 0B 6.2M 27M
-/+ buffers/cache: 25M 460M
Swap: 2.0G 0B 2.0G
root@firefly:~#
Make /tmp a ramdisc
For optimum performance it’s nice to have /tmp
in memory instead of being written to disc. With a large swap partition I needn’t worry about running out of memory. If /tmp
begins to fill up its least used files will be swapped out, leaving the active ones in memory.
All this requires is one extra line in /etc/fstab
:
tmpfs /tmp tmpfs size=256M 0 0