Forum

Full Version: Customize initramfs SD fallback
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everybody, I'm here to ask help in doing something I've never had to do before in my past Linux experience: customize the initramfs.

Problem: I want to run XBian on my RPi model B from USB hard drive, but I want to be able to power off my RPi and disconnect the USB disk while still being able to boot my RPi.

To achieve such goal I was initially thinking to just use the recovery shell and have two lines in my cmdline.txt file, one commented out so just a quick edit could do. Sadly my USB Cherry G320 keyboard is not recognized by the kernel modules and I'm back to square one.

I was thinking I can put a simple shell script in the recovery console init sequence: check if the USB disk is connected and if it is not try to switch the cmdline arguments and continue booting.

The problem is I don't know where I can put such script so to see it executed when entering the rescue console. I know I have to alter the initramfs.gz content and I can use the /etc/initramfs-tools folder, but by putting a simple

echo "Testing now!!!!"

inside a test.sh file in the scripts/premount-init and/or scripts/premount-local folder didn't produce any output change in my boot sequence (and yes, I did run the xbian-update-initramfs and I did uncomment the initramfs directive in the config.txt file).

Now I'm questioning myself regarding my comprehension of initramfs...

Should the scripts located in those folders get executed before the root filesystem gets mounted?
What is the difference between those prefixed with init and those with local?
Where those scripts should end up in the newly created initramfs?
Should them be visible from within the rescue console?


My plan for the sd-fallback.sh script is:
1. check for usb disk by LABEL/UUID (blkid)
2. check for /rootfs content (ls)
3. in case one of the above fails and cmdline.txt.usb does not exist
3.1. copy current cmdline.txt onto cmdline.txt.usb
3.2 copy cmdline.txt.sd onto cmdline.txt (force)
3.3 exit rescue mode

I believe this could be useful in many situations....

Any help is appreciated
No, as i said in the other topic. You have to alter the files in /etc/xbian-initramfs/.

I would suggest you put the following in the init file at line 182:
Code:
. /sd-fallback.sh || drop_shell

Of course, make sure the sd-fallback script is executable and exits with either 0 for success or 1 for failure.

Just put the sd-fallback.sh script in the /etc/xbian-initramfs/ folder and add the following line to the update-initramfs.sh file after cp /etc/xbian-initramfs/splash_updater.sh ./
Code:
cp /etc/xbian-initramfs/sd-fallback.sh ./
(31st Jul, 2014 06:20 PM)CurlyMo Wrote: [ -> ]No, as i said in the other topic. You have to alter the files in /etc/xbian-initramfs/.

I would suggest you put the following in the init file at line 182:
Code:
. /sd-fallback.sh || drop_shell

Of course, make sure the sd-fallback script is executable and exits with either 0 for success or 1 for failure.

Just put the sd-fallback.sh script in the /etc/xbian-initramfs/ folder and add the following line to the update-initramfs.sh file after cp /etc/xbian-initramfs/splash_updater.sh ./
Code:
cp /etc/xbian-initramfs/sd-fallback.sh ./

Ok, now I better understand the initramfs creation process Wink

I'll let you know and eventually attach my script for other to reuse.
Ok, I have it running now and it works nicely!

I'm going to write a blog post on this, but in the meanwhile:

Please find the little shell script attached. The script accepts one option which can either be fallback which tries to restore the SD as root filesystem, or restore which tries to restore the USB hard drive as rootfs.

The modified init script is attached as well: the initial suggestion to slightly alter line 182 wasn't perfect as that line happen to get executed too late in the init process, but the suggestion was more or less correct.

The altered init scripts tries to fallback to SD card root fs and, if it succeeds, it automatically restart your RPi to apply the modifications.

Don't worry the script doesn't keep rebooting your RPi indefinitely Wink

The other steps are the same as suggested by CurlyMo and I'll update this post to a link to my blog once I've the post published.

To use this script you need to copy your cmdline.txt file into cmdline.sd.txt before altering it to use the USB as rootfs: this file will be used by the fallback proceedure. Once the fallback proceedure is applied, in case you want to restore your USB rootfs you just have to execute the same script with the restore option.

Thanks a million to CurlyMo and everybody else.

Next step: automatically sync the root filesystem between USB and SD: going to open another topic!
@rlogiacco

to merge idea like this into XBian I would prefer more generalised approach. let's define some fix points during initramfs run (after-virtual-filesystems; before-root-mount; after-network-done; before-move-root).

then we can create folders
Code:
/etc/xbian-initramfs/after-virtual-filesystems
......before-root-mount
......after-network-done
......before-move-root

then any script existing there will be always copied to initramfs and started at the specific stage. that way we never have to update init script or update-initramfs again. user can just put / remove scripts from the directories.
(6th Aug, 2014 09:02 PM)mk01 Wrote: [ -> ]@rlogiacco

to merge idea like this into XBian I would prefer more generalised approach. let's define some fix points during initramfs run (after-virtual-filesystems; before-root-mount; after-network-done; before-move-root).

then we can create folders
Code:
/etc/xbian-initramfs/after-virtual-filesystems
......before-root-mount
......after-network-done
......before-move-root

then any script existing there will be always copied to initramfs and started at the specific stage. that way we never have to update init script or update-initramfs again. user can just put / remove scripts from the directories.

Totally with you! Please consider though I had to mount /boot inside my script as it was not mounted yet at that stage: to avoid any possible mess I unmounted it as well.

If those dirs did exist I would definitely have used them... I actually tried to use those named premount-init and premount-local under /etc/initramfs-tools but they aren't used for the purpose I thought....
@rlogiacco

just between us - I would happily go for standard initramfs-tools package. with current initramfs-tools structure it is possible to convert our scripts to it, ... but it would require significant time for testing. Undecided

so I won't do it, but will support (Q&A) anyone who will commit to that !
Reference URL's