Senaste hacket....

Här diskuterar vi skal, kommandon och klassiska linuxverktyg.
Användarvisningsbild
Osprey
Inlägg: 6310
Blev medlem: 06 apr 2008, 00:09
OS: Ubuntu
Ort: Göteborg/Mölndal/Falkenberg
Kontakt:

Senaste hacket....

Inlägg av Osprey »

...eller ja så då, helt nytt är det inte... men jag har inte släppt det här innan....

#1. Installera "alldisks" som ger info om alla diskar (och partitioner) som finns....

Kod: Markera allt

#! /bin/bash
#
function usage {
	cat << EOD
	alldisks [-d|-p] [-b bus] [-f filesystem] [-v]


	Get a list of all disks that meer ceratin criterias.
	
	-d|-p	
		Specifies if disks or partitions are to be shown. If none of them
		is specified, partitions is the default.

	-b bus	
		Specifies which kind of disks that are to be shown. Possible values
		are for example "ata" or "usb". If nor specified, all disks are shown.

	-f filesystem
		Specifies the filesystem a partition must have to be shown. Possible
		values are for example "ntfs", "fat", "ext3" or "ext4". It is also 
		possible to specify "ext", which means that "ext2", "ext3" and "ext4"
		is shown but! "extended" is not shown, as it is no filesystem. If not 
		specified, all partitions are shown.

	-v	
		Tells the command to be verbose and print the bus and the filesystem
		for each partition and the bus for each disk. This can for example be
		used to determine how to search for the right disks.

	Example:

		alldisks
		alldisks -p -b usb
		alldisks -p -b usb -f ext4

EOD
}
#
function filesystem {
	if [[ -z $1 ]]; then
		PART=$(mount | grep "$(dirname $(readlink -f $(pwd)) | awk -F / '{ print "/"$2 }') " | awk '{ print $1 }')
	else
		PART=$1
	fi
	FS=$(blkid -o value -s TYPE $PART)
	if [[ -z $FS ]]; then
		NUM=$(echo $PART | tr -dc '0-9')
		DEV=$(echo $PART | sed 's/[0-9]//g')
		TYP=$(parted $DEV print | sed -n '/Nummer/,$p' | tail -n +2 | head -c -1 | sed 's/^ //g' | grep "^$NUM" | awk '{ print $5 }')
		if [[ ! -z $TYP ]]; then
			echo $TYP
		else
			TYP=$(parted $DEV print | sed -n '/Number/,$p' | tail -n +2 | head -c -1 | sed 's/^ //g' | grep "^$NUM" | awk '{ print $5 }')
			if [[ ! -z $TYP ]]; then
				echo $TYP
			else
				echo "none1"
			fi
		fi
	else
		echo $FS
	fi
}
#
function alldisks {
	pFUNC=$1
	pBUS=$2
	pFS=$3
	pDEV=$4
	if [[ $pFUNC == "disk" ]]; then
		# -Show all disks
		DISKS=$(fdisk -l 2>> /dev/null | grep ^/dev | awk '{ print $1 }' | sed 's/[1-9]//g' | sort -u)
		for DISK in $DISKS; do
			if [[ ! -z $(echo $DISK | grep $pDEV) ]]; then
				BUS=$(udevadm info --query=all -n $DISK | grep ID_BUS | awk -F = '{ print $2 }')
				#FS=$(filesystem $PART)
				FS="disk"
				if [[ $pBUS == "all" ]]; then
					if [[ $pFS == "all" ]]; then
						if [[ $VERB == false ]]; then
							echo $DISK
						else
							printf "%-12s%4s    %s\n" $DISK $BUS $FS
						fi
					else
				  		if [[ ! -z $(echo $FS | grep $pFS) ]]; then
							if [[ $VERB == false ]]; then
								echo $DISK
							else
								printf "%-12s%4s    %s\n" $DISK $BUS $FS
							fi
						fi
					fi
				else
					if [[ $BUS == $pBUS ]]; then
						if [[ $pFS == "all" ]]; then
							if [[ $VERB == false ]]; then
								echo $DISK
							else
								printf "%-12s%4s    %s\n" $DISK $BUS $FS
							fi
				  		elif [[ ! -z $(echo $FS | grep $pFS) ]]; then
							if [[ $VERB == false ]]; then
								echo $DISK
							else
								printf "%-12s%4s    %s\n" $DISK $BUS $FS
							fi
						fi
					fi
				fi
			fi
		done
	elif [[ $pFUNC == "part" ]]; then
		# -Show all parts
		PARTS=$(fdisk -l 2>> /dev/null | grep ^/dev | awk '{ print $1 }')
		for PART in $PARTS; do
			if [[ ! -z $(echo $PART | grep $pDEV) ]]; then
				BUS=$(udevadm info --query=all -n $PART | grep ID_BUS | awk -F = '{ print $2 }')
				FS=$(filesystem $PART)
				if [[ $pBUS == "all" ]]; then
					if [[ $pFS == "all" ]]; then
						if [[ $VERB == false ]]; then
							echo $PART
						else
								printf "%-12s%4s    %s\n" $PART $BUS $FS
						fi
					elif [[ ! -z $(echo $FS | grep $pFS) ]]; then
						if [[ $pFS != "ext" || $FS != "extended" ]]; then
							if [[ $VERB == false ]]; then
								echo $PART
							else
								printf "%-12s%4s    %s\n" $PART $BUS $FS
							fi
						fi
					fi
				elif [[ $BUS == $pBUS ]]; then
					#echo "BUS:  $BUS"
					#echo "FS:   $FS"
					#echo "P3:   $3"
					if [[ $pFS == "all" ]]; then
						if [[ $VERB == false ]]; then
							echo $PART
						else
							printf "%-12s%4s    %s\n" $PART $BUS $FS
						fi
				  	elif [[ ! -z $(echo $FS | grep $pFS) ]]; then
						if [[ $pFS != "ext" && $FS != "extended" ]]; then
							if [[ $VERB == false ]]; then
								echo $PART
							else
								printf "%-12s%4s    %s\n" $PART $BUS $FS
							fi
						fi
					fi
				fi
			fi
		done
	fi
}
#
##########################################################################################################
TEMP=`getopt -ob:df:hpv --long bus:,disk,fs:,help,part,verb -n $(basename $0) -- "$@"`
if [[ $? -ne 0 ]]; then 
        usage
        exit
fi
eval set -- "$TEMP"
FS="all"
BUS="all"
DISK=false
HELP=false
PART=false
VERB=false
while true; do
        case $1 in
                -b|--bus)
                        shift
			BUS=$1
			shift
                        ;;
		-d|--disk)
			DISK=true
			shift
			;;
		-f|--fs)
			shift
			FS=$1
			shift
			;;
		-h|--help)
			HELP=true
			shift
			;;
		-p|--part)
			PART=true
			shift
			;;
		-v|--verb)
			VERB=true
			shift
			;;
		--)
			shift
			break
			;;
		*)
			shift
			;;
	esac
done
if [[ -z $1 ]]; then
	DEV="/dev"
else
	DEV=$1
fi
P1=$1
P2=$2
P3=$3
##########################################################################################################
#
if [[ $HELP == true ]]; then
	usage
	exit
elif [[ $DISK == false && $PART == false ]]; then
	PART=true
elif [[ $DISK == true && $PART == true ]]; then
	echo "-Both --disk and --part is specified, unable to do both"
	echo
	exit
fi
if [[ $DISK == true ]]; then
	alldisks disk $BUS $FS $DEV
else
	alldisks part $BUS $FS $DEV
fi
Lägg in "rsrvd" på samma plats, de kan ligga var som helst... enda kravet är att de ligger på samma plats....
rsrvd:

Kod: Markera allt

#! /bin/bash
#
# Needs:	alldisks
#
# -If DEBUG is set to "true", nothing is actually changed
DEBUG=true
#
SCRIPTDIR=$(dirname $(readlink -f $0))
#
function usage {
	cat << EOD

	rsrvd [-e] [partition]

	-e	Edit, possibly change the reserved space size

	partition	Name of the partition to show/modify (if -a is not specified).

EOD
	exit
}
#
function lcalc {
        awk --use-lc-numeric "BEGIN { print $* ; }"
}
#
function isnum {
	if [[ $1 =~ ^[0-9]+$ ]]; then
		return 0
	fi
	return 1
}
#
function isBlockfile {
	if [[ -b $1 ]]; then
		return 0
	fi
	return 1
}
#
function isDisk {
	if [[ -b $1 ]]; then
		partNum=$(echo $1 | grep [1-9])
		if [[ -z $partNum ]]; then
			return 0
		fi
		return 1
	fi
	return 1
}
#
function isPart {
	if [[ -b $1 ]]; then
		partNum=$(echo $1 | grep [1-9])
		if [[ ! -z $partNum ]]; then
			return 0
		fi
		return 1
	fi
	return 1
}
#
function btoh {
	echo $1 | awk '{sum=$1;
		#hum[1024**4]="Tb";hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024**1]="Kb";hum[1024**0]="B";
		hum[1024**3]="Tb";hum[1024**2]="Gb";hum[1024**1]="Mb";hum[1024**0]="Kb";
		for (x=1024**4; x>=1; x/=1024){ 
       			if (sum>=x) { 
				#printf "6.2f %s\n",sum/x,hum[x];break 		# For fixed length and positions
				printf "%.2f %s\n",sum/x,hum[x];break 
				}
			}
		}'
}
#
function checkInput {
	# input, min, max
	INP=$1
	MIN=$2
	MAX=$3
	isnum $INP; IRET=$?
	if [[ $IRET -gt 0 ]]; then
		echo "-Value is no positive integer" >> /dev/tty
		return 1
	elif [[ $INP -lt $MIN ]]; then
		echo "-Value is less than 0%" >> /dev/tty
		return 1
	elif [[ $INP -gt $MAX ]]; then
		echo "-Value is bigger than the default 5%" >> /dev/tty
		return 2
	fi
	return 0
}
#
function checkReserved {
	pPART=$1
	pEDIT=$2
	FS=$(blkid -o value -s TYPE $pPART)
	if [[ $FS == "ext3" || $FS == "ext4" ]]; then
		rsrvd=$(tune2fs -l $pPART 2> /dev/null | grep 'Reserved block count' | awk -F : '{ print $2 }' | xargs)
		blocks=$(tune2fs -l $pPART 2> /dev/null | grep 'Block count' | awk -F : '{ print $2 }' | xargs)
		blksiz=$(tune2fs -l $pPART 2> /dev/null | grep 'Block size' | awk -F : '{ print $2 }' | xargs)
		let factor=$blksiz/1024
		let rsrvd_kbytes=$rsrvd*$factor
		if [[ ! -z $rsrvd ]]; then
			# -Normally 5% of a disk is reserved for root etc. to make it possible
			# -for root to login and fix even if the disk is full.
			# -However, it is possible to set reserved count to 0 (zero).
			if [[ $rsrvd == 0 ]]; then
				RESERVED_SIZ="0"
				RESERVED_PCT="0"
			else
				RESERVED_SIZ=$(btoh $rsrvd_kbytes)
				RESERVED_PCT=$(lcalc 100*$rsrvd/$blocks)
			fi
			if [[ $pEDIT == true ]]; then
				printf "%-10s%15s%10.2f%%\n" "$PART" "$RESERVED_SIZ" "$RESERVED_PCT"
				read -p "_Trim? enter an Integer for percents or Enter to skip: " IN
				if [[ ! -z $IN ]]; then
					# input, min, max, value
					checkInput $IN 0 5; IRET=$?
					if [[ $IRET -eq 0 ]]; then
						if [[ $DEBUG == false ]]; then
							tune2fs -m $IN $pPART
						else
							echo "tune2fs -m $IN $pPART"
						fi
					elif [[ $IRET -eq 2 ]]; then
						echo "-Value converted to 5, do you want to continue? [y/N]: " IN
						if [[ $IN == "y" || $IN == "Y" ]]; then
							if [[ $DEBUG == false ]]; then
								tune2fs -m 5 $pPART
							else
								echo "tune2fs -m 5 $pPART"
							fi
						fi
					fi
				fi
				echo
			else
				printf "%-10s%15s%10.2f%%\n" "$PART" "$RESERVED_SIZ" "$RESERVED_PCT"
			fi
		fi
	else
		RESERVED_SIZ="0"
		RESERVED_PCT="0"
		if [[ -z $FS ]]; then
			DEV=$(echo $pPART | sed 's/[0-9]//g')
			NUM=$(echo $pPART | tr -dc '0-9')
			TYP=$(parted $DEV print | sed -n '/Nummer/,$p' | tail -n +2 | head -c -1 | sed 's/^ //g' | grep "^$NUM" | awk '{ print $5 }')
			if [[ ! -z $TYP ]]; then
				FS=$TYP
			else
				TYP=$(parted $DEV print | sed -n '/Number/,$p' | tail -n +2 | head -c -1 | sed 's/^ //g' | grep "^$NUM" | awk '{ print $5 }')
			if [[ ! -z $TYP ]]; then
					FS=$TYP
				else
					FS="none"
				fi
			fi
		fi
		echo "-$pPART "'does not seem to be a valid ext3/ext4 filesystem, it seems tp be "'$FS'"'
		return 1
	fi
return 0
}
#
############################################################################################################################################
...Och sedan kör..... :)

Det som scriptet gör är att visa hur stor del av diskarna som är reserverat. Varje disk/partition inom EXT3 och EXT4 reserverar 5% som extra urymme för att root ska kunna reda ut problem.... men på stora diskar är 5% vansinnigt mycket och definitivt mer än vad som behövs....

Med de här scripten kan man lätt se hur mycket som är upplåst och ändra det (rsrvd -e).... Slösa inte disk i onödan, även om funktionen i sig är bra.... :)
Agera genom att ta och behålla initiativet, ta de risker detta kräver...
http://www.enargo.com/it/
OpenVMS Shark - i Linux finns inte SYS$CMKRNL...

Återgå till "Terminalforum"