LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 06-09-2019, 12:17 PM   #1
LuckyCyborg
Member
 
Registered: Mar 2010
Posts: 446

Rep: Reputation: 239Reputation: 239Reputation: 239
About the ability to run custom commands from initrd's init


An idea exposed by me in another thread, which I think is worth more attention: I talk about customizing the initrd, because I think there's a huge lack on the infrastructure of initrd used by Slackware: the hooks for it.

Or at least, a way to merge a small custom script at the near end of "/boot/initrd-tree/init"

The single way I found today, even on slackware-current, to make my customization to survive to a clean generation of the initrd, is to modify this "/init" script within "/usr/share/mkinitrd/initrd-tree.tar.gz" then to careful watch if an upgrade of package will overide it or not.

Code:
...
else
  echo
  echo "RESCUE mode"
  echo
  echo "        You can try to fix or rescue your system now. If you want"
  echo "        to boot into your fixed system, mount your root filesystem"
  echo "        read-only under /mnt:"
  echo
  echo "            # mount -o ro -t filesystem root_device /mnt"
  echo
  echo "        Type 'exit' when things are done."
  echo
  /bin/sh
fi

##########################################################
# HERE YOU CAN RUN CUSTOM MOUNTING OR WHATEVER
##########################################################

# Need to make sure OPTIONS+="db_persist" exists for all dm devices
# That should be handled in /sbin/mkinitrd now
/sbin/udevadm info --cleanup-db
/sbin/udevadm control --exit

unset ERR
mount -o move /proc /mnt/proc
mount -o move /sys /mnt/sys
mount -o move /run /mnt/run

[ "$DEVTMPFS" = "1" ] && mount -o move /dev /mnt/dev
echo "${INITRD}:  exiting"
exec switch_root /mnt $INIT $RUNLEVEL
Why we need sometimes to run custom commands from this initrd script? For example, for mounting the /usr when it is hosted in a separate storage device, or any fine tuning considered to be done before switching to the real operating system.

I think that could be resolved this by modifying the /sbin/mkinitrd to look for a script named "/boot/initrd.sh" and if it exists, its content to be merged or injected using sed or whatever, in a certain place from "/boot/initrd-tree/init" before it to be compressed.

Please note that today any customization of "/boot/initrd-tree/init" is gone if the user generates cleanly another initrd.

Last edited by LuckyCyborg; 06-09-2019 at 12:27 PM.
 
Old 06-09-2019, 01:30 PM   #2
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado
Distribution: Slackware
Posts: 515

Rep: Reputation: 107Reputation: 107
How does this relate to using /etc/mkinitrd.conf to customize the /sbin/mkinitrd command? There are man pages for both mkinitrd and mkinitrd.conf as well as more info can be found in the /etc/mkinitrd.conf.sample file.
 
Old 06-09-2019, 01:39 PM   #3
LuckyCyborg
Member
 
Registered: Mar 2010
Posts: 446

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by Chuck56 View Post
How does this relate to using /etc/mkinitrd.conf to customize the /sbin/mkinitrd command? There are man pages for both mkinitrd and mkinitrd.conf as well as more info can be found in the /etc/mkinitrd.conf.sample file.
Please be kind to explain me how you configure today an initrd to run before to switch to the real operating system, the following command:
Code:
mount -t ext4 /dev/sda3 /mnt/usr
This use case, to run certain custom commands on initrd, well... it is not supported today.

There are many options given, but none for adding custom scripts or commands on initrd.

Last edited by LuckyCyborg; 06-09-2019 at 01:45 PM.
 
2 members found this post helpful.
Old 06-09-2019, 02:07 PM   #4
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 7,533

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Quote:
Originally Posted by LuckyCyborg View Post
Please be kind to explain me how you configure today an initrd to run before to switch to the real operating system, the following command:
Code:
mount -t ext4 /dev/sda3 /mnt/usr
This use case, to run certain custom commands on initrd, well... it is not supported today.

There are many options given, but none for adding custom scripts or commands on initrd.
Simple: by running mkinitd without the "-c" option. Customize whatever and wherever you want, just do not use the "-C" option.
 
Old 06-09-2019, 02:24 PM   #5
LuckyCyborg
Member
 
Registered: Mar 2010
Posts: 446

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by Alien Bob View Post
Simple: by running mkinitd without the "-c" option. Customize whatever and wherever you want, just do not use the "-C" option.
Well, it is not that simple. Usually, the user will want to generate a clean initrd, because s/he do that after a kernel update, or even some packages update.

Unless using "-C" option, the initrd will grown and grown, and forever grown, hosting modules for multiple kernels from the past and multiple variants of libraries. And what if the user updates the mkinitrd package itself?

I would like to note that to offer the ability to run custom commands on initrd, some of the other distributions uses the concept of "hooks", like described there for Arch Linux:

https://wiki.archlinux.org/index.php/Mkinitcpio#HOOKS

As another random example, Fedora uses instead "modules", like we see there:

https://git.kernel.org/pub/scm/boot/...tree/modules.d

Also the Debian uses "hooks", like they told there:

https://manpages.debian.org/jessie/i...l#HOOK_SCRIPTS

Howver, what I ask is not a full implementation of those initrd "hooks" or "modules", but just the ability to define somewhere a script and/or custom commands which could be integrated in initrd, no mater if it is is clean generated or not.

Last edited by LuckyCyborg; 06-09-2019 at 02:45 PM.
 
1 members found this post helpful.
Old 06-09-2019, 02:59 PM   #6
ZhaoLin1457
Member
 
Registered: Jan 2018
Posts: 268

Rep: Reputation: 231Reputation: 231Reputation: 231
I guess that it is hard to notice the disadvantages of the monolithic setup used for initrd by Slackware, unless you need to customize it in a daily basis.

Regarding the customization of the boot script from initrd, I have an even more generic idea than the OP:

the mkinird to check for the file "/etc/initrd.d/init.sh" and if it exists, to copy it on the place of "/boot/initrd-tree/init" before to generate the initrd.

This way the user have the full control over his custom init script from initrd, no matter what combination of options he use, with no fear that it will be overridden in a way or other.

Last edited by ZhaoLin1457; 06-09-2019 at 03:13 PM.
 
1 members found this post helpful.
Old 06-09-2019, 03:28 PM   #7
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 7,533

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Too bad then. But you can create your own solution of course.
 
Old 06-09-2019, 03:29 PM   #8
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,006
Blog Entries: 16

Rep: Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602Reputation: 2602
edit: nevermind. I was just repeating what eric said earlier. I guess I should have read all the thread before responding.

Last edited by GazL; 06-09-2019 at 03:40 PM.
 
1 members found this post helpful.
Old 06-10-2019, 06:22 PM   #9
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado
Distribution: Slackware
Posts: 515

Rep: Reputation: 107Reputation: 107
So for grins and giggles I wrote the following script and txt file to address the OP's suggestion. It works on my system and creates an initrd.gz that contains a modified init file based on a separate txt file. It should be run as root and on a vanilla Slackware installation is in the standard path.

This is the script file /usr/local/bin/mkinitrd.sh to generate the initrd.gz with the modified init file. Just replace the "# [...]" lines (2 of them) with your preferred mkinitrd commands.
Code:
#!/bin/sh

# 1st of 2 runs of mkinitrd 
# use "-c" option, clear existing init-tree and recreate init-tree
# [Insert your preferred mkinitrd command here. Make sure to use the "-c" option so the initrd-tree is cleared]

# insert mkinitrd_init.txt into init file
sed -i -e '/# Need to make sure OPTIONS+/{r /usr/local/etc/mkinitrd_init.txt' -e 'N}' /boot/initrd-tree/init

# protect init file from delete or overwrite
# make init file immutable, add flag
chattr +i /boot/initrd-tree/init

# 2nd of 2 runs of mkinitrd
# do not use "-c" option, keep existing init-tree and overwrite init-tree
# [Insert your preferred mkinitrd command here. Make sure to not use the "-c" option so the existing initrd-tree is maintained.]

# unprotect init file from delete or overwrite
# make init file not immutable, remove flag
chattr -i /boot/initrd-tree/init
This is the txt file /usr/local/etc/mkinitrd_init.txt that gets copied into the init file using sed. Just replace the "# [...]" lines with the command(s) you want to add (as many or as few since the whole file is copied).

Code:
##########
# inserted contents from /usr/local/etc/mkinitrd_init.txt
# [add command #1 to insert into the init file here]
# [add command #2 to insert into the init file here]
# [add command #3 to insert into the init file here]
##########
Interesting fact, this is one of the few times I've used the immutable bit to preserve a file during processing.

Have fun!
 
Old 06-11-2019, 09:53 AM   #10
Tonus
Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-current
Posts: 481
Blog Entries: 3

Rep: Reputation: 131Reputation: 131
About the ability to run custom commands from initrd's init

Reading this I was wondering if that could be integrated upstream, with an mkinitrd.conf option...
 
Old 06-12-2019, 11:04 PM   #11
gus3
Member
 
Registered: Jun 2014
Distribution: Slackware (x86 and ARM)
Posts: 171

Rep: Reputation: Disabled
This seems like a nice thread to drop in a little suggestion.

I customized my /etc/mkinitrd.conf concerning loaded modules, but I found no documented default for KERNEL_VERSION (-k on the mkinitrd command line). So I scripted it into my 64-bit desktop's mkinitrd.conf:
Code:
KERNEL_VERSION="$(ls -1 /boot/vm*generic* | tail -n 1 | sed 's/.*generic-//')"
This way, if someone (like me) has used installpkg instead of upgradepkg to load a new kernel, the command
Code:
# mkinitrd -F
will automatically detect the version of the newest kernel-generic, and use that for the module path.

For my 32-bit netbook, it's slightly different:
Code:
KERNEL_VERSION="$(ls -1 /boot/vm*generic* | tail -n 1 | sed 's/.*generic-smp//')"
This takes into account the Slackware kernel version naming scheme.

Side note: my sysadmin experience (i.e. how many ways can I mess up my desktop?) says "keep the working boot files until you can replace them with minimal chance of disaster." Since I use LILO to boot, I leave the old files in place until all the new files are ready. Then, just running "lilo" takes a few seconds to re-configure the file mappings for /boot/kernel-generic and /boot/initrd.gz. By keeping a previous /boot/kernel-huge around, I have that escape route to finish a proper kernel upgrade in the event of a power failure or other catastrophic system crash.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Vivaldi 1.2 Brings Ability To Set Custom Mouse Gestures For Commands/Actions? LXer Syndicated Linux News 0 06-02-2016 07:03 PM
kernel panic: init not found (problem with custom built initrd.img) cpio command pumoritech Linux - General 4 07-23-2014 02:26 PM
Custom initrd init problem while booting from USB hoes Linux - Software 6 05-25-2008 07:20 AM
custom initrd not working -- INIT errors michael_util Slackware 9 11-24-2004 11:58 AM
Custom Kernel / No INIT / initrd? azl Linux - Newbie 2 11-01-2003 09:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration