Skip to main content

Command Palette

Search for a command to run...

[EN] Create, mount, and make persistent a new ext4 filesystem on a new disk

Updated
2 min read
[EN] Create, mount, and make persistent a new ext4 filesystem on a new disk
M
Started my IT career as a Technical Support at an Indonesian web hosting provider, then progressed through various roles as a Linux SysAdmin, Network Engineer, Product Designer, and DevOps Engineer. I moved to a SaaS company and since then I’ve built hands-on experience mainly with AWS and GCP and work daily with popular cloud native tools.

When managing a Linux server, sometimes you need to add new partitions, e.g, directory to store your backup in a separate physical disk, or new place to put your files. In this article, I’ll create a new ext4 partition with the mkfs.ext4 command.

Preparing the partition

Here I have a new block device /dev/sdb of 5GB and will create one partition on it.

# List all block devices
vagrant@vagrant:~$ lsblk 
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0   64G  0 disk 
├─sda1                      8:1    0    1M  0 part 
├─sda2                      8:2    0    2G  0 part /boot
└─sda3                      8:3    0   62G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0   31G  0 lvm  /
sdb                         8:16   0    5G  0 disk 

# Create a new partition
vagrant@vagrant:~$ sudo fdisk /dev/sdb

# Let's see all the block devices again
# There should be /dev/sdb1 if that's the only partition
vagrant@vagrant:~$ lsblk 
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
...
sdb                         8:16   0    5G  0 disk 
└─sdb1                      8:17   0    5G  0 part

Creating the filesystem

Then let’s create an ext4 filesystem on top of that new partition.

vagrant@vagrant:~$ sudo mkfs.ext4 /dev/sdb1 
...
Writing superblocks and filesystem accounting information: done 

# Let's create a /backup directory and try to mount that FS onto that
vagrant@vagrant:~$ sudo mkdir /backup
vagrant@vagrant:~$ sudo mount /dev/sdb1 /backup/

# Check what's inside the partition. It's empty ofc
vagrant@vagrant:~$ ls /backup/
lost+found

# Check the FS type, size, and the mount
vagrant@vagrant:~$ df -Th
Filesystem                        Type    Size  Used Avail Use% Mounted on
...
/dev/sdb1                         ext4    4.9G   24K  4.6G   1% /backup

Making it persistent between reboots

At this point if you reboot the server, the /backup won’t be mounted to the partition automatically. You need to make it persistent by adding a new entry into the /etc/fstab file.

vagrant@vagrant:~$ sudo nano /etc/fstab
...
# Let's mount /dev/sdb1 to /backup using the ext4 filesystem with standard options
# and don’t worry about backing it up or checking it during boot (it's the 0 0 at the end).
# Read more: https://www.redhat.com/en/blog/etc-fstab
/dev/sdb1 /backup ext4 defaults 0 0

Now you can safely reboot without worrying the disk mount is gone.

25 views

More from this blog

Syaifuddin’s Growing Space

10 posts

A personal documentation of what I've learned. Mostly about tech-related stuff.