Linux Red Hat 6 - Add a new disk

List disk devices to locate and verify the disk is seen by the OS. You can also verify that there are no existing partitions or that the disk is empty if its a new blank disk.

If you are reusing an existing disk, ensure you are using the correct device to make sure you don’t overwrite a disk accidentally.

Note: All of the commands listed here were performed as the root user. It is also possible to perform these commands with an appropriately privileged user account using sudo.

List disks

Use fdisk -l to list disks and their partition tables. In the following example, sdb does not have a partition table and is the disk that needs to be partitioned, formatted to be used as a new disk for this server.

[root@svr1 ~]# fdisk -l

Disk /dev/sda: 17.2 GB, 17179869184 bytes
64 heads, 32 sectors/track, 16384 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005764c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         201      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             202        2249     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3            2250       16384    14474240   83  Linux
Partition 3 does not end on cylinder boundary.

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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

[root@svr1 ~]#

Create a partition

Use fdisk to create a partition table. You can use m to get a list of commands.

When prompted, enter n to create a new partition table. Create primary table 1. In this case, a full disk partition is being created so the defaults are used to start at the first block (1) and continue to the last block (13054). The last block is automatically determined by fdisk.

The next step is to save the partition table to disk using the w command. Then to verify the partition was written correctly.

[root@svr1 ~]# 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 0x621198ca.
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):


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)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054):
Using default value 13054

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

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

Now that the partition table has been created and written to the disk, use fdisk -l again to verify it is correct.

[root@svr1 ~]# fdisk -l

Disk /dev/sda: 17.2 GB, 17179869184 bytes
64 heads, 32 sectors/track, 16384 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005764c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         201      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             202        2249     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3            2250       16384    14474240   83  Linux
Partition 3 does not end on cylinder boundary.

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x621198ca

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       13054   104856223+  83  Linux
[root@svr1 ~]#

Format the Partition

Once the partition is created, the next step is to create a file system on the partition. The disk format used in this example is ext4. The make file system command is used - mkfs. Depending on the file system, there are variations of the mkfs command you can use. In this example, mkfs.ext4 is the command. The syntax specifies the device name and the device name now includes the partition to format. In this example, device sdb and partition 1, which gives the actual device name of sdb1.

mkfs.ext4 DEVICE

[root@svr1 ~]# mkfs.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
6553600 inodes, 26214055 blocks
1310702 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 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, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

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

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

The mke2fs command can be used to create ext2, ext3 and ext4 file systems. Use mke2fs -t ext4. See man mke2fs.

Create a mount point

After a file system is created on the new partition, the disk is ready for use. The disk can be mounted and it is ready for storing files.

mkdir DIRECTORY

[root@svr1 ~]# mkdir /hd1

Mount the disk

mount DEVICE PATH

[root@svr1 ~]# mount /dev/sdb1 /hd1
[root@svr1 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        14G  8.3G  4.7G  64% /
tmpfs           3.9G  196K  3.9G   1% /dev/shm
/dev/sda1       194M   38M  147M  21% /boot
/dev/sdb1        99G   60M   94G   1% /hd1
[root@svr1 ~]#

Add to fstab to mount at boot

The linux fstab contains details about file systems including information about which file systems to mount at boot. Entries include options, file system type, mount point and the order to check filesystems at boot.

Edit the /etc/fstab file and add the following line to automatically mount this drive at boot time.

/dev/sdb1 /hd1 ext4 defaults 0 0

vi /etc/fstab

The following example shows the complete fstab file.

[root@svr1 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Mar 25 16:13:52 2015
#
# 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
#
UUID=9b84015b-f996-4042-ae0b-09429b5665a0 /                       ext3    defaults        1 1
UUID=4e91f9c0-144c-4753-820a-5979342f0b90 /boot                   ext3    defaults        1 2
UUID=488e147d-12b3-4755-991e-e0a38f54860c swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

/dev/sdb1        /hd1                       ext4    defaults        0 0
[root@svr1 ~]#

Verify the fstab entry is correct

It is possible to pass the mount command a file system mount point from the fstab as an option to mount a file system. This is a simple method for testing the entries to ensure there are no errors in the fstab.

First unmount the file system that was mounted previously. Then mount it using the mount point from fstab.

Type mount without any options to show all the files systems mounted.

[root@svr1 ~]# umount /hd1
[root@svr1 ~]# mount /hd1
[root@svr1 ~]# mount
/dev/sda3 on / type ext3 (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 ext3 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
none on /sys/kernel/config type configfs (rw)
/dev/sdb1 on /hd1 type ext4 (rw)
[root@svr1 ~]#

Now the new disk has a file system and is ready for use.

Advertisement