Is there a command to see where a disk is mounted?
Is there a simple command that takes a disk’s device node as input, and tells me where (and whether) that disk is mounted? Is it possible to get the mount point by itself, so I can pass it to another command? I’m working on a Debian Squeeze live system with a minimal install (I can install extra packages if need be).
6 Answers 6
On Linux, you can now use the findmnt command from util-linux (since version 2.18):
$ findmnt -S /dev/VG_SC/home TARGET SOURCE FSTYPE OPTIONS /home /dev/mapper/VG_SC-home ext4 rw,relatime,errors=remount-ro,data=ordered
Or lsblk (also from util-linux , since 2.19):
$ lsblk /dev/VG_SC/home NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT VG_SC-home 254:2 0 200G 0 lvm /home
That one is also useful to find all the file system mounted under a specific device (disk or partition. ):
$ lsblk /dev/sda2 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda2 8:2 0 59.5G 0 part ├─linux-debian64 (dm-1) 252:1 0 15G 0 lvm └─linux-mint (dm-2) 252:2 0 15G 0 lvm /
To get the mountpoint only:
$ findmnt -nr -o target -S /dev/storage/home /home $ lsblk -o MOUNTPOINT -nr /dev/storage/home /home
Above findmnt does return with a failure exit status if the device is not mounted, not lsblk .
if mountpoint=$(findmnt -nr -o target -S "$device"); then printf '"%s" is mounted on "%s"\n' "$device" "$mountpoint" else printf '"%s" does not appear to be directly mounted\n' "$device" fi
Under Linux, you can get mount point information directly from the kernel in /proc/mounts . The mount program records similar information in /etc/mtab . The paths and options may be different, as /etc/mtab represents what mount passed to the kernel whereas /proc/mounts shows the data as seen inside the kernel. /proc/mounts is always up-to-date whereas /etc/mtab might not be if /etc was read-only at some point that wasn’t expected by the boot scripts. The format is similar to /etc/fstab .
In both files, the first whitespace-separated field contains the device path and the second field contains the mount point.
awk -v needle="$device_path" '$1==needle ' /proc/mounts
grep "^$device_path " /proc/mounts | cut -d ' ' -f 2
There are a number of edge cases where you might not get what you expect. If the device was mounted via a different path in /dev that designates the same device, you won’t notice it this way. In /proc/mounts , bind mounts are indistinguishable from the original. There may be more than one match if a mount point shadows another (this is unusual).
In /proc/self or /proc/$pid , there is a per-process mounts file that mimics the global file. The mount information may vary between processes, for example due to chroot . There is an additional file called mountinfo that has a different format and includes more information, in particular the device major and minor numbers. From the documentation:
36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue (1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11) (1) mount ID: unique identifier of the mount (may be reused after umount) (2) parent ID: ID of parent (or of self for the top of the mount tree) (3) major:minor: value of st_dev for files on filesystem (4) root: root of the mount within the filesystem (5) mount point: mount point relative to the process's root (6) mount options: per mount options (7) optional fields: zero or more fields of the form "tag[:value]" (8) separator: marks the end of the optional fields (9) filesystem type: name of filesystem of the form "type[.subtype]" (10) mount source: filesystem specific information or "none" (11) super options: per super block options
So if you’re looking for a device by number, you can do it like this:
awk -v dev="$major:minor" '$3==dev ' awk -v dev="$(stat -L -c %t:%T /dev/block/something)" '$3==dev '
Mounting a disk in Linux
In this tutorial we will show you how to create a partition and a filesystem. After we will mount the disk and make sure the changes are kept when a reboot occurs.
Before we start mounting a disk, we will make sure we have the correct disk. In the example below we used the command lsblk to see which disks are in the system and where they are used.
root@worldstream:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 119.2G 0 disk |-sda1 8:1 0 953M 0 part /boot |-sda2 8:2 0 110.6G 0 part / |-sda3 8:3 0 1K 0 part |-sda5 8:5 0 1M 0 part |-sda6 8:6 0 243M 0 part /boot/efi `-sda7 8:7 0 7.5G 0 part [SWAP] sdb 8:16 0 10.9T 0 disk |
If we look at the output, we see that /dev/sda contains several partitions, most notably the root partition. Because of the root partition we know this is where the operating system is installed. The disk at /dev/sdb has no mountpoint or partitions, so we can partition and mount this.
Note: If you see that there already is a partition available on the disk and no mountpoint you can skip to: ‘Creating a folder and mounting the disk’.
Creating a filesystem and partition
There are several programs to create a partition, but we will use the program parted to create a filesystem that spans over the entire disk. In our previous paragraph we found that /dev/sdb was unpartitioned. We will start the program with the disk we want to partition.
Then we’ll create a gpt filesystem with this command:
mklabel gpt Do you want to continue: Yes |
Next we will create one partition so we can assign the full size of the disk to a folder.
Once this is created use q to exit the parted shell. We will have to perform one last step, creating a filesystem. In the below example we use ext4, but you can substitute this part with xfs or ext3 if preferred:
In the above example we created /dev/sdb1, since this is the partition we created in the above example. If you have created a partition on another device path, adjust /dev/sdb1 to the partition where you want to create a filesystem on. Below is a summary that explains what we have done so far:
root@worldstream:~# parted /dev/sdb GNU Parted 3.2 Using /dev/sdb Welcome to GNU Parted! Type ‘help’ to view a list of commands. (parted) mklabel gpt (parted) mkpart 1 ext4 0% 100% (parted) quit Information: You may need to update /etc/fstab. mkfs.ext4 /dev/sdb1 |
Creating a folder and mounting the disk
When you mount a filesystem, make sure this folder does not exist already! You can make the system inoperable or temporarily lose access to files. We will create a folder called /home2.
When this is created we can mount the created filesystem there.
To summarize we have included the output:
root@worldstream:~# mkdir /home2 root@worldstream:~# mount /dev/sdb1 /home2 root@worldstream:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 119.2G 0 disk |-sda1 8:1 0 953M 0 part /boot |-sda2 8:2 0 110.6G 0 part / |-sda3 8:3 0 1K 0 part |-sda5 8:5 0 1M 0 part |-sda6 8:6 0 243M 0 part /boot/efi `-sda7 8:7 0 7.5G 0 part [SWAP] sdb 8:16 0 10.9T 0 disk `-sdb1 8:17 0 10.9T 0 part /home2 |
Reviewing and filesystem configuration file
When you have mounted the disk, you are able to review by running df -h at the terminal.
In order to make sure the filesystem will be kept after a reboot we will add the new filesystem at /etc/fstab. This file tells your machine what filesystem to mount and where on startup. We will need the UUID of the filesystem in order to proceed. If you type ‘blkid’ in the terminal you will get the following output:
root@worldstream:~# blkid /dev/sda1: UUID=”4a18d6e5-54d3-4005-9a86-a01954f863c4″ TYPE=”ext4″ PARTUUID=”a3122a02-01″ /dev/sda2: UUID=”66933e94-5f5a-48c8-9fd8-5c1684792e5a” TYPE=”ext4″ PARTUUID=”a3122a02-02″ /dev/sda6: UUID=”9EB6-2422″ TYPE=”vfat” PARTUUID=”a3122a02-06″ /dev/sda7: UUID=”41c56b66-49f5-48e0-8a26-2578262a3a96″ TYPE=”swap” PARTUUID=”a3122a02-07″ /dev/sdd: PTUUID=”49dc9ef9-ac7b-4add-8f51-718a3f6620a7″ PTTYPE=”gpt” /dev/sdc: PTUUID=”0fa77077-d52a-40fe-86b4-858699b26a07″ PTTYPE=”gpt” /dev/sda5: PARTUUID=”a3122a02-05″ /dev/sdb1: UUID=”cef3dd7c-5ba6-4ad6-ac38-b496413e33c5″ TYPE=”ext4″ PARTLABEL=”1″ PARTUUID=”b747fe1e-48ce-4217-bc33-d4301dd66270″ |
As you can see the filesystem from our example (/dev/sdb1 starts with UUID=”cef3dd7c-5ba6-4ad6-ac38-b496413e33c5″. This is the UUID. Now do the following:
You can follow the logic of the other mounted filesystems as below. Note that we have used the UUID from our last paragraph and removed the double quotation marks.
# /etc/fstab: static file system information. # # Use ‘blkid’ to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # # / was on /dev/sda2 during installation UUID=66933e94-5f5a-48c8-9fd8-5c1684792e5a / ext4 errors=remount-ro 0 1 # /boot was on /dev/sda1 during installation UUID=4a18d6e5-54d3-4005-9a86-a01954f863c4 /boot ext4 defaults 0 2 # /boot/efi was on /dev/sda6 during installation UUID=9EB6-2422 /boot/efi vfat umask=0077 0 1 # swap was on /dev/sda7 during installation UUID=41c56b66-49f5-48e0-8a26-2578262a3a96 none swap sw 0 0 UUID=cef3dd7c-5ba6-4ad6-ac38-b496413e33c5 /home2 ext4 defaults 0 0 |
When you exit this editor you have succesfully mounted the disk.
Note that this is an example set-up, but many formats are possible. If you require any assistance feel free to send us an email here.
Learn more about our services: worldstream.com
In terminal, what is the path of my HDDs?
How can I do that on Ubuntu? How can I navigate to files in a HDD using a terminal? /dev and /dev/disk are not making sense to me.
2 Answers 2
On OS X those would be the mount points. Under Ubuntu you would want to look at /media/USERNAME/ for those. Though if the volume has not been mounted you could look at /dev/disk/by-label .
If the drives are paritioned and formatted, then you can navigate to them by doing something like cd /media/USERNAME/LABEL where USERNAME is your user name and LABEL is the label for the partition.
If your drives haven’t been formatted, then you wouldn’t see them in either location and you would instead want to look for /dev/sd[a-z] . That is, you’d have an entry like /dev/sda and /dev/sdb . If they are partitioned, then you should also see /dev/sda5 and /dev/sdb6 (for example, /dev/sda1 .) But you’d have to at least format them before you can mount them and navigate to them.
If you haven’t mounted the volumes, you can do it through nautilus by just clicking on the volume name. Or from the terminal you can use udisksctl . For example, I have a drive that is partitioned with just one file system. The label of the drive is «d_extra». I can view it in /dev/disk/by-label :
$ ls -l /dev/disk/by-label lrwxrwxrwx 1 root root 10 Jul 17 15:34 d_extra -> ../../sdb1
Then, once I know the name, I can mount it via udisksctl like so:
$ udisksctl mount -o noatime -b /dev/disk/by-label/d_extra
Then I can cd to it and view files:
$ cd /media/stephen/d_extra $ ls