1

I would like to check if my hard drive is active or not and set a variable or boolean to use later in the program. I have hdparm installed and working properly to spin down the drive after a set period of time. The drive is on /dev/sda2 and automounts at boot.

I read about popen() but am still not great with parsing outputs in c. Is there another way to do this?

Alternatively checking if it is in standby mode will work also.

Ethan Morris
  • 95
  • 1
  • 1
  • 8
  • When you say "active" do you mean "not in a power-saving mode?" Or do you mean "servicing read or write requests?" – John Zwinck Jan 28 '17 at 02:47
  • I mean servicing request. Alternatively I could set the boolean if it is in standby mode if that helps. – Ethan Morris Jan 28 '17 at 02:50
  • @EthanMorris Pick one -- the answers will be significantly different. Also, why do you need to know? –  Jan 28 '17 at 03:08
  • Trying to learn more about c and programming my banana pro with the wiringpi library. Id like to check this condition and light an led as a hdd status indicator through the gpio pins. – Ethan Morris Jan 28 '17 at 03:42

1 Answers1

1

You can just read from "/sys/block/sda2/stat". For documentation see https://www.kernel.org/doc/Documentation/block/stat.txt. You program should work like this:

  1. Open "/sys/block/sda2/stat" with fopen
  2. Use sscanf to parse field 3 (read sectors) and field 7 (write sectors).
  3. fclose the file
  4. wait for few seconds
  5. Repeat steps 1-3. If you got different numbers, the disk was active during that time.
theamk
  • 1,420
  • 7
  • 14
  • I don't have sda2 at all but after checking sda/stat and spinning up the drive I do see a difference. All of the read values went up but remain unchanged after the drive went back into standby. – Ethan Morris Jan 28 '17 at 03:59