It looks the same as fdisk -l but …
[ul]
[li]it doesn’t require root privileges (except for option -b) [/li][li]it lists mount points of mounted partitions [/li][li]it works with GPT [/li][li]it optionally displays bootloaders (requires sudo). It only list Grub stage1. Use findgrub if you need info about stage2. [/li][li]it can display all partition names (option -l) [/li][li]it can display any partition property available in udev. [/li][/ul]
Here’s the code:
#! /bin/bash
#: Title : lspart
#: Date Created: Mon Apr 30 13:58:35 PDT 2012
#: Last Edit : Sat May 5 18:47:18 PDT 2012
#: Author : Agnelo de la Crotche (please_try_again)
#: Version : 1.1
#: License : GPL
#: Description : list partition properties from udev
#: : this is based of halinfo but using udev as default
#: usage : lspart [ -h|-l|-g ] [-] [ property [ property ] ... ]
#: options: : -h --help : lists available properties
#: : -l --links : shows DEVLINKS only
#: : -b --boot : tries to identify the bootsectors
#: : (requires root privileges)
#: Requires: : udev & udisks
#:
#: Copyright (C) 2012 by Agnelo de la Crotche
#: ------------------------------------------------------------------
# default properties if lspart is run without arguments (Do not chnange!)
properties="ID_MODEL ID_PART_TABLE_TYPE ID_PART_ENTRY_SCHEME ID_BUS UDISKS_PARTITION_FLAGS MAJOR MINOR UDISKS_PARTITION_OFFSET UDISKS_PARTITION_SIZE ID_PART_ENTRY_SIZE ID_FS_TYPE UDISKS_PARTITION_TYPE ID_FS_VERSION ID_FS_USAGE"
# color for hdd devices
HDDC=$(tput sgr0)$(tput setaf 1) # red
# (Some) Boot sector signatures
BS5272="Grub1"
BSaa75="Grub1"
BS48eb="Grub1"
BS63eb="Grub2"
BS7c3c="Grub2"
BS020="Grub2"
BS6616="FAT16"
BS19d="FAT32"
BS6f74="FAT32"
BS745="FAT32"
BSe3e1="FreeBSD"
BS6639="NetBSD"
BS8c7="openBSD"
BS8cd="Win XP"
BS55aa="Win7/Vista"
BSc031="Generic"
BSc033="Generic"
BS8ec031="SUSE Gen"
BS8ec033="MS Gen"
BSb8fa="None"
udevadm=/sbin/udevadm
udisks=$(which udisks 2>/dev/null)
[ -x $udevadm ] || exec echo "udevadm not found"
[ -x $udisks ] || exec echo "udisks not found"
# Make sure we have root privileges for option -g
if [ "$1" == "-b" -o "$1" == "--boot" ] ; then
if [[ $UID -ne 0 ]]; then
id -Gn | grep -q wheel && exec sudo $0 $@ || exec echo "Root User Permissions are required with option -b"
fi
fi
case $1 in
"-h"|"--help")
P=""
for dev in $(cat /proc/partitions | awk 'NR>2{ print $4}') ; do
P="$P $($udevadm info --query=property --name=$dev | awk -F "=" '{print $1};')"
done
echo $P | tr " " "
" | sort -u | tr "
" " "
echo ; exit ;;
"-l"|"--links") properties="DEVLINKS" ;;
"-b"|"--boot") shift ; HEADER=1 ; BS=1 ;;
"-") shift ; uproperties="$@" ; properties="$properties $@" ;;
"") HEADER=1 ;;
*) uproperties="$@" ; properties="$@" ;;
esac
properties=$(echo $properties | tr -cd "[:alnum:]_[:space:]")
PROPERTIES=$(echo $properties | tr " " "|")
properties="DEV $properties"
i=-1
for dev in $(cat /proc/partitions | awk 'NR>2{ if ($1 != 11) print $4}') ; do
let i++
DEV[$i]=$dev
eval $($udevadm info --query=property --name=$dev | awk -F "=" 'BEGIN {I='$i'} ; /'$PROPERTIES'/ {printf "%s[%s]=\"%s\";", $1, I, $2}')
done
echo ${ID_PART_TABLE_TYPE
[li]} | grep -q 'gpt' && ID_width=38 || ID_width=6[/li]Fs_width=$(($(echo ${ID_FS_TYPE
[li]} | tr " " "[/li]" | wc -L)+2))
Ver_width=$(($(echo ${ID_FS_VERSION
[li]} | tr " " "[/li]" | wc -L)+2))
if [ "$HEADER" ]; then
[ "$BS" ] && Model="BS Model/Mount" || Model="Model/Mount"
printf "Dev Boot Maj Min Bsize/Start Size%${Fs_width}s%${ID_width}s%${Ver_width}s %s
" "Fs" "ID" "Ver" "$Model"
fi
for (( j=0 ; j <= $i ; j++ )) ; do
dev=${DEV[$j]}
C=$(tput sgr0) ; MP=""
if [ ${#dev} -eq 3 ]; then
C=$HDDC
MP=" ${ID_MODEL[$j]}"
bsize=$(udisks --show-info /dev/$dev | sed -n 's/.*block size:[^0-9]*//p')
bsize=${bsize:-512}
else
C=$(tput sgr0)
mounted=$($udisks --show-info /dev/$dev | awk '/is mounted/ { print $NF }')
if [ $mounted -eq 1 ] ; then
C=$C$(tput bold)
MP=$($udisks --show-info /dev/$dev | sed -n 's/mount paths:[ ]*//p')
fi
fi
for p in ${properties} ; do
L=0 ; P=""
case "$p" in
"ID_PART_ENTRY_SIZE"|"ID_MODEL"|"ID_PART_TABLE_TYPE"|"ID_PART_ENTRY_SCHEME"|"ID_BUS"|"ID_FS_USAGE")
[ "${uproperties}" == "${uproperties##*$p}" ] && continue ;;
"DEVLINKS") echo " ${DEVLINKS[$j]}" | tr " " "
" ;;
"DEV") [ ${#dev} -eq 3 ] && printf "
" ; printf "%s%-7s" $C $dev ; continue ;;
"UDISKS_PARTITION_OFFSET")
L=11
if [ ${#dev} -gt 3 -a "${UDISKS_PARTITION_OFFSET[$j]}" ]; then
P="$((${UDISKS_PARTITION_OFFSET[$j]}/$bsize))"
elif [ ${#dev} -eq 3 ]; then
P="$bsize B"
fi ;;
"UDISKS_PARTITION_SIZE")
L=11
if [ ${#dev} -eq 3 ]; then
P=$(($($udisks --show-info /dev/$dev | sed -n 's/^ *size: *//p')*1))
P=${P:-0} ; P=$(($P*1)); P=$((P/1048576))
printf "%'12.0f" $P ; continue
elif [ "${ID_FS_TYPE[$j]}" == "ufs" ]; then
if [ "${ID_PART_ENTRY_SIZE[$j]}" ]; then
P="${ID_PART_ENTRY_SIZE[$j]}"
elif [ "${UDISKS_PARTITION_SIZE[$j]}" ];then
P="$((${UDISKS_PARTITION_SIZE[$j]}/$bsize))"
else
P="-"
fi
if [ "${ID_PART_ENTRY_SCHEME[$j]}" == "dos" ] ; then
psize=$P
else
[ $P -eq $psize ] && P="?"
fi
else
[ "${UDISKS_PARTITION_SIZE[$j]}" ] && P="$((${UDISKS_PARTITION_SIZE[$j]}/$bsize))"
[ "$psize" ] && [ $P -eq $psize ] && [ "x${ID_FS_USAGE[$j]}" == "x" ] && P="?"
fi ;;
"UDISKS_PARTITION_FLAGS")
[ "${UDISKS_PARTITION_FLAGS[$j]}" ] && P="*" || P=" " ; L=1 ;;
"UDISKS_PARTITION_TYPE")
if [ ${#dev} -eq 3 ]; then
P=${ID_PART_TABLE_TYPE[$j]} ; P=${P/dos/mbr}
else
P=${UDISKS_PARTITION_TYPE[$j]} ; P=${P/0x/}
fi ;;
"ID_FS_TYPE")
if [ ${#dev} -eq 3 ]; then
printf " %-${Fs_width}s" "GB" ; continue
fi ;;
"ID_FS_VERSION")
if [ ${#dev} -eq 3 ]; then
P=${ID_BUS[$j]}
fi ;;
"MAJOR"|"MINOR") L=2 ;
esac
[ "$P" ] || eval 'P=${'$p[$j]} ; P=${P:--}
[ $L -eq 0 ] && L=$(eval echo '${'$p'[@]}' | tr " " "
" | wc -L)
let L++
printf "%s%${L}s " "$C" "$P"
done
if [ "$BS" ] ; then
if [ ${#dev} -eq 3 ] ; then
B=$(hexdump -v -n 2 -e '"%02x"' /dev/$dev)
else
B=$(hexdump -v -s 128 -n 2 -e '/1 "%x"' /dev/$dev)
fi
bs=BS${B} ; bs=${!bs} ; bs=${bs:-0x$B}
if [ "$bs" == "Generic" ];then
B=$(hexdump -v -n 3 -e '"%02x"' /dev/$dev)
bs=BS${B} ; bs=${!bs} ; bs=${bs:-0x$B}
fi
[ "$bs" == "0x00" ] && bs="-"
printf " %-9s" "$bs"
fi
printf "%s%s
" $C "$MP"
done
If you have an OS boot sector signature not included in the script, I will be glad to add it.