2012-04-15 02:14:00

Abel Gancsos
format a new hard drive in Unix

So, you have been visited by the gigabyte fairy and you have a brand new external USB hard drive, you might be irked to find that it's already formatted with the NTFS filesystem because it's a default world out there ...
What to do?

(This will work for any old hard drive, so even if you have an internal one - you're good )

Stuff I assume
Your drive needs to be plugged-in and your system must see it. You should see an icon appear on your desktop. If not then open a console and try this:
If you have an external USB drive:
Code:
cd /dev
ls s*
If you have an internal drive:
Code:
cd /dev
ls hd*
You should see a list of things something like this for example:
Code:
sda sda1 sdb sdb1
That shows you that your system knows about two physical external drives (sda and sdb) as well as the partition that is on each. So, now you know that your machine knows about your new drive(s).

Make sure that your new drive is not mounted. Let's assume it's name is sda1 (please substitute your own details), at the console type:
Code:
mount | grep sda1
If you see any results then it's mounted, so do this:
Code:
umount -l /dev/sda1
Warning
Please ensure that you know exactly which drive you are going to erase and format... I speak from experience, hoo-boy! So, make sure you know which name identifies your drive. hdb or sda or sdc or whatever. Be sure. If you are not sure, then use whatever built-in tools that your distro has to try and get yourself oriented. You can also do this at the console:
Code:
fdisk -l
Now look at all the drives carefully, look at their types and sizes and make your mind up.

GUIs and Consoles
I use Kubuntu 6.06. I quite frankly could not figure out how to prepare my new USB drive from any of the system control panels. It was too confusing and ambigous. I don't know (and can only hope) whether Ubuntu is any better. The upshot? I will use the command line!

Let's get going!
Removing and creating a partition
Having decided your drive's name (let's call it sda) we must now remove the default partition that the manafacturer put there:
Code:
sudo fdisk /dev/sda
This will start fdisk. We want to remove the partion(s), there should only be one.
Press d (for delete).
It might ask you for the partition number, press 1 and enter. If there are more then delete them too.

Now to make a new partition.
Press n (for new), then p (for primary), then 1 and then simply press enter for the next two questions.
This will make a new partition that uses the entire disk. If you want more complex partitioning then read the fdisk manual (man fdisk) or use parted or some other app.
Here's what we did with n, the values will differ from yours:
Code:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (197-621, default 197):
Using default value 197
Last cylinder or +size or +sizeM or +sizeK (197-621, default 621): +128M
Now to write the new partion and exit, press w and enter.
You should be ready to make a filesystem now.

Making a filesystem : "Formatting"
You need to put a system in-place, on the disk, such that it can handle files. This is called (unsurprisingly) a filesystem.
The one we are going to use is called "ext3". On Gnu/Linux we are spoiled for choice and there are loads of filesystems you can use, go do some research if you want to.
So, let's make the filesystem:
Code:
sudo mkfs.ext3 /dev/sda1
* Note the 1 at the end, because we are making the filesystem in that partition (thanks Mike)

Now it will go off and do strange stuff, simply wait for it to finish.

Using the new file system
Well, at this point you should be able to right-click on the icon (on your Desktop) and choose "mount" (I assume that's the verb it will present to you). After that you should be able to open a window and use the drive*
* all this assumes it's an external USB drive.
If you cannot then you will need to mount it yourself, try:
Code:
sudo mkdir /media/sda1
sudo mount /dev/sda1 /media/sda1
Note: We are actually mounting the first partition on the drive, hence the 1 at the end: sda1
And the mounted directory can be anywhere you like, but its common location is /media.

If those two steps worked then you are 90% done.

You may need to make a single folder in the new drive and give it your user permissions:
Code:
cd /media/sda1
sudo mkdir afolder
chown you:you afolder
Where you insert your username and desired folder name as appropriate.

Please search around on the subject of your fstab and getting the drive to mount automatically when you reboot. There are plenty of howto's out there on that subject. I must post and run now.


Sidenote:
I think you intend to mkfs.ext3 the partition, not the whole device, hence:
Code:
sudo mkfs.ext3 /dev/sda1
Also, you need to specify a mount point for the device, since it's new, it won't be in fstab, so this:

Code:
sudo mount /dev/sda1
Should be this:

Code:
sudo mkdir /where/you/want/this/drive/to/be/mounted
sudo mount /dev/sda1 /where/you/want/this/drive/to/be/mounted