How to format and mount a blank disk
When you add a new blank disk to your server, you will probably need to do some steps before you can use it.
Table of Contents
- Creating partition
- Formatting and labelling the partition
- Mount the partition
- Mounting partition automatically when system boots up
Creating partition
- List all available disks and its partitions with
lsblk
, look for your blank disk and note its name (in this case,vdb
).$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 55.4M 1 loop /snap/core18/1944 loop1 7:1 0 31.1M 1 loop /snap/snapd/10707 loop2 7:2 0 69.9M 1 loop /snap/lxd/19188 loop3 7:3 0 32.3M 1 loop /snap/snapd/12704 loop4 7:4 0 55.4M 1 loop /snap/core18/2128 sr0 11:0 1 1024M 0 rom vda 252:0 0 20G 0 disk ├─vda1 252:1 0 1M 0 part ├─vda2 252:2 0 1G 0 part /boot └─vda3 252:3 0 19G 0 part └─ubuntu--vg-ubuntu--lv 253:0 0 19G 0 lvm / vdb 252:16 0 10G 0 disk
- We are going to use
fdisk
to create a new partition onvdb
. Typefdisk /dev/
+ your disk name.sudo fdisk /dev/vdb
fdisk
allows you to do several tasks (you can typem
to see every option) but now you are only going to create a new partition. Typen
and press Enter.- If you want to create one partition that fills up entire disk, simply press Enter to all prompts.
Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p):
Using default response p. Partition number (1-4, default 1): First sector (2048-20971519, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519):
Created a new partition 1 of type 'Linux' and of size 10 GiB.
- Type
w
and press Enter to save the changes. - If you type
lsblk
again you will notice there is a new partition on your disk (in this case,vdb1
). You will need this name for the next step.
Formatting and labelling the partition
- Format the new partition to
ext4
(default format for Linux systems) by typing:sudo mkfs.ext4 /dev/vdb1
- Replace partition name for yours.
- You can label the partition with
e2label
:sudo e2label /dev/vdb1 "New partition"
- For NTFS partitions, use
ntfslabel
. - For FAT (MS-DOS) partitions, use
fatlabel
.
- For NTFS partitions, use
Mount the partition
- Now you can mount your formatted partition. First, create a folder where you will mount the partition. Mount points are usually created under
/mnt
or/media
folder, but you can use any other path.sudo mkdir /mnt/mydisk
- Finally, mount the disk partition.
sudo mount /dev/vdb1 /mnt/mydisk
- You can mount a filesystem as “read-only” by using the command parameter
-o ro
.
- You can mount a filesystem as “read-only” by using the command parameter
- When you want to unmount, simply type
sudo umount
+ mount point or partition path.sudo umount /mnt/mydisk #or sudo umount /dev/vdb1
Mounting partition automatically when system boots up
- If you want your disk partition automatically mounts when system boots up, you will need to edit
/etc/fstab
file. First, note filesystem UUID by typing:$ sudo blkid /dev/vdb1: UUID="14bf9ffc-708c-4cf8-b7b2-7a5fb205a6d0" TYPE="ext4" PARTUUID="0a77ac42-01"
- You can also type
lsblk -o name,uuid
.
- You can also type
- Open
/etc/fstab
with your preferred text editor.sudo vim /etc/fstab
- At the end of the file, type this (using your UUID and mount point):
UUID=14bf9ffc-708c-4cf8-b7b2-7a5fb205a6d0 /mnt/mydisk ext4 defaults 0 0
- Save changes and close the file.
- You can check if everything works as expected, typing
sudo mount -a
(first unmount the partition if it’s mounted) and checking if partition is mounted. - Next time you boot up your system, disk partition will be automatically mounted.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: