From mboxrd@z Thu Jan 1 00:00:00 1970 From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH 1/3] extrahd: Rewrite the mount script in shell Date: Wed, 02 Aug 2023 09:14:13 +0000 Message-ID: <20230802091415.3168597-1-michael.tremer@ipfire.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============2607350550589750708==" List-Id: --===============2607350550589750708== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 @@ # = # ############################################################################= ### =20 -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 =3D (); -my $ok =3D "true"; -my @devices =3D (); -my @deviceline =3D (); -my $deviceentry =3D ""; -my $devicefile =3D "/var/ipfire/extrahd/devices"; -my $fstab =3D "/var/ipfire/extrahd/fstab"; - -### Values that have to be initialized -$extrahdsettings{'PATH'} =3D ''; -$extrahdsettings{'FS'} =3D ''; -$extrahdsettings{'DEVICE'} =3D ''; -$extrahdsettings{'ACTION'} =3D ''; - -open( FILE, "< $devicefile" ) or die "Unable to read $devicefile"; -(a)devices =3D ; -close FILE; - -############################################################################= ################################################ -############################################################################= ################################################ - -if ( "$ARGV[0]" eq "mount" ) { - system("/bin/cp -f /etc/fstab $fstab"); - - foreach $deviceentry (sort @devices) - { - @deviceline =3D split( /\;/, $deviceentry ); - if ( "$ARGV[1]" eq "$deviceline[2]" ) { - print "Insert $deviceline[0] ($deviceline[1]) --> $deviceline[2] into /et= c/fstab!\n"; - unless ( -d $deviceline[2] ) { system("/bin/mkdir -p $deviceline[2] && ch= mod 0777 $deviceline[2]"); } - open(FILE, ">>$fstab"); - print FILE "$deviceline[0]\t$deviceline[2]\t$deviceline[1]\tdefaults\t0\t= 0\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=3D"${@}" + + logger -t "extrahd" "${message}" +} + +extrahd_mount() { + local _mountpoint=3D"${1}" + + local device + local filesystem + local mountpoint + local rest + local failed=3D0 + + while IFS=3D';' read -r device filesystem mountpoint rest; do + # Filter by mountpoint if set + if [ -n "${_mountpoint}" ] && [ "${mountpoint}" !=3D "${_mountpoint}" ]; t= hen + 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=3D777 "${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=3D1 + fi + done < /var/ipfire/extrahd/devices + + return ${failed} +} + +extrahd_umount() { + local _mountpoint=3D"${1}" + + local device + local filesystem + local mountpoint + local rest + local failed=3D0 + + while IFS=3D';' read -r device filesystem mountpoint rest; do + # Filter by mountpoint if set + if [ -n "${_mountpoint}" ] && [ "${mountpoint}" !=3D "${_mountpoint}" ]; t= hen + 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=3D1 + fi + done < /var/ipfire/extrahd/devices +} + +main() { + local command=3D"${1}" + shift + + local rc=3D0 + + case "${command}" in + mount) + extrahd_mount "${@}" || rc=3D"${?}" + ;; + umount) + extrahd_umount "${@}" || rc=3D"${rc}" + ;; + scanhd) + exec /usr/local/bin/scanhd "${@}" + ;; + + # No command + "") + echo "${0}: No command given" >&2 + rc=3D2 + ;; + + # Unknown command + *) + echo "${0}: Unsupported command: ${command}" >&2 + rc=3D2 + ;; + esac + + return ${rc} } =20 -############################################################################= ################################################ -############################################################################= ################################################ +# Call main() +main "${@}" || exit ${?} --=20 2.39.2 --===============2607350550589750708==--