From b999254e24b001780f8ef882001886310d0c9898 Mon Sep 17 00:00:00 2001 From: Ulrich Dangel Date: Sat, 11 Dec 2010 13:37:28 +0100 Subject: [PATCH] Introduce grml-udev-rebuildfstab --- debian/grml-udev-config.grml-blockdevices.udev | 2 +- debian/rules | 1 + scripts/grml-udev-rebuildfstab | 281 +++++++++++++++++++++++++ 3 files changed, 283 insertions(+), 1 deletion(-) create mode 100755 scripts/grml-udev-rebuildfstab diff --git a/debian/grml-udev-config.grml-blockdevices.udev b/debian/grml-udev-config.grml-blockdevices.udev index 27e23f7..1fc7f96 100644 --- a/debian/grml-udev-config.grml-blockdevices.udev +++ b/debian/grml-udev-config.grml-blockdevices.udev @@ -1,2 +1,2 @@ # regenerate /etc/fstab for new/removed devices: -ENV{ID_FS_USAGE}=="filesystem", ACTION=="add|change|remove", SUBSYSTEM=="block", RUN+="/usr/sbin/rebuildfstab -r -config" +ENV{ID_FS_USAGE}=="filesystem", ACTION=="add|change|remove", SUBSYSTEM=="block", RUN+="/etc/udev/scripts/grml-udev-rebuildfstab" diff --git a/debian/rules b/debian/rules index 2cae256..32bbd33 100755 --- a/debian/rules +++ b/debian/rules @@ -22,6 +22,7 @@ install: build dh_installdirs /etc/udev/scripts install -m 755 scripts/forensic-mark-readonly debian/grml-udev-config/etc/udev/scripts/forensic-mark-readonly + install -m 755 scripts/grml-udev-rebuildfstab debian/grml-udev-config/etc/udev/scripts/ # Build architecture-dependent files here. binary-arch: install diff --git a/scripts/grml-udev-rebuildfstab b/scripts/grml-udev-rebuildfstab new file mode 100755 index 0000000..7831e71 --- /dev/null +++ b/scripts/grml-udev-rebuildfstab @@ -0,0 +1,281 @@ +#!/bin/bash +# Filename: rebuildfstab +# Purpose: calls scanpartitions as root and adds entries to /etc/fstab +# Authors: grml-team (grml.org), (c) Klaus Knopper Nov 2002, (c) Michael Prokop +# Bug-Reports: see http://grml.org/bugs/ +# License: This file is licensed under the GPL v2. +################################################################################ + +PATH="/bin:/sbin:/usr/bin:/usr/sbin" +TMP=$(mktemp 2>/dev/null) +TMPFILE=$(mktemp 2>/dev/null) +ADDEDBYGRML="# Added by GRML" +MNTFILE="/mnt/.grml-auto-created_do-not-delete-this-file" +MOUNTPOINT_PREFIX=/mnt/ + + +logit() { + if [ -x /usr/bin/logger ] ; then + /usr/bin/logger -i -t rebuildfstab "$*" + fi +} + +bailout() { + [ -n "$1" ] && EXITCODE="$1" || EXITCODE=1 + rm -f $TMP $TMPFILE /var/run/rebuildfstab.pid + exit $EXITCODE +} + +echo "$$" > /var/run/rebuildfstab.pid + + +umask 022 + + +[ -e /etc/grml/autconfig ] && . /etc/grml/autoconfig + +if ! [ -w "$(dirname $MNTFILE)" ] ; then + echo "$0: can not write to $MNTFILE">&2 + logit "can not write to $MNTFILE" + bailout 10 +fi + +if [ -z "$TMP" -o -z "$TMPFILE" ] ; then + echo "$0: could not create tmpfile (is / mounted read-only?), exiting" >&2 + logit 'fatal - could not create tmpfile (is / mounted read-only?), exiting' + bailout 3 +fi + +# trap "rm -f $TMP $TMPFILE" 2 3 11 + + +if grep -q " nofstab" /proc/cmdline ; then + exit +fi + +grep -q 'nolabel' /proc/cmdline && NOLABEL='TRUE' || LABEL='TRUE' + + + +if [ "$CONFIG_FSTAB" = "no" ] ; then + exit +fi + + +if grep -q ' /mnt ' /proc/mounts ; then + logit '/mnt is a mounted directory, exiting.' + exit 1 +fi + +# make sure we have the $MNTFILE before reading/writing into it +if [ -z "$NO_AUTO_FILE" ] ; then + [ -r $MNTFILE ] || touch $MNTFILE +fi + + +# make sure we have a /etc/fstab at all +( + flock -x 200 +if ! [ -f /etc/fstab ] ; then + cat > /etc/fstab << EOF +# /etc/fstab - static file system information +# +proc /proc proc rw,nosuid,nodev,noexec 0 0 +none /proc/bus/usb usbfs defaults,noauto 0 0 +sysfs /sys sysfs rw,nosuid,nodev,noexec 0 0 +devpts /dev/pts devpts noauto,mode=0622 0 0 +/dev/fd0 /mnt/floppy auto user,noauto,exec 0 0 +/dev/external /mnt/external auto user,noauto,exec,rw,uid=grml,gid=grml 0 0 +/dev/external1 /mnt/external1 auto user,noauto,exec,rw,uid=grml,gid=grml 0 0 +/dev/cdrom /mnt/cdrom auto user,noauto,exec,ro 0 0 +/dev/dvd /mnt/dvd auto user,noauto,exec,ro 0 0 +# some other examples: +# /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2 +# //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0 +# linux:/pub /beer nfs defaults 0 0 +# tmpfs /tmp tmpfs size=300M 0 0 +# none /proc/bus/usb usbfs defaults,nodev,noexec,nosuid,noauto,devgid=1001,devmode=664 0 0 +# 192.168.1.101:/backups /mnt/nfs nfs defaults,user,wsize=8192,rsize=8192 0 0 +# +# Warning! Please do *not* change any lines below because they are auto-generated by rebuildfstab! +# If you want to disable rebuildfstab set CONFIG_FSTAB='no' in /etc/grml/autoconfig! +# See 'man grml-rebuildfstab' for more details about the following entries. +EOF +fi +) 200>/var/run/rebuildfstab.lock + +[ ! -w /etc/fstab ] && { + logit "fatal - /etc/fstab not writeable, exiting" + exit +} + +# Simple shell grep, searches for lines STARTING with string +stringinfile () { + while read line ;do + case "$line" in + ($1*) return 0 ;; + esac + done < "$2" + return 1 +} + +# Remove comment line $1 and the following line from file $2 +# sed '/^# Added by GRML/{N;d;}' +removeentries () { + entry="$1" + entry=$(echo $1 | sed "s#/#\\\/#g") + ( + flock -x 200 + sed -i -e "/$entry/{N;d}" /etc/fstab + ) 200>/var/run/rebuildfstab.lock +} + + +get_device_name() { +case $ID_FS_TYPE in +# dont use labels for some filesystem +iso9660) +break; +;; +*) +[ -n "$LABEL" ] && [ -n "$ID_FS_LABEL" ] && echo "LABEL=$ID_FS_LABEL_ENC" && return +;; +esac +[ -n "$DM_NAME" ] && echo /dev/mapper/$DM_NAME && return + +NAME="$DEVNAME" +for name in $DEVLINKS ; do + case $name in + *usb*|*cdrom*|*dvd*) + NAME="$name" + break; + ;; +esac +done +echo "$NAME" +} + +get_mount_point() { + device="$1" + mountpoint="${device##*/}" + mountpoint="${mountpoint#*=}" + echo /mnt/$mountpoint +} + +append_fstab() { + ( + flock -x 200 + echo $ADDEDBYGRML $1 >> /etc/fstab + echo "$2" >> /etc/fstab + ) 200>/var/run/rebuildfstab.lock + +} + + +if [ -n "$CONFIG_FSTAB_USER" ] ; then + user="$CONFIG_FSTAB_USER" +else + user='1000' # take default +fi +if [ -n "$CONFIG_FSTAB_GROUP" ] ; then + group="$CONFIG_FSTAB_GROUP" +else + group='users' # take default +fi + +if [ -z "$ACTION" ] ; then + logit "Seems that $0 is not run in a udev environment, exiting." >&2 + exit 1 +fi + +# ignore loop devices for now. +case $DEVNAME in +/dev/loop*) + exit 0 + ;; +esac + +removeentries "$ADDEDBYGRML $DEVNAME" /etc/fstab + +device=$(get_device_name) +mountpoint=$(get_mount_point $device) + +# if entry is already present in /etc/fstab ignore the current event +( + flock -x 200 + grep -v '^#' /etc/fstab > $TMPFILE +) 200>/var/run/rebuildfstab.lock + +for devicelink in $DEVLINKS ; do + # ignore external + case "$devicelink" in "*external*") continue ;; esac + stringinfile $devicelink $TMPFILE && exit +done + +# if entry is already present ignore it +stringinfile $device $TMPFILE && exit + +options=noauto,user,dev,suid,exec +case $ID_FS_TYPE in +ntfs) + options=$options,ro,umask=000 + ;; +vfat|msdos) + options=$options,umask=000 + ;; +ufs) + options=$options,ro + ;; +swap) + options=defaults + ;; +esac +case $ID_FS_TYPE in +ntfs|vfat|msdos) + test -n "$user" && options=$options,uid=$user + test -n "$group" && options=$options,gid=$group + ;; +esac + +case $ACTION in +add|change) +( + flock -x 200 + echo $ADDEDBYGRML $DEVNAME >> /etc/fstab + echo $device $mountpoint $ID_FS_TYPE $options 0 0 $addinfo >> /etc/fstab +) 200>/var/run/rebuildfstab.lock + + +;; +esac + +# Clean /mnt/... according to info $MNTFILE +if [ -r "$MNTFILE" ] ; then + for directory in $(cat $MNTFILE) ; do + rmdir $directory 2>/dev/null && sed -i "s#$directory##" $MNTFILE + done + grep '^/.*$' $MNTFILE | sort -u > $TMPFILE + cat $TMPFILE > $MNTFILE +fi + +# Make sure we have all /mnt/* directories +while read p m f relax; do + case "$m" in *none*|*proc*|*sys*|'') continue ;; esac + if ! grep -q "$m" /proc/mounts ; then + if ! [ -d "$m" ] ; then + if mkdir -m 755 -p "$m" ; then + [ -r "$MNTFILE" ] && grep -q "$m" $MNTFILE || echo "$m" >> $MNTFILE + fi + fi + fi +done < $TMPFILE + cat $TMPFILE > $MNTFILE +fi + +## END OF FILE ################################################################# -- 2.1.4