Creating a new filesystem in Linux/Unix

We are going to see  creating a new filesystem in Linux/Unix and especially in RHEL7.

As a first step we should know how many physical disks available in our machine and available free space in those disks.

For that we can use below commands. fdisk command will list all the physical disks with partitions and size of the disk.

[root@server ~]# 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: 0x00060f18

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 17803263 8388608 83 Linux
/dev/sda3 17803264 21997567 2097152 83 Linux
/dev/sda4 21997568 41943039 9972736 5 Extended
/dev/sda5 21999616 30388223 4194304 82 Linux swap / Solaris
/dev/sda6 30390272 41943039 5776384 8e Linux LVM

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 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: 0x1f51ec05

Device Boot Start End Blocks Id System
/dev/sdb1 2048 12584959 6291456 8e Linux LVM
/dev/sdb2 12584960 16777215 2096128 8e Linux LVM

Disk /dev/sdc: 8589 MB, 8589934592 bytes, 16777216 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

df command will list the free space in the mentioned disk

[root@server ~]# df -h /dev/sdc
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.4G 0 1.4G 0% /dev

We are going to use /dev/sdc disk, as its having free space and none FS created in this.

Use the below command to do changes and create filesystem in this disk

[root@server ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x52240e5b.

Command (m for help):

Once you executed fdisk command with disk name(/dev/sdc), you will get in to the fdisk tool to do changes and it will show like above. type ‘m’ and press enter to get to know the keys which will be helpful in this tool

[root@server ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x52240e5b.

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
 g create a new empty GPT partition table
 G create an IRIX (SGI) partition table
 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)

Now we are going to create a filesystem/partition. So for that type  to create new filesystem. Once pressed enter you will get prompt to select the partition type. Select Primary.

Than provide the partition number or else just press enter to take default value.

First Sector:  Provide the sector value, from there only filesystem/partition will start.

Last Sector:  Finally we have to provide the last sector value where the filesystem/partition will get end or in number value with G(GB),M(MB),K(KB). For best practise we can use the number value with like this +4G and than press enter to complete it.

Command (m for help): n
Partition type:
 p primary (0 primary, 0 extended, 4 free)
 e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-16777215, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

and now type  and press enter to save the changes and quit from the fdisk.

We created partition in physical disk. Use partprobe command to affect the disk changes without restarting the machine in this session.(Note: for hardware changes system restart is must. So here specially we are using this command to avoid machine reboot)

[root@server ~]# partprobe
[root@server ~]# fdisk -l /dev/sdc

Disk /dev/sdc: 8589 MB, 8589934592 bytes, 16777216 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: 0x61352143

Device Boot Start End Blocks Id System
/dev/sdc1 2048 8390655 4194304 83 Linux

We have to format the partition using anyone of the filesystem type(XFS, EXT4,ETX3…).

Note: if you are going to use this partition in LVM, no need to follow the further steps.

As we are using RHEL7, going to use default one which XFS to format.

Command to format the partition using XFS filesystem.

[root@server ~]# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1 isize=512 agcount=4, agsize=262144 blks
 = sectsz=512 attr=2, projid32bit=1
 = crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1048576, imaxpct=25
 = sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
 = sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

After formating need to create a directory to mount this and the directory will act as mount point.

[root@server ~]# mkdir /app

Finally filesystem should be mounted under the created directory to make use it.

We ahev two type in mounting filesystem.

Temporary mount: Will mount the filesystem. But, after restarting mount will be lost. Again we need to mount it.

Permanent mount:  Need to make entry in /etc/fstab file and this mount will not lost even after restarting the machine as well.

Temporary mount:

[root@server ~]# mount /dev/sdc1 /app

Permanent mount:

Open the /etc/fstab file using vi editor and provide the below entry. Save and exit from the file using :wq.

[root@server ~]# vi /etc/fstab

/dev/sdc1 /app xfs defaults 0 0

Use mount command with grep to check the filesystem whether its listing or not.

[root@server ~]# mount | grep /app
/dev/sdc1 on /app type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

Successfully we created a filesystem now.

 

 

13 Replies to “Creating a new filesystem in Linux/Unix”

    1. i am also new to wordpress. So, dont have much idea. But we can take backup when its finely working. That backup will help us to restore.

  1. I just want to say I am all new to blogging and site-building and honestly liked you’re web blog. More than likely I’m going to bookmark your blog . You absolutely come with really good articles and reviews. Thank you for sharing your website.

  2. Hey there would you mind stating which blog platform you’re using? I’m planning to start my own blog soon but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S Sorry for being off-topic but I had to ask!

    1. I am using GoDaddy for hosting and WordPress to build my site from the scratch and it’s having its own style and customize too. But, I’m using the existing WordPress style.

Leave a Reply

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