Adding A New Hard Drive To A Live Linux System

Linux LogoThis is a little guide I came up with while adding a new drive to my file server.

First ordeal is to find out where the new hard drive ended up. I like to eliminate as much guesswork as possible. In that regard, fdisk is a great tool because it’ll let you know which devices have invalid partition tables.

sudo fdisk -l

Look for your new hard drive. It’ll look something like this.

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

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

If you have software raid, /dev/md[n] partitions will also show up and fdisk will say they have an invalid partition table.

Once you determine where your new hard drive is, run sudo fdisk /dev/sd[x]. You’ll know you got it right if you see:

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

Next, you’ll want to hit n (for new), p (for primary), 1 (for primary partition 1). To accept the default start and end sectors, which will use up the entire disk, you can simply hit enter on each line. Lastly, hit w to write and save the partition table. The log will look something like this:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-60801, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-60801, default 60801): 60801

Command (m for help): p

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xfc5e6344

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1       60801   488384001   83  Linux

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

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

Next, use mkfs -t [ ext[n] | reiserfs | xfs | etc. ] /dev/sd[x]1 to format the new drive with a file system.

$ sudo mkfs -t ext2 /dev/sda1
mke2fs 1.40.8 (13-Mar-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
30531584 inodes, 122096000 blocks
6104800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
3727 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

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

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

Now you’ll need a mount point for the file system. I already had one from the old backup drive. But if you need a new one, sudo mkdir [mountpoint] works fine. There’s no need to fiddle with chmod settings if you plan on permanently adding this drive using fstab.

Now get the UUID of the disk using sudo blkid /dev/sd[x][n]. The output will look something like this:

/dev/sda1: UUID="1907a51d-898f-4850-bcaf-41e5e3779506" TYPE="ext2"

Here’s the real fun: typing that UUID into fstab. If you’re running a desktop system, you can simply copy and paste the UUID inside the terminal. If you’re running a server, hopefully you have ssh set up so you can still use a GUI terminal with copy/paste functionality. If neither apply, I feel bad for you.

UUID=1907a51d-898f-4850-bcaf-41e5e3779506 /backup ext2 relatime 0 2

That’s it! After a reboot your new disk should be up and running!