0

I'm crafting up a shell script (to be called by cron) that runs smartctl on the booted disk on a weekly basis. Is there a fairly universal way to determine what the boot block device is (IE - /dev/sda, /dev/hdb, etc)? The expected install would be standard, I think I could get away with using grep " / " on /etc/fstab, I'm just wondering if there's a more graceful way.

This script will specifically be deployed on Fedora and possibly Ubuntu boxes.

Chris Weiss
  • 25
  • 1
  • 6
  • 1
    Are you really looking for the boot device, or are you looking for the root device? If you are looking for / in fstab, and your boot device is different from your root device then you may not get the results you want. – Zoredache Dec 08 '11 at 19:13
  • That is a valid point. For my specific needs, it will always be the same device. – Chris Weiss Dec 08 '11 at 20:17
  • What boot-loader are you using on these machines? – Zoredache Dec 08 '11 at 20:24
  • Grub currently, but the best solution should be future-proofed against alternatives. – Chris Weiss Dec 08 '11 at 20:55

4 Answers4

4

df -P / | tail -n 1 | awk '/.*/ { print $1 }'

Will return the root FS block device. Not necessarily the boot device though.

Better question might be why you're not scanning SMART on all the disks in the server?

Chris S
  • 77,945
  • 11
  • 124
  • 216
  • In my specific case, this is a script that will be deployed to hundreds of single-disk kiosk machines with either a SATA or PATA disk (so 99% of them would be /dev/sda or /dev/hda, I just want to make sure I don't miss the 1% that somehow ended up being /dev/sdb or something). Your solution almost works. Unfortunately, what I need is the boot device (That returns /dev/sda3, I just need /dev/sda). – Chris Weiss Dec 08 '11 at 20:07
1

for /boot:

readlink -f /dev/block/$(mountpoint -d /boot)

for rootfs:

readlink -f /dev/block/$(mountpoint -d /)

Explanation: 1) mountpoint -d - gives major:minor numbers of device mounted at

2) /dev/block folder - contains symlinks os all block devices by device numbers pointing back to regular device file in /dev

3) readlink -f - reconstruct device name from sym link

Works on all modern Linuxes

0
cat /proc/mounts |grep /boot |awk '{print $1}'

Will return something like /dev/sda1 if your /boot is not on the root device.
Combine this with the answer from Chris S.

Bart
  • 203
  • 1
  • 2
0

You've probably long since moved on from this task, but I was trying to do something similar and figured I'd look into the missing piece of your puzzle, namely to get the drive designation (e.g. /dev/sda) instead of the partition designation (e.g. /dev/sda1).

I started off with Chris S's excellent answer (which also solved the task I was seeking to accomplish), and then just used good old fashioned bash string manipulation to trim the number off the end (leaving you with the drive vs the partition):

# first, save the output (e.g. /dev/sda1) to a variable
rootpart=$(df -P / | tail -n 1 | awk '/.*/ { print $1 }')

# now you can trim the number off:
echo ${rootpart//[[:digit:]]/}

# if ${rootpart} was "/dev/sda1", output would be "/dev/sda"
Doktor J
  • 1,107
  • 1
  • 10
  • 20