Repository 32bit  Forum
Repository 64bit  Wiki

Autorilevamento CD-DVD-NTFS-FAT

Da Slacky.eu.

Descrizione

Questo script dovrebbe essere utile a tutti coloro che hanno problemi nel convincere la propria slack che si hanno delle partizioni ntfs/fat o + lettori cd/dvd. Il suo scopo è rilevare e configurare automaticamente tutto ciò che può, dovete lanciarlo da root, se lo lanciate con l'opzione -n ce

Script

#!/bin/bash
#
# ***************************************************************************
# *   Copyright (C) 2004-2005 by SukkoPera                                  *
# *   enjoy.the.silence@iol.it                                              *
# *                                                                         *
# *   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.                                   *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU General Public License for more details.                          *
# *                                                                         *
# *   You should have received a copy of the GNU General Public License     *
# *   along with this program; if not, write to the                         *
# *   Free Software Foundation, Inc.,                                       *
# *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
# ***************************************************************************

# Changelog:
#
# 2005-06-10	SukkoPera	enjoy.the.silence@iol.it
# * Bumped version to 0.2
# * Now the program also detects and installs FAT (16 and 32) partitions.
# * Added a lot of preliminary checks (filesystems support by the kernel and
#   auxiliary programs).
# * Improved the code and the coding style a lot (even though Bash is not
#   that elegant by itself...).
# * Switched CD/DVD drivers detection from dmesg-grepping to /proc reading.
# * Added displaying of CD/DVD drive model.
# * Fixed CD_FS and CD_MODE being ignored.


####################### USER-CONFIGURABLE PARAMETERS ######################

# User group whose members will be able to access all of the files on NTFS
# partitions. Will be created if it does not exist yet.
NTFS_GROUP="windows"

# User group whose members will be able to access all of the files on FAT
# partitions. Will be created if it does not exist yet. This can be set
# the same as the above.
FAT_GROUP="windows"

# Filesystem which will be used for CD-ROM AND DVD-ROM drives.
# Probably "auto" is the best choice, but "iso9660" and "udf" might work too,
# or even "udf,iso9660".
CD_FS="auto"

# Mode to mount CD-ROM and DVD-ROM drives. Possibile choices are:
# - "owner": Only device owner will be able to mount and unmount the
#            device.
# - "user": Every user will be able to mount the device. Only he can then
#           unmount it.
# - "users": Every user will be able to mount the device and to unmount it.
CD_MODE="users"

# Path to the sfdisk program
SFDISK="/usr/sbin/sfdisk"

# Path to GNU awk
GAWK="/bin/gawk"

########################### SCRIPT STARTS HERE ############################


# Version number
VERSION="0.2 10/06/2005"

function welcome() {
	cat << EOF
##############################################################
HDConfig V $VERSION
Copyright (C) 2004-2005 SukkoPera <enjoy.the.silence@iol.it>

This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see the GNU GPL for details.
##############################################################

EOF
}


function get_mountpoint() {
	local mnt

	# $1 is device or partition
	mnt="/mnt/`basename $1`"
	if [ -d $mnt ]
	then
		echo "Mountpoint $mnt already exists." >&2
	else
		echo "Mountpoint $mnt does not exist, creating." >&2
		if [ $PRETEND -eq 1 ]
		then
			mkdir $mnt
		fi
	fi

	echo $mnt
}


function get_gid() {
	local group gid

	group=$1
	gid=`grep ^$group /etc/group | $GAWK 'BEGIN {FS=":";} {print $3}'`
	if [ -n "$gid" ]
	then
		echo "Group \"$group\" already exists with GID $gid" >&2
	else
		echo "No \"$group\" group detected, creating" >&2
		if [ $PRETEND -eq 1 ]
		then
			groupadd $group
			gid=`grep ^$group /etc/group | $GAWK 'BEGIN {FS=":";} {print $3}'`
		else
			gid="N/A"
		fi
		echo "Done. GID is $gid" >&2
	fi

	echo $gid
}


# $1 = mode: FAT16|FAT32|NTFS
function parts_detect() {
	local mode ntfs

	mode=$1
	ntfs=`$SFDISK -l 2>/dev/null | grep $mode | $GAWK '{print $1}'`

	echo $ntfs
}


function cd_detect() {
	local out i media
	
	cd /proc/ide
	out=""
	for i in hd*
	do
		if [ -f $i/media ]
		then
			media=`cat $i/media`
			if [ "$media" == "cdrom" ]
			then
				out="$out $i"
			fi
		fi
	done

	echo $out
}


# $1 = device
# $2 = partition type
# $3 = gid
function fstab_add() {
	local mnt type gid entry

	mnt=`get_mountpoint $1`
	type=$2
	case $type in
		"NTFS")
			gid=$3
			entry="$1\t$mnt\tntfs\tro,gid=$gid,umask=0007\t0 0"
			;;
		"FAT16")
			gid=$3
			entry="$1\t$mnt\tmsdos\trw,gid=$gid,umask=0007\t0 0"
			;;
		"FAT32")
			gid=$3
			entry="$1\t$mnt\tvfat\trw,gid=$gid,umask=0007\t0 0"
			;;
		"CDROM")
			entry="$1\t$mnt\t$CD_FS\tnoauto,ro,$CD_MODE\t0 0"
			;;
		*)
			echo "HUH!?" >&2
			;;
	esac
	echo "Drive/Partition $1 is now accessible from $mnt" >&2
	if [ $PRETEND -eq 1 ]
	then
		echo -e $entry >> /etc/fstab
	fi
}


# $1 = partition type (FAT16|FAT32|NTFS)
# $2 = group
# $3- = devices
function parts_go() {
	local type group gid parts e mnt

	type=$1
	echo >&2
	if [ $# -gt 2 ]
	then
		group=$2
		gid=`get_gid $group`
		shift 2
		parts=$@
		echo "Detected $type partition(s): $parts" >&2
	
		for part in $parts
		do
			echo "-------------- Partition $part ---------------" >&2
			e=`grep ^$part /etc/fstab`
			if [ -n "$e" ]
			then
				mnt=`echo $e | $GAWK '{print $2}'`
				echo "Already appears in fstab (mounted on $mnt), skipping" >&2
			else
				echo "Does not appear in fstab, installing" >&2
				fstab_add $part $type $gid
			fi
		done
	else
		echo "No $type partitions detected" >&2
	fi
}


function cd_go() {
	local drive dev e mnt

	parts=$@
	echo >&2
	echo "Detected CD-ROM/DVD-ROM drive(s): $parts" >&2

	for drive in $parts
	do
		dev="/dev/$drive"
		echo "---------- CD-ROM/DVD-ROM drive $dev ----------" >&2
		echo -n "Model: " >&2
		cat /proc/ide/$drive/model >&2
		e=`grep ^$dev /etc/fstab`
		if [ -n "$e" ]
		then
			mnt=`echo $e | $GAWK '{print $2}'`
			echo "Already appears in fstab (mounted on $mnt), skipping" >&2
		else
			echo "Does not appear in fstab, installing" >&2
			fstab_add $dev "CDROM"
		fi
	done
}


function show_help() {
	cat << EOF
This script will scan the system for FAT and NTFS partitions,
CD-ROM and DVD-ROM drives and will modify /etc/fstab so that
you can access all of them.
Permissions for FAT partitions will be set so that all and
only members of the "$FAT_GROUP" user group will be able to
access the data, so remember to add users to this group.
The same happens for NTFS partitions, with the "$NTFS_GROUP"
group.

Available options:
-t	Scan for FAT partitions
-n	Scan for NTFS partions
-c	Scan for CD-ROM/DVD-ROM drives
-a	Scan for everything supported
-f	Apply changes
EOF
}


function checkup() {
	local out

	if [ ! -x $SFDISK ]
	then
		cat << EOF
WARNING: the "$SFDISK" program was not found on the system.
         Partitions detection will not work without this program.
         You can find it in the "util-linux" package on Slackware systems.
         If it's already installed the the path might be wrong: edit this
         script and change it opportunely.

EOF
		out=1
	fi

	if [ ! -x $GAWK ]
	then
		cat << EOF
WARNING: the "$GAWK" program was not found on the system.
         This script will not work at all without it.
         You can find it in the "gawk" package on Slackware systems.
         If it's already installed the the path might be wrong: edit this
         script and change it opportunely.
         The script *might* also work with a non-GNU version of awk.

EOF
		out=2
	fi

	return $out
}


function fs_supported() {
	local fs out

	fs=$1
	if ! grep -v nodev /proc/filesystems | grep -q $fs
	then
		echo
		echo "WARNING: Filesystem type \"$fs\" is not supported by the running kernel!" >&2
		out=1
	else
		out=0
	fi

	return $out
}


function main() {
	local do_ntfs do_fat do_cd help p devs

	do_ntfs=1
	do_fat=1
	do_cd=1
	help=0

	# By default do not apply changes
	PRETEND=0

	welcome
	checkup
	chk=$?
	if [ $chk -eq 2 ]
	then
		# We don't have awk, nothing will work!
		exit 1
	fi

	for p in $@
	do
		case $p in
		"-c")
			# Detect CD-ROM/DVD-ROM drives
			do_cd=0
			help=1
			;;
		"-n")
			# Detect NTFS partitions
			do_ntfs=0
			help=1
			;;
		"-t")
			# Detect FAT(32) partitions
			do_fat=0
			help=1
			;;
		"-a")
			# Detect everything we support! ;)
			do_fat=0
			do_ntfs=0
			do_cd=0
			help=1
			;;
		"-f")
			# Apply changes
			PRETEND=1
			;;
		esac
	done

	if [ $help -eq 0 ]
	then
		show_help
	else
		if [ $PRETEND -eq 0 ]
		then
			echo ">>> WARNING: No changes to the current fstab will be made. <<<" >&2
			echo ">>>          Use the -f switch to force changes.           <<<" >&2
		fi

		# Partitions detection only works if we have sfdisk installed
		if [ $chk -eq 0 ]
		then
			if [ $do_fat -eq 0 ]
			then
				if fs_supported "msdos"
				then
					devs=`parts_detect "FAT16"`
					parts_go "FAT16" $FAT_GROUP $devs
				fi
				if fs_supported "vfat"
				then
					devs=`parts_detect "FAT32"`
					parts_go "FAT32" $FAT_GROUP $devs
				fi
			fi
	
			if [ $do_ntfs -eq 0 ]
			then
				if fs_supported "ntfs"
				then
					devs=`parts_detect "NTFS"`
					parts_go "NTFS" $NTFS_GROUP $devs
				fi
			fi
		fi

		if [ $do_cd -eq 0 ]
		then
			devs=`cd_detect`
			cd_go $devs
		fi
	fi
}

main $@

  • Data: 03 Jun 2006
  • Autore: Useless
  • Versione: 0.2
Strumenti personali
Namespace

Varianti