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 [ -z "${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 #*******************************************************************************
450 # This will ensure compatibility with previous LFS Bootscripts
451 if [ -n "${PIDFILE}" ]; then
475 log_failure_msg
"Unknown Option: ${1}"
476 return 2 #invalid or excess argument(s)
484 if [ "${#}" = "0" ]; then
485 log_failure_msg
"Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
486 return 2 #invalid or excess argument(s)
489 if [ -z "${forcestart}" ]; then
490 if [ -z "${pidfile}" ]; then
493 pidofproc
-s -p "${pidfile}" "${1}"
498 log_warning_msg
"Unable to continue: ${1} is running"
502 log_warning_msg
"Unable to continue: ${pidfile} exists"
508 log_failure_msg
"Unknown error code from pidofproc: ${?}"
516 if [ -n "${nicelevel}" ]; then
517 cmd
="nice -n "${nicelevel}" ${cmd}"
520 if [ -n "${background}" ]; then
527 evaluate_retval
# This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
533 #*******************************************************************************
534 # Function - killproc [-p pidfile] pathname [signal]
538 # Inputs: -p pidfile, uses the specified pidfile
539 # pathname, pathname to the specified program
540 # signal, send this signal to pathname
542 # Outputs: return 0 - Success
543 # return 2 - Invalid of excessive number of arguments,
545 # return 4 - Unknown Status
549 # Todo: LSB does not say that it should call evaluate_retval
550 # It checks for PIDFILE, which is deprecated.
551 # Will be removed after BLFS 6.0
553 #*******************************************************************************
560 # This will ensure compatibility with previous LFS Bootscripts
561 if [ -n "${PIDFILE}" ]; then
573 log_failure_msg
"Unknown Option: ${1}"
582 if [ "${#}" = "2" ]; then
584 elif [ "${#}" != "1" ]; then
586 log_failure_msg
"Usage: killproc [-p pidfile] pathname [signal]"
590 if [ -z "${pidfile}" ]; then
593 pidofproc
-s -p "${pidfile}" "${1}"
597 if [ -n "${pidlist}" ]; then
598 for pid
in ${pidlist}
600 kill -${killsig:-TERM} ${pid} 2>/dev
/null
601 if [ -z "${killsig}" ]; then
602 # Wait up to 3 seconds, for ${pid} to terminate
603 local dtime
=${KILLDELAY}
604 while [ "${dtime}" != "0" ]
606 kill -0 ${pid} 2>/dev
/null ||
break
608 dtime
=$
(( ${dtime} - 1))
610 # If ${pid} is still running, kill it
611 kill -0 ${pid} 2>/dev
/null
&& kill -KILL ${pid} 2>/dev
/null
615 if [ -z "${killsig}" ]; then
618 # Program was terminated
619 if [ "$?" != "0" ]; then
621 if [ -f "${pidfile}" ]; then
626 else # Program is still running
628 return 4 # Unknown Status
631 if [ -z "${pidfile}" ]; then
634 pidofproc
-s -p "${pidfile}" "${1}"
638 evaluate_retval
# This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
641 print_status warning not_running
646 #*******************************************************************************
647 # Function - log_success_msg "message"
649 # Purpose: Print a success message
651 # Inputs: $@ - Message
653 # Outputs: Text output to screen
659 #*******************************************************************************
662 echo -n -e "${BOOTMESG_PREFIX}${@}"
663 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
667 #*******************************************************************************
668 # Function - log_failure_msg "message"
670 # Purpose: Print a failure message
672 # Inputs: $@ - Message
674 # Outputs: Text output to screen
680 #*******************************************************************************
682 echo -n -e "${BOOTMESG_PREFIX}${@}"
683 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
687 #*******************************************************************************
688 # Function - log_warning_msg "message"
690 # Purpose: print a warning message
692 # Inputs: $@ - Message
694 # Outputs: Text output to screen
700 #*******************************************************************************
702 echo -n -e "${BOOTMESG_PREFIX}${@}"
703 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
709 for i
in $
(ls -v ${DIR}* 2> /dev
/null
); do
711 OUT
=$
(echo $
(basename ${i}) |
awk -F- '{ print $2 }')
725 local pagesize
="$(getconf PAGESIZE)"
726 local pages
="$(getconf _PHYS_PAGES)"
728 echo "$(( ${pagesize} * ${pages} / 1024 / 1024 ))"
732 eval $
(/usr
/local
/bin
/readhash
/etc
/sysconfig
/ramdisk
)
734 case "${RAMDISK_MODE}" in
745 # Automatic mode - use ramdisk if sufficient
746 # memory is available
748 local mem_avail
="$(mem_amount)"
750 if [ ${mem_avail} -ge 400 ]; then
757 # Fail for everything else
766 local path_tmpfs
="${path}.tmpfs"
768 # Check if the ramdisk is already mounted
769 if mountpoint
"${path}" &>/dev
/null
; then
774 mkdir
-p "${path_tmpfs}"
775 mount
-t tmpfs none
"${path_tmpfs}"
777 # Restore ramdisk content
778 cp -pR ${path}/* "${path_tmpfs}"
780 # Move ramdisk to final destination
781 mount
--move "${path_tmpfs}" "${path}"
782 rm -rf "${path_tmpfs}"
787 local path_tmpfs
="${path}.tmpfs"
789 # Check if a ramdisk is actually mounted
790 if ! mountpoint
"${path}" &>/dev
/null
; then
795 mkdir
-p "${path_tmpfs}"
796 mount
--move "${path}" "${path_tmpfs}"
798 # Backup ramdisk content
799 cp -pR ${path_tmpfs}/* "${path}"
801 # Destroy the ramdisk
802 umount
"${path_tmpfs}"
803 rm -rf "${path_tmpfs}"
806 # Returns true when this system running in a virtual environment
807 running_on_hypervisor
() {
808 grep -qE "^flags\s+:.*hypervisor" /proc
/cpuinfo
811 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
815 # Check if the DMI product UUID starts with EC2
816 if [ -r "/sys/devices/virtual/dmi/id/product_uuid" ]; then
817 uuid
=$
(</sys
/devices
/virtual
/dmi
/id
/product_uuid
)
819 # Convert the UUID as uppercase
822 [ "${uuid:0:3}" = "EC2" ] && return 0
825 # We are not running on AWS EC2
830 # Check if the vendor is Microsoft
831 if [ -r "/sys/devices/virtual/dmi/id/sys_vendor" ] && \
832 [ "$(</sys/devices/virtual/dmi/id/sys_vendor)" = "Microsoft Corporation" ]; then
833 # Check if this product is a "Virtual Machine"
834 if [ -r "/sys/devices/virtual/dmi/id/product_name" ] && \
835 [ "$(</sys/devices/virtual/dmi/id/product_name)" = "Virtual Machine" ]; then
836 # Yes, we are running on Azure
841 # We are not running on Azure
845 running_on_exoscale
() {
846 if [ -r "/sys/devices/virtual/dmi/id/sys_vendor" ]; then
847 local sys_vendor
="$(</sys/devices/virtual/dmi/id/sys_vendor)"
849 [ "${sys_vendor}" = "Exoscale" ] && return 0
852 # We are not running on Exoscale
857 # Check if the BIOS vendor is "Google"
858 if [ -r "/sys/devices/virtual/dmi/id/bios_vendor" ]; then
859 local bios_vendor
="$(</sys/devices/virtual/dmi/id/bios_vendor)"
861 [ "${bios_vendor}" = "Google" ] && return 0
864 # We are not running on GCP
869 if [ -r "/sys/devices/virtual/dmi/id/chassis_asset_tag" ]; then
870 local asset_tag
="$(</sys/devices/virtual/dmi/id/chassis_asset_tag)"
872 [ "${asset_tag}" = "OracleCloud.com" ] && return 0
875 # We are not running on OCI