Tutorials — 29 August 2011
Adding a new hard drive in CentOS

Cách thêm HDD trong linux.

There comes a time in every linux guys life that you are out of space.  You add a new hard drive but this isn’t Windows and doesn’t automatically show up; especially if you’re in a server environment with nothing but a command line.

*** NOTE : All commands must be from root user.  Also, this is assuming you are just making one big partition from the whole hard drive and does not account for separating partitions of specified sizes ***

First, lets get a list of all physical drives connected:

# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        1044     8281507+  8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

You can see that /dev/sdb is connected but doesn’t contain any kind of a partition what-so-ever and you need *a* partition obviously.  Lets go ahead and start to format the disk, /dev/sdb and pull up the menu.

# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

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)

Now, lets go ahead and Press N for a new partition and just write with all defaults.  We will use a Primary Partition and make it Partition 1.

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 or +size or +sizeM or +sizeK (1-261, default 261):
Using default value 261

Now, lets go ahead and write these changes to the secondary disk (/dev/sdb) with the “w” command.

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Ok, now we have the disk ready but still no file system.  This is when mkfs comes in and we will want to specify the type which will be ext3 and will use the first (and only) partition on the secondary disk (/dev/sdb1).

# mkfs -t ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
262144 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
16384 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 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Great!  We are almost there.  All we need to do now is mount it.  You do have to create a target folder before you can mount to it.  Lets go ahead and create the target folder (mkdir /drive2) and mount it (mount -t ext3 /dev/sdb1 /drive2).  Then, we will get the amount of disk space free to make sure it is mounted at the right place and reads the correct amount of space for the partition.  For this example I am only using a 2GB virtual disk (df -h).

# mount -t ext3 /dev/sdb1 /drive2
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
5.7G  1.4G  4.1G  26% /
/dev/sda1              99M   18M   76M  20% /boot
tmpfs                 506M     0  506M   0% /dev/shm
/dev/sdb1             2.0G   36M  1.9G   2% /drive2

You can now see that we are successful in adding the new hard drive, adding a partition, formatting it with ext3 file system, and then mounting it to mount point /drive2.

Related Articles

Share

About Author

centosvn.com

(0) Readers Comments

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>