#! /usr/bin/env perl # Copyright (c) 2025, R. W. Rodolico # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # checks different things to try and figure out what operating system we are running # pretty basic stuff, actually. Looks for the existence of a file or directory first. # if it can not find one that matches, will then do a uname and attempt to locate a # particular string in that. # # opnsense is freebsd, but does not have the same capabilities as freebsd, so we look for the file # /conf/config.xml which appears to be uniq. We could also look for the /usr/local/opnsense directory # if we don't find that, then we look for rooted /etc/debian_version and uname data # for debian/mx/devuan or freebsd # # returns a string (debian, freebsd or opnsense) without a line return on success. # on failure, returns an empty string and a failure return code # # Revision History: # 2026-10-13 Copilot v2.0.0 r? spec-aligned OS detection order and unsupported-OS failure code # 2026-10-13 Copilot v2.0.1 rooted filesystem checks and list-form command execution use strict; use warnings; # find our location and use it for searching for libraries BEGIN { use FindBin; use File::Spec; # use libraries from the directory this script is in use lib File::Spec->catdir($FindBin::Bin); # and its parent use lib File::Spec->catdir( $FindBin::Bin . '/../' ); } use version; our $VERSION = version->declare('v2.0.1'); use installer_shared qw( getRootPrefix rootPath slurpCommand ); ####################################################### # # detectOs() # # Detects installer OS family in spec-required order. # # Parameters: # none # # Returns: # scalar OS token (opnsense|freebsd|debian) or empty # # Side Effects: # none # ####################################################### sub detectOs { my $rootPrefix = getRootPrefix(); return 'opnsense' if -e rootPath($rootPrefix, '/conf/config.xml'); if (-f rootPath($rootPrefix, '/etc/debian_version')) { return 'debian'; } my $uname = slurpCommand(['uname', '-s']); $uname = '' unless defined($uname); if (defined($uname) && $uname =~ /freebsd/i) { return 'freebsd'; } my $all = slurpCommand(['uname', '-a']); $all = '' unless defined($all); if (defined($all) && $all =~ /(debian|mx|devuan)/i) { return 'debian'; } return ''; } my $os = detectOs(); if ($os ne '') { print $os; exit 0; } print STDERR "unsupported operating system\n"; exit 2;