Create an ext4 File System in RedHat Linux

To create a an ext3 or ext4 file system in Linux there are a few steps involved. The below is a step by step guide how to create, format and mount an ext 4 disk in Linux.

Create an ext4 File System:
1. Partition the disk with fdisk and specify the type
# fdisk /dev/sdb (n,p,1,1,+500MB,t,1,L,83,w)
2. Format the disk with mkfs
# mkfs -t ext4 /dev/sdb1
3. Add a label to the disk with e2label ‘/mnt/data1’ is LABEL
# e2label /dev/sdb1 /mnt/data1
4. View the label
# blkid or tune2fs -l /dev/sdb1 | grep volume
5. Add the disk to /etc/fstab so it mounts on startup
LABEL=/mnt/data1 /mnt/data1 ext4 defaults 1 2
6. Reload /etc/fstab file so it mounts the disk
# mount -a or mount /dev/sdb1 /mnt/data1

Partition the disk

\\Identify your disk, in this example sdb is the disk and after i
\\create partitions sdb1 will be partiton1 and sdb2 will be partition2

[root@localhost ~]# fdisk -l

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x54352f5a

\\Create a partiton on the sdb disk (n,p,1,1,+500MB,t,1,L,83,w)
\\If you want to create one partition that spans across the entire
\\disk press enter to accept the defaults instead of +500

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-130, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +500MB
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 83
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Format the disk

-Now that we created the ‘/dev/sdb1’ partition we need to format it
-Type the following to format: mkfs -t ext4 /dev/sdb1

\\Now the partition sdb1 is visible in fdisk

[root@localhost ~]# fdisk -l

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x54352f5a

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          62      497983+  83  Linux

\\Now we need to format the partition with ext4

[root@localhost ~]# /sbin/mkfs -t ext4 /dev/sdb1

mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
124928 inodes, 497980 blocks
24899 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
61 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]#

Make a directory to mount the disk

[root@localhost ~]# mkdir /mnt/data1

Add a label to the disk

//Adding a label makes it easier to identify when addint to fstab
//Checking if a label exists:
[root@localhost ~]# /sbin/tune2fs -l /dev/sdb1  | grep volume
Filesystem volume name:   <none>

//Creating the label:
[root@localhost ~]# e2label /dev/sdb1 /mnt/data1

//Checking again if the label exists:
[root@localhost ~]# /sbin/tune2fs -l /dev/sdb1  | grep volume
Filesystem volume name:   /mnt/data1

Add the disk to /ect/fstab

//Add the disk to /etc/fstab so that it gets mounted on start up.
//blkid provides detail about block devices we can see the LABEL

[root@localhost ~]# blkid
/dev/sdb1: LABEL="/mnt/data1" UUID="3c4b6730-f337-47c2-b165-a02bcb58ae78" TYPE="ext4"

//Add the below line to /etc/fstab

[root@localhost ~]# echo "LABEL=/mnt/data1 /mnt/data1 ext4 defaults 1 2" >> /etc/fstab

//Defining /etc/fstab options:
//defaults - includes many options, mounts file system rw,
//permissions, etc.
//1 - Ensures that data is backed up before reboot/shutdown
//(also called dumping).  This value is usually 1.  
//A value of 0 would be for /tmp disk or something not critical
// like a disk.
//2 - Defines order in which file system is checked.  
//For root file system value should be 1, everything else 
//should be 2.  If you have CD-rom value of 0 to skip checking.
//Instead of using a LABEL="/mnt/data1" in fstab file you can also use
//UUID="3c4b6730-f337-47c2-b165-a02bcb58ae78" in it's place.

Mount the disk

\\Mount -a command reloads the /etc/fstab file and mounts
\\the partition, it is a good idea to run this after editing
\\the /etc/fstab file to verify that it can mount the partition.
\\When the server reboots this file gets loaded automatically.
\\To mount the disk manually instead of using mount -a you can 
\\use this command "mount /dev/sdb1 /mnt/data1"
\\After mounting it below i can cd into the 
\\mount directory /mnt/data1 and i can see the lost+found folder,
\\i can also create files here so that tells me all is working
\\as expected.

[root@localhost ~]# mount -a
[root@localhost ~]# cd /mnt/data1
[root@localhost data1]# ls
[root@localhost data1]# touch testing

\\Mount -l will list mounted partitions and disks
[root@localhost data1]# mount -l
/dev/sdb1 on /mnt/data1 type ext4 (rw) [/mnt/data1]
This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *