View the disks on your machine

fdisk is a tool used on Linux to manipulate disk partition tables. Using the fdisk -l command, you can list all the disks and their partitions recognized by the system.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Disk /dev/sda: 599.1 GB, 599051206656 bytes, 1170021888 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk label type: # masked
Disk identifier: # masked

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   204802047   102400000   83  Linux
/dev/sda2       204802048   213190655     4194304   82  Linux swap / Solaris
/dev/sda3   *   213190656  1170020351   478414848   83  Linux

Disk /dev/sdb: 6118.5 GB, 6118546079744 bytes, 11950285312 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes

The sd? in the /dev directory is the physical disk. For example, sda is the first disk read by the system, and sdb is the second disk. In older kernels, the physical disk may be /dev/hd? (IDE) or /dev/sd? (SCSI) since there is no ATA support.

/dev/sda? is the partition on the physical disk sda. /dev/sda1 is the first partition on that disk.

Here the disk /dev/sdb has not been mounted properly and it is the one that holds our data.

Creating a mount point

Before you can mount a disk, you must first create a mount point for it. A mount point is, in short, a directory that must already exist before you can mount it with the mount command. After a successful mount, you can access the directory to access the contents of the corresponding disk. If the contents of the mount point existed before the mount, they will be temporarily invisible after the mount, and will become visible again when the mount is done.

For example, if I want to mount a disk on /mnt/data, I need to create such an empty directory.

1
mkdir -p /mnt/data

Determining the disk file system type

Before mounting the disk, you also need to determine the file system type of the disk (partition). Otherwise, selecting the wrong file system type when mounting may cause a series of strange phenomena. To do this, we need to execute the parted -l command.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# parted -l
Model: # masked
Disk /dev/sda: 599GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size    Type     File system     Flags
 1      1049kB  105GB  105GB   primary  xfs
 2      105GB   109GB  4295MB  primary  linux-swap(v1)
 3      109GB   599GB  490GB   primary  xfs             boot


Model: # masked
Disk /dev/sdb: 6119GB
Sector size (logical/physical): 512B/4096B
Partition Table: loop
Disk Flags:

Number  Start  End     Size    File system  Flags
 1      0.00B  6119GB  6119GB  ext4

This shows that the target disk /dev/sdb has the file system type ext4.

Mount the disk

At this point, we are able to mount the disk.

1
mount -t ext4 /dev/sdb /mnt/data

Here -t ext4 means that the target disk (partition) has the file system type ext4, /dev/sdb is the disk (partition) to be mounted, and /mnt/data is the target mount point.

Unmounting a disk

The opposite command to mount is umount. Note that it is umount and not unmount - there is no n.

When unmounting, the argument given to umount can be either the mount point or the name of the disk (partition). In this case, the following two operations are equivalent.

1
2
umount /dev/sdb
umount /mnt/data

If the disk you want to mount is being read or written by another process, Linux will prompt device is busy. In this case, you can run the umount -l command and let Linux unmount the disk when no other processes are reading or writing to it. Alternatively, you can use the ps command to see which processes are using the disk and then dispose of them properly before unmounting.