Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Gentoo and read-only root file system.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
cwr
Veteran
Veteran


Joined: 17 Dec 2005
Posts: 1969

PostPosted: Tue Jul 02, 2013 8:57 am    Post subject: Gentoo and read-only root file system. Reply with quote

I need to run Gentoo from an SD Card, and since these are notoriously failure-prone I'd like
to make the card read-only and use a ramdisk for /var, which as far as I can tell is the only
place which has to be writeable.

Someone's done this before - are there any major snags I should watch for?

Thanks - Will
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 54632
Location: 56N 3W

PostPosted: Wed Jul 03, 2013 6:57 pm    Post subject: Reply with quote

cwr,

For root to be read only you need to make /etc/mtab a symlink to /proc/mounts
Thats not needed if you will tolerate /etc/mtab being written.

You will need to put /tmp in RAM too
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
slis
Retired Dev
Retired Dev


Joined: 11 Oct 2010
Posts: 67
Location: Limanowa

PostPosted: Wed Jul 03, 2013 8:05 pm    Post subject: Reply with quote

I have prepared this kind of system about year ago (with kernel 3.2.6). It was based on gentoo, and I've made it 346MB in total.
As far as I can remember, I've done this like this:
1. prepare VirtualBox machine and install some base gentoo on it
2. copy all of this into directory (devel) where I can chroot and update & install packages
3. do some size-optimizations, such as busybox symlinks, remove unnecessary libraries and so
4. copy all necessary files into new directory (release) - that skips linux source, portage tree and some more
5. create two squashfs filesystems - root and usr (separated)
6. prepare disc images (loop filesystems) and copy kernel & root.squashfs into first, and "burn" usr into second
7. this image can be dd'ed into real device (in my situation, it was CF-card)

I have also done some sort of auto-backup, so the system may recover, when something will broke. As I can remember now, I've done some tests, where I've manually damaged filesystem, and all goes auto-recovered.

fstab on this looks like this:
Code:

/dev/sda1               /boot           ext2            noauto,noatime  1 2
/boot/root.squashfs     /               squashfs        noatime,ro      0 0
/dev/sda2               /usr            squashfs        noatime,ro      0 0
/dev/sda3               /backup         ext3            noatime,noauto  0 1
/dev/sda4               /var            ext3            noatime         0 1
tmpfs                   /tmp            tmpfs           size=10M        0 0


I haven't touch this for very long time and many things I just don't remember, but if you wish I could give you the script that creates 'release' from my 'devel' filesystem, so you could try to dig something worthy for you from there.

Regards
Back to top
View user's profile Send private message
cwr
Veteran
Veteran


Joined: 17 Dec 2005
Posts: 1969

PostPosted: Thu Jul 04, 2013 9:28 am    Post subject: Reply with quote

slis - very, very many thanks. That gives me a clear starting point and a good idea
of the way ahead, just what I was looking for.

Will
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Sat Jul 06, 2013 6:05 am    Post subject: Reply with quote

Yeah, nice one slis. Added to my favourite tips under Advanced usage.

I for one would be interested to see your script.
Back to top
View user's profile Send private message
slis
Retired Dev
Retired Dev


Joined: 11 Oct 2010
Posts: 67
Location: Limanowa

PostPosted: Sat Jul 06, 2013 9:38 am    Post subject: Reply with quote

Thanks!

I did not expect such an interest.

As I said if someone would want, then I may publish some more details. But they are very solution-specific, so it may need much improvements before it would be useful for you. Also, the system is quite old for now, but that's the smallest problem :)
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Sat Jul 06, 2013 6:35 pm    Post subject: Reply with quote

slis wrote:
As I said if someone would want, then I may publish some more details. But they are very solution-specific, so it may need much improvements before it would be useful for you.

Sure, but with a few eyeballs we can probably make it generic.
Back to top
View user's profile Send private message
slis
Retired Dev
Retired Dev


Joined: 11 Oct 2010
Posts: 67
Location: Limanowa

PostPosted: Sat Jul 06, 2013 7:13 pm    Post subject: Reply with quote

If someone will help - then, why not?

fun fact: this system was running one Python+QT app, which was auto-started (something like kiosk?) So, if someone will need only console for example, without all X11 & qt stuff, it'll have huge size benefits.

I'll try to refresh this topic and publish something for few weeks, just after I'll finish my current task. I'll let you know here.
Back to top
View user's profile Send private message
trumee
Guru
Guru


Joined: 02 Mar 2003
Posts: 551
Location: London,UK

PostPosted: Fri Oct 31, 2014 5:34 pm    Post subject: Reply with quote

This is exactly what i am trying to do. Did you get around to posting the details?
Back to top
View user's profile Send private message
slis
Retired Dev
Retired Dev


Joined: 11 Oct 2010
Posts: 67
Location: Limanowa

PostPosted: Fri Oct 31, 2014 7:54 pm    Post subject: Reply with quote

It's nice to know that it still usefull for someone.

I'm currently working on preparing LinuxCNC + Gentoo system, which among others would contain: prepared images (about 300-400MB) with full system, and a script for creating one from scratch.

For now, I can paste only some very-very-developement version of script for creating image from directory with gentoo inside.

If you're interested, it looks like this:

1. Download stage3 and portage
2. Create ``ROOT'' directory, make it like your regular '/' (mkdir usr mnt dev proc sys tmp lib etc var bin sbin)
3. Unpack stage3 and portage as usual
4. You should be able to chroot inside. I, for convenience, prepared a script:
Code:

mount -t proc proc ROOT/proc
mount -o bind /dev ROOT/dev

linux32 chroot ROOT /bin/bash

umount ROOT/proc
umount ROOT/dev


You're now inside your new os, which would be then 'burned' into raw image, and then into regular sd, cf or so.
Remember to env-update && source /etc/profile.
Prepare what you need. Emerge all stuff needed. Remember to emerge and build some kernel.

CTRL+D or exit

5. Now it's fun. You have to create some loopback devices, create image, copy files, optionally create squashfs and so on.
I'll post my very-very-dev-dev script, please do modify it to your needs.

What it does:
- creates empty image with four partitions on it (/, /boot, /var, /home) - do change that to your needs
- /boot have to be regular partition, as known on your 'normal' system
- I've left /home and /var as writable space for files
- the rest is squashfs (what implies that you can't emerge anything, can't modify /etc settings and so on)

Remember: change that for your needs, it's created to fit my needs right now, it's not optimized, nor flexible anyway. And I don't have time right now to prepare this that way. I hope I'll do this some day :)

You work with that way:
- create what you want, emerge what you need and config everything on your ROOT (chrooted)
- create_release.sh -> release.img
- dd if=release.img of=/dev/YOUR_DEVICE bs=1M


Code:

losetup -d /dev/loop[012]

TMPROOT=.tmp_root

for f in boot bin lib sys usr sbin etc root home; do
        if [ ! -d release/$f ] ; then
                mkdir release/$f;
        fi
        rsync -avh --delete --exclude include --exclude portage --exclude src ROOT/$f/ release/$f/ ;
done


# remove unnecessary files
rsync -avh --delete ROOT/usr/share/keymaps release/usr/share/
rsync -avh --delete ROOT/usr/share/include release/usr/share/


# YOU MAY WANT TO RSYNC SOME ADDITIONAL FILES HERE
# like your 'kiosk' program files etc


if [ -f release.squashfs ] ; then
        rm release.squashfs
fi
if [ -f release.img ] ; then
        rm release.img
fi

mksquashfs release/ release.squashfs

ROOT_SIZE=$(stat -c%s release.squashfs)
ROOT_SIZE_MB=$(( $ROOT_SIZE / 1024 / 1024 + 2 ))


# There are some constant size declarations, you may want to modify them
# 15M for boot
# 100 + 50 M for home + var in my case
dd if=/dev/zero of=release.img bs=1M count=$(( $ROOT_SIZE_MB + 15 + 100 + 50 ))
echo 'n
p
1

+15M
n
p
2

+'$ROOT_SIZE_MB'M
n
p
3

+100M
n
p
4


w' | fdisk release.img

O1=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f2 | tail -n4 | head -n1`
O2=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f2 | tail -n3 | head -n1`
O3=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f2 | tail -n2 | head -n1`
O4=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f2 | tail -n1 | head -n1`

S1=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f4 | tail -n4 | head -n1`
S2=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f4 | tail -n3 | head -n1`
S3=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f4 | tail -n2 | head -n1`
S4=`fdisk -l release.img | grep release.img | tr -s ' ' ' ' | cut -d" " -f4 | tail -n1 | head -n1`


O1=$(( $O1 * 512 ))
S1=$(( $S1 * 512 ))
O2=$(( $O2 * 512 ))
S2=$(( $S2 * 512 ))
O3=$(( $O3 * 512 ))
S3=$(( $S3 * 512 ))
O4=$(( $O4 * 512 ))
S4=$(( $S4 * 512 ))


losetup -o $O1 --sizelimit $S1 /dev/loop1 release.img
losetup -o $O2 --sizelimit $S2 /dev/loop2 release.img
losetup -o $O3 --sizelimit $S3 /dev/loop3 release.img
losetup -o $O4 --sizelimit $S4 /dev/loop4 release.img
losetup /dev/loop0 release.img


mkfs.ext2 /dev/loop1
mkfs.ext2 /dev/loop3
mkfs.ext2 /dev/loop4
dd if=release.squashfs of=/dev/loop2

mkdir $TMPROOT

mount /dev/loop2 $TMPROOT
mount /dev/loop1 $TMPROOT/boot
mount /dev/loop3 $TMPROOT/var
mount /dev/loop4 $TMPROOT/home

cp -vr release/boot/* $TMPROOT/boot/
rsync -avh release/home/ $TMPROOT/home/


umount $TMPROOT/boot
umount $TMPROOT/var
umount $TMPROOT/home
umount $TMPROOT
rm -rfv $TMPROOT
losetup -D

echo -e "device (hd0) release.img\nroot (hd0,0)\nsetup (hd0)" | grub --batch



Hope I haven't forgot anything. Write for help, when needed :)
Cheers!
Back to top
View user's profile Send private message
N8Fear
Tux's lil' helper
Tux's lil' helper


Joined: 15 Apr 2013
Posts: 140
Location: Berlin (Germany)

PostPosted: Sat Nov 01, 2014 7:46 pm    Post subject: Reply with quote

I think the problem with SD-cards is vastly overstated.
I've been running Gentoo for about 1 1/2 years on a ultra cheap micro sd (from a german discounter called aldi - was something about 6 euro for 16 GB) on my raspberry pi. It's one of the first models with just 256MB ram, so I don't use tmpfs for anything except /dev while I even run emerge locally.
I have had no issues so far.
On the other hand I had 3 normal HDDs dying within 3 months after being bought. Just backup your data (for (small) SDcards dd is relatively quick so you can even have a pre-readied backup card at hand so you just need to reinstall userdata
Back to top
View user's profile Send private message
slis
Retired Dev
Retired Dev


Joined: 11 Oct 2010
Posts: 67
Location: Limanowa

PostPosted: Sat Nov 01, 2014 8:05 pm    Post subject: Reply with quote

I agree that when properly used sd/cf cards are good (safe enough).
Using read-only filesystems has several advantages and in some scenarios is the only real choice.
Back to top
View user's profile Send private message
slis
Retired Dev
Retired Dev


Joined: 11 Oct 2010
Posts: 67
Location: Limanowa

PostPosted: Fri Dec 12, 2014 1:58 pm    Post subject: Reply with quote

If someone is interested, I've created a script for creating gentoo-based system, which also is able to create raw image and write it directly into for example CF/SD card.

There is no WIKI page yet, but I'm working on it.

Basically, it's possible to create "blank" gentoo system, chroot into - look around and build what is needed, then build and write image.
There is no wiki page with tutorials, but --help should be sufficient.

https://sourceforge.net/p/gentoocnc/code/ci/default/tree/gaston/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum