Bashster
Posts: 41
Joined: Sat Jan 19, 2013 3:00 pm

Mounting the SD card read-only

Fri Mar 15, 2013 11:31 pm

Hi all,

I see the Pi mounts the SD card r/w by default, even when the card's little switch is in the locked position.
I'd like it to be mounted r/o, however, in order to eliminate the corruption issues many of us are having. But I'm not sure if this is feasible given the entire OS and everything else is on the one card.
Since the app I'm working on is for an embedded controller, I'm not bothered about losing logging, session data and such similar stuff that the system normally wants to write to the card.
Can I therefore change the defaults to mount the card r/o without adverse consequences?

Thanks!

User avatar
RichardBronosky
Posts: 82
Joined: Thu Feb 14, 2013 5:38 pm
Location: Atlanta, GA, USA

Re: Mounting the SD card read-only

Sat Mar 16, 2013 11:30 pm

I would love to do this for one of my other projects as well. I'd like to partition the SD into 1 FAT and 2 EXT. the 2nd EXT would be a small partition for logging and such. Then it would also mount r/o a USB HD full of ripped movies. The end result is a consumer-proof video player appliance.

Please keep us up to date with what you discover.
-- Bruno Bronosky
1x RPi 1 (B)
1x RPi 1+ (B)
2x RPi 2
11x RPi 3
4x RPi Zero W
2x RPi camera module
3x AIY Voice kits
https://github.com/RichardBronosky/pi-gen-extender
https://github.com/RichardBronosky/raspbian-boot-setup

Bashster
Posts: 41
Joined: Sat Jan 19, 2013 3:00 pm

Re: Mounting the SD card read-only

Sun Mar 17, 2013 1:04 pm

Well it looks like I'm just going to have to do it myself and see what happens, since no one seems to know! I posted the question on the leading Linux forum on the web and only got one reply there from a senior member. For the sake of interest I'll post it here now without attribution:
I suggest you to buy a pendrive or split your sd into two (or more) partitions and move the log files and other files onto another (writable) partition. Changing /var to read only will probably lock the OS (there can be other dirs also).
So basically just what one would expect, but this guy's only speculating like the rest of us.
Time to do some experimenting, then...

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: Mounting the SD card read-only

Sun Mar 17, 2013 1:21 pm

Yes it would be a solution to the FAT16 corruption when you pull the power. In theory disable logging and mount /var on tmpfs. I assume after booting you can unmount /boot

Bashster
Posts: 41
Joined: Sat Jan 19, 2013 3:00 pm

Re: Mounting the SD card read-only

Sun Mar 17, 2013 5:15 pm

Done!
I changed the entries in etc/fstab and now the card is write-protected. There were the expected error messages on re-boot of course (can't create this, can't create that, r-o file system and such like) but the world didn't end and I still had a working, responsive system at the end of it.
This isn't a cure-all for anyone who wants a fully functioning OS at the end of the day, of course, but for my purposes and for anyone else using their Pi solely as a controller for specific applications it's definitely the way to go, IMO. :D

PS: the /var directory structure is now also write-protected; I didn't remount it elsewhere, yet as I said above, there have been no fatal issues resulting. :)

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: Mounting the SD card read-only

Sun Mar 17, 2013 6:19 pm

How about posting the /etc/fstab entries that worked for you, to help others?
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

Bashster
Posts: 41
Joined: Sat Jan 19, 2013 3:00 pm

Re: Mounting the SD card read-only

Sun Mar 17, 2013 8:10 pm

Hmm. Not too convinced of the advisability of doing this as I'm not recommending anyone else simply 'cut n paste' my alteration for their particular purposes which I'm sure will be totally different. But let's say for "discussion purposes only" here's what my fstab looks like now:

Code: Select all

proc            /proc           proc    ro,suid,dev,exec,nouser,async          0       0
/dev/mmcblk0p1  /boot           vfat    ro,suid,dev,exec,nouser,async          0       0
/dev/mmcblk0p2  /               ext4    ro,noatime,suid,dev,exec,nouser,async  0       0
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that
The original contents were:

Code: Select all

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that.
As I say, it works for me; YMMV...

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: Mounting the SD card read-only

Sun Mar 17, 2013 8:47 pm

/proc is a virtual filesystem (not on disk) you're going to have no end of pain making that readonly, like programs not being able to start

the fat16 /boot partition should be fine to be readonly as its only used during booting and never written to (other than when you do a kernel/firmware upgrade) and that's the partition that gets corrupted

/ being ext4 is has journalling anyway so shouldn't get corrupted when the power is yanked

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: Mounting the SD card read-only

Sun Mar 17, 2013 9:48 pm

Thanks for posting that. I would certainly not copy/paste in /etc/fstab, but it helps to have working examples to study when figuring out what will work for me.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

Bashster
Posts: 41
Joined: Sat Jan 19, 2013 3:00 pm

Re: Mounting the SD card read-only

Sun Mar 17, 2013 10:29 pm

The corruption problems I have been having are nothing to do with power loss. I always close down the system properly before switching off the power. But it is daft of the Pi people to suggest that you simply turn the thing off by just cutting the power. You can, I suppose, but not without adverse consequences such as corruption and resultant instability and perhaps data loss.
Many Pi buyers will probably only have experience with Windows and are in the habit of just yanking out thumb drives without unmounting them first. That's a no-no with other OSs like Linux. It's probably the same people who are yanking out the power lead and wondering why it won't boot up again. :lol:

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: Mounting the SD card read-only

Sun Mar 17, 2013 10:40 pm

Bashster wrote:But it is daft of the Pi people to suggest that you simply turn the thing off by just cutting the power.
i think a lot of that is because they didn't include a proper power setup that actually shuts down when you "shutdown -h now"

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: Mounting the SD card read-only

Mon Mar 18, 2013 3:30 am

I dunno, I have no Windows experience, just Linux, and was silly enough to believe the Foundation saying pull out the plug - I think they were just assuming people know ya gotta shut down properly first, in a quick post or conversation, the details don't always get mentioned.

But I also sometimes have to yank the power cord without a proper shutdown as it is the only way to poweroff and reboot if my ssh connection dies and keyboard is not recognized. the one thing I do try really hard to do when that happens, though, is wait until it looks like the ACT light is off. So far, so good, but I also need to perform fsck fairly often. I am suspicious that power spikes may be entering into the equation for me, spike that a surge suppressor cannot do anything about AFAIK when it is power backfeeding from the hub sometimes.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

User avatar
RichardBronosky
Posts: 82
Joined: Thu Feb 14, 2013 5:38 pm
Location: Atlanta, GA, USA

Re: Mounting the SD card read-only

Mon Mar 18, 2013 6:28 pm

-- Bruno Bronosky
1x RPi 1 (B)
1x RPi 1+ (B)
2x RPi 2
11x RPi 3
4x RPi Zero W
2x RPi camera module
3x AIY Voice kits
https://github.com/RichardBronosky/pi-gen-extender
https://github.com/RichardBronosky/raspbian-boot-setup

Bashster
Posts: 41
Joined: Sat Jan 19, 2013 3:00 pm

Re: Mounting the SD card read-only

Tue Mar 19, 2013 11:46 pm

Some useful links there, Richard. Thanks for sharing!

Return to “Raspbian”