]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/system/functions
suricata: Change midstream policy to "pass-flow"
[ipfire-2.x.git] / src / initscripts / system / functions
CommitLineData
73d9a908 1#!/bin/sh
66c36198
PM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
73d9a908
MT
21
22## Environmental setup
23# Setup default values for environment
24umask 022
25export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
26
27# Signal sent to running processes to refresh their configuration
28RELOADSIG="HUP"
29
30# Number of seconds between STOPSIG and FALLBACK when stopping processes
cb1fb691 31KILLDELAY="10"
73d9a908
MT
32
33## Screen Dimensions
34# Find current screen size
35if [ -z "${COLUMNS}" ]; then
324bb888 36 COLUMNS=$(stty size 2>/dev/null)
73d9a908
MT
37 COLUMNS=${COLUMNS##* }
38fi
39
40# When using remote connections, such as a serial port, stty size returns 0
41if [ "${COLUMNS}" = "0" ]; then
42 COLUMNS=80
43fi
44
45## Measurements for positioning result messages
46COL=$((${COLUMNS} - 8))
47WCOL=$((${COL} - 2))
48
49## Set Cursor Position Commands, used via echo -e
50SET_COL="\\033[${COL}G" # at the $COL char
51SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
52CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
53
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
57#
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
62NORMAL="\\033[0;39m" # Standard console grey
63SUCCESS="\\033[1;32m" # Success is green
64WARNING="\\033[1;33m" # Warnings are yellow
65FAILURE="\\033[1;31m" # Failures are red
66INFO="\\033[1;36m" # Information is light cyan
67BRACKET="\\033[1;34m" # Brackets are blue
68
69STRING_LENGTH="0" # the length of the current message
70
71#*******************************************************************************
72# Function - boot_mesg()
73#
74# Purpose: Sending information from bootup scripts to the console
75#
76# Inputs: $1 is the message
77# $2 is the colorcode for the console
78#
79# Outputs: Standard Output
80#
81# Dependencies: - sed for parsing strings.
82# - grep for counting string length.
83#
84# Todo:
85#*******************************************************************************
86boot_mesg()
87{
88 local ECHOPARM=""
89
90 while true
91 do
92 case "${1}" in
93 -n)
94 ECHOPARM=" -n "
95 shift 1
96 ;;
97 -*)
98 echo "Unknown Option: ${1}"
99 return 1
100 ;;
101 *)
102 break
103 ;;
104 esac
105 done
106
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 \$`"
111
112 # Print the message to the screen
113 echo ${ECHOPARM} -e "${2}${1}"
114
115}
116
117boot_mesg_flush()
118{
119 # Reset STRING_LENGTH for next message
120 STRING_LENGTH="0"
121}
122
123boot_log()
124{
125 # Left in for backwards compatibility
126 echo -n ""
127}
128
129echo_ok()
130{
131 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]"
132 echo -e "${NORMAL}"
133 boot_mesg_flush
134}
135
136echo_failure()
137{
138 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
139 echo -e "${NORMAL}"
140 boot_mesg_flush
141}
142
143echo_warning()
144{
145 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
146 echo -e "${NORMAL}"
147 boot_mesg_flush
148}
149
150print_error_msg()
151{
152 echo_failure
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"
158 boot_mesg_flush
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"
4601019b 161 boot_mesg -n " ipfire, please be so kind to inform us at"
31a36bb9 162 boot_mesg " https://bugzilla.ipfire.org.\n"
73d9a908 163 boot_mesg_flush
e147d1e6 164 boot_mesg -n "Press Enter to continue or wait a minute..." ${INFO}
73d9a908 165 boot_mesg "" ${NORMAL}
e147d1e6 166 read -t 60 ENTER
73d9a908
MT
167}
168
169check_script_status()
170{
171 # $i is inherited by the rc script
172 if [ ! -f ${i} ]; then
173 boot_mesg "${i} is not a valid symlink." ${WARNING}
174 echo_warning
175 continue
176 fi
177
178 if [ ! -x ${i} ]; then
179 boot_mesg "${i} is not executable, skipping." ${WARNING}
180 echo_warning
181 continue
182 fi
183}
184
185evaluate_retval()
186{
187 error_value="${?}"
188
189 if [ ${error_value} = 0 ]; then
190 echo_ok
191 else
192 echo_failure
193 fi
194
195 # This prevents the 'An Unexpected Error Has Occurred' from trivial
196 # errors.
197 return 0
198}
199
200print_status()
201{
202 if [ "${#}" = "0" ]; then
203 echo "Usage: ${0} {success|warning|failure}"
204 return 1
205 fi
206
207 case "${1}" in
208
209 success)
210 echo_ok
211 ;;
212
213 warning)
214 # Leave this extra case in because old scripts
215 # may call it this way.
216 case "${2}" in
217 running)
218 echo -e -n "${CURS_UP}"
219 echo -e -n "\\033[${STRING_LENGTH}G "
220 boot_mesg "Already running." ${WARNING}
221 echo_warning
222 ;;
223 not_running)
224 echo -e -n "${CURS_UP}"
225 echo -e -n "\\033[${STRING_LENGTH}G "
226 boot_mesg "Not running." ${WARNING}
227 echo_warning
228 ;;
229 not_available)
230 echo -e -n "${CURS_UP}"
231 echo -e -n "\\033[${STRING_LENGTH}G "
232 boot_mesg "Not available." ${WARNING}
233 echo_warning
234 ;;
235 *)
236 # This is how it is supposed to
237 # be called
238 echo_warning
239 ;;
240 esac
241 ;;
242
243 failure)
244 echo_failure
245 ;;
246
247 esac
248
249}
250
251reloadproc()
252{
253 if [ "${#}" = "0" ]; then
254 echo "Usage: reloadproc [{program}]"
255 exit 1
256 fi
257
258 getpids "${1}"
259
260 if [ -n "${pidlist}" ]; then
261 failure="0"
262 for pid in ${pidlist}
263 do
264 kill -"${RELOADSIG}" "${pid}" || failure="1"
265 done
266
267 (exit ${failure})
268 evaluate_retval
269
270 else
271 boot_mesg "Process ${1} not running." ${WARNING}
272 echo_warning
273 fi
274}
275
276statusproc()
277{
278 if [ "${#}" = "0" ]
279 then
280 echo "Usage: statusproc {program}"
281 exit 1
282 fi
283
284 getpids "${1}"
285
286 if [ -n "${pidlist}" ]; then
287 echo -e "${INFO}${base} is running with Process"\
288 "ID(s) ${pidlist}.${NORMAL}"
289 else
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}"
293 else
294 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
295 echo -e "${WARNING}${1} is not running"\
296 "but ${PIDFILE} exists.${NORMAL}"
297 else
298 echo -e "${INFO}${1} is not running.${NORMAL}"
299 fi
300 fi
301 fi
302}
303
304# The below functions are documented in the LSB-generic 2.1.0
305
306#*******************************************************************************
307# Function - pidofproc [-s] [-p pidfile] pathname
308#
309# Purpose: This function returns one or more pid(s) for a particular daemon
310#
311# Inputs: -p pidfile, use the specified pidfile instead of pidof
312# pathname, path to the specified program
313#
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,
317# warning in stdout
318# return 3 - Program is not running
319#
320# Dependencies: pidof, echo, head
321#
322# Todo: Remove dependency on head
323# This depreciates getpids
324# Test changes to pidof
325#
326#*******************************************************************************
327pidofproc()
328{
329 local pidfile=""
330 local lpids=""
331 local silent=""
332 pidlist=""
333 while true
334 do
335 case "${1}" in
336 -p)
337 pidfile="${2}"
338 shift 2
339 ;;
340
341 -s)
342 # Added for legacy opperation of getpids
343 # eliminates several '> /dev/null'
344 silent="1"
345 shift 1
346 ;;
347 -*)
348 log_failure_msg "Unknown Option: ${1}"
349 return 2
350 ;;
351 *)
352 break
353 ;;
354 esac
355 done
356
357 if [ "${#}" != "1" ]; then
358 shift 1
359 log_failure_msg "Usage: pidofproc [-s] [-p pidfile] pathname"
360 return 2
361 fi
362
363 if [ -n "${pidfile}" ]; then
364 if [ ! -r "${pidfile}" ]; then
365 return 3 # Program is not running
366 fi
367
368 lpids=`head -n 1 ${pidfile}`
369 for pid in ${lpids}
370 do
371 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
372 kill -0 "${pid}" > /dev/null &&
373 pidlist="${pidlist} ${pid}"
374 fi
375
376 if [ "${silent}" -ne "1" ]; then
377 echo "${pidlist}"
378 fi
379
380 test -z "${pidlist}" &&
381 # Program is dead, pidfile exists
382 return 1
383 # else
384 return 0
385 done
386
387 else
388 pidlist=`pidof -o $$ -o $PPID -x "$1"`
389 if [ "x${silent}" != "x1" ]; then
390 echo "${pidlist}"
391 fi
392
393 # Get provide correct running status
394 if [ -n "${pidlist}" ]; then
395 return 0
396 else
397 return 3
398 fi
399
400 fi
401
402 if [ "$?" != "0" ]; then
403 return 3 # Program is not running
404 fi
405}
406
407# This will ensure compatibility with previous LFS Bootscripts
408getpids()
409{
dd8ef8cc 410 if [ -n "${PIDFILE}" ]; then
73d9a908
MT
411 pidofproc -s -p "${PIDFILE}" $@
412 else
413 pidofproc -s $@
414 fi
415 base="${1##*/}"
416}
417
418#*******************************************************************************
419# Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
420#
421# Purpose: This runs the specified program as a daemon
422#
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
428#
429# Outputs: return 0 - Success
430# return 2 - Invalid of excessive number of arguments,
431# warning in stdout
432# return 4 - Program or service status is unknown
433#
434# Dependencies: nice
435#
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
441#
442#*******************************************************************************
443loadproc()
444{
0c7ba652 445 local background=""
73d9a908
MT
446 local pidfile=""
447 local forcestart=""
b64d57aa 448 local nicelevel=""
c3019331 449 local pid
73d9a908
MT
450
451# This will ensure compatibility with previous LFS Bootscripts
452 if [ -n "${PIDFILE}" ]; then
453 pidfile="${PIDFILE}"
454 fi
455
456 while true
457 do
458 case "${1}" in
0c7ba652
MT
459 -b)
460 background="1"
461 shift 1
462 ;;
73d9a908
MT
463 -f)
464 forcestart="1"
465 shift 1
466 ;;
467 -n)
468 nicelevel="${2}"
469 shift 2
470 ;;
471 -p)
472 pidfile="${2}"
473 shift 2
474 ;;
475 -*)
476 log_failure_msg "Unknown Option: ${1}"
477 return 2 #invalid or excess argument(s)
478 ;;
479 *)
480 break
481 ;;
482 esac
483 done
484
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)
488 fi
489
490 if [ -z "${forcestart}" ]; then
491 if [ -z "${pidfile}" ]; then
492 pidofproc -s "${1}"
493 else
494 pidofproc -s -p "${pidfile}" "${1}"
495 fi
496
497 case "${?}" in
498 0)
499 log_warning_msg "Unable to continue: ${1} is running"
500 return 0 # 4
501 ;;
502 1)
503 log_warning_msg "Unable to continue: ${pidfile} exists"
504 return 0 # 4
505 ;;
506 3)
507 ;;
508 *)
509 log_failure_msg "Unknown error code from pidofproc: ${?}"
510 return 4
511 ;;
512 esac
513 fi
514
b64d57aa
MT
515 local cmd="${@}"
516
517 if [ -n "${nicelevel}" ]; then
518 cmd="nice -n "${nicelevel}" ${cmd}"
519 fi
520
0c7ba652
MT
521 if [ -n "${background}" ]; then
522 (
523 ${cmd} &>/dev/null
524 ) &
c3019331 525 pid="$!"
0c7ba652
MT
526 evaluate_retval
527 else
528 ${cmd}
c3019331 529 pid="$!"
0c7ba652
MT
530 evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
531 fi
532
c3019331
MT
533 # Write the pidfile
534 if [ -n "${pid}" -a -n "${pidfile}" ]; then
535 echo "${pid}" > "${pidfile}"
536 fi
537
73d9a908
MT
538 return 0
539}
540
541#*******************************************************************************
542# Function - killproc [-p pidfile] pathname [signal]
543#
544# Purpose:
545#
546# Inputs: -p pidfile, uses the specified pidfile
547# pathname, pathname to the specified program
548# signal, send this signal to pathname
549#
550# Outputs: return 0 - Success
551# return 2 - Invalid of excessive number of arguments,
552# warning in stdout
553# return 4 - Unknown Status
554#
555# Dependencies: kill
556#
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
560#
561#*******************************************************************************
562killproc()
563{
564 local pidfile=""
565 local killsig=""
566 pidlist=""
567
568# This will ensure compatibility with previous LFS Bootscripts
569 if [ -n "${PIDFILE}" ]; then
570 pidfile="${PIDFILE}"
571 fi
572
573 while true
574 do
575 case "${1}" in
576 -p)
577 pidfile="${2}"
578 shift 2
579 ;;
580 -*)
581 log_failure_msg "Unknown Option: ${1}"
582 return 2
583 ;;
584 *)
585 break
586 ;;
587 esac
588 done
589
590 if [ "${#}" = "2" ]; then
591 killsig="${2}"
592 elif [ "${#}" != "1" ]; then
593 shift 2
594 log_failure_msg "Usage: killproc [-p pidfile] pathname [signal]"
595 return 2
596 fi
597
598 if [ -z "${pidfile}" ]; then
599 pidofproc -s "${1}"
600 else
601 pidofproc -s -p "${pidfile}" "${1}"
602 fi
603
604 # Change....
605 if [ -n "${pidlist}" ]; then
606 for pid in ${pidlist}
607 do
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" ]
613 do
614 kill -0 ${pid} 2>/dev/null || break
615 sleep 1
616 dtime=$(( ${dtime} - 1))
617 done
618 # If ${pid} is still running, kill it
619 kill -0 ${pid} 2>/dev/null && kill -KILL ${pid} 2>/dev/null
620 fi
621 done
622
623 if [ -z "${killsig}" ]; then
624 pidofproc -s "${1}"
625
626 # Program was terminated
627 if [ "$?" != "0" ]; then
628 # Pidfile Exists
629 if [ -f "${pidfile}" ]; then
630 rm -f "${pidfile}"
631 fi
632 echo_ok
633 return 0
634 else # Program is still running
635 echo_failure
636 return 4 # Unknown Status
637 fi
638 else
639 if [ -z "${pidfile}" ]; then
640 pidofproc -s "${1}"
641 else
642 pidofproc -s -p "${pidfile}" "${1}"
643 fi
644 fi
645
646 evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
647
648 else
649 print_status warning not_running
650 fi
651}
652
653
654#*******************************************************************************
655# Function - log_success_msg "message"
656#
657# Purpose: Print a success message
658#
659# Inputs: $@ - Message
660#
661# Outputs: Text output to screen
662#
663# Dependencies: echo
664#
665# Todo: logging
666#
667#*******************************************************************************
668log_success_msg()
669{
670 echo -n -e "${BOOTMESG_PREFIX}${@}"
671 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
672 return 0
673}
674
675#*******************************************************************************
676# Function - log_failure_msg "message"
677#
678# Purpose: Print a failure message
679#
680# Inputs: $@ - Message
681#
682# Outputs: Text output to screen
683#
684# Dependencies: echo
685#
686# Todo: logging
687#
688#*******************************************************************************
689log_failure_msg() {
690 echo -n -e "${BOOTMESG_PREFIX}${@}"
691 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
692 return 0
693}
694
695#*******************************************************************************
696# Function - log_warning_msg "message"
697#
698# Purpose: print a warning message
699#
700# Inputs: $@ - Message
701#
702# Outputs: Text output to screen
703#
704# Dependencies: echo
705#
706# Todo: logging
707#
708#*******************************************************************************
709log_warning_msg() {
710 echo -n -e "${BOOTMESG_PREFIX}${@}"
711 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
712 return 0
713}
714
0e42072a 715run_subdir() {
905fbf3e
MT
716 DIR=$1
717 for i in $(ls -v ${DIR}* 2> /dev/null); do
0e42072a
MT
718 check_script_status
719 OUT=$(echo $(basename ${i}) | awk -F- '{ print $2 }')
720 case "$OUT" in
721 S) ${i} start ;;
722 K) ${i} stop ;;
723 RS) ${i} restart ;;
724 RL) ${i} reload ;;
ebca0d9a 725 U) ${i} up ;;
905fbf3e 726 D) ${i} down ;;
0e42072a
MT
727 *) ${i} ;;
728 esac
729 done
378cbbe0
MT
730}
731
c4a451ee
AM
732mem_amount() {
733 local pagesize="$(getconf PAGESIZE)"
734 local pages="$(getconf _PHYS_PAGES)"
735
736 echo "$(( ${pagesize} * ${pages} / 1024 / 1024 ))"
737}
738
ee3dec50
MT
739use_ramdisk() {
740 eval $(/usr/local/bin/readhash /etc/sysconfig/ramdisk)
741
742 case "${RAMDISK_MODE}" in
743 # Don't use ramdisk
744 0)
745 return 1
746 ;;
747
748 # Always use ramdisk
749 1)
750 return 0
751 ;;
752
753 # Automatic mode - use ramdisk if sufficient
754 # memory is available
755 2)
756 local mem_avail="$(mem_amount)"
757
c83d1614 758 if [ ${mem_avail} -ge 400 ]; then
ee3dec50
MT
759 return 0
760 else
761 return 1
762 fi
763 ;;
764
765 # Fail for everything else
766 *)
767 return 2
768 ;;
769 esac
770}
771
c4a451ee 772mount_ramdisk() {
6146d190
MT
773 local path="${1}"
774 local path_tmpfs="${path}.tmpfs"
c4a451ee 775
6146d190
MT
776 # Check if the ramdisk is already mounted
777 if mountpoint "${path}" &>/dev/null; then
778 return 0
c4a451ee 779 fi
c4a451ee 780
6146d190
MT
781 # Create ramdisk
782 mkdir -p "${path_tmpfs}"
783 mount -t tmpfs none "${path_tmpfs}"
784
785 # Restore ramdisk content
24f2144d 786 cp -pR ${path}/* "${path_tmpfs}"
5258a65d 787
6146d190
MT
788 # Move ramdisk to final destination
789 mount --move "${path_tmpfs}" "${path}"
b5e1360e 790 rm -rf "${path_tmpfs}"
c4a451ee
AM
791}
792
6146d190
MT
793umount_ramdisk() {
794 local path="${1}"
795 local path_tmpfs="${path}.tmpfs"
796
797 # Check if a ramdisk is actually mounted
798 if ! mountpoint "${path}" &>/dev/null; then
799 return 0
c4a451ee 800 fi
6146d190
MT
801
802 # Move the ramdisk
803 mkdir -p "${path_tmpfs}"
804 mount --move "${path}" "${path_tmpfs}"
805
806 # Backup ramdisk content
24f2144d 807 cp -pR ${path_tmpfs}/* "${path}"
6146d190
MT
808
809 # Destroy the ramdisk
810 umount "${path_tmpfs}"
b5e1360e 811 rm -rf "${path_tmpfs}"
c4a451ee 812}
5258a65d 813
0e457b13
MT
814# Returns true when this system running in a virtual environment
815running_on_hypervisor() {
816 grep -qE "^flags\s+:.*hypervisor" /proc/cpuinfo
817}
818
8bd0c4b1
MT
819# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
820running_on_ec2() {
821 local uuid
822
8bd0c4b1
MT
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)
826
80b1dc64
MT
827 # Convert the UUID as uppercase
828 uuid="${uuid^^}"
829
8bd0c4b1
MT
830 [ "${uuid:0:3}" = "EC2" ] && return 0
831 fi
832
833 # We are not running on AWS EC2
834 return 1
835}
836
837running_on_azure() {
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
845 return 0
846 fi
847 fi
848
849 # We are not running on Azure
850 return 1
851}
852
5ae3706d
MT
853running_on_exoscale() {
854 if [ -r "/sys/devices/virtual/dmi/id/sys_vendor" ]; then
855 local sys_vendor="$(</sys/devices/virtual/dmi/id/sys_vendor)"
856
857 [ "${sys_vendor}" = "Exoscale" ] && return 0
858 fi
859
860 # We are not running on Exoscale
861 return 1
862}
863
86c64598
MT
864running_on_gcp() {
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)"
868
869 [ "${bios_vendor}" = "Google" ] && return 0
870 fi
871
872 # We are not running on GCP
873 return 1
874}
875
7c24a0d9
MT
876running_on_oci() {
877 if [ -r "/sys/devices/virtual/dmi/id/chassis_asset_tag" ]; then
878 local asset_tag="$(</sys/devices/virtual/dmi/id/chassis_asset_tag)"
879
880 [ "${asset_tag}" = "OracleCloud.com" ] && return 0
881 fi
882
883 # We are not running on OCI
884 return 1
885}