Managing Disk Storage using fdsik

Managing disk storage is the day to day task for the Linux administrators. Few tasks are –

  • Adding new disks
  • create partitions
  • formatting partitions
  • mount and umounting partitions

Suppose you have recently added a new disk in your server or system, use the following steps to create partitions, format partition and mount the partition to use it.

For the learning/testing purpose, you can use a virtual machine on VMWare Workstation or Oracle VirtualBox, because. Add a new disk to the virtual machine and restart the virtual machine.

List available disks 
[code]
# fdisk -l
Disk /dev/sdb: 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: 0x6293cd80Device Boot Start End Blocks Id System
Device Boot Start End Blocks Id System[/code]

Create a patition on newly added disk /dev/sdb

[code]

[root@controller ~]# 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): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/sdb: 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: 0x6293cd80

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux

Command (m for help):w

The partition table has been altered!

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

[/code]

(if you are working with new/fresh disk then enter 1 for the partition number. if your are creating partition on existing disk then it depends on number of partitions. According to the MBR based you can create upto 4 parimary partitions or you can create 3 parimary and 1 extended partitions. Extended partitions can be further devided and disk can hold upto 15 partitions.)

[code]hdb1 – primary
hdb2 – primary
hdb3 – primary
hdb4 – extended
hdb5 – logical
hdb6 – logical
hdb7 – logical
hdb8 – logical
..
hdb13 – logical
hdb14 – logical
hdb15 – logical[/code]

Format partition with ext4 filesystem

[code][root@controller ~]# mkfs.ext4 -j /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
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done[/code]

Mount new created partition on /mnt for temporary use and check mounting

[code][root@controller ~]# mount -t ext4 /dev/sdb1 /mnt
[root@controller ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 9.8G 37M 9.2G 1% /mnt
[root@controller ~]# [/code]

Mount partition permanent on /DATA directory to become available at boot time

edit /etc/fstab file and append a new line, make sure that /DATA directory is available or already created

[code]
[root@controller ~]# vi /etc/fstab
# HEADER: This file was autogenerated at 2017-05-06 18:11:54 +0530
# HEADER: by puppet. While it can still be managed manually, it
# HEADER: is definitely not recommended.
#
# /etc/fstab
# Created by anaconda on Sat Apr 29 18:10:07 2017
#
# 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=8222ccb4-f05a-4926-a15c-a52c67197752 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults,usrquota 0 1
/dev/mapper/centos-swap swap swap defaults 0 0
/srv/loopback-device/swiftloopback /srv/node/swiftloopback ext4 noatime,nodiratime,nobarrier,loop,user_xattr 0 0
#/dev/sdb1 /DATA ext4 defaults,usrquota,grpquota 1 2[/code]

now mount
[code][root@controller ~]# mount -a
[root@controller ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 3.5G 47G 7% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 4.0K 3.9G 1% /dev/shm
tmpfs 3.9G 8.7M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/loop0 1.9G 6.1M 1.7G 1% /srv/node/swiftloopback
/dev/sda1 497M 170M 328M 35% /boot
/dev/mapper/centos-home 42G 33M 42G 1% /home
tmpfs 781M 0 781M 0% /run/user/0
/dev/sdb1 9.8G 37M 9.2G 1% /mnt[/code]

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">