mirror of synced 2026-11-21 09:26:28 +03:00

- Initial changes discussed with Gorka

- Dependencies: latest execline/s6/s6-portable-utils at this time
 - Interactive CMDs should now work properly with or without terminals. If not, play with s6-setsid a bit more.
 - The scandir is now in /var/run/s6/service, because it gets written into at run-time
 - User service directories are now listed in /etc/services.d and will be *copied* to /var/run/s6/service during init-stage2
 - Yeah, I really hate writing to /, and you should hate it, too.
 - Init order:
   * our own services in /etc/s6/service are copied to /var/run/s6/service (stage 1)
   * s6-svscan is run
   * scripts in /etc/cont-init.d are run (stage 2)
   * services in /etc/services.d are copied to /var/run/s6/service and picked up by s6-svscan
   * If a CMD was given, it is run.
   i.e. the whole stage 2 is executed whether a CMD was given or not, so CMDs are always executed in a properly initted environment.
 - Since all the user stuff is started in stage 2, we can guarantee all the dependencies for stage 1, so no fix-attrs in stage 1. Stage 1 is now extremely short, as it should be.
 - Can't escape the fix-attrs in stage 2 though, but provided a chunk of execline to do the functionality. (Probably very slow.)
 - Plus a bunch of tiny fixes, mostly cosmetic.
This commit is contained in:
Laurent Bercot committed 2015-03-07 10:24:12 +00:00
1 parent 58150ae305
commit 008f87e988
29 files changed
+250 -195

No files matched your search

-26
View File
@@ -1,26 +0,0 @@
[
{
"path": "/etc/fix-attrs.d/*",
"attr": "root:root:0600"
},
{
"path": "/etc/s6/*/finish",
"attr": "root:root:0700"
},
{
"path": "/etc/s6/*/run",
"attr": "root:root:0700"
},
{
"path": "/etc/s6/*/log/run",
"attr": "root:root:0700"
},
{
"path": "/etc/cont-init.d/*",
"attr": "root:root:0700"
},
{
"path": "/etc/s6/.s6-svscan/crash",
"attr": "root:root:0700"
}
]
@@ -1,59 +0,0 @@
#!/usr/bin/execlineb
##
## dump environment into files and a bash script
##
# base for file and path
define contenvsdir /etc/container_environment
# assure folder does exist
foreground { mkdir -p ${contenvsdir} }
foreground {
forbacktickx i {
pipeline { s6-env } s6-cut -d"=" -f1
}
import i import ${i}
foreground {
redirfd -w 1 ${contenvsdir}/${i}
s6-echo ${${i}}
}
}
##
## fix attributes (ownership and permissions)
##
foreground { s6-echo "[fix-attrs] fixing file attributes (ownership & permission)..." }
foreground {
forbacktickx i {
find /etc/fix-attrs.d -maxdepth 1 -type f ! -name ".*" ! -path /etc/fix-attrs.d
}
import i
foreground { s6-echo "[fix-attrs] ${i}: applying..." }
foreground { fix-attrs fix ${i} }
foreground { s6-echo "[fix-attrs] ${i}: done." }
}
foreground { s6-echo "[fix-attrs] done." }
# i don't know why but, it seems like 'fix-attrs' is not freeing file
# handles and therefore files which were accesed/modified cannot be used
# for execution. We flush all the dirty system buffers, and blocks until
# they're clean.
foreground { s6-sync }
##
## fork the "init-stage2" script
##
background
{
elgetpositionals
/etc/s6/.s6-init/init-stage2 $@
}
unexport !
# start stage 2.
s6-envdir ${contenvsdir}
s6-svscan -t0 /etc/s6
@@ -1,34 +0,0 @@
#!/usr/bin/execlineb
# this file is executed (not as process 1!) as soon as s6-svscan
# starts. It should perform all the remaining one-time initialization
# tasks.
if -nt
{
elgetpositionals
ifelse { s6-test $# -ne 0 }
{ foreground { $@ } s6-svscanctl -t /etc/s6 }
##
## cont-init.d
##
foreground { s6-echo "[cont-init.d] executing container initialization scripts..." }
foreground {
forbacktickx i {
find /etc/cont-init.d -maxdepth 1 -type f ! -name ".*" ! -path /etc/cont-init.d
}
import i
foreground { s6-echo "[cont-init.d] ${i}: executing..." }
foreground { with-contenv ${i} }
foreground { s6-echo "[cont-init.d] ${i}: done." }
}
foreground { s6-echo "[cont-init.d] done." }
}
# if anything in the if -nt { } block fails, control jumps here.
# no need to do much because there's already an operational getty running.
redirfd -w 1 /dev/console
s6-echo "\n!!!!!\ninit-stage2 failed, please log in and investigate.\n!!!!!"
@@ -1,54 +0,0 @@
#!/usr/bin/execlineb -S0
# This is the shutdown script, running as process 1.
foreground {
forbacktickx i { find /etc/s6 -maxdepth 1 -type d ! -name ".*" ! -path /etc/s6 }
import i
foreground {
if { s6-test -f ${i}/finish }
foreground { s6-echo "[s6-finish] executing ${i}/finish ..." }
foreground { ${i}/finish }
foreground { s6-echo "[s6-finish] done." }
}
}
# Make sure we have no open handle to anywhere else than /dev/console
cd /
redirfd -w 1 /dev/console
fdmove -c 2 1
# Sync before TERM'n
foreground { s6-echo "[s6-finish] syncing disks." }
foreground { s6-sync }
# Even if s6-svscan properly brought all the services down
# before exec'ing into this script, users might have launched
# background nohup processes, so we have to kill everything.
foreground { s6-echo "[s6-finish] sending all processes the TERM signal." }
foreground { s6-nuke -th }
foreground { s6-sleep 1 }
foreground { s6-echo "[s6-finish] sending all processes the KILL signal." }
foreground { s6-nuke -k }
# Close stdin here cause we wanted to pipe to echo in order to
# get timestamped lines.
fdclose 0
# Yes, it helps.
foreground { s6-echo "[s6-finish] syncing disks." }
foreground { s6-sync }
# Here, s6-nuke sends a SIGKILL to every process on the system.
# It kills itself, but not before the SIGKILL has been broadcast.
# The "foreground" process runs as process 1, so it survives,
# and execs into the rest of the script when s6-nuke dies.
# Reap the huge army of zombies we just created
wait { }
-2
View File
@@ -1,2 +0,0 @@
#!/usr/bin/execlineb
/etc/s6/.s6-init/init-stage3
@@ -1 +0,0 @@
^.*$
@@ -1 +0,0 @@
^.*$
-2
View File
@@ -1,2 +0,0 @@
#!/usr/bin/execlineb
s6-notifywhenup s6-fdholder-daemon -1 -i /etc/s6/fdholderd/rules /etc/s6/fdholderd/socket
+1
View File
@@ -0,0 +1 @@
/usr/bin:/bin
+4
View File
@@ -0,0 +1,4 @@
/etc/services.d/*/run false root 0755 0755
/etc/services.d/*/finish false root 0755 0755
/etc/services.d/*/log/run false root 0755 0755
/etc/services.d/*/log/finish false root 0755 0755
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/execlineb -S0
##
## dump environment into an envdir
##
/usr/bin/if { /usr/bin/s6-mkdir -pm 0755 -- /var/run/s6/container_environment }
/usr/bin/if { /usr/bin/s6-dumpenv -- /var/run/s6/container_environment }
##
## run everything else with only the environment defined in
## /etc/s6/init/env. Programs can get back the container
## environment by using "with-contenv program".
##
/usr/bin/exec -c --
/usr/bin/s6-envdir /etc/s6/init/env
/usr/bin/exec --
##
## ensure our vital fifo exists
##
if { s6-mkfifo -m 0600 -- /var/run/s6/uncaught-logs-fifo }
##
## init the scandir with our base services
##
if { s6-rmrf /var/run/s6/service }
if { s6-hiercopy /etc/s6/service /var/run/s6/service }
##
## fork the "init-stage2" script
##
background
{
# add some environment
s6-envdir -- /etc/s6/init/env-stage2
# block until the supervision tree is running
redirfd -w 3 /var/run/s6/uncaught-logs-fifo
fdclose 3
# run the script
/etc/s6/init/init-stage2 $@
}
unexport !
##
## run the rest of stage 1 with sanitized descriptors
##
redirfd -r 0 /dev/null
redirfd -wnb 1 /var/run/s6/uncaught-logs-fifo
fdmove -c 2 1
##
## start the supervision tree
##
s6-svscan -t0 /var/run/s6/service
+109
View File
@@ -0,0 +1,109 @@
#!/usr/bin/execlineb -S0
# This file is executed (not as process 1!) as soon as s6-svscan
# starts, with the original stdin/out/err, but NOT the original
# environment.
# Purpose of this file: to perform all the one-time initialization tasks.
if -nt
{
if
{
redirfd -w 1 /var/run/s6/uncaught-logs-fifo
fdmove -c 2 1
##
## Ensure owners and permissions are correct
##
if
{
export recfunction "\
foreground { s6-envuidgid \\${3} s6-chown -U -- \\${1} }
ifelse -- { if { s6-\\${2} } s6-test -d \\${1} }
{
foreground { s6-chmod \\${5} \\${1} }
forbacktickx -0 subfile { s6-ls -0 \\${1} }
multisubstitute
{
import -u subfile
import fn
}
execlineb -S5 -c \\${recfunction} \\${1}/${subfile} true \\${3} \\${4} \\${5
}
s6-chmod \\${4} \\${1}"
if { s6-test -f ${1} }
redirfd -r 0 ${1}
forstdin -d"\n" -- line
import -u line
if { s6-test -n ${line} }
multidefine -0 -Cd" \t" -- ${line} { globex recurse user fmode dmode }
elglob -0 -- files ${globex}
forx -p -- file { ${files} }
multisubstitute
{
import -u file
import recfunction
}
execlineb -S5 -c ${recfunction} ${file} ${recurse} ${user} ${fmode} ${dmode}
}
##
## cont-init.d: one-time init scripts
##
if { s6-echo "[cont-init.d] executing container initialization scripts..." }
if
{
pipeline { s6-ls -0 -- /etc/cont-init.d }
pipeline { s6-sort -0 -- }
forstdin -0 -- i
import -u i
if { s6-echo -n -- "[cont-init.d] executing ${i}... " }
foreground { with-contenv /etc/cont-init.d/${i} }
import -u ? s6-echo -- " ... exited ${?}"
}
if { s6-echo -- "[cont-init.d] done." }
##
## services.d: long-lived processes to be supervised
##
if { s6-echo "[services.d] starting services" }
if
{
pipeline { s6-ls -0 -- /etc/services.d }
forstdin -0 -- i
import -u i
if { s6-test -d /etc/services.d/${i} }
s6-hiercopy /etc/services.d/${i} /var/run/s6/service/${i}
}
if { s6-svscanctl -a /var/run/s6/service }
if { s6-echo -- "[services.d] done." }
}
##
## The init is complete. If the user has given a CMD, run it now, then
## kill everything when it exits.
##
if -t { s6-test $# -ne 0 }
foreground { s6-setsid -gq -- with-contenv $@ }
foreground { import -u ? s6-echo "${1} exited ${?}. Stopping the supervision tree." }
foreground { s6-svscanctl -t /var/run/s6/service }
# Race condition here, but it's ok: the sync in stage 3 takes ages,
# and if we get killed by the SIGTERM instead of the SIGKILL, meh, who cares.
# Wait to be nuked
s6-pause -hqbait
}
# if anything in the if -nt { } block fails, control jumps here.
s6-echo -- "\n!!!!!\n init-stage2 failed.\n Logs are in /var/log/s6/uncaught-logs\n!!!!!"
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/execlineb -S0
# This is the shutdown script, running as process 1.
cd /
# Sync before TERM'n
foreground { s6-echo "[s6-finish] syncing disks." }
foreground { s6-sync }
# Kill everything, gently.
foreground { s6-echo "[s6-finish] sending all processes the TERM signal." }
foreground { s6-nuke -th } # foreground is process 1: it survives
foreground
{
with-contenv
/usr/bin/import -u -D 3000 S6_KILL_GRACETIME
/usr/bin/s6-sleep -m -- ${S6_KILL_GRACETIME}
}
# Last message, then close our pipes and give the logger some time.
foreground { s6-echo "[s6-finish] sending all processes the KILL signal and exiting." }
fdclose 1 fdclose 2
s6-sleep -m 200
# Kill everything, brutally.
foreground { s6-nuke -k } # foreground is process 1: it survives again
# Reap all the zombies then sync, and we're done.
wait { }
s6-sync
@@ -3,11 +3,6 @@
# This file is run when an unrecoverable error happens
# to s6-svscan. Edit it to suit your needs.
cd /
redirfd -r 0 /dev/console
redirfd -w 1 /dev/console
fdmove -c 2 1
foreground { s6-echo "s6-svscan panicked! Dropping to a root shell.\n" }
/bin/sh -i
+2
View File
@@ -0,0 +1,2 @@
#!/usr/bin/execlineb -S0
/etc/s6/init/init-stage3 $@
+4
View File
@@ -0,0 +1,4 @@
#!/usr/bin/execlineb -P
s6-setuidgid nobody
exec -c --
s6-log -- t /var/log/s6-fdholderd
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/execlineb -P
s6-notifywhenup
s6-envuidgid daemon
exec -c --
s6-fdholder-daemon -U -1 -i rules -- /var/run/s6/fdholderd-socket
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/execlineb -P
redirfd -rnb 0 /var/run/s6/uncaught-logs-fifo
s6-setuidgid nobody
exec -c
s6-log -bp -- t /var/log/s6-uncaught-logs
Regular → Executable
+2 -3
View File
@@ -1,3 +1,2 @@
#!/usr/bin/execlineb
elgetpositionals
/etc/s6/.s6-init/init-stage1 $@
#!/usr/bin/execlineb -S0
/etc/s6/init/init-stage1 $@
+4 -5
View File
@@ -1,6 +1,5 @@
#!/usr/bin/execlineb
elgetpositionals
emptyenv
s6-envdir /etc/container_environment
#!/usr/bin/execlineb -S0
/usr/bin/exec -c --
/usr/bin/s6-envdir -fn -- /var/run/s6/container_environment
/usr/bin/exec --
$@
+2 -3
View File
@@ -1,5 +1,4 @@
#!/usr/bin/execlineb -S0
s6-notifywhenup
foreground { printf "\n" }
foreground { s6-notifywhenup -f s6-echo }
unexport ?
$@