Skip to content

July 29, 2010

How to extract storage device information (Parte 2 – using lshw and xmlstarlet)

by Joe Kuan

In Ubuntu 10.04, udevinfo is no longer available and I need to write a LiveCD tool to retrieve storage information (such as vendor, size, filesystem, label) from existing partitions (e.g /dev/sda1). So I cannot use the command lines anymore mentioned in my previous blog.

After a day of researching various tools and experiment parsing command line outputs. Eventually, I decide to use lshw to generate all the storage information and output into XML format. Then use xmlstarlet to extract specific device data.

To generate storage information with lshw, I use the following command to retrieve all the partitions and disk device data.

lshw -xml -class disk -class volume > /tmp/disk_info

First of all, the XML output is not a valid format which won’t work with any XML parsing tools. There are two problems with the generated output: 1) Missing root level tag. 2) Containing non ASCII characters.

To fix this problem, you need to run the following command. The first sed command is to insert the root level tag into the correct place. The second unix command, tr, is to delete any non printable characters.

sed -e '/?>/a\<lshw>' -e '$a\</lshw>' /tmp/disk_info | tr -cd '[:print:][:space:]' > /tmp/disk_info.1

Then you can use xmlstarlet to validate the XML output

root@ubuntu104:/tmp# xmlstarlet val /tmp/disk_info.1
/tmp/disk_info.1 - valid

There are several XML command line parser tools available. However, I choose xmlstarlet for the flexibility on matching with XPATH syntax and converting output. The following command line goes through a depth first search to get the partition with the mountable filesystem attribute. With such node existed indicates we are on the right node and works backwards to extract various other information.

xmlstarlet sel -t -m "/lshw/node[@id='disk']/node/configuration/setting[@id='filesystem']" -v '../../logicalname[1]' -o ',' -v "../setting[@id='label']/@value" -o ',' -v '@value' -o ',' -v '../../size' -o ',' -v '../../../product' -n /tmp/disk_info.1

Here is the screenshot from the command line output – Partition, Label, Filesystem, Size, Vendor.

Xmlstarlet is not a simple unix tool but it offers great flexibility. The arguments are order sensitive and you need to be familiar with XPath and possibly XSL. Here are some useful links for xmlstarlet usage: XMLStarlet: a Unix Toolkit for XML and Start working with XMLStarlet.

Note: lshw is missing the vendor information on some of the devices. On that end, hwinfo is a better tool for it.

hwinfo --short --disk

Here is the screenshot of the output


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: