0

Ubuntu 16.04.5 default environment.

I want to do some custom job before init. grub.cfg as

linux /boot/vmlinuz ... ro init=/root/init.sh

init.sh:

#!/bin/bash
# do some task
exec init 3

after boot, X started.

if grub linux /boot/vmlinuz ... ro init=/root/init.sh 3,can success boot into text mode.

according to https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html , 3 is not a kernel’s command-line parameters, kernel will pass it as an argument to init, here is /root/init.sh.

change /root/init.sh to :

#!/bin/bash
# do some task
echo $1
sleep 3s
exec init 3

I can see the value passed from kernel.

In my submission,no matter what value grub -> kernel -> /root/init.sh , I will discard the value and exec init as runlevel 3. why it doesn't work.

0

according to poettering's response to my issue at systemd: https://github.com/systemd/systemd/issues/10752

we generally read /proc/cmdline, not argv[], since the kernel removes some bits from the latter that we need to know. We essentially ignore argv[] altogether.

Hence, what you are trying to do doesn't work, you'd have to overmount /proc/cmdline instead with some file with the new arguments.

about 'overmount', according to https://unix.stackexchange.com/questions/364442/cant-mount-on-proc-cmdline/364448 ,

  1. make a copy of /proc/cmdline at /root/cmdline
  2. add 3 at the end of /root/cmdline
  3. run command mount -n --bind -o ro /root/cmdline /proc/cmdline
  4. run command exec init

now system will boot with runlevel 3.

In summary, change /root/init.sh to

#!/bin/bash
# do some task
cp /proc/cmdline /root/cmdline
sed -i.bck '$s/$/ 3/' /root/cmdline
mount -n --bind -o ro /root/cmdline /proc/cmdline
exec init

system will boot into text mode after custom job finish as I except.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.