Manage Drives and Disk Partitions using fdisk

One of the common administrative tasks under UNIX/Linux is to add a new disk to the system to increase its storage capacity.  This tutorial will walk through on adding disk to a running system, making a partition, assigning file system and creating mount point for the new disk. The fdisk utility is very helpful especially when adding new drives to be used for Logical Volume Management (LVM).

Check current Disks and re-scan

First, verify the newly inserted disk is detected by the system using the following commands.
dmesg | tail -n 20
lsblk
fdisk -l

If the system wasn’t able to detect the new drive, try to reboot the server or force the re-scanning of iscsi drives using the following commands:
echo "- - -" > /sys/class/scsi_host/host2/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host0/scan

The path hostX may vary depending on OS versions.

Verify that the new drive is detected.
[[email protected] ~]# dmesg | tail -20
scsi target2:0:1: Domain Validation skipping write tests
scsi target2:0:1: Ending Domain Validation
scsi target2:0:1: FAST-40 WIDE SCSI 80.0 MB/s ST (25 ns, offset 127)
sd 2:0:1:0: Attached scsi generic sg2 type 0 ---> New drive
sd 2:0:1:0: [sdb] 4194304 512-byte logical blocks: (2.14 GB/2.00 GiB)
sd 2:0:1:0: [sdb] Write Protect is off
sd 2:0:1:0: [sdb] Mode Sense: 61 00 00 00
sd 2:0:1:0: [sdb] Cache data unavailable
sd 2:0:1:0: [sdb] Assuming drive cache: write through
sd 2:0:1:0: [sdb] Cache data unavailable
sd 2:0:1:0: [sdb] Assuming drive cache: write through
sdb: unknown partition table
sd 2:0:1:0: [sdb] Cache data unavailable
sd 2:0:1:0: [sdb] Assuming drive cache: write through
sd 2:0:1:0: [sdb] Attached SCSI disk
ata2: soft resetting link
ata2.00: configured for UDMA/33
ata2: EH complete
ata1: soft resetting link
ata1: EH complete
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 300M 0 part /boot
└─sda2 8:2 0 19.7G 0 part
├─vg_nerdtron-lv_root (dm-0) 253:0 0 18.6G 0 lvm /
└─vg_nerdtron-lv_swap (dm-1) 253:1 0 1.2G 0 lvm [SWAP]
sdb 8:16 0 2G 0 disk
—> New drive

[[email protected] ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000288ae

Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2611 20663296 8e Linux LVM

Disk /dev/mapper/vg_nerdtron-lv_root: 19.9 GB, 19922944000 bytes
255 heads, 63 sectors/track, 2422 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: 0x00000000

Disk /dev/mapper/vg_nerdtron-lv_swap: 1233 MB, 1233125376 bytes
255 heads, 63 sectors/track, 149 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: 0x00000000

Disk /dev/sdb: 2147 MB, 2147483648 bytes —> New drive
255 heads, 63 sectors/track, 261 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: 0x00000000

Create new partitions on the new disk

Now that the raw disk is detected, we’ll create a partition on it using the fdisk utility. Follow the steps below. Be sure that the fdsik utility uses the correct hard drive as this command can destroy entire partitions.
[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xe6a125bd.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0xe6a125bd

Device Boot Start End Blocks Id System
/dev/sdb1 1 261 2096451 83 Linux

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

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

In the fdisk utility, we created a new primary partition assigned as /dev/sdb1 shown above and wrote the changes to disk.

The next step is to assign a file system to the new partition /dev/sdb1 using the mkfs utility. Here we assign it as ext4.
[[email protected] ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524112 blocks
26205 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

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

The drive is now ready for mounting so that the system can use it to read/write data.

Mount the new disk

Create a mount point. Make a folder in which to mount the new partition. Here we create a folder /mnt/data
[[email protected] ~]# mkdir -p /mnt/data

Use the mount command to mount the new drive.
[[email protected] ~]# mount -t ext4 /dev/sdb1 /mnt/data

To confirm the drive is mounted, use the mount command again or df command.
[[email protected] ~]# mount
/dev/mapper/vg_nerdtron-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sdb1 on /mnt/data type ext4 (rw) ---> New drive mounted
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_nerdtron-lv_root
19G 3.4G 14G 20% /
tmpfs 379M 224K 379M 1% /dev/shm
/dev/sda1 283M 28M 240M 11% /boot
/dev/sdb1 2.0G 3.0M 1.9G 1% /mnt/data
—> New drive mounted

You should now be able to read/write data on the /mnt/data folder which is on the newly created partition.


– masterkenneth

Leave a Reply

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