So you’ve got a shiny new hard drive that you want to add to your Linux system but not quite sure what to do once you open the case and plug the drive in. Or you’re brand new to Linux and you need to know how to partition and format your drive for a new install. This guide will help you accomplish that so that you can use all of that new space.
But don’t fret, oh external drive users and flash memory device owners. This guide also applies to all external and internal storage devices. If you can store files on it, we’ll format it.
Newbie note: I’m not going to cover actually how to install your new hard drive if it’s an internal model or how to plug an external drive into the USB port. This guide starts from the point that you’ve installed it or plugged it in. You also need to be familiar with how to use the shell, issue commands from the command line and log in as root.
A note on any Warnings that you might see. Pay attention to these! Warning notes tell you something important that can help you avoid trashing your system. Technical notes are interesting to know and might help you understand the topic in more detail, but you can safely skip over them if you want to.
Prepping for the format
So what are you using this storage device for? Is this just one large chunk of storage or do you need to slice this space up? What file system do you want to use? Ext2? ReiserFS? Vfat? If you need to access the information on this drive on both Windows and Linux systems then you should use a format that Windows can read. Linux can read a great many filesystem types, but Windows is very limited. Vfat would be the best choice for your new filesystem if you need to access this drive in Windows and Linux. If it’s for Linux only, use a Linux filesystem like ext2, ext3 or reiserfs.
Basically, a filesystem describes how information is stored and organized on your storage device. When you buy a new hard drive from the computer store, you must format the drive with a filesystem type that your operating system can use. Windows uses NTFS as its default filesystem for hard drives nowadays and vfat for removable storage, like USB memory sticks. Linux can use a wide variety of filesystems. The most common are ext2, ext3 and reiserfs but Windows cannot “see” any drives formatted as these filesystem types unless you install a viewer for that specific type. However, Linux can use NTFS and vfat drives which is why I recommend you use one of the Windows types if you’re going to use the drive on both Windows and Linux. Otherwise, any type is fine.
Before you can format this new drive, you have to partition it. We will use the fdisk utility to accomplish this task. But before you partition the drive, you must know what drive it is. If you already have a drive on your system then we don’t want to accidentally re-partition that drive and wipe out all of your data.
WARNING: Partitioning a drive will wipe out any data currently on that drive. Always back up your data before performing any partition work.
So, let’s figure out what drive you currently have. If it’s an IDE drive, Linux uses /dev/hd* for the device designation where the asterisk (*) represents a letter. Usually, the first IDE drive on the system will be /dev/hda. Partitions on that drive are represented as numbers after the hda part; i.e. hda1 for the first partition, hda2 for the second, and so on…
SATA, SCSI, and USB drives will use a similar designation, but instead of /dev/hd* it will be /dev/sd*
To see what you currently have mounted, there are a couple of ways to check. The df is what we’re going to use and it shows the filesystem name, free space, space used, total size and how it’s mounted. Open up a terminal if you’re in X or log into the system and type the df -h command like this:
user@linux~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hda1 8.3G 2.4G 5.5G 30% /
/dev/hda2 99M 26M 69M 27% /boot
From the df command output above you can see on this imaginary system you have one drive, /dev/hda, which is an IDE drive. It is sliced up into 2 partitions, / and /boot.
The -h switch tells the df command to format the output as “human readable”. Basically it displays the size, used and available space on the filesystem in terms of kilobytes, megabytes, etc. instead of blocks.
If you were to add another IDE drive to this imaginary system, it would be known as /dev/hdb. Add another drive and you’ve got /dev/hdc, and so on.
Go ahead and physically add your drive to the system now if you haven’t already. When you’ve identified what device Linux is calling your new drive, go on to page 2 for partitioning your new storage device.
Partitioning the drive
On my imaginary system here, the drive I added is a 50GB IDE drive which the system calls /dev/hdb. First, I will log in as root and this is what I will pass to the fdisk command in order to partition this drive:
user@linux~# fdisk /dev/hdb
The fdisk command may not seem very user friendly at first, but if you follow the prompts you’ll get through it. At any time you can type “m” and press enter to display the menu.
Command (m for help): m
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)
Command (m for help):
I’m going to use this drive for storage only, so I will create one primary partition and use all available space on the drive. To do this, I choose new partition type by choosing “n” at the Command prompt:
Command (m for help): n
p primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-6081, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-6081, default 6081): 6081
Command (m for help):
Then I choose p for primary, 1 for the first partition and I then specify the first cylinder and last cylinder for the size. As you can see from the prompt you can also specify the size of this partition using the +size keywords.
You can only have 4 primary partitions on a storage device. If you need more than that, you must create an extended partition which is just a container that you can slice up even more with “logical partitions”. You can only have one extended partition and it counts against your 4 primary partition limit. It even has it’s own filesystem type of 0×05!
Under an extended partition, you can theoretically have an unlimited number logical partitions, although Linux limits the total system partition count to 63 on an IDE disk and 15 on a SCSI disk.
For more information on partitioning, reference the Linux Partitioning How-To.
None of your partition changes will take effect until you use the “w” command at the fdisk command prompt. This option writes the new partition table to the disk you are currently working with and exits the program. You might have to reboot your system if you have partitioned your primary disk in order for Linux to recognize the new partition structure.
Remember that nothing is permanent until you use the “w” fdisk command. If you mess up and want to start all over, you can always exit the fdisk program with the “q” option. This exits without saving any changes.
Note: If you are working with a disk that has existing partitions, you can delete the old partitions with the “d” fdisk option. Follow the on-screen prompts and you can then repartition the drive using the above directions.
Now that we have the new drive sliced up they way we want it, let’s format the filesystems. Go on to page 3 for those instructions or you can stay on this page if you are not completely sure you are done with partitioning.
Formatting the new filesystems
This is perhaps the easiest part of the process thus far. The mkfs command formats the partition with the filesystem you want to use. This command has a number of options that you can pass it and here is the general form of the command:
mkfs [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]
The -V switch causes the mkfs command to produce verbose output. You’ll see more of what mkfs is doing with this switch.
The -t switch tells the mkfs command what filesystem type you want to use. If I want to use the vfat filesystem on the first partition on my new imaginary drive for compatibility with Windows, pass the vfat option to the mkfs command (as root):
mkfs -t vfat /dev/hdb1
You can specify ext2, ext3, vfat, msdos (old fat), minix, aix, and many more types. If you leave this option out, the mkfs command will use the default Linux filesystem.
The filesys is the partition you want to format. For example, the first partition on my imaginary drive is /dev/hdb1.
Blocks refers to the size of the filesystem you want to create. We’re going to leave this option off at this time, since we’re formatting the entire partition.
Now your new drive or USB memory stick is ready to use. All that’s left is mounting the drive (which Windows and most modern Linux distro’s do automatically) and using it! Hopefully you’ve learned some important system administration concepts from this How-To guide and have less fear about working on the Linux shell. Performing these basic system administration tasks outlined here will help you use Linux and BSD to the fullest.
Continue to read! Don’t stop learning about your system. A great place to start is the Linux Documentation Project and the How-To guides that we have mirrored here on Open Addict.