Garry Conn

How to Format and Automatically Mount a Second Hard Drive in Linux

Adding, formatting and then configuring your Linux system to automatically mount a second hard drive isn\'t too difficult of a task. I will show you how to do this in a few quick and easy steps.

One thing I can\'t stand when I am trying to learn something new with Linux is having to read long and drawn out tutorials, articles and message board thread with people arguing and going off topic. I am a busy guy, and when I seek out information I like to get fast answers. I am sure you feel the same, so here goes:

That said, I also can\'t stand it when I see people asking legitimate questions and they get reprimanded for asking. If you have a question, by all means feel free to ask me. I will be happy to do what I can to help answer any questions you may have.

Step One: Check To See if Linux Recognizes Your Second Hard Drive

Before you can format the second hard drive and even dream of having your system automatically mount it each time you boot your system, you need to make sure that Linux sees it. To verify this simply open up a shell and change to the device directory. That\'s the /dev directory.

root@backup:~# cd /dev
root@backup:/dev#

Once you have changed into the device directory you have a couple of options towards discovering if Linux sees your additional hard drive. The first way is to simply use the ls command, which stands for list the contents within a directory.

root@backup:/dev# ls
block		 mem		     sdc1      tty17  tty40  tty7
bsg		 net		     sdd       tty18  tty41  tty8
cdrom		 network_latency     sdd1      tty19  tty42  tty9
char		 network_throughput  sg0       tty2   tty43  ttyS0
console		 null		     sg1       tty20  tty44  ttyS1
core		 port		     sg2       tty21  tty45  ttyS2
cpu_dma_latency  ppp		     sg3       tty22  tty46  ttyS3
disk		 psaux		     sg4       tty23  tty47  urandom
fd		 ptmx		     shm       tty24  tty48  vcs
fd0		 pts		     snapshot  tty25  tty49  vcs1
full		 random		     snd       tty26  tty5   vcs2
hpet		 root		     sndstat   tty27  tty50  vcs3
initctl		 rtc		     sr0       tty28  tty51  vcs4
input		 rtc0		     stderr    tty29  tty52  vcs5
kmsg		 scd0		     stdin     tty3   tty53  vcs6
log		 sda		     stdout    tty30  tty54  vcsa
loop0		 sda1		     tty       tty31  tty55  vcsa1
loop1		 sda2		     tty0      tty32  tty56  vcsa2
loop2		 sda5		     tty1      tty33  tty57  vcsa3
loop3		 sda6		     tty10     tty34  tty58  vcsa4
loop4		 sda7		     tty11     tty35  tty59  vcsa5
loop5		 sda8		     tty12     tty36  tty6   vcsa6
loop6		 sda9		     tty13     tty37  tty60  vga_arbiter
loop7		 sdb		     tty14     tty38  tty61  xconsole
MAKEDEV		 sdb1		     tty15     tty39  tty62  zero
mcelog		 sdc		     tty16     tty4   tty63
root@backup:/dev#

The results can get a little crazy, especially if you are new to Linux. Basically what you are looking for are sd* or hd*. In most cases, sd* such as in my case: sda, sda1, sda2, sdb, sdb1, sdc, sdc1, sdd, sdd1, etc.

If the ls results are confusing, another way would be to add the grep command to filter out all the crap. The grep command is like the Google Search command of Linux within a shell. It\'s pretty powerful and can help you find things you need quickly. Make sure that you are in the device directory and then try this command instead:

root@backup:/dev# grep sd* hd*

This will produce results like the ones shown below instead:

Binary file sda1 matches
Binary file sda5 matches
Binary file sda6 matches
Binary file sda7 matches
Binary file sda8 matches
Binary file sda9 matches
Binary file sdb matches
Binary file sdb1 matches
Binary file sdc matches
Binary file sdc1 matches
Binary file sdd matches
Binary file sdd1 matches
grep: hd*: No such file or directory
root@backup:/dev#

So what do these results mean? Well, in my case sda, sdb, sdc and sdd are found in the device directory. No devices with hd* were found. That means I have four hard drives installed. The numbers on the end are simply the partitions within each hard drive. sda is my main hard drive. When I installed Debian, I chose to have separate partitions. This can be reflected with the df command which basically means to display how much disk space you have used on each file system. The -h command added to df produces easy to read results. That command is df -h. Here\'s how that looks:

root@backup:/dev# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             323M  112M  195M  37% /
tmpfs                 252M     0  252M   0% /lib/init/rw
udev                  251M  124K  250M   1% /dev
tmpfs                 252M     0  252M   0% /dev/shm
/dev/sda9             3.0G   69M  2.8G   3% /home
/dev/sda8             249M   11M  226M   5% /tmp
/dev/sda5             3.0G  481M  2.3G  17% /usr
/dev/sda6             1.5G  198M  1.2G  15% /var
/dev/sdb1              17G  173M   16G   2% /backup01
/dev/sdc1              17G  173M   16G   2% /backup02
/dev/sdd1              34G  177M   32G   1% /backup03
root@backup:/dev#

In the results above, you will see that I have three backup directories: /backup01, /backup02 and /backup03. These are actually the three additional hard drives I have added to my system. The number 1 at the end of each represent the partition. When I formatted each drive, I chose to create one partition that uses the entire disk. Secondly, I chose to have the system automatically mount each drive into a /backup## directory that I created.

Step one is to basically check and see if your Linux system recognizes that your additional hard drive is there. All of the above information should help you verify if that is so. If Linux does not see your additional hard drive you may want to check to see if you have properly plugged it in and also check to see if the IDE or SATA cable is properly connected to the drive and motherboard. If the additional hard drive still doesn\'t appear, you might want to also check in BIOS to see if you have that particular drive port turned off.

Step Two: Partitioning The Additional Hard Drive

After you have verified that your Linux system actually sees and recognizes the additional hard drive installed, the next step is to partition the drive. Just like the first step, Partitioning isn\'t too difficult either. Two ways to do this is by using the fdisk or cfdisk command. The latter seems a little easier in my opinion. Let\'s assume that the second drive you installed in your system is recognized as sdb. If it\'s a used hard drive, perhaps pulled from a Windows XP computer, Linux may already see partitions such as sdb1 and sdb2. These partitions are useless. In these next steps you are going to be erasing and repartitioning the hard drive. It\'s important to backup any old data from the hard drive if needed because you will be erasing all of the existing data from the drive.

The first step to partition the hard drive, as mentioned above is to either use fdisk or cfdisk. I find cfdisk much easier, so I am going to include the steps for that utility. To get started, type this command:

root@backup:/dev# cfdisk /dev/sdb

Doing so, produces a new screen similar to this one shown below:

                         cfdisk (util-linux-ng 2.17.2)

                              Disk Drive: /dev/sdb
                        Size: 18209320960 bytes, 18.2 GB
              Heads: 64   Sectors per Track: 32   Cylinders: 17365

    Name        Flags      Part Type  FS Type          [Label]        Size (MB)
 ------------------------------------------------------------------------------
    sdb1        Boot        Primary   Linux ext3                       18208.53











     [ Bootable ]  [  Delete  ]  [   Help   ]  [ Maximize ]  [  Print   ]
     [   Quit   ]  [   Type   ]  [  Units   ]  [  Write   ]

                  Quit program without writing partition table

Now in my case, you will see that my second hard drive is already partitioned and configured bootable. The above illustrates the final product. At any rate, I am going to assume differently and use this drive to walk you thru the process. With the cfdisk utility opened and pointing to /dev/sdb you are going to want to delete any and all existing partitions. You can simply use the arrow keys on your keyboard to navigate to the [ Delete ] tab and proceed to delete every partition shown. The end result should look like this:

                         cfdisk (util-linux-ng 2.17.2)

                              Disk Drive: /dev/sdb
                        Size: 18209320960 bytes, 18.2 GB
              Heads: 64   Sectors per Track: 32   Cylinders: 17365

    Name        Flags      Part Type  FS Type          [Label]        Size (MB)
 ------------------------------------------------------------------------------
                            Pri/Log   Free Space                       18208.53











     [   Help   ]  [   New    ]  [  Print   ]  [   Quit   ]  [  Units   ]
     [  Write   ]
                               
                      Create new partition from free space

Now with all the existing partitions deleted, it\'s time to create a new primary partition. In this example, I will be creating only ONE partition; however, you can create multiple logical partitions if you desire to. In my case, I have added additional hard drive to a Linux system for the purpose of backing up data from another Linux system in my office. I only want one partition per drive.

To create a new primary partition, use the arrow keys on your keyboard to toggle to the [ New ] tab and then hit enter. Next, choose [ Primary ] and then hit enter again to select the full disk size. The last two tasks are to select the correct type and to make the disk bootable. So toggle over to the [ Type ] tab and press enter on your keyboard. Doing so produces a screen like this:

                         
 01 FAT12                 50 OnTrack DM            AB Darwin boot
 02 XENIX root            51 OnTrack DM6 Aux1      AF HFS / HFS+
 03 XENIX usr             52 CP/M                  B7 BSDI fs
 04 FAT16                 53 OnTrack DM6 Aux3      B8 BSDI swap
 05 Extended              54 OnTrackDM6            BB Boot Wizard hidden
 06 FAT16                 55 EZ-Drive              BE Solaris boot
 07 HPFS/NTFS             56 Golden Bow            BF Solaris
 08 AIX                   5C Priam Edisk           C1 DRDOS/sec (FAT-12)
 09 AIX bootable          61 SpeedStor             C4 DRDOS/sec (FAT-16 <
 0A OS/2 Boot Manager     63 GNU HURD or SysV      C6 DRDOS/sec (FAT-16)
 0B W95 FAT32             64 Novell Netware 286    C7 Syrinx
 0C W95 FAT32 (LBA)       65 Novell Netware 386    DA Non-FS data
 0E W95 FAT16 (LBA)       70 DiskSecure Multi-Boo  DB CP/M / CTOS / ...
 0F W95 Ext\'d (LBA)       75 PC/IX                 DE Dell Utility
 10 OPUS                  80 Old Minix             DF BootIt
 11 Hidden FAT12          81 Minix / old Linux     E1 DOS access
 12 Compaq diagnostics    82 Linux swap / Solaris  E3 DOS R/O
 14 Hidden FAT16          83 Linux                 E4 SpeedStor



                               
                            Press a key to continue

Basically this is a menu letting you know the codes for each different type of partition. It includes most all of the common partitions such as FAT32 and NTFS. The partition you want to use is Linux, which is code 83. Go ahead and press any key to continue and then this screen will appear next:

                        
 16 Hidden FAT16          84 OS/2 hidden C: drive  EB BeOS fs
 17 Hidden HPFS/NTFS      85 Linux extended        EE GPT
 18 AST SmartSleep        86 NTFS volume set       EF EFI (FAT-12/16/32)
 1B Hidden W95 FAT32      87 NTFS volume set       F0 Linux/PA-RISC boot
 1C Hidden W95 FAT32 (LB  88 Linux plaintext       F1 SpeedStor
 1E Hidden W95 FAT16 (LB  8E Linux LVM             F4 SpeedStor
 24 NEC DOS               93 Amoeba                F2 DOS secondary
 39 Plan 9                94 Amoeba BBT            FB VMware VMFS
 3C PartitionMagic recov  9F BSD/OS                FC VMware VMKCORE
 40 Venix 80286           A0 IBM Thinkpad hiberna  FD Linux raid autodetec
 41 PPC PReP Boot         A5 FreeBSD               FE LANstep
 42 SFS                   A6 OpenBSD               FF BBT
 4D QNX4.x                A7 NeXTSTEP
 4E QNX4.x 2nd part       A8 Darwin UFS
 4F QNX4.x 3rd part       A9 NetBSD




     Enter filesystem type: 83

By default, 82 may be shown. This is Linux Swap / Solaris. You do not want that. Instead, simply enter 82 and press enter. After doing that, you will be taken back to the main menu. The next step is to make the disk bootable. Do so by toggling to the [ Bootable ] tab. In fact, this should already be slected by default. Press enter on your keyboard and magically the disk is now bootable. The last and final step is to toggle to the [ Write ] tab, click enter and then type yes. This will write the changes to the disk. After this is done, toggle to the [ Quit ] tab and click enter. Congratulations! You have now erased and repartitioned your second hard drive. Let\'s move on to the next step of formatting your hard drive.

Step Three: Formatting Your Hard Drive For Use in Linux

After verifying that your second hard drive is recognized by your Linux system, then correctly creating the proper Linux partition and making sure it\'s bootable; the next step is to format the hard drive. This, just like the previous two steps is fairly easy to do. Once again, lets assume that your second hard drive is recognized as sdb and that you have created a primary partition that is recognized as sdb1. To format the hard drive to use for Linux you will want to use either the ext2, ext3 or ext4 format. The latter is the most current; however, I generally prefer using ext3.

The command you need basically needs to tell your system to find the second hard drive, locate the primary partition on the second hard drive and to format it to ext3. The mkfs.ext3 command is what you need. Here\'s the command to enter:

root@backup:~# mkfs.ext3 /dev/sdb1

Unlike formatting a drive using Microsoft Windows operating system, this only take a few moments, and here\'s what the results will look like once the disk has been formatted.

root@backup:~# mkfs.ext3 /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
1111936 inodes, 4445436 blocks
222271 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
136 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

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

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
root@backup:~#

You\'re all done! The second hard drive has been formatted and now can be used in your Linux system. The next step is to command the system to automatically mount the drive when it boots up. Follow along and we\'ll do that next.

Step Four: Configuring Linux To Automatically Mount Second Hard Drive During Boot Up

Now that you have successfully formatted your second hard drive for use in your Linux system, it\'s time to configure your system to automatically mount the drive each time you boot up your system. This is probably the most difficult step simply because it involves editing the fstab file. Messing this part up can easily make your system non-bootable, so be careful.

There\'s a few ways of going about this, but generally I prefer to automatically mount a hard drive by configuring the fstab file to look for UUID numbers. The first step towards automatically mounting your second hard drive is figuring out what the UUID number of primary partition of the second hard drive is. The command to use is ls -l /dev/disk/by-uuid/. Here\'s the command to enter:

root@backup:~# ls -l /dev/disk/by-uuid/

This will produce results similar to the results shown below:

root@backup:~# ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Jul 19 09:36 0f3195b0-2bb2-491d-99eb-a0cf11039ed4 -> ../../sdd1
lrwxrwxrwx 1 root root 10 Jul 19 09:36 1e3e581c-47b5-4cc5-a56e-2ef3ccbc5c4c -> ../../sda9
lrwxrwxrwx 1 root root 10 Jul 19 09:36 4300bca4-ead0-4352-bf44-a2e7f803aa4b -> ../../sda8
lrwxrwxrwx 1 root root 10 Jul 19 09:36 64481119-3a26-4449-8263-ac9248031d0b -> ../../sda5
lrwxrwxrwx 1 root root 10 Jul 19 09:36 7352b0d8-b6af-4a3c-8ed2-b92db9b4aa2c -> ../../sda6
lrwxrwxrwx 1 root root 10 Jul 19 09:36 872a369d-8b0a-45d0-90fb-e5563cdb892e -> ../../sda7
lrwxrwxrwx 1 root root 10 Jul 19 09:36 9787804c-409a-499c-8029-cd36309099db -> ../../sdc1
lrwxrwxrwx 1 root root 10 Jul 19 09:37 bf418ae1-c673-425b-ae22-2600978b6199 -> ../../sdb1
lrwxrwxrwx 1 root root 10 Jul 19 09:36 c20b6bd8-a027-4647-a645-6ea9b5fd53cf -> ../../sda1
root@backup:~#

In our example, we know that we created a primary partition on sdb and that partition has been recognized by the system as sdb1. So, based on the results above, the UUID number for sdb1 on my system is bf418ae1-c673-425b-ae22-2600978b6199.

lrwxrwxrwx 1 root root 10 Jul 19 09:37 bf418ae1-c673-425b-ae22-2600978b6199 -> ../../sdb1

Now that we know the UUID number, we can now proceed to edit the fstab file. Basically what you are about to do is tell the system each time it boots up to look for this drive and the primary partition, and automatically mount it, and mount it to a directory that you have created. In my case, /backup01. That said, lets proceed to edit the fstab using nano. Here\'s the command to enter:

root@backup:~# nano /etc/fstab

Doing so will open a screen similar to this:

  GNU nano 2.2.4                    File: /etc/fstab                                                

# /etc/fstab: static file system information.
#
# Use \'blkid\' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
proc            /proc           proc    defaults        0       0
# / was on /dev/sda1 during installation
UUID=c20b6bd8-a027-4647-a645-6ea9b5fd53cf /               ext3    errors=remount-ro 0       1
# /home was on /dev/sda9 during installation
UUID=1e3e581c-47b5-4cc5-a56e-2ef3ccbc5c4c /home           ext3    defaults        0       2
# /tmp was on /dev/sda8 during installation
UUID=4300bca4-ead0-4352-bf44-a2e7f803aa4b /tmp            ext3    defaults        0       2
# /usr was on /dev/sda5 during installation
UUID=64481119-3a26-4449-8263-ac9248031d0b /usr            ext3    defaults        0       2
# /var was on /dev/sda6 during installation
UUID=7352b0d8-b6af-4a3c-8ed2-b92db9b4aa2c /var            ext3    defaults        0       2
# swap was on /dev/sda7 during installation
UUID=872a369d-8b0a-45d0-90fb-e5563cdb892e none            swap    sw              0       0
/dev/scd0       /media/cdrom0   udf,iso9660 user,noauto     0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto  0       0
UUID=17813bf3-dd8e-4edd-aa24-9f198710408f     /backup01       ext3    defaults        0       2
UUID=9787804c-409a-499c-8029-cd36309099db       /backup02       ext3    defaults        0       2
UUID=0f3195b0-2bb2-491d-99eb-a0cf11039ed4       /backup03       ext3    defaults        0       2






^G Get Help     ^O WriteOut     ^R Read File    ^Y Prev Page    ^K Cut Text     ^C Cur Pos
^X Exit         ^J Justify      ^W Where Is     ^V Next Page    ^U UnCut Text   ^T To Spell

Now, look at the very bottom of the file shown above. These are the three entries I added for the three additional hard drives I added into my system:

UUID=17813bf3-dd8e-4edd-aa24-9f198710408f     /backup01       ext3    defaults        0       2
UUID=9787804c-409a-499c-8029-cd36309099db       /backup02       ext3    defaults        0       2
UUID=0f3195b0-2bb2-491d-99eb-a0cf11039ed4       /backup03       ext3    defaults        0       2

Basically what you are doing is adding the new file system, which is identified by the UUID, telling the system where the mount point is and what the format is. So to do that, scroll to the bottom of the file and add UUID=############# then click tab on your keyboard. Then enter the mount point, which in my case is (or will be -- proceeding the next steps below) /backup01, then click tab on your keyboard. From there enter the format type of ext3, then click tab again and enter the word defaults, then click tab and then enter 0, then click tab again and then enter 2 and then you\'re done. Lastly, to save the edited file, hit the command key + the letter x on your keyboard to save the file.

Finally, before rebooting your system, you need to create the directory you assigned in the fstab file. Once again, in my case, the directory is /backup01. You will need to change to the root directory by typing cd / and then here\'s the command to type:

root@backup:~# cd /
root@backup:/# mkdir /backup01
root@backup:/#

Once you have made the directory, it\'s safe to reboot your system. Prior to doing so, you may want to manually attempt to mount the drive. You can do this by entering this command:

root@backup:~# mount -t ext3 /dev/sdb1 /backup01

Doing so should produce results similar to those shown below:

root@backup:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             323M  112M  195M  37% /
tmpfs                 252M     0  252M   0% /lib/init/rw
udev                  251M  128K  250M   1% /dev
tmpfs                 252M     0  252M   0% /dev/shm
/dev/sda9             3.0G   69M  2.8G   3% /home
/dev/sda8             249M   11M  226M   5% /tmp
/dev/sda5             3.0G  481M  2.3G  17% /usr
/dev/sda6             1.5G  199M  1.2G  15% /var
/dev/sdb1              17G  173M   16G   2% /backup01
/dev/sdc1              17G  173M   16G   2% /backup02
/dev/sdd1              34G  177M   32G   1% /backup03
root@backup:~#

In the above results, you will see that /backup01 is mounted, which is basically the primary partition of the second hard drive. If you need help or have any questions, please feel free to use the comment form below. I will answer any questions you have and provide you with as much support as I can. Thanks for reading and following this tutorial, I really hope that my approach enabled you to find success with performing what many people consider to be a difficult task. My goal is to make the process as simple as possible while also balancing in some lessons on helping you see the bigger picture of how all this works. Feel free to let me know if this goal has been achieved.