LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to detect size of IDE device (https://www.linuxquestions.org/questions/programming-9/how-to-detect-size-of-ide-device-378739/)

kushalkoolwal 10-31-2005 05:10 PM

how to detect size of IDE device
 
Hi. I am writing a script to detect the sizes of the IDE devices connected to my system. What is the most accuate way of doing it. Right now I am mouting the device on one of my directory adn then give the command df -h to check the size.

Is there any other way by which I can check the total size of device with mounting them??

Thanks

homey 10-31-2005 05:22 PM

Try the command: sfdisk -s /dev/hda

kushalkoolwal 10-31-2005 05:48 PM

Quote:

Originally posted by homey
Try the command: sfdisk -s /dev/hda

hey that worked. Also, it gives the output in KBs. Is there any way(through a command) by which i can divide it with 1000(just corrected) so that I get the output in MB straightaway. Some thing like:
sfdisk -s /dev/hdc | (divide operation here).


Thank you once again.

exvor 10-31-2005 06:05 PM

1mb is actually 1024kb not 100

of course this is file system size for a meg and not the hardware industry standard meg

to make this more confusing blocks change size too with file system.


as far as what command can be used mabye calc but im not sure at this very moment what the baisc program is for math operations with bash.

jschiwal 10-31-2005 06:46 PM

If you don't care about the remainder, you can perform the division in bash.
mbsize=(( $kbsize/1024 ))

kushalkoolwal 10-31-2005 07:11 PM

Yeah I understand what you are saying . But in the first place how can I get the output of the command sfdisk -s /dev/hdc into the variable $kbsize?

Thanks

homey 10-31-2005 08:03 PM

I made a script a while back to show how much un-partitioned space was left on a drive.
Here is a snip for what you want....
Code:

#!/bin/bash
# Example: ./test /dev/hda
drive=`/sbin/sfdisk -s $1`
size=$(($drive / 1024))
echo " Total drive size    $size  MB"

Here is the original script.
Code:

#!/bin/bash
##### This script calculates hard drive space.
##### example:  ./drive /dev/hda

##############################################
#  Ensure that root is running the script.
#
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
        echo
        echo "You must be root to run this!"
        echo
      exit 1
fi
##############################################

usage()
{
  echo "Usage: $0 /dev/hd#"
  exit 1;
}

test "$1" || usage

if ! [ -e $1 ]; then
    echo "$1 does not exist. Exiting."
    exit 1
fi

if [ -e $1 ]; then

drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i 2> /dev/null`
part=$((($a + 0) / 1024))
totalused=$(($totalused + ($a + 0)))
echo "Partition $i used $part MB"
done
echo

else
    exit 1
fi

#####
size=$(($drive / 1024))
used=$(($totalused / 1024))
free=$((($drive - $totalused) / 1024 +1))
#
echo " Total drive size    $size  MB"
echo " Partitioned size    $used  MB"
echo " Unpartitioned size  $free  MB"
echo
echo
#####End


kushalkoolwal 11-06-2005 02:38 PM

That;sGreat!!! Thanks you so much. :)

keefaz 11-06-2005 02:47 PM

To know the total size in MB you could also do :
Code:

sfdisk -s -uM /dev/hda


All times are GMT -5. The time now is 06:57 AM.