How to Create and Convert Linux File Systems

Steps to Create and Convert Linux File Systems

The file system refers to a structure or a rule for operating systems to store the files in disk or partition. File systems has some types like extended file systems ext2, ext3 and ext4.

Second Extended File System - ext2

It is developed by Remy Card, which is introduced in 1993. In several Linux distro like RedHat and Debian, it was considered as the first default file system. It is used to overcome the legacy " ext" file system limitation with Flash based storage media like USB Flash drive, SD Card. 16GB &ndash 2TB is its maximum size but journaling feature is not available.

Third Extended File System - ext3

It was integrated in Kernel 2.4.15 with journaling feature, which was introduced in 2001. It provides facility to upgrade from ext2 to ext3 file systems without back up and to restore data. 16GB &ndash 2TB is its maximum size.

Fourth Extended File System - ext4

Here stable code were merged in the Kernel 2.6.28, which was introduced in 2008. It contains ext4 file system to Turn Off journaling feature. It has an additional features like Sub Directory Scalability, Multiblock Allocation, Delayed Allocation and Fast FSCK. Its maximum file size is 16GB to 16TB and as well as Backward compatibility.

To Determine the File System Type

Run the following command as a root user to determine the linux file system type on your machine.

[root@linuxhelp ~]# df -hT | awk ' {print $1,$2,$NF}'  | grep " ^/dev" 
/dev/sda3 xfs /
/dev/sda1 xfs /boot
/dev/sr0 iso9660 x86_64

To Create An Extended File Systems

Execute the mke2fs or mkfs commands after creating the file system using fdisk or parted command. Before this process ensure to replace the device path.

Before executing these commands, take backup of your important data to avoid data-loss.

Here are the partition’ s I have created in /dev/sdb hard drive as follows.

[root@linuxhelp ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b5211

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048     5122047     2048000   82  Linux swap / Solaris
/dev/sda3         5122048    41943039    18410496   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x59d5ea56

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   83  Linux
/dev/sdb3         8390656    12584959     2097152   83  Linux
/dev/sdb4        12584960    20971519     4193280    5  Extended
/dev/sdb5        12587008    16781311     2097152   83  Linux

To Create ext2 File System for the partitions /dev/sdb1 and /dev/sdb2

[root@linuxhelp ~]# mkfs.ext2 /dev/sdb1
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=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

[root@linuxhelp ~]# mkfs.ext2 /dev/sdb2
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=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

To Create ext3 File System for /dev/sdb3 partition

[root@linuxhelp ~]# mkfs.ext3 /dev/sdb3
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=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 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

To Create ext4 File System for /dev/sdb5 partition

[root@linuxhelp ~]# mkfs.ext4 /dev/sdb5
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=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 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

Create directories under /mnt for the mount point.

[root@linuxhelp ~]# mkdir /mnt/disk1
[root@linuxhelp ~]# mkdir /mnt/disk2
[root@linuxhelp ~]# mkdir /mnt/disk3
[root@linuxhelp ~]# mkdir /mnt/disk4

Now mount all partitions.

[root@linuxhelp ~]# mount /dev/sdb1 /mnt/disk1
[root@linuxhelp ~]# mount /dev/sdb2 /mnt/disk2
[root@linuxhelp ~]# mount /dev/sdb3 /mnt/disk3
[root@linuxhelp ~]# mount /dev/sdb5 /mnt/disk4
[root@linuxhelp ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  4.0G   14G  23% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           494M  140K  494M   1% /dev/shm
tmpfs           494M  7.1M  487M   2% /run
tmpfs           494M     0  494M   0% /sys/fs/cgroup
/dev/sda1       497M  116M  382M  24% /boot
/dev/sr0        3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1       2.0G  3.0M  1.9G   1% /mnt/disk1
/dev/sdb2       2.0G  3.0M  1.9G   1% /mnt/disk2
/dev/sdb3       2.0G  3.1M  1.8G   1% /mnt/disk3
/dev/sdb5       2.0G  6.0M  1.8G   1% /mnt/disk4

Run the below command to verify about file system in your machine.

[root@linuxhelp ~]# df -hT | awk ' {print $1,$2,$NF}'  | grep " ^/dev" 
/dev/sda3 xfs /
/dev/sda1 xfs /boot
/dev/sr0 iso9660 x86_64
/dev/sdb1 ext2 /mnt/disk1
/dev/sdb2 ext2 /mnt/disk2
/dev/sdb3 ext3 /mnt/disk3
/dev/sdb5 ext4 /mnt/disk4

Here you can see we have extended partitions ext2, ext3 and ext4.

To Convert An Extended File Systems

It is recommended to unmount all the partitions before converting.

[root@linuxhelp ~]# umount /mnt/disk1
[root@linuxhelp ~]# umount /mnt/disk2
[root@linuxhelp ~]# umount /mnt/disk3
[root@linuxhelp ~]# umount /mnt/disk4

To Convert ext2 to ext3

Use the following command to change an ext2 file system to ext3 with enabling the journal feature.

[root@linuxhelp ~]# tune2fs -j /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)
Creating journal inode: done

To Convert ext2 to ext4

Execute the below command to convert old ext2 to new ext4 file system with recent journaling feature.

[root@linuxhelp ~]# tune2fs -O dir_index,has_journal,uninit_bg /dev/sdb2
tune2fs 1.42.9 (28-Dec-2013)
Creating journal inode: done

To Convert ext3 to ext4

Use the below command to enable the ext4 features on an existing ext3 filesystem,

[root@linuxhelp ~]# tune2fs -O extents,uninit_bg,dir_index /dev/sdb3
tune2fs 1.42.9 (28-Dec-2013)

Run fsck to fix some on-disk structures, which modifies the tune2fs.

[root@linuxhelp ~]# e2fsck -f /dev/sdb1
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 11/131072 files (0.0% non-contiguous), 25405/524288 blocks
[root@linuxhelp ~]# e2fsck -f /dev/sdb2
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb2: 11/131072 files (0.0% non-contiguous), 25405/524288 blocks
[root@linuxhelp ~]# e2fsck -f /dev/sdb3
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb3: 11/131072 files (0.0% non-contiguous), 25405/524288 blocks
[root@linuxhelp ~]# e2fsck -f /dev/sdb5
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb5: 11/131072 files (0.0% non-contiguous), 26156/524288 blocks
Tag : File system
FAQ
Q
How to Convert ext2 to ext3 ?
A
To convert ext2 to ext 3 Use the following command to change an ext2 file system to ext3 with enabling the journal feature.

tune2fs -j /dev/sdb1
Q
How to Create directories under /mnt for the mount point.?
A
To Create directories under /mnt for the mount point use the following commands



mkdir /mnt/disk1

mkdir /mnt/disk2

mkdir /mnt/disk3

mkdir /mnt/disk4

Now mount all partitions.

mount /dev/sdb1 /mnt/disk
mount /dev/sdb2 /mnt/disk2
mount /dev/sdb3 /mnt/disk3
Q
How to Create ext2 File System for the partitions /dev/sdb1 and /dev/sdb2 ?
A
To Create ext2 File System for the partitions /dev/sdb1 and /dev/sdb2 use the following command



mkfs.ext2 /dev/sdb1
mkfs.ext2 /dev/sdb2
Q
How to determine the linux file system type on your machine.?
A
Run the following command as a root user to determine the linux file system type on your machine.

df -hT | awk ' {print $1,$2,$NF}' | grep " ^/dev"
Q
mkfs.ext4 command not working
A
Do you have root user privilege, or else prefix the command with sudo