Click here to Skip to main content
15,886,362 members
Articles
Tip/Trick
(untagged)

Run Raspbian in Read Only Mode and Increase Lifetime of Your SD Card

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
22 Jun 2014CPOL3 min read 10.9K   3   8  
Run Raspbian in Read Only Mode and Increase Lifetime of Your SD Card

Introduction

I had problems with SD card corruption when my raspberry pi unintentionally loses power. Problem accrues because I had lots of code that communicates with other devices connected to GPIO pins and SPI pins. That communication uses pipes and those pipes have lots of RW operations preformed on SD card that wears out SD card and can cause corruption when Raspberry pi unintentionally loses power. To solve that problem, I have put Raspbian OS in read only mode.

Background

Putting Raspbian OS in read only mode is not so simple as it seems to because there are lots of services that require some write operations on SD card like write to log, write to pipeline, write to temp database, etc. Raspbian ignores lock switch on SD card (lock does not put SD in read only mode !!!!!) it is used only for decorative purposes.

Raspbian enables some cool feature which enables you to use RAM as temporary RW storage for such purposes but you must do little configuring.

All configurations are made in file /etc/fstab.

Using the Code

I will only explain the basic concept that I used in my project, detailed description of how to change fstab file can be found at this site:

In the fstab that I attached in this tip, I mounted:

  • boot

Folder as read only because every service/program that I used does not require write operations on that directories to work. Folders that I mounted as RW in ram memory are:

  • /var/log (I needed RW operations for debugging)
  • /dev/mem (some riparian services require RW on this folder)
  • /dev/spidev0.0 (I needed RW operations because SPI communication uses pipes)
  • /sys/class/gpio (I needed RW operations because GPIO communication uses pipes)
  • /dev/mem (some riparian services require RW on this folder)
  • /proc (some riparian services require RW on this folder)
  • /boot (some riparian services require RW on this folder)

In runtime, you can dynamically change read only mount to rw mount using the following commands:

// Return read only mounted folders to RW mode

     sudo mount / -o remount,rw

// Return to read only mode

     sudo mount / -o remount,ro

// This command gives you information who is using drive if previous commands failed

    fuser -v ./ ko koristi drive

If you want to put your folder to read only mode, use the following in fstab file:

 # <device>     <mountpoint>                <filesystemtype><options>       <dump>  <pass>

tmpfs           /dev/mem                   vfat   defaults,size=10M            0       0

<device> tmpfs is a temporary filesystem that resides in memory and/or your swap partition(s), depending on how much you fill it up. Mounting directories as tmpfs can be an effective way of speeding up accesses to their files, or to ensure that their contents are automatically cleared upon reboot.

<mountpoint> /dev/mem folder that you want to put to read only access

<filesystemtype><options> vfat defaults,size=10M file system (fat, ext,...) size of 10 MB maximal size in ram memory that can be used to store data.

<dump> The fifth field, (fs_freq), is used for these file systems by the dump(8) command to determine which file systems need to be dumped. If the fifth field is not present, a value of zero is returned and dump will assume that the file system does not need to be dumped.

<pass> The sixth field, (fs_passno), is used by the fsck(8) program to determine the order in which file system checks are done at reboot time. The root file system should be specified with a fs_passno of 1, and other filesystems should have a fs_passno of 2. File systems within a drive will be checked sequentially, but filesystems on different drives will be checked at the same time to utilize parallelism available in the hardware. If the sixth field is not present or zero, a value of zero is returned and fsck will assume that the file system does not need to be checked.

History

  • First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Croatia Croatia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --