diskparts: list disk partitions, including any ext2 labels

D. Hugh Redelmeier hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org
Tue Mar 2 07:15:50 UTC 2004


| From: William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>

| On Mon, Mar 01, 2004 at 09:21:01PM -0500, D. Hugh Redelmeier wrote:

I should have given an example of output:

    # diskparts /dev/hda
    Disk /dev/hda: 16 heads, 63 sectors, 232581 cylinders
    Units = cylinders of 1008 * 512 bytes
       Device Boot    Start       End    Blocks   Id  System
    /dev/hda1             1      1016    512032+  83  Linux: boot
    /dev/hda2          1017      4064   1536192   83  Linux: spare
    /dev/hda3          4065     16318   6176016   83  Linux: spare2
    /dev/hda4         16319    232581 108996552    5  Extended
    /dev/hda5         16319     20382   2048224+  82  Linux swap
    /dev/hda6         20383     32573   6144232+  83  Linux: home
    /dev/hda7         32574     49212   8386024+  83  Linux: rhl8.0
    /dev/hda8         49213     69530  10240240+  83  Linux: scratch6
    /dev/hda9   *     69531     85784   8191984+  83  Linux: rhl9
    /dev/hda10        85785    106102  10240240+  83  Linux: scratch7
    /dev/hda11       106103    126420  10240240+  83  Linux: scratch8
    /dev/hda12       126421    146738  10240240+  83  Linux: ???


| > for drive
| > do
| > 	# oddity: the each line of the fdisk output is prepended with a backslash
| > 	# to prevent any leading whitespace being discarded by the sh read command.
| > 
| > 	/sbin/fdisk -l "$drive" | sed -e 's/^/\\/' |
| > 		while read line
| > 		do
| > 			case "$line" in
| > 			*Linux)
| > 				vol=` echo "$line" | sed -e 's/ .*//' `
| > 				lab=` /sbin/e2label "$vol" 2>/dev/null || echo '???'`
| > 				line="$line: $lab"
| > 				;;
| > 			esac
| > 			echo "$line"
| > 		done
| > done


| It seems to me, you're appending ext2 label or '???' to those partition
| with 'Linux' tag.

Yes.  See the example.

BTW, /dev/hda12 in the example has ??? because teh partition isn't yet
mkfsed.

|  But, 'fdisk -l' will print out all known partitions
| (/proc/partitions), so use that.

I hadn't thought about /proc/partitions.  It looks as if it would be
possible to extract the drives from that file.  But I have a nagging
doubt: the kernel does not know the pathname for the device.
Otherwise, it doesn't look useful.

My script does use fdisk -l.  So I'm not really sure what you are saying.

| 
|     fdisk -l | awk '{print $1 ":" $0}' | while IFS=':' read vol line; do
| 	case $line in
| 	    *Linux) echo "$line:" `e2label $vol 2>/dev/null || echo '???'` ;;
| 	    *) echo "$line" ;;
| 	esac
|     done

This code seems similar to mine, but more compact.  My extra
lines are for:
- allowing multiple drives to be specified (the for loop)
- defaulting to /dev/hda
- safe programming (set -u)
- more spacious formatting

Have you tested your version?

On my RHL 7.0 system, "fdisk -l" without a drive pathname does not do
anything.

Your ??? isn't sufficiently quoted -- it matches all three-letter file
names in the current directory.

I'd have used quotes around other things too.  For example, around
most references to $line.

Perhaps foolishly, I don't have /sbin on my path.  So I used absolute
paths for fdisk and e2label.  This is a little fragile.

| or just go for the throat,
| 
|     fdisk -l | awk '/Linux$/ {print $1}' | while read vol; do
| 	echo "$vol:" `e2label $vol 2>/dev/null || echo '???'`
|     done

This too needs more quotes.

I want all the infor from the fdisk AND the e2label.

| From: William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>

| After checking 'e2fsprogs' package,
|     blkid -s TYPE
| is simpler than 'e2label'.

That does not seem to be on my systems.

Thanks for your comments.  Would you every use this script?w

Hugh Redelmeier
hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org  voice: +1 416 482-8253

--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list