Sunday, June 8, 2014

Bash Script to determine capacity of a SCSI device


Please use above script to determine the capacity or device size of your SCSI device like Hard Disk (sda) or CD-ROM (sr0).
Copy the script into a script file e.g readcap.sh and run as below:

./readcap /dev/sda

Above command determines capacity of Hard Disk.

Script:

#This script display the SCSI device capacity you are quering about !!
#e.g. usage: ./readcap /dev/sda
#!/bin/bash
ls $1 2>/dev/null 1>/dev/null
if [ $(echo $?) -eq 0 ]
then
sudo sg_readcap  $1 | tail -1 | awk 'BEGIN{ORS ="";print "SCSI Device Capacity: "}{print $7;ORS="\n"}END{print " GB"}'
else
echo "Wrong SCSI device entered/Incorrect usage of script"
fi

Guys please report me back any issues found during script execution.

Tuesday, June 3, 2014

Querying SCSI devices

Sometimes we want to query about our devices present in our computer. e.g. Hard Disks , DVD drives etc.These devices comes into the family of SCSI (Small Computer Systems Interface) devices.So we can use some of the SCSI commands to query the status of the SCSI devices.I use linux system to demonstrate this.

First installation of sg3-utils package is required.You can get more information on sg3-utils package by clicking on below link:

http://sg.danny.cz/sg/sg3_utils.html

So first step is to install sg3-utils package using command: sudo apt-get install sg3-utils

E.g. a-saurabh@a-saurabh:~$ sudo apt-get install sg3-utils

Now once the package is installed , we get access to numerous and helpful utilities which makes possible to send SCSI commands from terminal to SCSI devices.

Suppose I want to see serial number of my hard disk, I can issue the below command.

a-saurabh@a-saurabh:~$ sudo sg_inq -p 0x80 /dev/sda
VPD INQUIRY: Unit serial number page
  Unit serial number:      WD-WMC2E7086390

Basically we have send an SCSI INQUIRY command with page code 0x80 (Representing Unit Serial Number) to our hard disk node under /dev directory.

We can also list the supported pages by the SCSI device by sending the command:

a-saurabh@a-saurabh:~$ sudo sg_inq -p 0x00 /dev/sda
 Only hex output supported. sg_vpd decodes more pages.
VPD INQUIRY, page code=0x00:
   [PQual=0  Peripheral device type: disk]
   Supported VPD pages:
     0x0 Supported VPD pages
     0x80 Unit serial number
     0x83 Device identification
     0x89 ATA information
     0xb0 Block limits (sbc2)
     0xb1 Block device characteristics (sbc3)
     0xb2 Logical block provisioning (sbc3)


Here we query about the supported inquiry pages by the device.

We use -p option in the sg_inq command for specifying the page which is being queried,

Likewise we can request information for more inquiry pages from SCSI devices.