LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-31-2005, 05:10 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 48
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
 
Old 10-31-2005, 05:22 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Try the command: sfdisk -s /dev/hda
 
Old 10-31-2005, 05:48 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
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.

Last edited by kushalkoolwal; 10-31-2005 at 06:30 PM.
 
Old 10-31-2005, 06:05 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,488

Rep: Reputation: 67
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.
 
Old 10-31-2005, 06:46 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If you don't care about the remainder, you can perform the division in bash.
mbsize=(( $kbsize/1024 ))
 
Old 10-31-2005, 07:11 PM   #6
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
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
 
Old 10-31-2005, 08:03 PM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
 
Old 11-06-2005, 02:38 PM   #8
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
That;sGreat!!! Thanks you so much.
 
Old 11-06-2005, 02:47 PM   #9
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,614

Rep: Reputation: 136Reputation: 136
To know the total size in MB you could also do :
Code:
sfdisk -s -uM /dev/hda
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to detect IDE devices kushalkoolwal Programming 8 09-28-2005 11:15 AM
How to detect second IDE controller david.skinner Linux - Hardware 1 06-09-2004 04:30 PM
How long should i take for a bios to detect ide? PlatinumRik Linux - Hardware 10 05-30-2003 11:05 AM
Mandrake 7.0 - 8 freeze at ide detect and partiion linuxAv8r Linux - Software 0 06-24-2001 02:47 AM
Problem with RH 7.1 (doesn't detect IDE devices) jcontess Linux - Software 3 06-14-2001 12:43 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration