3

Assume that I have some device mounted to /backups. I'm copying selected files from the system to backup and I would like to reduce the amount of noise the device is making. I know that I can can use hdparm -M to adjust the device "Automatic Acoustic Management (AAM)" setting. However, I don't know how I should get the device (e.g. /dev/sdc) from a given directory (for example, if I had a script that computed the latest backup location as /backups/2017/12/31).

The best I can do is

echo /dev/$(lsblk -no pkname $(findmnt -nvoSOURCE -T "$DIRECTORY"))

but that requires hardcoding /dev/ prefix and assumes that there's only one backing disk. How to make this more stable?

Note that this question is specifically about locating the correct disk(s), not the partition. In case you only need to find the correct partition df or findmnt will be enough.

Also note that in case a directory is mounted on md device, the parent device will be something like md0 which cannot be used with hdparm. In that case there will be more than one underlying disks so in reality this question is about mapping a single file or directory to one or more disks.

  • 2
    df $DIRECTORY will give you the device name (among other information) of the partition. – ridgy Jan 02 '18 at 10:06
  • 1
    I don't see how this is a duplicate of the linked question. This question is the direct opposite of what that one is trying to accomplish. – phemmer Jan 02 '18 at 13:44
  • 1
    @MikkoRantalainen What's wrong with hard-coding /dev? All devices live under /dev. – phemmer Jan 02 '18 at 13:48
  • @Patrick lsblk --help documents PKNAME as internal parent kernel device name. I wouldn't trust that to mean a device name under /dev unless explicitly spelled out in some documentation. – Mikko Rantalainen Jan 02 '18 at 18:11

2 Answers2

2

This kind of script seems to be most stable (works with both single SATA disks and software raids):

 lsblk --list -no type,name --inverse $(findmnt -nvoSOURCE -T "$DIRECTORY") \
 | grep ^disk | awk '{ print $2 }' | sort -u \
 | while read name; do echo "Data on /dev/$name"; done

Of course, replace the echo command with the real action you want to execute for each disk.

Example output for directory on 4 disk MD software raid:

Data on /dev/sda
Data on /dev/sdb
Data on /dev/sdc
Data on /dev/sdd

Example output for directory on 1 disk regular partition:

Data on /dev/sdf
0

A simple way of knowing is the use of df invoked with the (absolute) path of your folder:

$ df $DIRECTORY
file system    1K-blocks used     available used% mounted at
/dev/sda1      303538544 74465700 213630924   26% /root/of/variable/DIRECTORY
  • 1
    Note that this says /dev/sda1 not /dev/sda which is required for hdparm. Also note that not all block devices use logic "partition name is block device plus some number". Also note that findmnt that I mentioned in the question already returns partition name without any need for extra parsing. – Mikko Rantalainen Jan 02 '18 at 18:14