LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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: 49
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: 61
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: 49
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: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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: 49
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: 61
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: 49
That;sGreat!!! Thanks you so much.
 
Old 11-06-2005, 02:47 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:49 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration