Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init scripts doesn't work for Debian + OpenRC setup. #8204

Closed
cdluminate opened this issue Dec 11, 2018 · 11 comments
Closed

Init scripts doesn't work for Debian + OpenRC setup. #8204

cdluminate opened this issue Dec 11, 2018 · 11 comments
Labels
Status: Stale No recent activity for issue

Comments

@cdluminate
Copy link
Contributor

cdluminate commented Dec 11, 2018

System information

Type Version/Name
Distribution Name Debian
Distribution Version unstable
Linux Kernel 4.18.20
Architecture amd64
ZFS Version 0.7.12
SPL Version 0.7.12

Describe the problem you're observing

Unlike Gentoo, Debian doesn't use /sbin/openrc-init as the pid1, but still use the init program from sysvinit-core. As a result, executing init scripts in the openrc way on Debian won't work, that because e.g. https://github.com/zfsonlinux/zfs/blob/master/etc/init.d/zfs-zed.in#L101 assumes the system pid1 is openrc once the /sbin/openrc-run is detected, and sysvinit doesn't understand the openrc way.

A dirty hack is provided in #8063 which works quite well for both Debian+sysvinit and Debian+openrc setups. A better solution requires us to detect the actual init program type. According to https://unix.stackexchange.com/questions/18209/detect-init-system-using-the-shell , a possible solution may look like

-if [ ! -e /sbin/openrc-run ]; then
-the sysvinit way
-else
-the openrc way
-fi
+if [ -e /sbin/openrc-run ] && strings $(strings /proc/1/cmdline) | grep openrc >/dev/null; then
+the openrc way
+else
+the sysvinit way
+fi

Describe how to reproduce the problem

  1. setup Debian unstable base system
  2. install zfsutils-linux (with debian init patch reverted)
  3. install aptitude
  4. aptitude install sysvinit-core
  5. apt install openrc
  6. reboot

Include any warning/errors/backtraces from the system logs

None, but zpools will no longer be automatically imported.

@cdluminate
Copy link
Contributor Author

cdluminate commented Dec 12, 2018

@kpande My point is that the if condition used to select sysvinit/openrc code branches is not complete. It enters a wrong code branch in Debian's case, where openrc is installed and detected, but the actuall init is still sysvinit instead of openrc. I'll test my proposed patch in a virtual machine. Should I submit a PR if the result looks good for Debian?

sysvinit/openrc is indeed not the first class init system of Debian, but to some extent such init diversity is good to have.


update: The proposed patch has been verified on my Debian vm, which selected the correct code branch for Debian+Openrc. If you think this patch is acceptable, I can also setup Gentoo and test it to see if I broke anything.

@gyakovlev
Copy link
Contributor

gyakovlev commented Dec 20, 2018

nak from gentoo.

gentoo does not use /sbin/openrc-init by default. Users can configure it, but it's experimental and not feature-complete yet. So by default gentoo uses sysvinit's /sbin/init, same way as debian does according to your description (not sure how it's configured in that case, it's probably unsupported)

/sbin/openrc-run is a different beast, it's used to interpret initscripts and not related to /sbin/init or /sbin/openrc-init, don't confuse it.

again, the script checks for /sbin/openrc-run and not /sbin/openrc-init!

@gyakovlev
Copy link
Contributor

your problem seems to be that the script assumes it should be run by /sbin/openrc-run if it's found and creates openrc-specific start/stop functions and omits the case loop, but in reality you expect it to be run by sysvinit with openrc installed, right?

@cdluminate
Copy link
Contributor Author

cdluminate commented Dec 20, 2018

your problem seems to be that the script assumes it should be run by /sbin/openrc-run if it's found and creates openrc-specific start/stop functions and omits the case loop, but in reality you expect it to be run by sysvinit with openrc installed, right?

@gyakovlev Yes. For Debian+sysvinit+openrc, it should enter the case code branch instead of the openrc-run code branch because in fact the openrc-run branch doesn't do anything since sysvinit don't recogonize the way openrc-run registers hooks.

@gyakovlev
Copy link
Contributor

on gentoo system the shebang of the file is
#!/sbin/openrc-run, on debian it probably is #!/sbin/runscript but that condition forces openrc functions despite expecting runscript. can you check it what's your shebang?

not sure what is the correct way of detecting if the script is really run under openrc as service manager, I'll check it and report back.

we need to make sure it works in all scenarios: gentoo-sysvinit, gentoo-openrc-init, debian-sysvinit, debian-openrc, and it makes me want just create gentoo-only initscripts and don't use this fragile hackery.

@heroxbd
Copy link
Contributor

heroxbd commented Dec 20, 2018

if [ ! -e /sbin/openrc-run ] is not a predictor, a system can have OpenRC installed but not used.

@cdluminate openrc-run This has nothing to do with OpenRC-init.

@gyakovlev :

#!/sbin/openrc-run, on debian it probably is #!/sbin/runscript but that condition forces openrc functions despite expecting runscript.

runscript is the old name of openrc-run, they are the same thing.

can you check it what's your shebang?

This is the cause of this bug, the shebang of this script in Debian is /bin/sh, not openrc-run.

we need to make sure it works in all scenarios: gentoo-sysvinit, gentoo-openrc-init, debian-sysvinit, debian-openrc, and it makes me want just create gentoo-only initscripts and don't use this fragile hackery.

It does not matter what PID 1 you have. Therefore you are facing with only 2 cases here: OpenRC and sysv-rc.

@gyakovlev
Copy link
Contributor

gyakovlev commented Dec 20, 2018

runscript is the old name of openrc-run, they are the same thing.

yeah, I haven't used debian for a while, thanks for clarification.

It does not matter what PID 1 you have. Therefore you are facing with only 2 cases here: OpenRC and sysv-rc.

true, I meant is the script should not make assumptions about init, but pick up correct service manager and not guess by checking if file exists. Like you said it can exists but be unused.

This is the cause of this bug, the shebang of this script in Debian is /bin/sh, not openrc-run.

it may be a symptom, not the cause, it depends how debian wants to run the script.
seems they want to run the script under sysvinit despite having openrc installed or active.

looks like Makefile makes (no pun intended) some assumptions about openrc the same way as initscript
https://github.com/zfsonlinux/zfs/blob/a8577bdb32e091645df901d8501e44ef50748389/etc/init.d/Makefile.am#L24-L28

I guess the if zfs package is built on a system without openrc shebang is set to #!/bin/sh, but the script still misses the case loop and tries to set openrc-specific functions.

@cdluminate @heroxbd can you help me understand how exactly openrc is used on debian in your case?
does it replace service manager completely or it works with it in conjunction with sysvinit and manages it's own set of services?
I checked this page https://wiki.debian.org/OpenRC and it's still not quite clear.

my current thoughts are that the script should:
if shebang is #!/sbin/openrc-run - try to use openrc-run unconditionally
if shebang is #!/bin/sh - never check if openrc-run is executable and create sysvinit functions unconditionally.

any ideas how to correctly check the caller of the script in POSIX sh or dash, without bashisms?
bash has $PPID, caller builtin, $BASH_SOURCE but it seems those things can't be used in that case.

@cdluminate
Copy link
Contributor Author

@cdluminate @heroxbd can you help me understand how exactly openrc is used on debian in your case?
does it replace service manager completely or it works with it in conjunction with sysvinit and manages it's own set of services?

It only replaces sysv-rc and works in conjunction with other parts of sysvinit. And I learned that this script has nothing to do with init.

my current thoughts are that the script should:
if shebang is #!/sbin/openrc-run - try to use openrc-run unconditionally
if shebang is #!/bin/sh - never check if openrc-run is executable and create sysvinit functions unconditionally.

This looks good to me and should be able to solve this issue.

any ideas how to correctly check the caller of the script in POSIX sh or dash, without bashisms?
bash has $PPID, caller builtin, $BASH_SOURCE but it seems those things can't be used in that case.

If we stick to the above assumption, we can simply check the @SHELL@ string.

@cdluminate
Copy link
Contributor Author

How about this patch?

Index: zfs/etc/init.d/zfs-import.in
===================================================================
--- zfs.orig/etc/init.d/zfs-import.in
+++ zfs/etc/init.d/zfs-import.in
@@ -308,8 +308,7 @@ do_start()
 
 # ----------------------------------------------------
 
-if [ ! -e /sbin/openrc-run ]
-then
+if ! (echo @SHELL@ | grep openrc 1>/dev/null 2>/dev/null); then
 	case "$1" in
 		start)
 			do_start
Index: zfs/etc/init.d/zfs-mount.in
===================================================================
--- zfs.orig/etc/init.d/zfs-mount.in
+++ zfs/etc/init.d/zfs-mount.in
@@ -199,8 +199,7 @@ do_stop()
 
 # ----------------------------------------------------
 
-if [ ! -e /sbin/openrc-run ]
-then
+if ! (echo @SHELL@ | grep openrc 1>/dev/null 2>/dev/null); then
 	case "$1" in
 		start)
 			do_start
Index: zfs/etc/init.d/zfs-share.in
===================================================================
--- zfs.orig/etc/init.d/zfs-share.in
+++ zfs/etc/init.d/zfs-share.in
@@ -58,7 +58,7 @@ do_stop()
 
 # ----------------------------------------------------
 
-if [ ! -e /sbin/openrc-run ]; then
+if ! (echo @SHELL@ | grep openrc 1>/dev/null 2>/dev/null); then
 	case "$1" in
 		start)
 			do_start
Index: zfs/etc/init.d/zfs-zed.in
===================================================================
--- zfs.orig/etc/init.d/zfs-zed.in
+++ zfs/etc/init.d/zfs-zed.in
@@ -98,7 +98,7 @@ do_reload()
 
 # ----------------------------------------------------
 
-if [ ! -e /sbin/openrc-run ]; then
+if ! (echo @SHELL@ | grep openrc 1>/dev/null 2>/dev/null); then
 	case "$1" in
 		start)
 			do_start

@cdluminate
Copy link
Contributor Author

#8359

@stale
Copy link

stale bot commented Aug 25, 2020

This issue has been automatically marked as "stale" because it has not had any activity for a while. It will be closed in 90 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale No recent activity for issue label Aug 25, 2020
@stale stale bot closed this as completed Nov 24, 2020
jackpot51 pushed a commit to pop-os/zfs-linux that referenced this issue Jan 20, 2022
Applying such a support upstream would introduce concerns for Gentoo
users, and upstream has limited interest on supporting OpenRC for an
alternative distribution other than Gentoo.

Ref: openzfs/zfs#8204
heroxbd pushed a commit to heroxbd/zfs that referenced this issue Mar 9, 2024
Let Debian use the sysv-rc variant of the script, even when OpenRC is
installed. Unlike on Gentoo, OpenRC on Debian consumes both the
sysv-rc scripts and OpenRC ones. ZFS initscripts on Debian should be
the sysv-rc version to provide most compatibility and to integrate
with the rest of initscripts for dependency tracking.

Autoconf has the necessary functions to do substitution, removing one
level of indirection for deferring them to `make`.  Restrict the
substitution in the Makefile to the dedicated list.

This construct is inspired by Mo Zhou's runtime detection of the
execution shell, but is cleaner to the end users by handling the
boilerplate at build time.

Reference: openzfs#8063
Reference: openzfs#8204
Reference: openzfs#8359
Signed-off-by: Benda Xu <orv@debian.org>
heroxbd pushed a commit to heroxbd/zfs that referenced this issue Mar 9, 2024
Let Debian use the sysv-rc variant of the script, even when OpenRC is
installed. Unlike on Gentoo, OpenRC on Debian consumes both the
sysv-rc scripts and OpenRC ones. ZFS initscripts on Debian should be
the sysv-rc version to provide most compatibility and to integrate
with the rest of initscripts for dependency tracking.

Restrict the substitution in the Makefile to the dedicated list.

This construct is inspired by Mo Zhou's detection of the execution
shell and follows the strategy of Peter in 6ef28c5.

Reference: openzfs#8063
Reference: openzfs#8204
Reference: openzfs#8359
Signed-off-by: Benda Xu <orv@debian.org>
behlendorf pushed a commit that referenced this issue Apr 8, 2024
Let Debian use the sysv-rc variant of the script, even when OpenRC is
installed. Unlike on Gentoo, OpenRC on Debian consumes both the
sysv-rc scripts and OpenRC ones. ZFS initscripts on Debian should be
the sysv-rc version to provide most compatibility and to integrate
with the rest of initscripts for dependency tracking.

Restrict the substitution in the Makefile to the dedicated list.

This construct is inspired by Mo Zhou's detection of the execution
shell and follows the strategy of Peter in 6ef28c5.

As of 2024, the initscripts are mostly relevant on Debian, Gentoo and
their derivatives.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Benda Xu <orv@debian.org>
Issue #8063
Issue #8204
Issue #8359
Closes #15977
tonyhutter pushed a commit that referenced this issue May 2, 2024
Let Debian use the sysv-rc variant of the script, even when OpenRC is
installed. Unlike on Gentoo, OpenRC on Debian consumes both the
sysv-rc scripts and OpenRC ones. ZFS initscripts on Debian should be
the sysv-rc version to provide most compatibility and to integrate
with the rest of initscripts for dependency tracking.

Restrict the substitution in the Makefile to the dedicated list.

This construct is inspired by Mo Zhou's detection of the execution
shell and follows the strategy of Peter in 6ef28c5.

As of 2024, the initscripts are mostly relevant on Debian, Gentoo and
their derivatives.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Benda Xu <orv@debian.org>
Issue #8063
Issue #8204
Issue #8359
Closes #15977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale No recent activity for issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@gyakovlev @heroxbd @cdluminate and others