Managing Partitions and File Systems Using fdisk

In this tutorial, I am going to explaining how to create disk partitions, making the file systems, deleting a partition, Mounting the Filesystems and some troubleshooting steps while unmount the file systems.

Partition Means:

HDDs are the main storage device on most computers. A partition is a logical division on a hard disk drive (HDD). It is a contiguous set of blocks on a drive that are treated as an independent disk.  A partition index is an index that relates sections of the  hard drive to partitions.

Frame of Disk Partition(On BIOS-based machines)

  • On the disk where operating system installed, will have the first partition as MBR(Master Boot Record). MBR contains two important utilities i.e IPL (Intial Program Loader) and PTI (Partition Table Information).
  • IPL contains the boot loader, So it is responsible for booting the operating system.
  • The PTI is the information about the number of partitions on the disk, sizes of the partition and types of partitions.
  • On Centos7, the default boot loader is grub2.

I already covered that the how to create GPT (Unified Extensible Firmware Interface (UEFI) machines) disk partitions, Refer below link.

Managing GTP Partitions and File Systems Using gdisk

Creating MBR Disk Partitions

1.) To view the existing partitions.

# fdisk -l

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    27609087    11399168   8e  Linux LVM
/dev/sda4        27609088    41943039     7166976    5  Extended
/dev/sda5        27611136    41943039     7165952   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: 0x9e759391

   Device Boot      Start         End      Blocks   Id  System

Note: Observe in the above picture that there are two disk devices, named /dev/sda,/dev/sdb and You can also use parted -l to view the existing partitions.

2.) Specify the disk device to create the partition on.

Execute the fdisk command and specify the disk device name as shown below.

# fdisk /dev/sdb

[root@server1 ~]# fdisk /dev/sdb
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.

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)

Command (m for help):

Note: When we execute the fdisk command with disk device name. It will show the command prompt(as shown above). Use “m” to list various options that can be used in fdisk. 

3.) Request a new primary or extended partition.

Use “p” to list the partition information first and Use “n” to request a new partition  and specify whether the partition should be created as a primary or extended partition.

Command (m for help): p

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: 0x9e759391
   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p

Note: The default selection is the primary partition type and For situation where we want more than four partition are needed. This limit can be bypassed by creating three primary partitions and one extended partition. This extended partition will create the multiple logical partitions.

4.) Specify the partition details.

Specify the identification number of the new partition on the disk then specify the first sector of disk and specify the last sector on the disk that the new partition will end on(you can also specify the size of new partition in units of K,M,G).

Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +5G
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): p
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: 0x9e759391
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

Command (m for help):

Verify the new partition is created or not using “p”.

5.) Define partition type.

Enter the “t” command to change a partition type then enter the hex code for the new partition type.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 82
Changed type of partition 'Empty' to 'Linux swap / Solaris'

Command (m for help):p
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: 0x9e759391

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   82  Linux swap / Solaris

Note: “L” is used to list all codes.

6.) Save partition tables changes.

“w” command is used to finalize creation request.

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server1 ~]#

7.) Updating the new partition table without reboot.

After creating a new partition the changes will be effected in the partition table only after reboot the system.But there is the way to avoid this circumstance, run the “partprobe” command with the disk device name as an argument to force a re-read of its partition table.

[root@server1 ~]# partprobe /dev/sdb1
[root@server1 ~]#

Making the File system

8.) Creating the file systems with xfs.

After creating the block device, we need to assign some file system to it so we can start storing the data into it.

# mkfs -t xfs /dev/sdb1 (Where, t is used to specify the file system type)

[root@server1 ~]# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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
[root@server1 ~]#

Note: Likewise you can format the different partition with different file systems like

# mkfs -t ext4 /dev/sdb1, # mkfs -t vfat /dev/sdb1

Deleting a Partitions

9.) How to Removing Disk Partitions.

Let’s take the example to delete the  partition, we have created above (i.e /dev/sdb1). Use “d” to delete a partition and specify the device name, in our case it is 1. Then we have  save the partition table changes using “w” option and last inform the kernel to re-read that partition table with partprobe command.

[root@server1 ~]# fdisk /dev/sdb
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.

Command (m for help): p

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: 0x9e759391

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   82  Linux swap / Solaris

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): p
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: 0x9e759391

   Device Boot      Start         End      Blocks   Id  System
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server1 ~]# partprobe /dev/sdb
[root@server1 ~]# 

Mount the File Systems

10.) How to Mounting the file systems.

Once the file system format has been applied. The last step to adding a new file system is to attach into the directory structure.There are two type of mounting which will be used in Linux and any Unix.

  • Manually mounting file systems.
  • Persistently mounting file systems.

Manually mounting file systems

In this case, we will create a directory and mount it, but it will mount point will last only till the system is up, Once it is rebooted the mounting will be lost.

#mount <Device name> <directory name (mount point)>

[root@server1 ~]# mkdir /Softwarebackup
[root@server1 ~]# mount /dev/sdb1 /Softwarebackup/
[root@server1 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=498172k,nr_inodes=124543,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)

. . . . . .
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=101688k,mode=700)
/dev/sdb1 on /Softwarebackup type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@server1 ~]#

To view all mounted partitions “mount” command used.We have successfully mount the partition. Now we can access it and can store the data on it.

Persistently mounting file systems

Here we have to update the mounting details on /etc/fstab file, so that it will be mounted even after the system reboot.

Steps:

  • Make a directory or use existing directory(/Softwarebackup).
  • Find out the UUID using “blkid” command.
  • Add entry on fstab file(/etc/fstab).
  • Use mount -a command to check it is mounting.

Find out the UUID number of device using,

# blkid <Device Name>

[root@server1 ~]# blkid /dev/sdb1
 /dev/sdb1: UUID="a6dc4f16-35fc-42a6-8d03-7ccb73df203f" TYPE="xfs" 
 [root@server1 ~]#

# vim /etc/fstab

#
 # /etc/fstab
 # Created by anaconda on Sat Sep 17 02:26:32 2016
 #
 # Accessible filesystems, by reference, are maintained under '/dev/disk'
 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
 #
 /dev/mapper/centos-root                         /                       xfs     defaults        0 0
 UUID=107451a1-3675-46bf-9074-dd9fd19ca2f9       /boot                   xfs     defaults        0 0
 /dev/mapper/centos-home                         /home                   xfs     defaults        0 0
 /dev/mapper/centos-var                          /var                    xfs     defaults        0 0
 UUID=0fc8a8f0-641c-42fd-8f06-34e40adb15ea       swap                    swap    defaults        0 0
 UUID="a6dc4f16-35fc-42a6-8d03-7ccb73df203f"     /Softwarebackup         xfs     defaults        0 0
~
~
!wq

Note: Here we will be using existing /Softwarebackup directory as mount point earlier.

Understanding Each Entry Of  Fstab ( /etc/fstab ) File.

fstb1

Device/remote filesystem: The first field is the Device name. You can also use  devices name (like /dev/mapper/centos-root,  /dev/mapper/centos-home)  instead of UUID number for device filesystem.

Mount Point: The second field is the mount point on which the device needs to be mounted(in our case mount point is /Softwarebackup directory).
Type of FS: The third field is the filesystem type on the device from the first filed. The various important file system types like ext2 and ext3, xfs, vfat and ntfs etc.
Mount Options: The fourth field is used for the mount options(in our case the default list selected(For more information read manual pages for mount using “man mount” command)).
Dumping: The fifth field is used by dump (a backup utility) to decide if a filesystem should be backed up. If zero then dump will ignore that filesystem.
Filesystem Check Sequence: The last field is used fsck (the filesystem check utility) to determine the order in which filesystems should be checked. If zero then fsck won’t check the filesystem.

Now mount the file system using “mount -a” command.

#mount -a

[root@server1 /]# df -h
 Filesystem               Size  Used Avail Use% Mounted on
 /dev/mapper/centos-root  7.0G  824M  6.2G  12% /
 devtmpfs                 487M     0  487M   0% /dev
 tmpfs                    497M     0  497M   0% /dev/shm
 tmpfs                    497M  6.6M  490M   2% /run
 tmpfs                    497M     0  497M   0% /sys/fs/cgroup
 /dev/mapper/centos-home  2.0G   33M  2.0G   2% /home
 /dev/mapper/centos-var   1.9G  184M  1.7G  10% /var
 /dev/sda1                297M  114M  184M  39% /boot
 tmpfs                    100M     0  100M   0% /run/user/0
 [root@server1 /]# mount -a
 [root@server1 /]# df -h
 Filesystem               Size  Used Avail Use% Mounted on
 /dev/mapper/centos-root  7.0G  824M  6.2G  12% /
 devtmpfs                 487M     0  487M   0% /dev
 tmpfs                    497M     0  497M   0% /dev/shm
 tmpfs                    497M  6.6M  490M   2% /run
 tmpfs                    497M     0  497M   0% /sys/fs/cgroup
 /dev/mapper/centos-home  2.0G   33M  2.0G   2% /home
 /dev/mapper/centos-var   1.9G  184M  1.7G  10% /var
 /dev/sda1                297M  114M  184M  39% /boot
 tmpfs                    100M     0  100M   0% /run/user/0
 /dev/sdb1                5.0G   33M  5.0G   1% /Softwarebackup
 [root@server1 /]

As shown above “/Softwarebackup” directory has been mounted.

Some troubleshooting steps while unmount the file systems.

Sometimes a directory throws error while unmounting, possible causes for it are.

  • You are in the same directory and try to umount it.
[root@server1 Softwarebackup]# umount /Softwarebackup
umount: /Softwarebackup: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@server1 Softwarebackup]#pwd
/Softwarebackup
[root@server1 Softwarebackup]#

Note: Check with pwd command.

  • We also face the error while unmount the disk when some of the users are working on present directory. To check  which user use the mount point, the following command is used.
[root@server1 ~]# fuser -cu /Softwarebackup
/Softwarebackup_SERVER: 22467c(nagios)
[root@server1 ~]#

You can use kill all open connection use following command  “fuser -ck /Softwarebackup”. Now you can use “umount” command to unmount the filesystems. !!!!

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz