#!/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/quota-modify-fstab - /etc/fstab manipulation script # # Creation: 2004-01-01 jb # Last Update: $Id$ # # Copyright (c) 2004-2008 Jens Berger, jberger(at)gmx(dot)net # Copyright (c) 2012-@@YEAR@@ Holger Bruenjes, holgerbruenjes(at)gmx(dot)net # # 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 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # 1=action (add/remove/clear) # 2=target filesystem targetfile=/etc/fstab ext_quota=usrquota,grpquota xfs_quota=usrquota,grpquota configfile=/etc/config.d/quota quota_needs_reboot=/var/quota/quota_needs_reboot remount_root=/var/quota/quota_remount_root action=$1 targetfs=$2 # only for testing, ie. /test_etc/fstab if [ -n "$3" ] then targetfile=$3 quota_needs_reboot=/tmp/quota_needs_reboot remount_root=/tmp/quota_remount_root fi backupsuffix=`date +%Y-%m-%d-%H-%M-%S` backup_before_add=before_quota_added.$backupsuffix backup_before_clr=before_quota_cleared.$backupsuffix new=$targetfile.new # reading eislib . /var/install/include/eislib clear_fstab () { if [ "`grep -e 'quota' $targetfile`" != "" ] then mecho --info "Removing all quota mount options from $targetfile ..." rm -f /etc/fstab.*_quota_* # backing up /var/install/bin/backup-file $targetfile $backup_before_clr # set error counter if there were problems if [ "$?" -ne "0" ] then errors=1 fi # delete all quota mount options cat $targetfile | sed -e "s/usrquota,//g" -e "s/grpquota,//g" -e "s/quota,//g" >$new # set error counter if there were problems if [ "$?" -ne "0" ] then errors=1 fi cp $new $targetfile # set error counter if there were problems if [ "$?" -ne "0" ] then errors=1 fi mecho --info "Done. $targetfile should be clean now." if [ "$errors" -eq "0" ] then anything_changed=1 fi else mecho --info "$targetfile contains no quota mount options ..." fi } set_quota_type () { # extract mount options fsopts=$(grep -E "[[:space:]]+$mountpoint[[:space:]]+" $targetfile | awk '{print $4}') # extract filesystem type fstype=$(grep -E "[[:space:]]+$mountpoint[[:space:]]+" $targetfile | awk '{print $3}') if [ "$fstype" = "ext2" -o "$fstype" = "ext3" -o "$fstype" = "ext4" ] then quota_type=$ext_quota elif [ "$fstype" = "xfs" ] then quota_type=$xfs_quota else mecho --error "No quota support for the filesystem on mountpoint '$mountpoint'." fi } . $configfile anything_changed=0 errors=0 if [ -f $targetfile ] then if [ "$action" = "clear" -a "$targetfs" = "" ] then clear_fstab else FS=$(/bin/mount | grep -E "[[:space:]]+$targetfs[[:space:]]+" | grep -E "ext[2,3,4]|xfs" | sed -e 's/.* on //' -e 's/ .*//') if [ -z "$FS" ] then mecho --error "Filesystem '$targetfs' is not mounted, does not exist" mecho --error "or is no ext2/ext3/ext4/xfs filesystem! Get help. Exit." exit 1 fi # determining mountpoint mountpoint=$(echo $FS | sed 's/\//\\\//g' | sed 's/[ ].*//g') # | ^^^^^^^^^^^^^^^ the list contains more # | than one item if '/' is used, it is # | separated by space, so cut off all after. # | # `--> global: important for mountpoints with depth >1 i.e. '/data/public' if [ "$action" = "add" -a -n "$mountpoint" ] then # check if target fs is listed with any quota option if [ "`cat $targetfile | grep -E "^\/dev\/.*[[:space:]]+$targetfs[[:space:]]+" | grep "quota,"`" = "" ] then # doing backup /var/install/bin/backup-file $targetfile $backup_before_add if [ "$?" -ne "0" ] then errors=1 fi mecho --info "Adding quota mountoptions for '$targetfs' in $targetfile ..." set_quota_type # this was somewhat difficult, because we need to match spaces and tabs in /etc/fstab # this should be done by the character class '[[:space:]]' and a trailing '+' for multiple # occurencies, but sed seems to need an escaped '+' sign to match multiple tabs/spaces! cat $targetfile | sed -e "s/$mountpoint\([[:space:]]\+\)\($fstype\)\([[:space:]]\+\)/$mountpoint\1\2\3$quota_type,/" > $new # set error counter if there were problems if [ "$?" -ne "0" ] then errors=1 fi cp $new $targetfile # set error counter if there were problems if [ "$?" -ne "0" ] then errors=1 fi if [ "$errors" -eq "0" ] then anything_changed=1 fi fi elif [ "$action" = "remove" ] then if [ "`cat $targetfile | grep -E "[[:space:]]+$targetfs[[:space:]]+" | grep "quota,"`" != "" ] then # backing up /var/install/bin/backup-file $targetfile $backup_before_clr if [ "$?" -ne "0" ] then errors=1 fi mecho --info "Removing quota mount options for '$targetfs' from $targetfile ..." set_quota_type # this was somewhat difficult, because we need to match spaces and tabs in /etc/fstab # this should be done by the character class '[[:space:]]' and a trailing '+' for multiple # occurencies, but sed seems to need an escaped '+' sign to match multiple tabs/spaces! cat $targetfile | sed -e "s/$mountpoint\([[:space:]]\+\)\($fstype\)\([[:space:]]\+\)$quota_type,/$mountpoint\1\2\3/" > $new # set error counter if there were problems if [ "$?" -ne "0" ] then errors=1 fi cp $new $targetfile # set error counter if there were problems if [ "$?" -ne "0" ] then errors=1 fi if [ "$errors" -eq "0" ] then anything_changed=1 fi else mecho --warn "'$targetfs' does not have any quota mount option in $targetfile." fi fi fi else mecho --error "Targetfile $targetfile not found!" exit 1 fi if [ "$anything_changed" -eq "1" ] then if [ "$action" != "clear" ] then touch $quota_needs_reboot fi mecho --warn "************************************************************************" mecho --warn "Please check $targetfile manually before rebooting!" mecho --warn "Take a serious look at your modified $targetfile NOW!" if [ "$action" = "clear" ] then mecho --warn "If there is something wrong - restore $targetfile from" mecho --warn "$targetfile.$backup_before_clr" mecho --warn "before rebooting!" fi mecho --warn "************************************************************************" cat $targetfile mecho --warn "************************************************************************" fi if [ "$errors" -ne "0" ] then mecho --error "Errors occured. Restoring $targetfile. Get Help." if [ "$action" = "add" ] then cp $targetfile.$backup_before_add $targetfile elif [ "$action" = "remove" -o "$action" = "clear" ] then cp $targetfile.$backup_before_clr $targetfile fi exit 1 fi # deleting temporary fstab file rm -f $new exit 0