Home / Learning Linux and Unix / How To Format and Mount Hard Drive in Linux

How To Format and Mount Hard Drive in Linux

Everything so far has been rudimentary file copying, unzipping, and command lines. This is where it starts to get interesting. We’re going to build a minimal booting, networking install of Debian into the virtual drive. From the Windows DOS shell, make sure you’re in the directory that has the recently fdisk formatted virtual disk. It wouldn’t hurt to copy the harddrive.raw file now for undo purposes.

To start Linux up again, you will type a similar command to last time, however because we just made a bootable drive, but didn’t set it up, you will have to use the following command to boot from the Knoppix iso image again. Notice the -boot d at the end. That’s telling it to boot from the CD-ROM instead of the default boot disk. Otherwise, it will probably hang.

qemu -hda harddrive.raw -cdrom knoppix.iso -boot d

Okay, once again Knoppix starts booting, and you should select Shell (option #10 at the time I’m writing this), and upon getting the shell prompt, type “sudo su”. The prompt that previously looked like “knoppix@Microknoppix:~$” will now look like this “root@Microknoppix:/home/knoppix#” That number-sign (#) at the end is important to see, because it means we have the elevated root permissions we need.

As an aside, typing “sudo su” to elevate the current shell to superuser is considered bad practice (least you momentarily step away from your terminal) and most sysadmins will cringe at the fact I’m instructing you to do this. But the fact that it is a “throw away” testing machine with undo ability makes it safe for you, and the “virtual LAN” or VLAN security context of QEMU makes it safe for your network. Much more on security in later post.

Now, it’s time to format the drives. So far, we’ve partitioned them but haven’t formatted them. You could format it with the simple command:

mkfs.ext3 /dev/sda1

…but we are going to format and label it at the same moment, with the command:

mkfs.ext3 -L HOME /dev/sda1

Now, let’s format the swap partition with the command:

mkswap -L SWAP /dev/sda2

So we now have two formatted drive locations, but the swap one is for the exclusive use of the operating system, and we will never use it directly. Rather, we will simply inform the operating system about it’s existence by editing a file called /etc/fstab in a later step. But meanwhile, let’s actually test the one we can use by changing directories over to our new boot location… /dev/sda1… whoops!

It turns out that in Linux, you can’t go directly to device locations without mounting that device. So to make this location actually accessible for cd’ing over to, we have to type the following command…

mount /dev/sda1 /mnt/sda1

…and with that done, we can type:

cd /mnt/sda1

…and see the nearly empty directory, in this case, only having a lost+found directory.

We made the decision to mount the first hard drive into a directory mount point that automatically existed, /mnt/sda1. I could have mounted /dev/sda1 anywhere that did not conflict with Linux operation. So, I could have made a new directory in /mnt with a name of my choosing, such as /mnt/new_root, as many people do. The /mnt directory simply provides a formal location so you don’t have to make some arbitrary choice elsewhere in the directory tree. Typically in Linux, this mount feature is used to make the largest /usr directory where applications and data files get installed, to physically reside somewhere other than the boot partition. In our case, we’re simply mapping the physical device known to the /dev directory where you can’t treat it like a hard drive, to /mnt/sda1 where you can.