From 576786c97065c8e5638e9e0a7cad688175cd3746 Mon Sep 17 00:00:00 2001 From: Kim Date: Sun, 14 Oct 2018 21:33:06 +0200 Subject: Rework vyos router init (#11) * Add a systemd vyos.target and split the vyatta-router init script to multiple systemd scripts. * remove ref to cloud-init * Remove vyatta-router init and rename vyatta-router to vyos-initialize. * Install files to correct dirs * try to fix systemd boot issues * Update script names run config load in background to not block systemd * Prevent cat: write error: Broken pipe * Rework vyos startup * add systemd to Makefile.am * Update script name * Fix inclusion of getty conf * Fix indentation --- scripts/init/vyos-config | 18 ++++ scripts/init/vyos-router | 244 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100755 scripts/init/vyos-config create mode 100755 scripts/init/vyos-router (limited to 'scripts/init') diff --git a/scripts/init/vyos-config b/scripts/init/vyos-config new file mode 100755 index 0000000..c3b826f --- /dev/null +++ b/scripts/init/vyos-config @@ -0,0 +1,18 @@ +#!/bin/bash + +while ps -ef | grep my_commit | grep -q -v grep; do + sleep 1 +done + +while [ ! -f /tmp/vyos-config-status ] +do + sleep 1 +done + +status=$(cat /tmp/vyos-config-status) + +if [ $status -ne 0 ]; then + echo "Configuration error" +else + echo "Configuration success" +fi diff --git a/scripts/init/vyos-router b/scripts/init/vyos-router new file mode 100755 index 0000000..078e118 --- /dev/null +++ b/scripts/init/vyos-router @@ -0,0 +1,244 @@ +#!/bin/bash +# **** License **** +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# A copy of the GNU General Public License is available as +# `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution +# or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'. +# You can also obtain it by writing to the Free Software Foundation, +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# Author: Tom Grennan +# **** End License **** + +. /lib/lsb/init-functions + +: ${vyatta_env:=/etc/default/vyatta} +source $vyatta_env + +declare progname=${0##*/} +declare action=$1; shift + +declare -x BOOTFILE=$vyatta_sysconfdir/config/config.boot + +# If vyos-config= boot option is present, use that file instead +for x in $(cat /proc/cmdline); do + [[ $x = vyos-config=* ]] || continue + VYOS_CONFIG="${x#vyos-config=}" +done + +if [ ! -z "$VYOS_CONFIG" ]; then + if [ -r "$VYOS_CONFIG" ]; then + echo "Config selected manually: $VYOS_CONFIG" + declare -x BOOTFILE="$VYOS_CONFIG" + else + echo "WARNING: Could not read selected config file, using default!" + fi +fi + +declare -a subinit +declare -a all_subinits=( + rl-system + firewall ) + +if [ $# -gt 0 ] ; then + for s in $@ ; do + [ -x ${vyatta_sbindir}/${s}.init ] && subinit[${#subinit}]=$s + done +else + for s in ${all_subinits[@]} ; do + [ -x ${vyatta_sbindir}/${s}.init ] && subinit[${#subinit}]=$s + done +fi + +GROUP=vyattacfg + +# check if bootup of this portion is disabled +disabled () { + grep -q -w no-vyos-$1 /proc/cmdline +} + +# if necessary, provide initial config +init_bootfile () { + if [ ! -r $BOOTFILE ] ; then + if [ -f $vyatta_sysconfdir/config.boot.default ]; then + cp $vyatta_sysconfdir/config.boot.default $BOOTFILE + else + $vyatta_sbindir/vyatta_current_conf_ver.pl > $BOOTFILE + fi + + chgrp ${GROUP} $BOOTFILE + chmod 660 $BOOTFILE + fi +} + +# if necessary, migrate initial config +migrate_bootfile () +{ + if [ -x $vyatta_sbindir/vyatta_config_migrate.pl ]; then + log_progress_msg migrate + sg ${GROUP} -c "$vyatta_sbindir/vyatta_config_migrate.pl $BOOTFILE" + fi +} + +# load the initial config +load_bootfile () +{ + log_progress_msg configure + ( + if [ -f /etc/default/vyatta-load-boot ]; then + # build-specific environment for boot-time config loading + source /etc/default/vyatta-load-boot + fi + sg ${GROUP} -c "$vyatta_sbindir/vyatta-boot-config-loader $BOOTFILE" + ) +} + +# execute the pre-config script +run_preconfig_script () +{ + if [ -x /config/scripts/vyos-preconfig-bootup.script ]; then + /config/scripts/vyos-preconfig-bootup.script + fi +} + +run_postupgrade_script () +{ + if [ -f /config/.upgraded ]; then + # Run the system script + /usr/libexec/vyos/system/post-upgrade + + # Run user scripts + if [ -d /config/scripts/post-upgrade.d ]; then + run-parts /config/scripts/post-upgrade.d + fi + + rm -f /config/.upgraded + fi +} + +# +# On image booted machines, we need to mount /boot from the image-specific +# boot directory so that kernel package installation will put the +# files in the right place. We also have to mount /boot/grub from the +# system-wide grub directory so that tools that edit the grub.cfg +# file will find it in the expected location. +# +bind_mount_boot () +{ + persist_path=$(/opt/vyatta/sbin/vyos-persistpath) + if [ $? == 0 ]; then + if [ -e $persist_path/boot ]; then + image_name=$(cat /proc/cmdline | sed -e s+^.*vyos-union=/boot/++ | sed -e 's/ .*$//') + + if [ -n "$image_name" ]; then + mount --bind $persist_path/boot/$image_name /boot + + if [ $? -ne 0 ]; then + echo "Couldn't bind mount /boot" + fi + + if [ ! -d /boot/grub ]; then + mkdir /boot/grub + fi + + mount --bind $persist_path/boot/grub /boot/grub + if [ $? -ne 0 ]; then + echo "Couldn't bind mount /boot/grub" + fi + fi + fi + fi +} + +start () +{ + # Fixup for FRR + + # In 5.1 master, zebra thinks existence of /var/run/netns is + # a requirement for netns support + mkdir -p /var/run/netns + + # Since systemd doesn't work without watchfrr and watchfrr restarts + # all daemons if just one failed, do the start manually + /usr/lib/frr/frr start + + log_action_begin_msg "Mounting VyOS Config" + # ensure the vyatta_configdir supports a large number of inodes since + # the config hierarchy is often inode-bound (instead of size). + # impose a minimum and then scale up dynamically with the actual size + # of the system memory. + local tmem=$(sed -n 's/^MemTotal: \+\([0-9]\+\) kB$/\1/p' /proc/meminfo) + local tpages + local tmpfs_opts="nosuid,nodev,mode=775,nr_inodes=0" #automatically allocate inodes + mount -o $tmpfs_opts -t tmpfs none ${vyatta_configdir} \ + && chgrp ${GROUP} ${vyatta_configdir} + log_action_end_msg $? + + disabled bootfile || init_bootfile + + log_daemon_msg "Starting VyOS router" + disabled migrate || migrate_bootfile + + run_preconfig_script + + run_postupgrade_script + + for s in ${subinit[@]} ; do + if ! disabled $s; then + log_progress_msg $s + if ! ${vyatta_sbindir}/${s}.init start + then log_failure_msg + exit 1 + fi + fi + done + + disabled configure || load_bootfile + log_end_msg $? + + telinit q + bind_mount_boot + chmod g-w,o-w / +} + +stop() +{ + local -i status=0 + log_daemon_msg "Stopping VyOS router" + for ((i=${#sub_inits[@]} - 1; i >= 0; i--)) ; do + s=${subinit[$i]} + log_progress_msg $s + ${vyatta_sbindir}/${s}.init stop + let status\|=$? + done + log_end_msg $status + log_action_begin_msg "Un-mounting VyOS Config" + umount ${vyatta_configdir} + log_action_end_msg $? + + /usr/lib/frr/frr stop +} + +case "$action" in + start) start ;; + stop) stop ;; + restart|force-reload) stop && start ;; + *) log_failure_msg "usage: $progname [ start|stop|restart ] [ subinit ... ]" ; + false ;; +esac + +exit $? + +# Local Variables: +# mode: shell-script +# sh-indentation: 4 +# End: -- cgit v1.2.3 From 512553a3e388b546b7792e627be99c089f62ad9a Mon Sep 17 00:00:00 2001 From: UnicronNL Date: Sun, 21 Oct 2018 13:09:43 +0200 Subject: Move systemd mods to /lib directory Remove redundant my_commit check Do not show output on serial-getty (double output issue) --- Makefile.am | 7 +++++-- etc/systemd/aftervyos.conf | 3 --- lib/systemd/aftervyos.conf | 3 +++ lib/systemd/serial/aftervyos.conf | 3 +++ scripts/init/vyos-config | 14 ++++++-------- 5 files changed, 17 insertions(+), 13 deletions(-) delete mode 100644 etc/systemd/aftervyos.conf create mode 100644 lib/systemd/aftervyos.conf create mode 100644 lib/systemd/serial/aftervyos.conf (limited to 'scripts/init') diff --git a/Makefile.am b/Makefile.am index 81c1b1b..affc413 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,8 +38,11 @@ systemdinit_SCRIPTS += scripts/init/vyos-config systemdgeneratordir = /lib/systemd/system-generators/ systemdgenerator_SCRIPTS = scripts/system-generators/vyos-generator -systemdgettydir = /etc/systemd/system/getty@.service.d/ -systemdgetty_DATA = etc/systemd/aftervyos.conf +systemdgettydir = /lib/systemd/system/getty@.service.d/ +systemdgetty_DATA = lib/systemd/aftervyos.conf + +systemdserialgettydir = /lib/systemd/system/serial-getty@.service.d/ +systemdserialgetty_DATA = lib/systemd/serial/aftervyos.conf src/cparse/cparse.cpp: src/cparse/cparse.ypp src/cparse/cparse_def.h bison -p cparse_ --defines=src/cparse/cparse.h -o $@ $< diff --git a/etc/systemd/aftervyos.conf b/etc/systemd/aftervyos.conf deleted file mode 100644 index c575390..0000000 --- a/etc/systemd/aftervyos.conf +++ /dev/null @@ -1,3 +0,0 @@ -[Service] -ExecStartPre=-/usr/libexec/vyos/init/vyos-config -StandardOutput=journal+console diff --git a/lib/systemd/aftervyos.conf b/lib/systemd/aftervyos.conf new file mode 100644 index 0000000..c575390 --- /dev/null +++ b/lib/systemd/aftervyos.conf @@ -0,0 +1,3 @@ +[Service] +ExecStartPre=-/usr/libexec/vyos/init/vyos-config +StandardOutput=journal+console diff --git a/lib/systemd/serial/aftervyos.conf b/lib/systemd/serial/aftervyos.conf new file mode 100644 index 0000000..8ba4277 --- /dev/null +++ b/lib/systemd/serial/aftervyos.conf @@ -0,0 +1,3 @@ +[Service] +ExecStartPre=-/usr/libexec/vyos/init/vyos-config SERIAL +StandardOutput=journal+console diff --git a/scripts/init/vyos-config b/scripts/init/vyos-config index c3b826f..3564270 100755 --- a/scripts/init/vyos-config +++ b/scripts/init/vyos-config @@ -1,9 +1,5 @@ #!/bin/bash -while ps -ef | grep my_commit | grep -q -v grep; do - sleep 1 -done - while [ ! -f /tmp/vyos-config-status ] do sleep 1 @@ -11,8 +7,10 @@ done status=$(cat /tmp/vyos-config-status) -if [ $status -ne 0 ]; then - echo "Configuration error" -else - echo "Configuration success" +if [ -z "$1" ]; then + if [ $status -ne 0 ]; then + echo "Configuration error" + else + echo "Configuration success" + fi fi -- cgit v1.2.3