Informational Website publish

Blog

News about our company, tutorials about IT and much more you will find in this page.

How to mount second Hard Drive on linux

Janeiro 30th, 2018

This tutorial assumes that the new physical hard drive has been installed on the system and is visible to the operating system.

  • Finding the New Hard Drive in CentOS 6:

Typically, the disk drives in a system are assigned device names beginning hd or sd followed by a letter to indicate the device number. For example, the first device might be /dev/sda, the second /dev/sdb and so on. To know which letter your new hard drive is, run the following command:

lsblk
NAME   MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
sda      8:0    0  111.8G  0 disk
├─sda1   8:1    0    256M  0 part /boot
├─sda2   8:2    0      2G  0 part [SWAP]
└─sda3   8:3    0  109.6G  0 part /
sdb      8:16   0  931.5G  0 disk

As shown above, the new hard drive has been assigned to the device file /dev/sdb. Currently the drive has no partitions shown (because we have yet to create any).

  • Creating Linux Partitions:

The next step is to create one or more Linux partitions on the new disk drive. This is achieved using the fdisk utility which takes as a command-line argument the device to be partitioned:

fdisk /dev/sdb
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):

As instructed, switch off DOS compatible mode and change the units to sectors by entering the c and u commands:

Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): u
Changing display/entry units to sectors

In order to view the current partitions on the disk enter the p command:

Command (m for help): p

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
81 heads, 63 sectors/track, 382818 cylinders, total 1953525168 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 identifier: 0x00075f78

   Device Boot        Start        End       Blocks    Id   System

As we can see from the above fdisk output, the disk currently has no partitions because it is a previously unused disk. The next step is to create a new partition on the disk, a task which is performed by entering n (for new partition) and p (for primary partition):

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4):

In this example we only plan to create one partition which will be partition 1. Next we need to specify where the partition will begin and end. Since this is the first partition we need it to start at the first available sector and since we want to use the entire disk we specify the last sector as the end.
Note: If you wish to create multiple partitions you can specify the size of each partition by sectors, bytes, kilobytes or megabytes.

Partition number (1-4): 1
First sector (2048-1953525167, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1953525167, default 1953525167):
Using default value 1953525167

Now that we have specified the partition we need to write it to the disk using the w command:

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

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

If we now look at the devices again we will see that the new partition is visible as /dev/sdb1:

lsblk
NAME   MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
sda      8:0    0  111.8G  0 disk
├─sda1   8:1    0    256M  0 part /boot
├─sda2   8:2    0      2G  0 part [SWAP]
└─sda3   8:3    0  109.6G  0 part /
sdb      8:16   0  931.5G  0 disk
└─sdb1   8:17   0  931.5G  0 part
  • Creating a File System on a CentOS 6 Disk Partition

The next step is to create a Linux file system on the partition so that the operating system can use it to store files and data. The easiest way to create a file system on a partition is to use the mkfs.ext4 utility which takes as arguments the label and the partition device:

/sbin/mkfs.ext4 -L /Disk2 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=/Disk2
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
61054976 inodes, 244190390 blocks
12209519 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
7453 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, 71663616, 78675968,
        102400000, 214990848

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

This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
  • Mounting a File System:

Now that we have created a new file system on the Linux partition of our new disk drive we need to mount it so that it is accessible. In order to do this we need to create a mount point. A mount point is simply a directory or folder into which the file system will be mounted. For the purposes of this example we will create a /Disk2 directory to match our file system label.

mkdir /Disk2

The file system may then be manually mounted using the mount command:

mount /dev/sdb1 /Disk2

Running the mount command with no arguments shows us all currently mounted file systems (including our new file system):

/dev/sda3 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)
/dev/sdb1 on /Disk2 type ext4 (rw)
  • Configuring CentOS 6 to Automatically Mount a File System

In order to set up the system so that the new file system is automatically mounted at boot time an entry needs to be added to the /etc/fstab file.

vi /etc/fstab

Add the following line at the end of the file to auto-mount our /Disk2 partition:

LABEL=/Disk2              /Disk2              ext4    defaults        1 2

Done!!!