partinfo - displaying partition infos -

This is a full rewriting of halinfo. I started a new thread because the title “halinfo” became misleading and I renamed the script “partinfo”. It can still optionally query the haldaemon but it now defaults to udev. It shows the mount point (instead of a star) for mounted partitions and displays mounted partitions in red in the color output (when the option -c is used).

Get the script from here for color output: http://unixversal.com/linux/openSUSE/partinfo22.tgz. I had to remove the escape sequences from the code before posting (so the color won’t work in the script posted here).

Syntax       displays: 
[b]partinfo[/b]     device    mountpoint   filesystem    label   uuid
[b]partinfo -v[/b]  device    mountpoint   filesystem    label   uuid   start sector  size (in MB) 
[b]partinfo -V[/b]  device    mountpoint   filesystem    label   uuid   disk ID    start sector    size (in MB)

Use the option -c for color input. Don’t use this option while posting the output here!
Use the -h option to query hal (but hal should not be running anymore in most cases)

#! /bin/bash
#: Title       : partinfo (formerly halinfo)
#: Date Created: Wed Dec 29 22:59:33 PST 2010 
#: Last Edit   : Tue Jun 14 02:55:39 PDT 2011 
#: Author      : Agnelo de la Crotche (please_try_again) 
#: Version     : 2.2
#: Description : displays partitions properties from hal or udev daemons 
#:             : this is based of halinfo but using udev as default 
#: usage       : partinfo 
#: options:    : -u --udev    : query udev database
#:             : -h --hal     : query hal database
#:             : -v --verbose : verbose output (display offset and size of the partitions)
#:             : -c --color   : more readable colored ouput  
#
# Copy and paste this text into a text file and save it in your home area, bin folder (~/bin) as the file partinfo
# Once the file is saved, you must make the file executable using the terminal command: chmod +u ~/bin/partinfo
#

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prg=`basename $0`

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This function uses lsb_release to determine the distro
# This package is not installed by default on openSUSE.
# Btw this function is completely useless.
# Just don't use it.

function alternate_color {
	# set default alternate color
	color=33
	# set alternate color according to Linux/BSD distro.
	suse=32 ; ubuntu=33 ; fedora=34 ; gentoo=35 ; mandriva=36 ; arch=36 
	dist=$(lsb_release -si | tr "[:upper:]" "[:lower:]" | sed 's| *linux||')
	[ "$dist" == "n/a" -a -f /etc/arch-release ] && dist=arch
	ocolor=${!dist}
	color=${ocolor:-$color} 
	echo $color
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# default colors 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# primary partitions color (default is white)
pricolor=37

# alternate color (default is green)
#color=$(alternate_color)
color=32

# mounted partitions color (default is red)
mntcolor=31

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# HELP 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function syntax {
cat << EOFSYNTAX
usage:
   $prg [options]
options:
   -h --hal    : query hal database
   -u --udev   : query udev database
   -v --verbose: verbose output (display partions offset and size)
   -V --Verbose: more verbose output 
   -c --color  : colored output 	
EOFSYNTAX
exit
}


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# one way (not the best one) to guess where nfs4 exported fislesystems are mounted
nfs4=$(awk '/bind/ { sub(/\//,"", $2) ; sub( /\/.*/,"",$2) ;  print $2 }' /etc/fstab | sort -u)
[ "x$nfs4" == "x" -a -f /etc/exports ] && nfs4=$(awk '/nfs4/ { sub(/\//,"", $1) ; sub( /\/.*/,"",$1) ;  print $1 }' /etc/exports | sort -u) 

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# query HAL databases 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function queryHal {
# get hal volumes

vol=(`eval $(lshal -s | awk '/volume_part|volume_uuid/ { printf "echo -n %s: ; hal-get-property --udi /org/freedesktop/Hal/devices/%s --key block.device; 
", $1, $1}') | sed 's|:/dev/| |;s|sd\([a-x]\)\([1-9]\)$|sd\10\2|' | sort -t " " -k 2 | awk '{ print "/org/freedesktop/Hal/devices/"$1 }'`)
i=0

while [ $i -lt ${#vol[li]} ] ; do
[/li]	par[$i]=$(hal-get-property --udi ${vol[$i]} --key block.device | sed 's|/dev/||')
	if [ "$(hal-get-property --udi ${vol[$i]} --key volume.is_partition)" == "true" ] ; then
		mnt[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.mount_point)
		lbl[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.label)
		uuid[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.uuid)
		num[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.partition.number)
		fs[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.fstype)
	
		fsver[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.fsversion)
		fsusage[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.fsusage)
	
		# set extended partition fs to "DOS Ext"
		[ "${fsusage[$i]}" == "partitiontable" ] && fs[$i]="DOS Ext"
	
		offset[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.partition.start)
		size[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.size)
		blocksize[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.block_size)
	
		disk=$(hal-get-property --udi ${vol[$i]} --key info.parent)
		bus=$(hal-get-property --udi $disk --key storage.bus )
		vendor=$(hal-get-property --udi $disk --key storage.vendor | tr "[:upper:]" "[:lower:]")
		serial=$(hal-get-property --udi $disk --key storage.serial)
		case $bus in
			usb*) diskByID[$i]=$bus-$serial-part${num[$i]} ;;
			*) diskByID[$i]=$vendor-$serial-part${num[$i]} ;;
		esac
	else	
		num[$i]=0
	fi		
	let i++
done
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# query udev databases 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function queryUdev {
# get udev volumes
vol=(`udisks --enumerate | sed 's|.*/|/dev/|;s|sd\([a-z]\)\([1-9]\)$|sd\10\2|;/sd[a-x]$/d;/sr[0-9]/d;/fd[0-9]/d;/dm_/d' | sort -n | sed 's|sd\([a-z]\)0|sd\1|'`)

i=0
while [ $i -lt ${#vol[li]} ] ; do
[/li]	eval $(udisks --show-info ${vol[$i]} | awk -F ":" '/device-file:|by-id:|size:|usage:|type:|version:|uuid:|number:|offset:|label:|mount paths:/ { print }' | sed 's| ||g;s|:| |;/alignmentoffset/d;s/EFIsystempartition/EFI/' | awk '{ if ( NF==2 ) print }' | sort -u | sed 's|^by-id|#&|' | tr "
" "|" | sed 's|#||' | tr "|" "
" | sed '/^#/d;s/by-id/diskByID/;s/version/fsver/;s/usage/fsusage/;s|device-file /dev/|par |;s/mountpaths/mnt/;s/number/num/;s/type/fs/;s/label/lbl/;s|/dev/disk/by-id/||;s/ /[#]="/;s/$/" ;/' | sed "s|#|$i|")

	[ "$nfs4" ] && mnt[$i]=$(echo ${mnt[$i]} | tr "," "
" | sed "/$nfs4/d")

	# set extended partition fs to "DOS Ext"
	if [ "x${fsusage[$i]}"=="x" -a ${num[$i]} -le 4 ] ; then
		/sbin/udevadm info -q property -n ${par[$i]} | grep -q ID_FS_TYPE || fs[$i]="DOS Ext"
	fi

	let i++
done
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# parsing options 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

flag=0
uflag=0

ARGS=`getopt -q -o cvVhu --long verbose,Verbose,color,hal,udev -- "$@"`

[ "$?" == "0" ] || syntax

eval set -- "$ARGS"

while true ; do
	case "$1" in
		-u|--udev) uflag=$(($uflag | 1)) ; shift ;;
		-h|--hal) uflag=$(($uflag | 2)) ; shift ;;
		-c|--color) flag=$(($flag | 1)) ; shift ;;
		-v|--verbose) flag=$(($flag | 2)) ; shift ;;
		-V|--Verbose) flag=$(($flag | 4)) ; shift ;;
		--) shift ; break ;;
    esac
done


# using hal or udev
if [ $uflag -gt 2 ] ; then
	exec echo "you cannot use the options --hal and --udev together" 
elif [ $uflag -gt 1 ] ; then
	lshal -s >/dev/null || exit
	queryHal 
else
	queryUdev
fi

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# print to standard output
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

w=$(( $(echo ${mnt[li]} | tr " " "[/li]" | wc -L) + 3 ))

if [ $flag -lt 2 ] ; then
	W=72
elif [ $flag -lt 4 ] ; then
	W=101
else
	W=153
fi

W=$(($W+$w))
printf -v f "%${W}s" 
printf -v H1 "%s
" "${f// /-}"
printf -v H2 "%s
" "${f// /~}"

printf "|%s|
" $H1
if [ $flag -lt 2 ] ; then
	printf "| %-8s%-${w}s%-10s%-15s%-38s|
" dev mount fs label uuid
elif [ $flag -lt 4 ] ; then
	printf "| %-8s%-${w}s%-10s%-15s%-47s%-13s%-7s|
" dev mount fs label uuid "start" size
else
	printf "| %-8s%-${w}s%-10s%-15s%-39s%-60s%-13s%-7s|
" dev mount fs label uuid diskID "start" size
fi
printf "|%s|
" $H2

i=0

while [ $i -lt ${#vol[li]} ] ; do
[/li]
if [  ${num[$i]} -gt 0 ] ; then

	# unsupported partitions (like BSD swap slices)
	 [ "x${fs[$i]}" == "x" -a "x${fsusage[$i]}" == "x" ] && fs[$i]="unknown"

	# Add ufs version for BSD slices ; set   
	if [ "${fs[$i]}" == "ufs" ] ; then
		if [ ${num[$i]} -le 4 ] ; then
			fs[$i]="BSD pri"
 		else
			fs[$i]=${fs[$i]}${fsver[$i]}
		fi
	fi

	# convert offset in sectors (could be 512 or 4096 sectors, according to blocksize)
	offset[$i]=$((${offset[$i]}/${blocksize[$i]}))

	# convert size in MB
	size[$i]=$((${size[$i]}/1048576))

	if [ ${num[$i]} -le 4 ] ; then
		c="$pricolor;1m"
	else
		if [ $(($i % 2)) -eq 0 ] ; then
			c="$color;1m"
		else
			c="37;0m"	
		fi
	fi
	
	[ "${mnt[$i]}" ] && c="$mntcolor;1m"	

	case $flag in 
		0) # normal output
			printf "| %-8s%-${w}s%-10s%-15s%-38s|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" ;;
		1) # colored output
			printf "| %-8s%-${w}s%-10s%-15s%-38s|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" ;;
		2) # verbose output
			printf "| %-8s%-${w}s%-10s%-15s%-38s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" ;;
		3) # verbose coloured output
			printf "| %-8s%-${w}s%-10s%-15s%-38s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" ;;		
		4) # extra verbose output
			printf "| %-8s%-${w}s%-10s%-15s%-38s %-50s %14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" ;;
		5) # extra verbose coloured output
			printf "| %-8s%-${w}s%-10s%-15s%-38s %-50s %14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" ;;		
	esac
fi
let i++
done

printf "|%s|
" $H1

partinfo -cpartinfo -vcpartinfo -Vc

The original halinfo thread is here: Displaying partitions infos from hal daemon

You could use ‘+’ instead of ‘|’ for the corners of the box. Looks a (tiny) bit better.

+------------------------------------+
|                                    |
|                                    |
|                                    |
+------------------------------------+

Yes, I agree. It looks a (tiny) bit better. :wink:
Here’s a patch for now:

--- ./partinfo.orig       2011-06-14 06:31:00.707238166 -0700
+++ ./partinfo    2011-06-14 06:30:08.978189998 -0700
@@ -197,7 +197,7 @@
 printf -v H1 "%s
" "${f// /-}"
 printf -v H2 "%s
" "${f// /~}"
 
-printf "|%s|
" $H1
+printf "+%s+
" $H1
 if  $flag -lt 2 ] ; then
        printf "| %-8s%-${w}s%-10s%-15s%-38s|
" dev mount fs label uuid
 elif  $flag -lt 4 ] ; then
@@ -261,4 +261,4 @@
 let i++
 done
 
-printf "|%s|
" $H1
+printf "+%s+
" $H1

Yes, that patch works. Thank you.