WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Patch for a pre-boot script  (Read 9730 times)

Offline aw

  • Newbie
  • *
  • Posts: 45
Patch for a pre-boot script
« on: May 17, 2016, 11:47:58 AM »
Hi,

I occasionally want to run some commands (or a script) before the system boots. I've made a small patch to tc-config which will execute the file /opt/preboot.sh before "Booting Core".

Here's the patch file:

https://gist.github.com/aw/346c56912abd744803d054a9df04662f

Code: [Select]
diff --git a/etc/init.d/tc-config b/etc/init.d/tc-config
index f6002dd..c44876c 100755
--- a/etc/init.d/tc-config
+++ b/etc/init.d/tc-config
@@ -25,6 +25,8 @@ addUser(){
 
 # Main
 
+[ -f "/opt/preboot.sh" ] && /opt/preboot.sh
+
 clear                               
 echo "${GREEN}Booting ${YELLOW}Core $VERSION ${NORMAL}"
 echo "${GREEN}Running Linux Kernel ${YELLOW}$KERNEL${GREEN}.${NORMAL}"

Here's an example use-case:

On PiCore, if the system doesn't have an RTC, the year always resets/begins at 1970. To fix that, I've introduced a boot option called builddate which sets the system date very early in the boot process, based on a UNIX timestamp

Code: [Select]
# example /opt/preboot.sh
#
# builddate from /proc/cmdline = 1463499605

. /etc/init.d/tc-functions

builddate=$(getbootparam "builddate")
if [ ${builddate-} ]; then
  [ "$builddate" -lt `/bin/date +%s` ] || /bin/date +%s -s @${builddate}
fi

Would it be possible to patch the official tc-config to allow us to add our own preboot scripts? I'm sure others have encountered situations requiring certain actions before TinyCore starts booting.

Thanks!
github.com/aw

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Patch for a pre-boot script
« Reply #1 on: May 17, 2016, 12:27:38 PM »
Actually, that's what /etc/init.d/rcS is for. Anything you want to run before tc-config will need to be in the root file system. Update this file with your commands and rebuild it.

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: Patch for a pre-boot script
« Reply #2 on: May 17, 2016, 01:06:59 PM »
Hi
How you american say: my 2 cents:
The advantage of aw proposal is that if one has persistent opt there is no need to rebuild the initrd.
dCore user

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11041
Re: Patch for a pre-boot script
« Reply #3 on: May 17, 2016, 01:30:36 PM »
Persistent opt is not mounted that early, so he'd have to remaster still.
The only barriers that can stop you are the ones you create yourself.

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Patch for a pre-boot script
« Reply #4 on: May 17, 2016, 01:34:56 PM »
If you want /opt mounted before then, then you need to do it in rcS. Back to that file. Technically you don't have to rebuild the stock initrd, you could make your own with just the changed files and load it after the stock initrd in extlinux.config.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: Patch for a pre-boot script
« Reply #5 on: May 17, 2016, 04:46:18 PM »
Or use the pretce boot code as used here:
http://wiki.tinycorelinux.net/wiki:pretce

Offline aw

  • Newbie
  • *
  • Posts: 45
Re: Patch for a pre-boot script
« Reply #6 on: May 17, 2016, 08:55:03 PM »
Thanks for all the responses!

I didn't consider the fact that /opt is not mounted/restored at that time.

The pretce boot option has the correct functionality, except I think it happens too late in the boot process.

It appears in all cases I would need to remaster the initrd (particularly for PiCore which doesn't use extlinux / limited to one initrd).

In this case, modifying /etc/init.d/rcS would be the cleanest solution. Thanks @andyj
github.com/aw

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Patch for a pre-boot script
« Reply #7 on: May 17, 2016, 11:01:53 PM »
On piCore as far as I know there you can only have one initrd. I use rcS to mount overlays to cut boot time. But that requires a custom kernel, but again I have to have that to have the gpio display available at boot, so it all comes together. Good luck.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: Patch for a pre-boot script
« Reply #8 on: May 18, 2016, 07:35:40 AM »
Try creating an initrd with only your modified rcS.
Then "cat core.gz custom.gz" > newcore.gz.
Then use newcore.gz in your bootloader.

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Patch for a pre-boot script
« Reply #9 on: May 18, 2016, 01:32:04 PM »
Cat'ting two gz files works in theory, but only the first cpio archive is unpacked. Files in the second one didn't get extracted in my test.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11041
Re: Patch for a pre-boot script
« Reply #10 on: May 18, 2016, 02:07:54 PM »
Maybe a Pi peculiarity? It works on x86.
The only barriers that can stop you are the ones you create yourself.

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Patch for a pre-boot script
« Reply #11 on: May 18, 2016, 03:16:07 PM »
I was trying it on x86_64. I did a zcat of the new file then piped through cpio. Maybe the boot loader handles the file differently?

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: Patch for a pre-boot script
« Reply #12 on: May 18, 2016, 05:36:56 PM »
You need to cpio the full directory structure of your files. Then compress the cpio file.
See the remastering wiki articles describing the construction of an initrd.

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Patch for a pre-boot script
« Reply #13 on: May 18, 2016, 07:33:32 PM »
That's not what you said earlier:
Try creating an initrd with only your modified rcS.
Then "cat core.gz custom.gz" > newcore.gz.
Then use newcore.gz in your bootloader.

For x86 and x86_64 I've loaded them separately so I could use the stock initrd's unmodified:
Code: [Select]
DEFAULT pxe
LABEL pxe
KERNEL pxelinux.cfg/default-boot/vmlinuz64-7-1rc1
INITRD pxelinux.cfg/default-boot/rootfs64-7-1rc2.gz,pxelinux.cfg/default-boot/modules64-7-1rc1.gz,pxelinux.cfg/default-boot/my-syslog-data.gz
APPEND host=syslog-vm printk.time=1 syslog quiet noswap nozswap tce=/ httplist=192.168.50.5:/syslog.lst

On the Pi multiple initrd's don't work so I've always rebuilt my own. I'm familiar with the process.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11041
Re: Patch for a pre-boot script
« Reply #14 on: May 19, 2016, 04:24:18 AM »
I was trying it on x86_64. I did a zcat of the new file then piped through cpio. Maybe the boot loader handles the file differently?

Yes, the kernel's decompressor differs here. Userspace cpio can't unpack catted archives.
The only barriers that can stop you are the ones you create yourself.