Disks & Volumes in Linux (FOR 240) - Chromosom3/TechNotes GitHub Wiki
Commands:
lsblk
:
- Will list drives in linux. Typically want to ignore loop drives. Those are typically virtual loop backs.
fdisk
:
- Needs to be run as sudo. Can use -l to list all drives. Again loops can be ignored.
Drive notation:
Linux notates drives with /dev/sdX
, /dev/hdX
, or /dev/nvmeX
where X is the drive letter. Each drive will have its own letter. After the letter for the drive, there will be a number for the partition. For example /dev/sda1
. In Windows, the drives are notated with \\.\PHYSICALDRIVE#
where the hashtag represents the drive number.
Mounted Volumes:
When mounting volumes in Windows the volume will get a corresponding letter for the mount point. For example, the Windows OS drive is mounted at C:\
. Other volumes will be mounted the same way just with unique letters. Each volume will have its own unique letter. On Linux, the partitions are listed as numbers after the drive path. For example, if the drive /dev/sdc
has three partitions they would be /dev/sdc1
, /dev/sdc2
, and /dev/sdc3
.
Partitioning a Disk:
When setting up a disk in Linux you can use the lsblk
command to list all the disks and volumes as shown in the image below.
In addition to using lsblk
you can also use fdisk -l
. They will provide you with similar results, I just prefer lsblk
.
The new disk will not have any partitions yet. You will need to add them with fdisk
. Running fdisk /dev/sdb
(where sdb is your new drive) will start the process for creating partitions. Next use o
to create a new DOS partition table. Use n
to create a new partition. Use the default options or specify the size if you are configuring multiple partitions. When you are finished configuring the partitions use w
to write the changes to the disk.
Next, you will need to format the partition with a file system. You can use the mkfs
command for this. If you type mkfs.
into your terminal then double tap tab you will see a list of all the supported file systems you can create (you can install packages for more file systems). Once you select the file system you want to use you can run the mkfs
command with the file system and path to the partition. For example, mkfs.ext3 /de/sdb1
.
Mounting a Partition
Now that the partition has a file system we can mount it to put data onto it. Use the mount
command to mount the partition to a mount point like so mount /dev/sdb1 mountpoint/
. If you want to see the mounts you already set up you can run mount
with no additional options. When you mount the new partition you may need to adjust permissions so you have access. Using chown
, chgrp
, or chmod
you can adjust the file permissions on the mountpoint. For example, you can run chown class -R mountpoint/
where class
is your user and mountpoint/
is the mountpoint. The -R
flag applies this recursively (to all files and directories within the mount point). If you wanted to mount the partition as read-only you would need to include -o ro
or -r
when running mount
. When you are ready to unmount a partition simply run unmount
and then either the partition or the mount point. For example, umount mountpoint/
.
Mounting a Disk
Mounting a disk is a little more difficult then a partition. We will not be able to use the mount
command by itself for this. Using losetup
will allow us to mount the whole disk image and then we can use mount
to mount the partitions. First, run losetup -f -P $DD_Image
. This will have losetup
find the first available loop device and mount the disk image there. You can add -r
to mount the disk image as read only, only do this if you are mounting the partitions as read-only as well. Once the disk image is mounted you can run losetup -l
to see what loop device it is mounted to. From there you can run mount /dev/$LoopBack $MountPoint
to mount the specific partition(s) you want to be mounted. Once you are done using the disk image run the umount
command mentioned earlier and unmount all the partitions from the disk. Then run losetup -d /dev/$LoopBack
where $LoopBack
is the loop device that the disk image is mounted to.