alexchamberlain
Posts: 121
Joined: Thu Jun 14, 2012 11:20 am
Location: Leamington Spa, UK

Re: Boot with SD readonly?

Thu Jul 19, 2012 6:52 am

This should be as simple as editing /etc/fstab and changing everything to read-only. Having said this... consider using a different partition for /var (possibly even a tmpfs), so your RPi can still log stuff - Linux likes logging!
Developer of piimg, a utility for working with RPi images.

User avatar
jzu
Posts: 26
Joined: Mon Aug 01, 2011 8:02 am

Re: Boot with SD readonly?

Fri Jul 20, 2012 11:21 am

Here's my /etc/fstab which has been successfully tested by unplugging the power supply while running massive read operations (find /).

Code: Select all

proc            /proc     proc    defaults 0 0
/dev/mmcblk0p1  /boot     vfat    ro       0 0
/dev/mmcblk0p2  /         ext4    ro       0 0
none            /var/run  ramfs   size=1M  0 0
none            /var/log  ramfs   size=1M  0 0
Any idea to improve this setup, or to torture the system in various ways in order to find possible flaws?

jerrylamos
Posts: 27
Joined: Sun Jul 15, 2012 8:33 pm

Re: Boot with SD readonly?

Wed Jul 25, 2012 2:00 am

The small distro Puppy runs in memory and doesn't write until shutdown optionally. I don't know how much it could do with 192 MB (rest of the memory for the GPU). They're working on Raspberry Pi versions see
http://bkhome.org/blog/?viewDetailed=02910.
The SD image is
http://distro.ibiblio.org/quirky/arm/te ... 105.img.xz
at 87.1 MB. I haven't tried it to see what packages it has. Puppy typically has internet with flash available, utilities, word processor, file manager, various display configs etc. but I don't know about this one.

Jerry

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK

Re: Boot with SD readonly?

Thu Jul 26, 2012 1:20 pm

Puppy on RPi won't have flash, as Adobe don't have a publicly-available version of Flash for Linux for the ARMv6 CPU architecture the Pi uses. Everything else should be available though, as any open-source apps are usually easy to compile for different CPU architectures.

mji
Posts: 16
Joined: Sat Jul 07, 2012 12:51 am
Location: Los Angeles, California

Re: Boot with SD readonly?

Wed Oct 24, 2012 5:35 am

Is there some way of physically write protecting the SD card, insuring that the raspberry pi cannot write to it?

itimpi
Posts: 1090
Joined: Sun Sep 25, 2011 11:44 am
Location: Potters Bar, United Kingdom

Re: Boot with SD readonly?

Wed Oct 24, 2012 5:42 am

mji wrote:Is there some way of physically write protecting the SD card, insuring that the raspberry pi cannot write to it?
I am afraid not. The slider switch on SD cards is advisory only to the host system, and anyway on the Pi is not even wired up in the pi's SD socket.

mji
Posts: 16
Joined: Sat Jul 07, 2012 12:51 am
Location: Los Angeles, California

Re: Boot with SD readonly?

Wed Oct 24, 2012 5:49 am

I've done a bit of googling and it seems that there are 3 ways of write protecting an sd card... One is the the physical switch that can be ignored by a reader. The others involve setting particular bits on the SD card. One of the bits cannot be re-written once it is written (permanent write protection) What I'm not sure I'm understanding is if these bits are also simply read by the card reader (which can then also decide to ignore them) or if the firmware in the card will prevent further writes. I think it's the latter, but I was wondering if anyone has any experience with this and the raspberry pi.

dms75
Posts: 25
Joined: Mon Aug 06, 2012 1:41 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 8:54 am

Hi,

since I have used sdcards in several microcontroller related projects,
I might as well give some hints how it could be done.

In theory you can set the write protect Bits in the CSD register of the sdcard.
Accessing them on the Pi is not quite so easy though.
First those writeprotect-bits are processed by the sdcard itself and provide real protection,
unlike the slider on the card where it's in the responsibility of the cardreader
to respect the writeprotection setting (which it doesn't).
Physically those bits are implemented in flash cells, even the permanent write protect bit,
but the sdcard firmware won't let you reset the permanent one once it has been set to 1.
The permanent write protect bit can easily render your card useless, you won't be able to format it.
Not even a force_erase operation will recover the card.
It will really be read only rom-like card after setting this bit.
So be warned.
For testing stay with the temporary write protect bit wich will suffice for your needs,
since it cannot easily be set or reset by the regular RPi sdcard slot, which brings us to the tricky part.

The problem will be to send the sdcard the appropriate PROGRAM_CSD command (CMD27)
with the right writeprotect bits set (temporarily or permanently).
The spi driver in the kernel will only give you access to write normal data to the blockdevice
and you don't have access to send specific commands to the card.
So you'd have to modify the spi driver to provide extra functionality.
Unless you have experience in writing linux drivers this is probably not the easiest approach to take.
It might be easier to wire the SDCARD to the GPIO Port in SPI Mode and send the initialising
sequence and the CMD27 to the card wia the SPI port or via Bit-banging.
This will require connecting of a second sdcard slot to the GPIO Pins.
You'll only need this slot to set the write protection, once the card is write protected
you'll use the card in the regular SDCARD slot.
Ofc. you could use a microcontroller to write-protect the sdcard in the first place,
if you happen to have a microcontroller developmentboard lying around -
preferably one already fitted with a sdcard slot ;-)
Software for bitbanging GPIOs and to access sdcards wia spi protocol should be easy enough
to find on the net, most microcontrollers communicate with sdcards this way,
and most microcontroller verndors provide spi libraries or even sdcard libraries for communication.
If you want to take that approach I could provide you with the spi command sequences needed
to initialize the communication and to write protect the card if you need them.

Dirk

milhouse
Posts: 642
Joined: Mon Jan 16, 2012 12:59 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 9:33 am

mji wrote:Is there some way of physically write protecting the SD card, insuring that the raspberry pi cannot write to it?
Not in hardware, presumably a firmware change would be required to prevent all writes to SD card (hmmm, wonder if Dom would consider adding a cmdline.txt/config.txt option to disable SD card writes?)

Edit: Or maybe the cmdline.txt/config.txt "write protect" option could simply set the temporary CSD writeprotect bit (as described by dms75) during boot...?

dms75
Posts: 25
Joined: Mon Aug 06, 2012 1:41 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 10:02 am

milhouse wrote:
mji wrote:Is there some way of physically write protecting the SD card, insuring that the raspberry pi cannot write to it?
Not in hardware, presumably a firmware change would be required to prevent all writes to SD card (hmmm, wonder if Dom would consider adding a cmdline.txt/config.txt option to disable SD card writes?)

Edit: Or maybe the cmdline.txt/config.txt "write protect" option could simply set the temporary CSD writeprotect bit (as described by dms75) during boot...?
someone that used such a config.txt option would have a hard time getting rid of this option since when set the /boot directory will be readonly...
It will even still be readonly when used on annother PC in a cardreader, and I'm not sure wether a format from a PC actually does a force_erase on the sdcard, probably it would just fail.
I suppose a jumper between Pin number 6 and pin Number 4 on the GPIO Header will still load the emergency kernel and causes the firmware to ignore the config.txt, so it would still allow to recover form this situation if the firmware would reset the temporary CSD writeprotect bit when starting the emergency kernel.

Dirk

milhouse
Posts: 642
Joined: Mon Jan 16, 2012 12:59 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 10:14 am

dms75 wrote: It will even still be readonly when used on annother PC in a cardreader
Ah, so what you described as a "temporary" setting, I took to mean would be cleared when power is removed from the SD card (which would allow the config to be edited in a PC). So it's a non-volatile setting, but which isn't write-once. My misunderstanding. Still, one option would be as you described - boot the emergency kernel to clear the write protect bit (or a vcgencmd command to clear it?)

Such a straight forward feature would be hugely appealing to the embedded market.

dms75
Posts: 25
Joined: Mon Aug 06, 2012 1:41 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 1:00 pm

milhouse wrote:
dms75 wrote: Such a straight forward feature would be hugely appealing to the embedded market.
The proper way to do it would probably be an extension to the kernel sdcard driver that could be used in the manner of
"echo 1 > /sys/class/mmc_host/mmc0/mmc*/temporary_writeprotect"
That would allow to set and to unset this Bit. It could be implemented for the permanent write protection bit too, just not resettable ofc. It is not necessary to touch the bootloader at all for this kind of functionallity.

Dirk

User avatar
jojopi
Posts: 3653
Joined: Tue Oct 11, 2011 8:38 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 1:21 pm

dms75 wrote:The proper way to do it would probably be an extension to the kernel sdcard driver that could be used in the manner of
"echo 1 > /sys/class/mmc_host/mmc0/mmc*/temporary_writeprotect"
If you did this while any partition was mounted read-write then the card would effectively be locked in a dirty state, and the system would likely crash, and on future boots fsck would not be able to repair the filesystem. And if the distro expects to be able to mount the root filesystem for writing, then it will probably not even boot correctly with the card protected.

So basically this whole idea is wrong. It is the operating system that needs to support not writing to the card. You cannot try to impose the restriction upwards from the hardware.

User avatar
jzu
Posts: 26
Joined: Mon Aug 01, 2011 8:02 am

Re: Boot with SD readonly?

Wed Oct 24, 2012 1:28 pm

Remember my previous post with the /etc/fstab file? It works with minimal hassle, plus you can modify the contents on another computer.

dms75
Posts: 25
Joined: Mon Aug 06, 2012 1:41 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 1:48 pm

jojopi wrote: So basically this whole idea is wrong. It is the operating system that needs to support not writing to the card. You cannot try to impose the restriction upwards from the hardware.
I totally agree that for itself it would not be very usefull. Setting up the whole system with a initrd and ro mount of the root filesystem should provide ample protection against sudden power loss. Additionally setting the WP Bit would however give some additional safeguard against remounting the sdcard rw or low level write access with dd, gparted, fdisc etc. This is probably not relevant unless you wanted to restrict users from messing with the card (as in setting the permanent write protection bit to prevent your sister/brother/wife from borrowing the card for the camera - just kidding :twisted: ).

Dirk

milhouse
Posts: 642
Joined: Mon Jan 16, 2012 12:59 pm

Re: Boot with SD readonly?

Wed Oct 24, 2012 1:59 pm

Yes, it really was for belt and braces - of course if the OS expects to be able to write to the card, then it's going to end in major failure if it discovers the medium write protected... :)

ckristofek
Posts: 1
Joined: Tue Dec 11, 2012 4:26 pm

Re: Boot with SD readonly?

Mon Jan 14, 2013 9:13 pm

dms75 wrote:Hi,

If you want to take that approach I could provide you with the spi command sequences needed
to initialize the communication and to write protect the card if you need them.

Dirk
Dirk,

I am interested in making an SD card read-only. I'd like to take you up on your offer on the spi command sequences needed to init and write protect the card. I can use a Pi or an Arduino Uno (which ever is easiest).

Chuck

dms75
Posts: 25
Joined: Mon Aug 06, 2012 1:41 pm

Re: Boot with SD readonly?

Wed Jan 16, 2013 1:49 pm

Hi

I just sent you the needed spi command sequences to initialize and writeprotect a sdcard by setting the writeprotectbits in the CSD Register via a pm.
You can use this sequence if you connect the sdcard to the raspberry pi gpio header SPI Pins or with an external microcontroller board, wichever seems easier to you.

In both cases you'll need access to the SPI periferal or need to do SPI by bitbanging some IOs which will be slow, but not really a problem in this case since there is not that much data to send. For the connection you'll need some sort of cardholder and need to connect the cards Ground pins to the GPIO GND, the cards Powersupply Pins to the GPIO 3.3V Pin. You can tie the CS pin to GND too since we only need it to be '0' during CMD0, and after that it doesn't hurt to have the card selected all the time either, ofc. you can also connect it to the SPI CS pin from the SPI Module provided that it is active low ('0' during transmission). Tie unused card dataline inputs to 3.3V. Finally connect the RPi SPI MOSI pin (RPi SPI data out) to the Cards command input pin, the RPi SPI MISO (RPi SPI data input) pin will connect to the cards dataout pin, and the RPi SCLK pin provides the clock signal for the sdcard.
If you lack a sdcard holder and your microcontroller developmentboard doesn't have a sdcard slot either, and if you can live with using a Microsdcard you could solder some wires to an SDCARD Adaptor which comes for free with most microsdcards. You would need a second adaptor that was not handsoldered on for using your microsdcard in the RPi afterwards.
For the Pinout of sdcards a quick search in wikipedia should bring up the pin descriptions easily enough.

Dirk

jeanrocco
Posts: 9
Joined: Sun Jan 27, 2013 4:11 pm

Re: Boot with SD readonly?

Mon Jan 28, 2013 2:02 am

jzu wrote:Here's my /etc/fstab which has been successfully tested by unplugging the power supply while running massive read operations (find /).

Code: Select all

proc            /proc     proc    defaults 0 0
/dev/mmcblk0p1  /boot     vfat    ro       0 0
/dev/mmcblk0p2  /         ext4    ro       0 0
none            /var/run  ramfs   size=1M  0 0
none            /var/log  ramfs   size=1M  0 0
Any idea to improve this setup, or to torture the system in various ways in order to find possible flaws?
This means you run your RP with the original fstab when you are installing/compiling/modifying new soft, and when everything works, you use this modified fstab ? did I get this right ? suis meilleur en français ..

User avatar
jzu
Posts: 26
Joined: Mon Aug 01, 2011 8:02 am

Re: Boot with SD readonly?

Mon Jan 28, 2013 11:45 am

jeanrocco wrote:This means you run your RP with the original fstab when you are installing/compiling/modifying new soft, and when everything works, you use this modified fstab ? did I get this right ? suis meilleur en français ..
Actually, this is my default fstab. Prior to to editing/modifying anything, I use the following command.

Code: Select all

# mount / -o remount,rw
Next reboot will see filesystems back to readonly.

Je préférerais écrire en français, moi aussi, but this is an international forum after all. ;)

User avatar
dudeman
Posts: 22
Joined: Sun Jan 20, 2013 1:20 am
Location: USA

Re: Boot with SD readonly?

Mon Jan 28, 2013 5:52 pm

I have been thinking about this scenario. The meta solution I have is:

1. Bootstrap from SD card

2. Boot linux into RAM like KNOPPIX

3. Write any appropriate changes to user data (settings) back to SD card as a user initiated action, with warning not to remove power while saving

Even if you don't need step 3, this design would protect the SD card data.

Sorry if I missed where you already explained how KNOPPIX would not work.

jeanrocco
Posts: 9
Joined: Sun Jan 27, 2013 4:11 pm

Re: Boot with SD readonly?

Wed Feb 06, 2013 9:14 pm

jzu wrote:
jeanrocco wrote:This means you run your RP with the original fstab when you are installing/compiling/modifying new soft, and when everything works, you use this modified fstab ? did I get this right ? suis meilleur en français ..
Actually, this is my default fstab. Prior to to editing/modifying anything, I use the following command.

Code: Select all

# mount / -o remount,rw
Next reboot will see filesystems back to readonly.

Je préférerais écrire en français, moi aussi, but this is an international forum after all. ;)
Well thank you, the remount is a good solution, I had to use a another Linux machine to revert to the old fstab, still learning :-), aurevoir !

obcd
Posts: 917
Joined: Sun Jul 29, 2012 9:06 pm

Re: Boot with SD readonly?

Thu Feb 07, 2013 1:09 pm

Is there a way to remout read only again after you mounted with rw rights, without the need to reboot the system?
I tried this but couldn't get it to work.
Some stuff doesn't like "read only root fs" very much like smb sharing a folder. (read only of cause)
I think dhcpd is another one, as it likes to store the dns servers.
logging should be possible on a tmpfs folder in ram.
Some routines expect the files they use for logging to already exist there.

Motlib
Posts: 2
Joined: Mon Aug 06, 2012 6:12 pm

Re: Boot with SD readonly?

Fri Feb 15, 2013 9:29 am

Is there a way to remout read only again after you mounted with rw rights, without the need to reboot the system? I tried this but couldn't get it to work.
You can use

Code: Select all

mount / -o remount,rw
for read-write operations and

Code: Select all

mount / -o remount,ro
to mount the filesystem read-only again. You can check by executing mount without parameters if a filesystem is mounted read-only or read-write.

<advertising>In the last few days I have tried to set up a read-only root filesystem and it works quite well. You can find a few hints in my blog post here: http://blog.a-netz.de/2013/02/read-only ... ilesystem/. There are also two posts about how to work with ramdisks to prevent too much write cycles to the sd card: http://blog.a-netz.de/2013/02/ramdisks- ... raspberry/ and http://blog.a-netz.de/2013/02/persisten ... -ramdisks/.

oweno
Posts: 29
Joined: Sat Feb 16, 2013 3:40 pm

Re: Boot with SD readonly?

Sun Feb 17, 2013 2:12 pm

I have been mounting root read only (using the fstab posted here):

Code: Select all

proc            /proc     proc    defaults 0 0
/dev/mmcblk0p1  /boot     vfat    ro       0 0
/dev/mmcblk0p2  /         ext4    ro       0 0
none            /var/run  ramfs   size=1M  0 0
none            /var/log  ramfs   size=1M  0 0

power cycling many times so far, I have not had any problems!

But does this really eliminate all write operations to SD card? I ran across some confusing messages in /var/log/messages, trying to understand the boot process:

Code: Select all

...
Feb 16 16:38:16 raspberrypi kernel: [    1.283769] mmc0: new high speed SDHC card at address b368
Feb 16 16:38:16 raspberrypi kernel: [    1.287000] mmcblk0: mmc0:b368       3.73 GiB
Feb 16 16:38:16 raspberrypi kernel: [    1.291177]  mmcblk0: p1 p2
Feb 16 16:38:16 raspberrypi kernel: [    1.348654] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
Feb 16 16:38:16 raspberrypi kernel: [    1.360825] VFS: Mounted root (ext4 filesystem) on device 179:2.
...
and then later on

Code: Select all

...
Feb 16 16:38:16 raspberrypi kernel: [   12.863151] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
...
seems the card is mounted in ordered data mode, and then remounted later on (with Opts null, even though ro is specified in fstab) ... Can anyone shed some light on the boot process?

Return to “Advanced users”