Method to create a file system in an Linux partition or logical volume

  • 2021-01-18 06:58:23
  • OfStack

preface

Learn to create a file system on your system and mount it permanently or non-permanently.

In computing, the file system controls how data is stored and retrieved, and helps organize files in the storage medium. Without a file system, information would be stored as one big chunk of data, and you wouldn't know where one message ends and the next begins. The file system helps manage all this information by giving names to the files in which the data is stored, and by maintaining a list of files and directories on disk in the file system, along with their start and end locations, total size, and so on.

In Linux, when you create a hard disk partition or logical volume, the next step is usually to format the partition or logical volume to create a file system. This method assumes that you already know how to create a partition or logical volume, and you want to format it to include a file system, and mount it.

Creating a file system

Suppose you add a new hard disk to your system and create a partition called /dev/sda1 on it.

1. To verify that the Linux kernel has found this partition, you can select /proc/partitions from cat as follows:


[root@localhost ~]# cat /proc/partitions
major minor #blocks name

 253   0 10485760 vda
 253   1  8192000 vda1
 11   0  1048575 sr0
 11   1  374 sr1
 8   0 10485760 sda
 8   1 10484736 sda1
 252   0  3145728 dm-0
 252   1  2097152 dm-1
 252   2  1048576 dm-2
 8 16  1048576 sdb

2. Decide what kind of filesystem you want to create, such as ext4, XFS, or something else. Here are some of the options:


[root@localhost ~]# mkfs.<tab><tab>
mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs

3. For the purposes of this exercise, choose ext4. (I like ext4 because it allows you to compress file systems if you need to, which is not easy for ES32en.) Here's how to do it (the output may vary depending on the device name or size) :


[root@localhost ~]# mkfs.ext4 /dev/sda1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=8191 blocks
194688 inodes, 778241 blocks
38912 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=799014912
24 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks:
  32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

4. In the previous step, if you wanted to create a different filesystem, use a different variant of the mkfs command.

Mount the file system

After you have created the file system, you can mount it in your operating system.

1. First, identify the UUID encoding of the new file system. Use the blkid command to list all the recognizable block storage devices and look for sda1 in the output:


[root@localhost ~]# blkid
/dev/vda1: UUID="716e713d-4e91-4186-81fd-c6cfa1b0974d" TYPE="xfs"
/dev/sr1: UUID="2019-03-08-16-17-02-00" LABEL="config-2" TYPE="iso9660"
/dev/sda1: UUID="wow9N8-dX2d-ETN4-zK09-Gr1k-qCVF-eCerbF" TYPE="LVM2_member"
/dev/mapper/test-test1: PTTYPE="dos"
/dev/sda1: UUID="ac96b366-0cdd-4e4c-9493-bb93531be644" TYPE="ext4"
[root@localhost ~]#

Mount /dev/sd1 mount /dev/sd1 mount /dev/sd1


[root@localhost ~]# mkdir /mnt/mount_point_for_dev_sda1
[root@localhost ~]# ls /mnt/
mount_point_for_dev_sda1
[root@localhost ~]# mount -t ext4 /dev/sda1 /mnt/mount_point_for_dev_sda1/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 7.9G 920M 7.0G 12% /
devtmpfs 443M 0 443M 0% /dev
tmpfs 463M 0 463M 0% /dev/shm
tmpfs 463M 30M 434M 7% /run
tmpfs 463M 0 463M 0% /sys/fs/cgroup
tmpfs 93M 0 93M 0% /run/user/0
/dev/sda1 2.9G 9.0M 2.7G 1% /mnt/mount_point_for_dev_sda1
[root@localhost ~]#

The command df-h shows the mount point at which each file system is mounted. Find/dev/sd1. The mount command above uses the device name /dev/sda1. Replace it with the UUID code in the blkid command. Note that /dev/sda1 is mounted in the newly created directory under /mnt.

3. One problem with using the mount command directly from the command line (as in Step 1) is that the mount will not exist after the device is restarted. To mount the filesystem permanently, edit the /etc/fstab file to include your mount information:


UUID=ac96b366-0cdd-4e4c-9493-bb93531be644 /mnt/mount_point_for_dev_sda1/ ext4 defaults 0 0

4. After editing the /etc/fstab file, you can mount all the device files listed in the /etc/fstab file by running the mount-fev_sda1 command. If 1 goes well, you can use df-h to list and view your mounted filesystems:


root@localhost ~]# umount /mnt/mount_point_for_dev_sda1/
[root@localhost ~]# mount -a
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 7.9G 920M 7.0G 12% /
devtmpfs 443M 0 443M 0% /dev
tmpfs 463M 0 463M 0% /dev/shm
tmpfs 463M 30M 434M 7% /run
tmpfs 463M 0 463M 0% /sys/fs/cgroup
tmpfs 93M 0 93M 0% /run/user/0
/dev/sda1 2.9G 9.0M 2.7G 1% /mnt/mount_point_for_dev_sda1

You can also check if the file system is mounted:


[root@localhost ~]# mount | grep ^/dev/sd
/dev/sda1 on /mnt/mount_point_for_dev_sda1 type ext4 (rw,relatime,seclabel,stripe=8191,data=ordered)

Now you know how to create file systems and mount them permanently or non-permanently on your system.

conclusion


Related articles: