• Raspberry Pi Read-Only File System Raspberry Pi Read-Only File System

    Raspberry Pi Read-Only File System

Raspberry Pi Read-Only File System

There are many reasons to make your file system read-only. The primary reason is to prevent file system corruption from sudden shutdowns. I’ve been building a Raspberry-Pi consumer device for the past year and this was one of the requirements: the user should be able to unplug the device while running, with zero consequence. See my other post on how to move /tmp to RAM. This is a continuation of that discussion.

My information comes from the following posts:

http://blog.pi3g.com/2014/04/make-raspbian-system-read-only/

http://openenergymonitor.org/emon/node/5331

sudo cp /etc/default/rcS /etc/default/rcS.orig
sudo sh -c "echo ‘RAMTMP=yes’ >> /etc/default/rcS"
sudo mv /etc/fstab /etc/fstab.orig
sudo sh -c "echo ‘tmpfs /tmp tmpfs nodev,nosuid,size=30M,mode=1777 0 0’ >> /etc/fstab"
sudo sh -c "echo ‘tmpfs /var/log tmpfs nodev,nosuid,size=30M,mode=1777 0 0’ >> /etc/fstab"
sudo sh -c "echo ‘proc /proc proc defaults 0 0’ >> /etc/fstab"
sudo sh -c "echo ‘/dev/mmcblk0p1 /boot vfat defaults 0 2’ >> /etc/fstab"
sudo sh -c "echo ‘/dev/mmcblk0p2 / ext4 defaults,ro,noatime,errors=remount-ro 0 1’ >> /etc/fstab"
sudo sh -c "echo ‘ ‘ >> /etc/fstab"
sudo mv /etc/mtab /etc/mtab.orig
sudo ln -s /proc/self/mounts /etc/mtab

Now create the following two sripts in /usr/bin

sudo nano /usr/bin/rpi-rw

#!/bin/sh
sudo mount -o remount,rw /dev/mmcblk0p2 /
echo "Filesystem is unlocked – Write access"
echo "type ‘ rpi-ro ‘ to lock"

sudo nano /usr/bin/rpi-ro

#!/bin/sh
sudo mount -o remount,ro /dev/mmcblk0p2 /
echo "Filesystem is locked – Read Only access"
echo "type ‘ rpi-rw ‘ to unlock"

sudo chmod +x /usr/bin/rpi-rw
sudo chmod +x /usr/bin/rpi-ro

After creating the two scripts above, you can now type rpi-rw to make the system temporarily writable, and rpi-ro to lock it down again.

  • Raspberry Pi – Moving /tmp to RAM Raspberry Pi – Moving /tmp to RAM

    Raspberry Pi – Moving /tmp to RAM

Raspberry Pi – Moving /tmp to RAM

Sometimes you want to make sure that any file operations OS does don’t corrupt your file system because of a sudden shutdown. The most common culprit is usually the /tmp folder. Adding the following lines of code to your fstab will make the OS write any temporary files to RAM. This not only saves your SD card from multiple writes, but makes sure that, given no other file system activity, you can just unplug your Pi without consequences:

sudo nano /etc/fstab

tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0
tmpfs /tmp tmpfs defaults,noatime,nosuid,size=100m 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=30m 0 0
tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0
tmpfs /var/run tmpfs defaults,noatime,nosuid,mode=0755,size=2m 0 0
tmpfs /var/spool/mqueue tmpfs defaults,noatime,nosuid,mode=0700,gid=12,size=30m 0 0

I’m posting this with a caveat: I’m no Linux expert. But this, along with making the entire file system (save for a few special folders) read-only, allowed me to build a Raspberry Pi project which can be unplugged at any time while running without any file system corruption. I’ll be making a separate post on how to make the file system read-only in Raspbian very soon.

  • Power Cycling Raspberry Pi USB Hub Power Cycling Raspberry Pi USB Hub

    Power Cycling Raspberry Pi USB Hub

Power Cycling Raspberry Pi USB Hub

 

This is going to be my first post in a long time. Long story short: got married, got fat, got divorced. Back to tinkering with hardware 🙂

I’ve recently been engaged on a project that involves Raspberry Pi and computer vision. The computer vision part is accomplished via a cheap USB cam, using the UVC video driver that ships with most Linux distros. After turning the camera on and off multiple times I noticed that I started getting garbage over the wire. Unplugging the camera and plugging it back in always solved the problem. So I decided to find a way to programmatically power-cycle a specific USB device using a shell script, which I could invoke from my Java code. Here’s what worked for me:

First, you need to allow permissions to power cycle USB devices. I used a VERY CRUDE method, because I’m a noob and decided to abuse chmod:

# Allow USB power cycling via software
sudo chmod 777 /sys/bus/usb/drivers/usb/bind
sudo chmod 777 /sys/bus/usb/drivers/usb/unbind

Once you do this, your application (and ANY application) can now address the RPi’s USB hub. You can shut down individual devices, or the entire hub. Look up how USB device addressing works. If you’re not sure, you can print out what the specific device addresses for the driver you’re using (in my case it’s uvcvideo)

# Print out USB ports to which the camera is connected
echo ‘Device locations:’
ls /sys/bus/usb/drivers/uvcvideo/ | grep ‘1-‘

And here’s the golden nugget. This is how you power cycle a specific USB port:

nano power_cycle_usb.sh

#!/bin/sh
echo ‘Device locations:’
ls /sys/bus/usb/drivers/uvcvideo/ | grep ‘1-‘
echo ‘Unbinding device’
echo ‘1-1.2’ > /sys/bus/usb/drivers/usb/unbind
sleep 0.5
echo ‘Binding device’
echo ‘1-1.2’ > /sys/bus/usb/drivers/usb/bind
echo ‘Power cycle complete’

I’ve tested this for months. Works every time. Just be careful which device you power cycle, because the Ethernet […]