Displaying partitions infos from hal daemon

This script displays partitions (volumes) properties, including device names, mount status, filesystem, label, uuid and - when used with the --verbose option - partitions start sector and size. The point is not to gather informations about partitions - as fdisk and blkid would be faster and more appropriate for that purpose - but to get these informations from Hardware Abstraction Layer as they are passed to the applications (like the dolphin filemanager among others) through the message bus.

Usage: halinfo [ -c | --color ] [ -v | --verbose ]

The --color option produced a two colors output (might be easier to read if you have a lot of partitions). Since escape sequences get lost by copying/pasting, I removed them from the code before posting. A compressed version of the script with preserved escape sequences is available here: http://www.unixversal.com/linux/openSUSE/halinfo.gz.

The --verbose options displays two more columns: the partitions start sector (in sector - 512 or 4096 byte according to the value of volume.block_size property) and the size in MB, rather than sectors (as it would get too confusing on system with hard disks of different sector size). Both values are originally written in byte in the hal properties.


#! /bin/bash
#: Title       : halinfo
#: Date Created: Mon Dec 27 22:48:05 PST 2010 
#: Last Edit   : Mon Dec 27 22:48:05 PST 2010 
#: Author      : please_try_again 
#: Version     : 1.0
#: Description : displays hal volume (partitions) properties  
#                using volume_uuid for Linux and DOS partitions and volume_part for others (like ufs)
#: usage       : halinfo
#: options:    -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 halinfo
# Once the file is saved, you must make the file executable using the terminal command: chmod +u ~/bin/halinfo
#

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 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
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function syntax {
cat << EOFSYNTAX
usage:
   $prg [options]
options:
   -v --verbose: verbose output (display partions offset and size)
   -c --color  :  colored output 	
EOFSYNTAX
exit
}


# primary partitions color
pricolor=37

# alternate color
# color=$(alternate_color)
color=32


# parsing options 

flag=0

args=$(echo "$@ --" | sed s'|-\([cv]\)\([cv]\)|-\1 -\2|;s|^-| -|')
ARGS=`getopt -q -o cv --long verbose,color -- "$@"`
eval set -- "$ARGS"


[ "$ARGS" == "$args" ] || syntax

 
while true ; do
	case "$1" in
		-c|--color) flag=$(($flag | 1)) ; shift ;;
		-v|--verbose) flag=$(($flag | 2)) ; shift ;;
		--) shift ; break ;;
    esac
done

# get hal volumes
for vol in `lshal -s | grep -e volume_part -e volume_uuid` ; do
	dev=/org/freedesktop/Hal/devices/$vol
	par=$(hal-get-property --udi $dev --key block.device)
	devs=(${devs[li]} ${par}~${dev})
[/li]done

# get device properties of all partitions sorted numerically by device name (sda1, sda2, ... sdb1, sdb2, etc) 

if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                    |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
else
	printf "|-----------------------------------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                             start          size |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
fi

i=0

for vol in $(echo ${devs[li]} | tr " " "[/li]" | sed 's|\([0-9]\)| \1|;s|~| |;s| \([0-9]\) | 0\1 |' | sort -n | awk '{ print $3 }') ; do
	# get volume properties
	par=$(hal-get-property --udi $vol --key block.device)
	mnt=$(hal-get-property --udi $vol --key volume.is_mounted)
	mp=$(hal-get-property --udi $vol --key volume.mount_point)
	lbl=$(hal-get-property --udi $vol --key volume.label)
	fs=$(hal-get-property --udi $vol --key volume.fstype)
	fsver=$(hal-get-property --udi $vol --key volume.fsversion)
	offset=$(hal-get-property --udi $vol --key volume.partition.start)
	size=$(hal-get-property --udi $vol --key volume.size)
	uuid=$(hal-get-property --udi $vol --key volume.uuid)
	fsusage=$(hal-get-property --udi $vol --key volume.fsusage)
	blocksize=$(hal-get-property --udi $vol --key volume.block_size)
	num=$(hal-get-property --udi $vol --key volume.partition.number)
	mnt=${mnt/true/*}

	# skip unsupported partitions (like BSD swap slices)
	[ "x$fs" == "x" -a "x$fsusage" == "x" ] && continue 	

	# Add ufs version for BSD slices ; set   
 	if [ "$fs" == "ufs" ] ; then
		if [ $num -le 4 ] ; then
			fs="BSD pri"
 		else
			fs=${fs}${fsver}
		fi
	fi
	 
	# set extended partition fs to "DOS Ext"
	[ "$fsusage" == "partitiontable" ] && fs="DOS Ext"
	
	# convert offset in sectors (could be 512 or 4096 sectors, according to blocksize)
	offset=$(($offset/$blocksize))

	# convert size in MB
	size=$(($size/1048576))

	case $flag in 
		0) # normal output
			printf "| %-8s%-5s%-8s%-15s%-40s |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" ;;
		1) # colored output
			let i++
	        if [ $num -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid"
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid"
				else
					printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid"
				fi
			fi
			;;
		2) # verbose output
			printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size;;
		3) # verbose coloured output
			let i++
	        if [ $num -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size
				else
					printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size
				fi
			fi
	esac
done


if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
"
else
	printf "|-----------------------------------------------------------------------------------------------------------|
"
fi

Screenshot of a system with many partitions:

You can ignore or remove the text in gray. I didn’t want to write/maintain several scripts for my different linux distros.

This is very good please_try_again. Another fine program for my collection. Thank you for taking the time to post your script. Here is an example of the output on my main PC.

james@linux-ew60:~> halinfo -vc
|-----------------------------------------------------------------------------------------------------------|
| dev  mount   fs       label          uuid                                             start          size |
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
| sda1    *    ntfs-3g  LM_HDC         E65EF8E25EF8AC85                                  2048    1907727 MB |
| sdb1         swap                    de8587f5-93b9-4ca0-80c7-5b567e117104                63       2055 MB |
| sdb2    *    ext4                    12399547-0a3c-43fb-94a5-4079fb489fe7           4209030     147707 MB |
| sdb3    *    ext4                    5598c10e-56dc-402a-be86-6f488e7574f9         306712980     422563 MB |
| sdc1    *    ntfs-3g  LM_HDD         2446B85C46B83082                                  2048    1430797 MB |
|-----------------------------------------------------------------------------------------------------------|

I just have two questions for you. First, what would it take to get this type of name per disk partition:

/dev/disk/by-id/ata-WDC_WD6000HLHX-01JJPV0_WD-WXA1C20K0415-part2

And, can you use the uuid instead of the name above?

Also, I get no color. If you remember, escape codes don’t make the copy and past transition from a code block. When you posted your file findgrub to be downloaded directly, the color stuff worked. You could always consider using the tput color command.

Again, this is very good please_try_again, thanks for posting it!

Hi James!
I’m glad you like it.

Some work, an extra option an a definitely wider terminal window. But there it is:


#! /bin/bash
#: Title       : halinfo
#: Date Created: Mon Dec 27 22:48:05 PST 2010 
#: Last Edit   : Tue Dec 28 18:53:17 PST 2010
#: Author      : please_try_again 
#: Version     : 1.1
#: Description : displays hal volume (partitions) properties  
#                using volume_uuid for Linux and DOS partitions and volume_part for others (like ufs)
#: usage       : halinfo
#: options:    -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 halinfo
# Once the file is saved, you must make the file executable using the terminal command: chmod +u ~/bin/halinfo
#

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function syntax {
cat << EOFSYNTAX
usage:
   $prg [options]
options:
   -v --verbose: verbose output (display partions offset and size)
   -V --Verbose: more verbose output 
   -c --color  : colored output 	
EOFSYNTAX
exit
}

# primary partitions color
pricolor=37

# alternate color
#color=$(alternate_color)
color=32

# parsing options 

flag=0

args=$(echo "$@ --" | sed s'|-\([cvV]\)\([cvV]\)|-\1 -\2|;s|^-| -|')
ARGS=`getopt -q -o cvV --long verbose,Verbose,color -- "$@"`
eval set -- "$ARGS"

[ "$ARGS" == "$args" ] || syntax
 
while true ; do
	case "$1" in
		-c|--color) flag=$(($flag | 1)) ; shift ;;
		-v|--verbose) flag=$(($flag | 2)) ; shift ;;
		-V|--Verbose) flag=$(($flag | 4)) ; shift ;;
		--) shift ; break ;;
    esac
done


# exit now if the HAL daemon is not running
lshal -s >/dev/null || exit

# get hal volumes
for vol in `lshal -s | grep -e volume_part -e volume_uuid` ; do
	dev=/org/freedesktop/Hal/devices/$vol
	par=$(hal-get-property --udi $dev --key block.device)
	devs=(${devs[li]} ${par}~${dev})[/li]done

# get device properties of all partitions sorted numerically by device name (sda1, sda2, ... sdb1, sdb2, etc) 

if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                    |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
elif [ $flag -lt 4 ] ; then
	printf "|-----------------------------------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                             start          size |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
else
	printf "|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                     diskID                                                      start          size |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
fi

i=0

for vol in $(echo ${devs[li]} | tr " " "[/li]" | sed 's|\([0-9]\)| \1|;s|~| |;s| \([0-9]\) | 0\1 |' | sort -n | awk '{ print $3 }') ; do
	# get volume properties
	par=$(hal-get-property --udi $vol --key block.device)
	mnt=$(hal-get-property --udi $vol --key volume.is_mounted)
	mp=$(hal-get-property --udi $vol --key volume.mount_point)
	lbl=$(hal-get-property --udi $vol --key volume.label)
	fs=$(hal-get-property --udi $vol --key volume.fstype)
	fsver=$(hal-get-property --udi $vol --key volume.fsversion)
	offset=$(hal-get-property --udi $vol --key volume.partition.start)
	size=$(hal-get-property --udi $vol --key volume.size)
	uuid=$(hal-get-property --udi $vol --key volume.uuid)
	fsusage=$(hal-get-property --udi $vol --key volume.fsusage)
	blocksize=$(hal-get-property --udi $vol --key volume.block_size)
	num=$(hal-get-property --udi $vol --key volume.partition.number)
	disk=$(hal-get-property --udi $vol --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=$bus-$serial-part$(hal-get-property --udi $vol --key volume.partition.number) ;;
		*) diskByID=$vendor-$serial-part$(hal-get-property --udi $vol --key volume.partition.number) ;;
	esac
	mnt=${mnt/true/*}

	# skip unsupported partitions (like BSD swap slices)
	[ "x$fs" == "x" -a "x$fsusage" == "x" ] && continue 	

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

	# set extended partition fs to "DOS Ext"
	[ "$fsusage" == "partitiontable" ] && fs="DOS Ext"
	
	# convert offset in sectors (could be 512 or 4096 sectors, according to blocksize)
	offset=$(($offset/$blocksize))

	# convert size in MB
	size=$(($size/1048576))

	case $flag in 
		0) # normal output
			printf "| %-8s%-5s%-8s%-15s%-40s |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid"
			;;
		1) # colored output
			let i++
	        if [ $num -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid"
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid"
				else
					printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid"
				fi
			fi
			;;
		2) # verbose output
			printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size
			;;
		3) # verbose coloured output
			let i++
	        if [ $num -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size
				else
					printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $offset $size
				fi
			fi
			;;		
		4) # extra verbose output
			printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $diskByID $offset $size
			;;
		5) # extra verbose coloured output
			let i++
	        if [ $num -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $diskByID $offset $size
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $diskByID $offset $size
				else
					printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par##*/} "${mnt/false/ }" "$fs" "$lbl" "$uuid" $diskByID $offset $size
				fi
			fi
			;;		
	esac
done


if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
"
elif [ $flag -lt 4 ] ; then
	printf "|-----------------------------------------------------------------------------------------------------------|
"
else
	printf "|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
"
fi

Use halinfo -V f (or --Verbose) for extra verbose output.
Notice the difference between halinfo, halinfo -v and halinfo -V.

This link is created by udev. “WDC_WD6000HLHX-01JJPV0_WD-WXA1C20K0415” is stored by HAL in the storage.serial property of the parent device (sda) and “ata” is stored in storage.vendor key. For usb external disks, I had to take the value of storage.bus to match the name of the udev symlink, the storage.bus value of internal hard disk beeing “pci”. I cannot garantee that it will work in any case.

Absolutely. That’s what I do. You can also choose to “mount by UUID” or “mount by label” during openSUSE setup. IMHO the two methods are equivalent. /dev/disk/by-id are symlinks created by the udev daemon, while labels and uuids must be written in the partition somewhere. I would say it is a bit better since it doesn’t directly rely on udev. But it doesn’t make a big difference because if udev doesn’t work properly, nothing else is going to work. However I’ve seen cases where (some) /dev/disk/by-uuid symlinks mysteriously vanished but mounting by UUID (not by /dev/disk/by-uuid) was still possible.
Here’s the syntax I use personally in /etc/fstab:

# /dev/sda5
UUID=0ac89ec3-41c4-4bdb-82c1-bd4a3b11465d    swap                               swap          defaults                         0   0
# /dev/sdb5
UUID=6d36f7fe-be64-449d-8857-30f6888d2622    swap                               swap          defaults                         0   0
#
# /dev/sda11
UUID=316bc15c-b355-4857-98e2-3c623651c1c9    /                                  ext3          acl,user_xattr                   1   1
# /dev/sda12
UUID=dca526b4-4d5b-450f-ad07-1c39eaddd1a8    /usr                               ext4          acl,user_xattr                   1   2
# /dev/sda13
UUID=9e25233e-b7b8-4bb9-ac8e-dbeac2cd2f0b    /local                             ext4          acl,user_xattr                   1   2
# /dev/sda14
UUID=d2ae99e7-6a8e-4562-97d9-d20620574f67    /var                               ext4          acl,user_xattr                   1   2
# /dev/sda10
UUID=5cd4e12e-fd55-4015-bf4d-b5a78a5f11a2    /home                              ext3          defaults                         0   2
# /dev/sda20
UUID=f16d1fcb-27a4-4028-8c29-2297d39000b0    /srv                               ext4          defaults                         0   2
# /dev/sdb13
UUID=8922f1d3-88fd-4c35-8898-d68c58f53b38    /misc                              btrfs         defaults                         0   0
....

Carlos already mentionned he used labels. He’s right. I think labels and uuids (not /dev/disk/by-uuid !) are the best methods. To make it simple, while mounting by labels or uuid, you can duplicate your hard disk and use the copy you just made and your /etc/fstab will still work. It won’t if you use /dev/disk/by-id. On the other hand, it won’t work if you reformat a partition, as the uuid will change. Both methods have advantages and inconvenients. Labels are good since renaming a partition is easier than changing its uuid.

Yes I remember. That’s why I removed the escape sequences from the code I posted in the forum. If you use this code the -c [ --color ] option has no effect. For escape sequences, you can get the compressed file here: http://www.unixversal.com/linux/openSUSE/halinfo.gz

Yes … but tput syntax is different on systems using terminfo or termcap (which means more work to get a decent output on differents systems)… and it would probably require some more printf too.

Again, this script only queries the hal daemon - not the hard disks directly! It shows not more and not less that the infos your applications get from hal through the message bus. Hopefully it reflects the reality of your hardware (it does). But I insist that it is not the same as reading the disks directly (with tools like fdisk or blkid). I added a quicktest in this version so the script will abord early if the hal daemon is not running.

udevadm info -q symlink -n sdb2 | awk '{ print $2 }'

I guess I’ll have to write the same kind of script to query the udev database. :wink:

or:

udisks --show-info /dev/sdb2 | awk '/by-id:/ { print $2 }'

Just tried out your new version 1.1 of halinfo and works like a champ. I am impressed all the way around please_try_again, I highly approve. I must say I did get tickled with the -V option description: Use halinfo -V f (or --Verbose) for extra verbose output. The Extra Verbose was something I had not heard of before, but it does just what I requested. Good Job!

Thank You,

Well, the first version was quick and dirty. Here’s a much cleaner and more useful one, I think:

#! /bin/bash
#: Title       : halinfo
#: Date Created: Wed Dec 29 22:59:33 PST 2010 
#: Last Edit   : Thu Dec 30 03:31:32 PST 2010
#: Author      : please_try_again 
#: Version     : 2.0
#: Description : displays partitions properties from hal or udev daemons 
#:             : this is a much cleaner rewriting of halinfo 1.0
#: usage       : partinfo 
#: options:    : -h --hal     : query hal database
#:             : -u --udev    : query udev 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
pricolor=37

# alternate color
#color=$(alternate_color)
color=32


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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/||')
	mnt[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.is_mounted | sed 's|false| |;s|true|\*|')
	mp[$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

	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' | 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:|is mounted:|size:|usage:|type:|version:|uuid:|number:|offset:|label:/ { 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/ismounted/mnt/;s/number/num/;s/type/fs/;s/label/lbl/;s|/dev/disk/by-id/||;s/ /[#]="/;s/$/" ;/' | sed "s|#|$i|")
	mnt[$i]=$(echo ${mnt[$i]} | sed 's|0| |;s|1|*|') 

	# set extended partition fs to "DOS Ext"
	if [ "x${fsusage[$i]}"=="x" -a ${num[$i]} -le 4 ] ; then
		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
		-h|--hal) uflag=$(($uflag | 1)) ; shift ;;
		-u|--udev) 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
	queryUdev
else
	lshal -s >/dev/null || exit
	queryHal 
fi


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

if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                    |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
elif [ $flag -lt 4 ] ; then
	printf "|-----------------------------------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                             start          size |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
else
	printf "|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                     diskID                                                      start          size |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
fi

i=0

while [ $i -lt ${#vol[li]} ] ; do
[/li]
	# 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))

	case $flag in 
		0) # normal output
			printf "| %-8s%-5s%-8s%-15s%-40s |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
			;;
		1) # colored output
	        if [ ${num[$i]} -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
				else
					printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
				fi
			fi
			;;
		2) # verbose output
			printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
			;;
		3) # verbose coloured output
	        if [ ${num[$i]} -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
				else
					printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
				fi
			fi
			;;		
		4) # extra verbose output
			printf "| %-8s%-5s%-9s%-15s%-40s %-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
	        if [ ${num[$i]} -le 4 ] ; then
				printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" 
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" 
				else
					printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" 
				fi
			fi
			;;		
	esac

let i++
done

if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
"
elif [ $flag -lt 4 ] ; then
	printf "|-----------------------------------------------------------------------------------------------------------|
"
else
	printf "|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
"
fi

Get the color version (with escape sequences) from here: http://www.unixversal.com/linux/openSUSE/halinfo20.tgz

This version can query either the hal database or the udev database and should produce the same output in both cases. Querying udev seems significantly faster. I guess it’s nice to be able to see what hal and udev think your partitions are. I should have renamed the script to something like partinfo, but it’s not a big deal.

usage:

halinfo -h --hal: query hal
halinfo -u --udev: query udev.

Otherwise same options as before for color, verbose, extra verbose (didn’t know how to call it) :wink:

If you get an error with halinfo -u, please run the command:

udisks --enumerate

and tell me if you see other devices than sdx, srx and fdx that I need to remove.

Hey I downloaded your version 2 and it works without error on my system. Only thing, I still don’t see any color using the -c option. I looked through the code, but really, you are beyond me as I was unable to follow how the flag system worked for color. How many computer systems have you ran this on? I only ask because I try to run everything I write on a half dozen systems if possible. It starts to get silly compiling every sub kernel version for instance with SAKC, but I don’t know how else to find such issues. We are so likely to do everything the exact same way, that it makes it hard to find errors that occur when a system configuration is not identical to ours. Color is hardly important to a good script, but you did claim to have that feature.

Thank You,

Hi James, the color option is working … except that you don’t have “enough” partitions.lol!
Have a look at the picture in post #3. The primary partitions appear in white, while the others alternate between yellow and green. Since you have only primary partitions, according to the output you previously posted, your partitions will all be white … or have the color defined in the variable pricolor in the script. If you like to use another color, just set this variable to any other value between 31 and 36. Similarly you can change the green default to something else than 32 (but you won’t see it without logical partitions) or use the alternate_color function which set a different alternate color based on the distro (guess what it is for openSUSE!). However this function use the command lsb_release (in package lsb-release which is not installed by default on openSUSE). Btw lsb_release comes with a little bug which has been fixed in 11.4 (thanks to a bug report raised in the french group based of the infos I provided). But I don’t think that bug would affect this script, as - if I remember correctly - it just doesn’t show the release’s codename (teal) as expected.

OK, I understand now. Yes, I don’t have anywhere near enough partitions in comparison to you and I can see why you need the colors then. Sorry to have not caught that at first. When I see more than eight partitions on one hard disk, my eyes just glaze over anyway. lol!

So, it looks like you nailed it, just in time for New Years. Very nice and I will be asking others to run halinfo as well as findgrub to get even more Verbose information about their systems. Two super script file additions for openSUSE right here.

Thank You,

Just keep in mind that halinfo -h will display an error message and abort the script if the hal daemon is not running, while halinfo -u (querying udev) will work in all cases.

On 2010-12-31 03:36, jdmcdaniel3 wrote:

> So, it looks like you nailed it, just in time for New Years. Very nice
> and I will be asking others to run halinfo as well as findgrub to get
> even more Verbose information about their systems. Two super script
> file additions for openSUSE right here.

Do we have some space where scripts can be uploaded, with a brief info on
how to use them, and a link to the forum thread about it? So that you can
always give the direct link to it, instead of searching a forum thread to
find out.

A forum download area? It could be associated (for upload) to the user that
wrote the script.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

I think that would be a great feature for the forum users, as downloading or copying/pasting a script, saving it to file, making it executable is often the most difficult part (not to mention that this is something we have to explain every time). Or maybe … a small utilities repo … so users could just install the scripts from there in the right place (like /usr/local/bin) with zypper in. (?)

Of course, some volunteers should take a look of the scripts first and make sure they do what they claim to do (and nothing else) … also decide if they will be usefull for anyone - not any script is.

Just an idea at this time of year …

  • And NO - just in case someone is going to suggest I should create my own repo. The days are only 48 hours long here.

On 2011-01-02 09:06, please try again wrote:

>> A forum download area? It could be associated (for upload) to the user
>> that wrote the script.
>>
>
> I think that would be a great feature for the forum users, as
> downloading or copying/pasting a script, saving it to file, making it
> executable is often the most difficult part (not to mention that this is
> something we have to explain every time). Or maybe … a small utilities
> repo … so users could just install the scripts from there in the right
> place (like /usr/local/bin) with zypper in. (?)

A repo is probably excessive, it has its own syntax. A download area would
be just the thing. I know some forums have it, but I have idea how
easy/difficult it is. Otherwise, being scripts, the thing is a pastebin;
but it needs an associated linuk with explanations. Or rather, give the
link to the explanation, which is a fixed place (not a forum thread), and
this place links to the download pastebin or equivalent.

> Of course, some volunteers should take a look of the scripts first and
> make sure they do what they claim to do (and nothing else) … also
> decide if they will be usefull for anyone - not any script is.

Well, that would be the peer review in this forum. Mostly.

> Just an idea at this time of year …
>
> * And NO - just in case someone is going to suggest I should create my
> own repo. The days are only 48 hours long here.

48 hours? How easy are the immigration laws there? I want those 48 hours! >;-)


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

James just ran you latest script. I named it partinfo. You call it halinfo in title and partinfo in notes on
coping and pasting. What are the errors on udevadm. Do I need to load something? Will try again with
the color option.

**Great work! **

finemanruss@linux-fj8u:~/bin> udisks --enumerate
/org/freedesktop/UDisks/devices/sde1
/org/freedesktop/UDisks/devices/sde2
/org/freedesktop/UDisks/devices/sde3
/org/freedesktop/UDisks/devices/sde
/org/freedesktop/UDisks/devices/sdc
/org/freedesktop/UDisks/devices/sdb
/org/freedesktop/UDisks/devices/sdc1
/org/freedesktop/UDisks/devices/sdc2
/org/freedesktop/UDisks/devices/sdc3
/org/freedesktop/UDisks/devices/sdc4
/org/freedesktop/UDisks/devices/sdc5
/org/freedesktop/UDisks/devices/sdd1
/org/freedesktop/UDisks/devices/sdd
/org/freedesktop/UDisks/devices/sdd2
/org/freedesktop/UDisks/devices/sda
/org/freedesktop/UDisks/devices/sr0
/org/freedesktop/UDisks/devices/sda1
/org/freedesktop/UDisks/devices/sda2
/org/freedesktop/UDisks/devices/sda3
/org/freedesktop/UDisks/devices/sda4
/org/freedesktop/UDisks/devices/sdc6
finemanruss@linux-fj8u:~/bin> partinfo -u
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
/home/finemanruss/bin/partinfo: line 125: udevadm: command not found
|------------------------------------------------------------------------------|
| dev mount fs label uuid |
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
| sda1 DOS Ext 095357e2-b98e-4053-9b84-87ebfe89f7a3 |
| sda2 DOS Ext |
| sda3 DOS Ext |
| sda4 DOS Ext |
| sdc1 DOS Ext ede7132d-b50a-437e-9ee9-cebdcd08ee1d |
| sdc2 * DOS Ext 98bda876-8c42-4860-895f-58b211e28efa |
| sdc3 * DOS Ext fcf1b825-e40c-498e-9712-8b1df0f3a6d1 |
| sdc4 DOS Ext |
| sdc5 * ext4 68d04a59-1b12-4a04-8303-7180e03c1b2b |
| sdc6 * ext4 87b6826f-37af-4819-901a-b8ddaf4432a4 |
| sdd1 * DOS Ext F83A-1762 |
| sdd2 * DOS Ext Backup11.4 0f65a62c-209c-4aab-a453-9203174a0cf3 |
| sde1 * DOS Ext MyBook 63E5-2BD3 |
| sde2 * DOS Ext Backup11.2 0213e309-b063-44dc-8575-f72e1a550102 |

sde3 * DOS Ext Backup11.3 b6ef2a06-5ac5-4fb4-b04d-2fab9d6a539b

/home/finemanruss/bin/partinfo: line 263: syntax error near unexpected token (' /home/finemanruss/bin/partinfo: line 263: Get the color version (with escape se’
finemanruss@linux-fj8u:~/bin> partinfo -h
|------------------------------------------------------------------------------|
| dev mount fs label uuid |
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
| sda1 ext4 095357e2-b98e-4053-9b84-87ebfe89f7a3 |
| sda2 unknown |
| sda3 unknown |
| sda4 DOS Ext |
| sdc1 swap ede7132d-b50a-437e-9ee9-cebdcd08ee1d |
| sdc2 * ext4 98bda876-8c42-4860-895f-58b211e28efa |
| sdc3 * ext4 fcf1b825-e40c-498e-9712-8b1df0f3a6d1 |
| sdc4 DOS Ext |
| sdc5 * ext4 68d04a59-1b12-4a04-8303-7180e03c1b2b |
| sdc6 * ext4 87b6826f-37af-4819-901a-b8ddaf4432a4 |
| sdd1 * vfat F83A-1762 |
| sdd2 * ext4 Backup11.4 0f65a62c-209c-4aab-a453-9203174a0cf3 |
| sde1 * vfat My Book 63E5-2BD3 |
| sde2 * ext4 Backup11.2 0213e309-b063-44dc-8575-f72e1a550102 |

sde3 * ext4 Backup11.3 b6ef2a06-5ac5-4fb4-b04d-2fab9d6a539b

/home/finemanruss/bin/partinfo: line 263: syntax error near unexpected token (' /home/finemanruss/bin/partinfo: line 263: Get the color version (with escape se’

Note: Got rid of errors on line 263. My copy went a little to far. Also I notice color version runss as root, shou it? I’m running black and white version as me.

James just ran you latest script. I named it partinfo. You call it halinfo in title and partinfo in notes on
coping and pasting. What are the errors on udevadm. Do I need to load something? Will try again with
the color option.
Glad you like the script upscope, but actually please-try-again is the author and very knowledgeable about hard disk partitions. Perhaps the smartest person I have ever worked with on the subject. I sure he knows who you meant.

Thank You,

OK. I think I fixed that udevadm problem.

http://www.unixversal.com/linux/openSUSE/halinfo201.tgz

#! /bin/bash
#: Title       : halinfo
#: Date Created: Wed Dec 29 22:59:33 PST 2010 
#: Last Edit   : Thu Dec 30 03:31:32 PST 2010
#: Author      : please_try_again 
#: Version     : 2.0.1
#: Description : displays partitions properties from hal or udev daemons 
#:             : this is a much cleaner rewriting of halinfo 1.0
#: usage       : halinfo 
#: options:    : -h --hal     : query hal database
#:             : -u --udev    : query udev 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 halinfo
# Once the file is saved, you must make the file executable using the terminal command: chmod +u ~/bin/halinfo
#

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
pricolor=37

# alternate color
color=$(alternate_color)
#color=32


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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/||')
	mnt[$i]=$(hal-get-property --udi ${vol[$i]} --key volume.is_mounted | sed 's|false| |;s|true|\*|')
	mp[$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

	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' | 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:|is mounted:|size:|usage:|type:|version:|uuid:|number:|offset:|label:/ { 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/ismounted/mnt/;s/number/num/;s/type/fs/;s/label/lbl/;s|/dev/disk/by-id/||;s/ /[#]="/;s/$/" ;/' | sed "s|#|$i|")
	mnt[$i]=$(echo ${mnt[$i]} | sed 's|0| |;s|1|*|') 

	# 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
		-h|--hal) uflag=$(($uflag | 1)) ; shift ;;
		-u|--udev) 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
	queryUdev
else
	lshal -s >/dev/null || exit
	queryHal 
fi


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

if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                    |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
elif [ $flag -lt 4 ] ; then
	printf "|-----------------------------------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                             start          size |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
else
	printf "|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
\
| dev  mount   fs       label          uuid                                     diskID                                                      start          size |
\
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
"
fi

i=0

while [ $i -lt ${#vol[li]} ] ; do[/li]
	# 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))

	case $flag in 
		0) # normal output
			printf "| %-8s%-5s%-8s%-15s%-40s |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
			;;
		1) # colored output
	        if [ ${num[$i]} -le 4 ] ; then
				printf "|[$pricolor;1m %-8s%-5s%-9s%-15s%-40s[37;0m|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "|[$color;1m %-8s%-5s%-9s%-15s%-40s[37;0m|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
				else
					printf "| %-8s%-5s%-9s%-15s%-40s|
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}"
				fi
			fi
			;;
		2) # verbose output
			printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
			;;
		3) # verbose coloured output
	        if [ ${num[$i]} -le 4 ] ; then
				printf "|[$pricolor;1m %-8s%-5s%-9s%-15s%-40s%14s%11s MB[37;0m |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "|[$color;1m %-8s%-5s%-9s%-15s%-40s%14s%11s MB[37;0m |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
				else
					printf "| %-8s%-5s%-9s%-15s%-40s%14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${offset[$i]}" "${size[$i]}" 
				fi
			fi
			;;		
		4) # extra verbose output
			printf "| %-8s%-5s%-9s%-15s%-40s %-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
	        if [ ${num[$i]} -le 4 ] ; then
				printf "|[$pricolor;1m %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB[37;0m |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" 
			else
				if [ $(($i % 2)) -eq 0 ] ; then
					printf "|[$color;1m %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB[37;0m |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" 
				else
					printf "| %-8s%-5s%-9s%-15s%-40s %-50s %14s%11s MB |
" ${par[$i]} "${mnt[$i]}" "${fs[$i]}" "${lbl[$i]}" "${uuid[$i]}" "${diskByID[$i]}" "${offset[$i]}" "${size[$i]}" 
				fi
			fi
			;;		
	esac

let i++
done

if [ $flag -lt 2 ] ; then
	printf "|------------------------------------------------------------------------------|
"
elif [ $flag -lt 4 ] ; then
	printf "|-----------------------------------------------------------------------------------------------------------|
"
else
	printf "|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
"
fi

halinfo 2.0.2
http://www.unixversal.com/linux/openSUSE/halinfo202.tgz

Fix: ignore unpartitionated HD while querying the hal daemon.

halinfo 2.0.3
http://unixversal.com/linux/openSUSE/halinfo203.tgz

Fix: ignore lvm devices in udev.