2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 ## Environmental setup
23 # Setup default values for environment
25 export PATH
="/bin:/usr/bin:/sbin:/usr/sbin"
27 # Signal sent to running processes to refresh their configuration
30 # Number of seconds between STOPSIG and FALLBACK when stopping processes
34 # Find current screen size
35 if [ -z "${COLUMNS}" ]; then
36 COLUMNS
=$
(stty size
2>/dev
/null
)
37 COLUMNS
=${COLUMNS##* }
40 # When using remote connections, such as a serial port, stty size returns 0
41 if [ "${COLUMNS}" = "0" ]; then
45 ## Measurements for positioning result messages
46 COL
=$
((${COLUMNS} - 8))
49 ## Set Cursor Position Commands, used via echo -e
50 SET_COL
="\\033[${COL}G" # at the $COL char
51 SET_WCOL
="\\033[${WCOL}G" # at the $WCOL char
52 CURS_UP
="\\033[1A\\033[0G" # Up one line, at the 0'th char
54 ## Set color commands, used via echo -e
55 # Please consult `man console_codes for more information
56 # under the "ECMA-48 Set Graphics Rendition" section
58 # Warning: when switching from a 8bit to a 9bit font,
59 # the linux console will reinterpret the bold (1;) to
60 # the top 256 glyphs of the 9bit font. This does
61 # not affect framebuffer consoles
62 NORMAL
="\\033[0;39m" # Standard console grey
63 SUCCESS
="\\033[1;32m" # Success is green
64 WARNING
="\\033[1;33m" # Warnings are yellow
65 FAILURE
="\\033[1;31m" # Failures are red
66 INFO
="\\033[1;36m" # Information is light cyan
67 BRACKET
="\\033[1;34m" # Brackets are blue
69 STRING_LENGTH
="0" # the length of the current message
71 #*******************************************************************************
72 # Function - boot_mesg()
74 # Purpose: Sending information from bootup scripts to the console
76 # Inputs: $1 is the message
77 # $2 is the colorcode for the console
79 # Outputs: Standard Output
81 # Dependencies: - sed for parsing strings.
82 # - grep for counting string length.
85 #*******************************************************************************
98 echo "Unknown Option: ${1}"
107 ## Figure out the length of what is to be printed to be used
108 ## for warning messges.
109 STRING_LENGTH
="`echo "${1}" | sed \
110 -e 's,.,.,g' -e 'l 1' | grep -c \$`"
112 # Print the message to the screen
113 echo ${ECHOPARM} -e "${2}${1}"
119 # Reset STRING_LENGTH for next message
125 # Left in for backwards compatibility
131 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]"
138 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
145 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
153 # $i is inherited by the rc script
154 boot_mesg -n "FAILURE
:\n\nYou should not be reading this error message.
\n\n" ${FAILURE}
155 boot_mesg -n " It means that an unforeseen error took
"
156 boot_mesg -n " place
in ${i}, which exited with a
return value of
"
157 boot_mesg " ${error_value}.
\n"
159 boot_mesg -n "If you
're able to track this"
160 boot_mesg -n " error down to a bug in one of the files provided by"
161 boot_mesg -n " ipfire, please be so kind to inform us at"
162 boot_mesg " https://bugzilla.ipfire.org.\n"
164 boot_mesg -n "Press Enter to continue or wait a minute..." ${INFO}
165 boot_mesg "" ${NORMAL}
169 check_script_status()
171 # $i is inherited by the rc script
172 if [ ! -f ${i} ]; then
173 boot_mesg "${i} is not a valid symlink." ${WARNING}
178 if [ ! -x ${i} ]; then
179 boot_mesg "${i} is not executable, skipping." ${WARNING}
189 if [ ${error_value} = 0 ]; then
195 # This prevents the 'An Unexpected Error Has Occurred
' from trivial
202 if [ "${#}" = "0" ]; then
203 echo "Usage: ${0} {success|warning|failure}"
214 # Leave this extra case in because old scripts
215 # may call it this way.
218 echo -e -n "${CURS_UP}"
219 echo -e -n "\\033[${STRING_LENGTH}G "
220 boot_mesg "Already running." ${WARNING}
224 echo -e -n "${CURS_UP}"
225 echo -e -n "\\033[${STRING_LENGTH}G "
226 boot_mesg "Not running." ${WARNING}
230 echo -e -n "${CURS_UP}"
231 echo -e -n "\\033[${STRING_LENGTH}G "
232 boot_mesg "Not available." ${WARNING}
236 # This is how it is supposed to
253 if [ "${#}" = "0" ]; then
254 echo "Usage: reloadproc [{program}]"
260 if [ -n "${pidlist}" ]; then
262 for pid in ${pidlist}
264 kill -"${RELOADSIG}" "${pid}" || failure="1"
271 boot_mesg "Process ${1} not running." ${WARNING}
280 echo "Usage: statusproc {program}"
286 if [ -n "${pidlist}" ]; then
287 echo -e "${INFO}${base} is running with Process"\
288 "ID(s) ${pidlist}.${NORMAL}"
290 if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
291 echo -e "${WARNING}${1} is not running but"\
292 "/var/run/${base}.pid exists.${NORMAL}"
294 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
295 echo -e "${WARNING}${1} is not running"\
296 "but ${PIDFILE} exists.${NORMAL}"
298 echo -e "${INFO}${1} is not running.${NORMAL}"
304 # The below functions are documented in the LSB-generic 2.1.0
306 #*******************************************************************************
307 # Function - pidofproc [-s] [-p pidfile] pathname
309 # Purpose: This function returns one or more pid(s) for a particular daemon
311 # Inputs: -p pidfile, use the specified pidfile instead of pidof
312 # pathname, path to the specified program
314 # Outputs: return 0 - Success, pid's
in stdout
315 # return 1 - Program is dead, pidfile exists
316 # return 2 - Invalid or excessive number of arguments,
318 # return 3 - Program is not running
320 # Dependencies: pidof, echo, head
322 # Todo: Remove dependency on head
323 # This depreciates getpids
324 # Test changes to pidof
326 #*******************************************************************************
342 # Added for legacy opperation of getpids
343 # eliminates several '> /dev/null'
348 log_failure_msg
"Unknown Option: ${1}"
357 if [ "${#}" != "1" ]; then
359 log_failure_msg
"Usage: pidofproc [-s] [-p pidfile] pathname"
363 if [ -n "${pidfile}" ]; then
364 if [ ! -r "${pidfile}" ]; then
365 return 3 # Program is not running
368 lpids
=`head -n 1 ${pidfile}`
371 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
372 kill -0 "${pid}" > /dev
/null
&&
373 pidlist
="${pidlist} ${pid}"
376 if [ "${silent}" -ne "1" ]; then
380 test -z "${pidlist}" &&
381 # Program is dead, pidfile exists
388 pidlist
=`pidof -o $$ -o $PPID -x "$1"`
389 if [ "x${silent}" != "x1" ]; then
393 # Get provide correct running status
394 if [ -n "${pidlist}" ]; then
402 if [ "$?" != "0" ]; then
403 return 3 # Program is not running
407 # This will ensure compatibility with previous LFS Bootscripts
410 if [ -n "${PIDFILE}" ]; then
411 pidofproc
-s -p "${PIDFILE}" $@
418 #*******************************************************************************
419 # Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
421 # Purpose: This runs the specified program as a daemon
423 # Inputs: -f, run the program even if it is already running
424 # -n nicelevel, specifies a nice level. See nice(1).
425 # -p pidfile, uses the specified pidfile
426 # pathname, pathname to the specified program
427 # args, arguments to pass to specified program
429 # Outputs: return 0 - Success
430 # return 2 - Invalid of excessive number of arguments,
432 # return 4 - Program or service status is unknown
436 # Todo: LSB says this should be called start_daemon
437 # LSB does not say that it should call evaluate_retval
438 # It checks for PIDFILE, which is deprecated.
439 # Will be removed after BLFS 6.0
440 # loadproc returns 0 if program is already running, not LSB compliant
442 #*******************************************************************************
451 # This will ensure compatibility with previous LFS Bootscripts
452 if [ -n "${PIDFILE}" ]; then
476 log_failure_msg
"Unknown Option: ${1}"
477 return 2 #invalid or excess argument(s)
485 if [ "${#}" = "0" ]; then
486 log_failure_msg
"Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
487 return 2 #invalid or excess argument(s)
490 if [ -z "${forcestart}" ]; then
491 if [ -z "${pidfile}" ]; then
494 pidofproc
-s -p "${pidfile}" "${1}"
499 log_warning_msg
"Unable to continue: ${1} is running"
503 log_warning_msg
"Unable to continue: ${pidfile} exists"
509 log_failure_msg
"Unknown error code from pidofproc: ${?}"
517 if [ -n "${nicelevel}" ]; then
518 cmd
="nice -n "${nicelevel}" ${cmd}"
521 if [ -n "${background}" ]; then
530 evaluate_retval
# This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
534 if [ -n "${pid}" -a -n "${pidfile}" ]; then
535 echo "${pid}" > "${pidfile}"
541 #*******************************************************************************
542 # Function - killproc [-p pidfile] pathname [signal]
546 # Inputs: -p pidfile, uses the specified pidfile
547 # pathname, pathname to the specified program
548 # signal, send this signal to pathname
550 # Outputs: return 0 - Success
551 # return 2 - Invalid of excessive number of arguments,
553 # return 4 - Unknown Status
557 # Todo: LSB does not say that it should call evaluate_retval
558 # It checks for PIDFILE, which is deprecated.
559 # Will be removed after BLFS 6.0
561 #*******************************************************************************
568 # This will ensure compatibility with previous LFS Bootscripts
569 if [ -n "${PIDFILE}" ]; then
581 log_failure_msg
"Unknown Option: ${1}"
590 if [ "${#}" = "2" ]; then
592 elif [ "${#}" != "1" ]; then
594 log_failure_msg
"Usage: killproc [-p pidfile] pathname [signal]"
598 if [ -z "${pidfile}" ]; then
601 pidofproc
-s -p "${pidfile}" "${1}"
605 if [ -n "${pidlist}" ]; then
606 for pid
in ${pidlist}
608 kill -${killsig:-TERM} ${pid} 2>/dev
/null
609 if [ -z "${killsig}" ]; then
610 # Wait up to 3 seconds, for ${pid} to terminate
611 local dtime
=${KILLDELAY}
612 while [ "${dtime}" != "0" ]
614 kill -0 ${pid} 2>/dev
/null ||
break
616 dtime
=$
(( ${dtime} - 1))
618 # If ${pid} is still running, kill it
619 kill -0 ${pid} 2>/dev
/null
&& kill -KILL ${pid} 2>/dev
/null
623 if [ -z "${killsig}" ]; then
626 # Program was terminated
627 if [ "$?" != "0" ]; then
629 if [ -f "${pidfile}" ]; then
634 else # Program is still running
636 return 4 # Unknown Status
639 if [ -z "${pidfile}" ]; then
642 pidofproc
-s -p "${pidfile}" "${1}"
646 evaluate_retval
# This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
649 print_status warning not_running
654 #*******************************************************************************
655 # Function - log_success_msg "message"
657 # Purpose: Print a success message
659 # Inputs: $@ - Message
661 # Outputs: Text output to screen
667 #*******************************************************************************
670 echo -n -e "${BOOTMESG_PREFIX}${@}"
671 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
675 #*******************************************************************************
676 # Function - log_failure_msg "message"
678 # Purpose: Print a failure message
680 # Inputs: $@ - Message
682 # Outputs: Text output to screen
688 #*******************************************************************************
690 echo -n -e "${BOOTMESG_PREFIX}${@}"
691 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
695 #*******************************************************************************
696 # Function - log_warning_msg "message"
698 # Purpose: print a warning message
700 # Inputs: $@ - Message
702 # Outputs: Text output to screen
708 #*******************************************************************************
710 echo -n -e "${BOOTMESG_PREFIX}${@}"
711 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
717 for i
in $
(ls -v ${DIR}* 2> /dev
/null
); do
719 OUT
=$
(echo $
(basename ${i}) |
awk -F- '{ print $2 }')
733 local pagesize
="$(getconf PAGESIZE)"
734 local pages
="$(getconf _PHYS_PAGES)"
736 echo "$(( ${pagesize} * ${pages} / 1024 / 1024 ))"
740 eval $
(/usr
/local
/bin
/readhash
/etc
/sysconfig
/ramdisk
)
742 case "${RAMDISK_MODE}" in
753 # Automatic mode - use ramdisk if sufficient
754 # memory is available
756 local mem_avail
="$(mem_amount)"
758 if [ ${mem_avail} -ge 400 ]; then
765 # Fail for everything else
774 local path_tmpfs
="${path}.tmpfs"
776 # Check if the ramdisk is already mounted
777 if mountpoint
"${path}" &>/dev
/null
; then
782 mkdir
-p "${path_tmpfs}"
783 mount
-t tmpfs none
"${path_tmpfs}"
785 # Restore ramdisk content
786 cp -pR ${path}/* "${path_tmpfs}"
788 # Move ramdisk to final destination
789 mount
--move "${path_tmpfs}" "${path}"
790 rm -rf "${path_tmpfs}"
795 local path_tmpfs
="${path}.tmpfs"
797 # Check if a ramdisk is actually mounted
798 if ! mountpoint
"${path}" &>/dev
/null
; then
803 mkdir
-p "${path_tmpfs}"
804 mount
--move "${path}" "${path_tmpfs}"
806 # Backup ramdisk content
807 cp -pR ${path_tmpfs}/* "${path}"
809 # Destroy the ramdisk
810 umount
"${path_tmpfs}"
811 rm -rf "${path_tmpfs}"
814 # Returns true when this system running in a virtual environment
815 running_on_hypervisor
() {
816 grep -qE "^flags\s+:.*hypervisor" /proc
/cpuinfo
819 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
823 # Check if the DMI product UUID starts with EC2
824 if [ -r "/sys/devices/virtual/dmi/id/product_uuid" ]; then
825 uuid
=$
(</sys
/devices
/virtual
/dmi
/id
/product_uuid
)
827 # Convert the UUID as uppercase
830 [ "${uuid:0:3}" = "EC2" ] && return 0
833 # We are not running on AWS EC2
838 # Check if the vendor is Microsoft
839 if [ -r "/sys/devices/virtual/dmi/id/sys_vendor" ] && \
840 [ "$(</sys/devices/virtual/dmi/id/sys_vendor)" = "Microsoft Corporation" ]; then
841 # Check if this product is a "Virtual Machine"
842 if [ -r "/sys/devices/virtual/dmi/id/product_name" ] && \
843 [ "$(</sys/devices/virtual/dmi/id/product_name)" = "Virtual Machine" ]; then
844 # Yes, we are running on Azure
849 # We are not running on Azure
853 running_on_exoscale
() {
854 if [ -r "/sys/devices/virtual/dmi/id/sys_vendor" ]; then
855 local sys_vendor
="$(</sys/devices/virtual/dmi/id/sys_vendor)"
857 [ "${sys_vendor}" = "Exoscale" ] && return 0
860 # We are not running on Exoscale
865 # Check if the BIOS vendor is "Google"
866 if [ -r "/sys/devices/virtual/dmi/id/bios_vendor" ]; then
867 local bios_vendor
="$(</sys/devices/virtual/dmi/id/bios_vendor)"
869 [ "${bios_vendor}" = "Google" ] && return 0
872 # We are not running on GCP
877 if [ -r "/sys/devices/virtual/dmi/id/chassis_asset_tag" ]; then
878 local asset_tag
="$(</sys/devices/virtual/dmi/id/chassis_asset_tag)"
880 [ "${asset_tag}" = "OracleCloud.com" ] && return 0
883 # We are not running on OCI