how to mount additional storage device to a directory in ubuntu os?
Objective of this article
Lets say you have a virtual machine with 20gb of disk space assigned to root(/), but this space is not enough for you to work with. You are unable to increase the disk space for root(/), but you are able to add additional disk to the virtual machine. Just by adding an additional disk to the virtual machine, you won’t be able to consume it.
Objective of this article is to show how to use this disk for user consumption, assuming that you have already added the disk to the virtual machine.
This article is written from a software developer point of view and not from a Linux administrator point of view
Explanation
Below details are verified in
Ubuntu 18.04 (64 bit)
Below image (screenshot1) shows the result of lsblk
command in-order to view the available disks in the operating system.
i have added the additional disk to the virtual machine. The additional disk(we will use the term device
instead of disk
) is named sdb
of size 38.2GB, i actually added 40GB, but only 38.2GB
is showing up here. As you can see, there is no MOUNTPOINT
for sdb. sdb indicates the device name or disk name
Before mounting the device sdb
to a MOUNTPOINT
, the device has to be formatted with any of the available filesystem like ext4
, NTFS
,XFS …etc
. Read more about file systems here . As ubuntu uses ext4
filesystem by default, we will format the device with ext4
filesystem
The device sdb
which was added should be formatted with ext4.
sudo mkfs.ext4 /dev/sdb
Suddenly there /dev
infront of sdb
, what is /dev
? /dev/sdb
is the device path and sdb
is the device name
The device /dev/sdb
has to be mounted on a directory for user consumption.
Create a new directory sudo mkdir /mydivdata
Execute below command to mount /dev/sdb
device to /mydivdata
directory
sudo mount -t ext4 /dev/sdb /mydivdata
Execute the command lsblk
again
On above screenshot2 , you are able to see that sdb
device is mapped /mydivdata
directory.
At this point, we will be able to store any files or install any tools in the directory /mydivdata
, this directory will be located in the device /dev/sdb
The mapping between /dev/sdb
device and/mydivdata
directory will disappear after reboot. If you want this mapping to persist after reboot, then add below line in /etc/fstab
file/dev/sdb /mydivdata ext4 defaults 0 0
Please let me know if you have any concerns in this article