From patchwork Wed Aug 2 09:14:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tremer X-Patchwork-Id: 7063 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4RG5s03Tnqz3wmm for ; Wed, 2 Aug 2023 09:14:28 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4RG5rr5zlYz1Z8; Wed, 2 Aug 2023 09:14:20 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4RG5rr14Y1z2yqJ; Wed, 2 Aug 2023 09:14:20 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4RG5rp04rmz2yT2 for ; Wed, 2 Aug 2023 09:14:18 +0000 (UTC) Received: from michael.haj.ipfire.org (michael.haj.ipfire.org [172.28.1.242]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384 client-signature ECDSA (secp384r1) client-digest SHA384) (Client CN "michael.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4RG5rn0mPZz6Z; Wed, 2 Aug 2023 09:14:17 +0000 (UTC) Received: by michael.haj.ipfire.org (Postfix, from userid 0) id 4RG5rm4tzMzTgrJ; Wed, 2 Aug 2023 09:14:16 +0000 (UTC) From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH 1/3] extrahd: Rewrite the mount script in shell Date: Wed, 2 Aug 2023 09:14:13 +0000 Message-Id: <20230802091415.3168597-1-michael.tremer@ipfire.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Tremer Errors-To: development-bounces@lists.ipfire.org Sender: "Development" This is probably a lot easier than calling all sorts of shell commands from Perl. The script has also changed that it will try to mount/umount all configured mountpoints unless a specific mountpoint is being given. An initscript will be needed to mount everything when the system is booting up and umount everything on shutdown. Signed-off-by: Michael Tremer --- config/extrahd/extrahd.pl | 173 ++++++++++++++++++++++---------------- 1 file changed, 101 insertions(+), 72 deletions(-) diff --git a/config/extrahd/extrahd.pl b/config/extrahd/extrahd.pl index 3b57e9230..be91e27f6 100644 --- a/config/extrahd/extrahd.pl +++ b/config/extrahd/extrahd.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # -# Copyright (C) 2010 IPFire Team # +# Copyright (C) 2023 IPFire Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # @@ -19,75 +19,104 @@ # # ############################################################################### -use strict; -# enable only the following on debugging purpose -# use warnings; - -require '/var/ipfire/general-functions.pl'; -require "${General::swroot}/lang.pl"; -require "${General::swroot}/header.pl"; - -my %extrahdsettings = (); -my $ok = "true"; -my @devices = (); -my @deviceline = (); -my $deviceentry = ""; -my $devicefile = "/var/ipfire/extrahd/devices"; -my $fstab = "/var/ipfire/extrahd/fstab"; - -### Values that have to be initialized -$extrahdsettings{'PATH'} = ''; -$extrahdsettings{'FS'} = ''; -$extrahdsettings{'DEVICE'} = ''; -$extrahdsettings{'ACTION'} = ''; - -open( FILE, "< $devicefile" ) or die "Unable to read $devicefile"; -@devices = ; -close FILE; - -############################################################################################################################ -############################################################################################################################ - -if ( "$ARGV[0]" eq "mount" ) { - system("/bin/cp -f /etc/fstab $fstab"); - - foreach $deviceentry (sort @devices) - { - @deviceline = split( /\;/, $deviceentry ); - if ( "$ARGV[1]" eq "$deviceline[2]" ) { - print "Insert $deviceline[0] ($deviceline[1]) --> $deviceline[2] into /etc/fstab!\n"; - unless ( -d $deviceline[2] ) { system("/bin/mkdir -p $deviceline[2] && chmod 0777 $deviceline[2]"); } - open(FILE, ">>$fstab"); - print FILE "$deviceline[0]\t$deviceline[2]\t$deviceline[1]\tdefaults\t0\t0\n"; - close(FILE); - } - } - - system("/bin/cp -f $fstab /etc/fstab"); - if ( `/bin/mount -a` ) { - exit(0); - } else { - exit(1); - } - -} elsif ( "$ARGV[0]" eq "umount" ) { - system("/bin/umount $ARGV[1]"); - if ( ! `/bin/mount | /bin/fgrep $ARGV[1]` ) { - system("/bin/cp -f /etc/fstab $fstab"); - system("/bin/fgrep -v $ARGV[1] <$fstab >/etc/fstab"); - print "Successfully umounted $ARGV[1].\n"; - exit(0); - } else { - print "Can't umount $ARGV[1].\n"; - exit(1); - } - -} elsif ( "$ARGV[0]" eq "scanhd") { - system("/usr/local/bin/scanhd $ARGV[1]"); - -} else { - print "Usage: $0 (mount|umount|scanhd) mountpoint\n"; +log() { + local message="${@}" + + logger -t "extrahd" "${message}" +} + +extrahd_mount() { + local _mountpoint="${1}" + + local device + local filesystem + local mountpoint + local rest + local failed=0 + + while IFS=';' read -r device filesystem mountpoint rest; do + # Filter by mountpoint if set + if [ -n "${_mountpoint}" ] && [ "${mountpoint}" != "${_mountpoint}" ]; then + continue + fi + + # Skip mounting if something is already mounted at the mountpoint + if mountpoint "${mountpoint}" &>/dev/null; then + continue + fi + + # Ensure the mountpoint exists + mkdir --parents --mode=777 "${mountpoint}" &>/dev/null + + if mount --types "${filesystem}" "${device}" "${mountpoint}"; then + log "Successfully mounted ${device} to ${mountpoint}" + else + log "Could not mount ${device} to ${mountpoint}: $?" + failed=1 + fi + done < /var/ipfire/extrahd/devices + + return ${failed} +} + +extrahd_umount() { + local _mountpoint="${1}" + + local device + local filesystem + local mountpoint + local rest + local failed=0 + + while IFS=';' read -r device filesystem mountpoint rest; do + # Filter by mountpoint if set + if [ -n "${_mountpoint}" ] && [ "${mountpoint}" != "${_mountpoint}" ]; then + continue + fi + + # Umount and try lazy umount if failed + if umount --quiet --recursive "${mountpoint}" || \ + umount --quiet --recursive --lazy "${mountpoint}"; then + log "Successfully umounted ${device} from ${mountpoint}" + else + log "Could not umount ${device} from ${mountpoint}: $?" + failed=1 + fi + done < /var/ipfire/extrahd/devices +} + +main() { + local command="${1}" + shift + + local rc=0 + + case "${command}" in + mount) + extrahd_mount "${@}" || rc="${?}" + ;; + umount) + extrahd_umount "${@}" || rc="${rc}" + ;; + scanhd) + exec /usr/local/bin/scanhd "${@}" + ;; + + # No command + "") + echo "${0}: No command given" >&2 + rc=2 + ;; + + # Unknown command + *) + echo "${0}: Unsupported command: ${command}" >&2 + rc=2 + ;; + esac + + return ${rc} } -############################################################################################################################ -############################################################################################################################ +# Call main() +main "${@}" || exit ${?}