Sunday 6 November 2016

Get disk size in GB from prtvtoc command




Prtvtoc is a great command to examine the disk geometry of a Solaris LUN but the size component is displayed in the form of number of sectors. For someone who is not a mathematical genius, converting sectors to GB with using calculators isn't possible.

So here is a one liner to get that disk size in gigs!


ROOT@salvation# SIZE=$(prtvtoc /dev/rdsk/c2t3d0s2 | awk '$1 ~ /2/ {print $5}'); echo disk size is $(bc <<< "scale=4 ; ${SIZE} *512/1024/1024/1024") GB
disk size is .2958 GB

Explanation:

The trick is to take apart the prtvtoc output in the form of rows & columns.Then I used awk to do a regex match to print column 5 if the corresponding row counterpart in column 1 is equal to 2. 
Since in Solaris the backup slice (slice 2) represents the overall size of the disk, there would be a slice 2 showing up in the partition column.
That gave me the sector count which I then fed to bc. We couldn't do the arithmetic via bash built ins because bash doesn't work with floating point values. In the bc command, the parameter 'scale' represents the level of precision after the decimal point.

Using this one liner as a script:

If you have more than one disks to check, a script should be the way to go.
This is a short script which does what the one liner mentioned above did:

 ROOT@salvation# cat disk_size.sh
#!/bin/bash

##Script to calculate disk size in GB using prtvtoc##

SIZE=$(prtvtoc /dev/rdsk/${1}s2 | awk '$1 ~ /2/ {print $5}')

SIZEinGB=$(bc <<< "scale=4 ; ${SIZE} *512/1024/1024/1024")

echo "Size of disk $1 is $SIZEinGB GB"


Script execution:

To run the script just supply the disk name as an argument to the script & that's it.
Here are a couple of sample runs:

AVAILABLE DISK SELECTIONS:
       0. c0d0 <▒x▒▒▒▒▒▒▒▒▒@▒▒▒ cyl 3260 alt 2 hd 255 sec 63>
          /pci@0,0/pci-ide@7,1/ide@0/cmdk@0,0
       1. c2t0d0 <VMware,-VMware Virtual S-1.0-15.00GB>
          /pci@0,0/pci15ad,1976@10/sd@0,0
       2. c2t1d0 <VMware,-VMware Virtual -1.0 cyl 98 alt 2 hd 64 sec 32>
          /pci@0,0/pci15ad,1976@10/sd@1,0
       3. c2t2d0 <VMware,-VMware Virtual -1.0 cyl 200 alt 2 hd 64 sec 32>
          /pci@0,0/pci15ad,1976@10/sd@2,0
       4. c2t3d0 <VMware,-VMware Virtual -1.0 cyl 303 alt 2 hd 64 sec 32>
          /pci@0,0/pci15ad,1976@10/sd@3,0
       5. c2t4d0 <VMware,-VMware Virtual -1.0 cyl 405 alt 2 hd 64 sec 32>
          /pci@0,0/pci15ad,1976@10/sd@4,0
       6. c2t5d0 <VMware,-VMware Virtual -1.0 cyl 508 alt 2 hd 64 sec 32>
          /pci@0,0/pci15ad,1976@10/sd@5,0
Specify disk (enter its number): Specify disk (enter its number):

ROOT@salvation#
ROOT@salvation# ./disk_size.sh c2t1d0
Size of disk c2t1d0 is .0957 GB
ROOT@salvation# ./disk_size.sh c2t2d0
Size of disk c2t2d0 is .1953 GB
ROOT@salvation# ./disk_size.sh c2t3d0
Size of disk c2t3d0 is .2958 GB
ROOT@salvation# ./disk_size.sh c2t4d0
Size of disk c2t4d0 is .3955 GB
ROOT@salvation# ./disk_size.sh c2t5d0
Size of disk c2t5d0 is .4960 GB


For loop variation:

You can also construct a one liner for loop using the script for multiple disks in one go like the example shown below:

ROOT@salvation# for i in  c2t{1..5}d0; do ./disk_size.sh $i ; done
Size of disk c2t1d0 is .0957 GB
Size of disk c2t2d0 is .1953 GB
Size of disk c2t3d0 is .2958 GB
Size of disk c2t4d0 is .3955 GB

Size of disk c2t5d0 is .4960 GB

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...