Storage device information (partitioning, formatting and mounting in Linux)
Disk drive and partition information
To find out information about disk drives and partitions on a system:
- [as root] fdisk -l
- Note: the command must be entered as root (administrator), so for Ubuntu, you would type: sudo fdisk -l and then enter your root password
Partitioning
A drive can be partitioned using the fdisk utility. Start fdisk using the following command:
- [as root] fdisk /dev/hdDrive
- Note: where [as root] is your superuser account and where /dev/hdDrive is the location of your device
Some basic fdisk commands include:
- m lists the options available
- p prints the partition table
- n creates a new partition
- d deletes a partition
- q quit without saving changes
- w write the new partition table and exit
Formatting
All storage devices require some form of file system so that they can be used by an operating system.
The most popular file systems in use for Linux desktop computers are ext2 and ext3. Simply put, ext3 is a newer version of ext2 with journalling, which allow the system to recover from serious errors more easily.
A device can be formatted using the utility mke2fs:
- mkfs.ext3 -m /dev/hdDrive
Mounting
To mount a drive:
- mount /dev/hdDrive /mnt/hdDrive
- Note: where /dev/hdDrive is your drive and /mnt/hdDrive is your mount point (A mount point is just a directory on your system, which can be created using mkdir: mkdir /mnt/hdDrive).
To unmount a drive:
- umount /mnt/hdDrive
- Note: where /mnt/hdDrive is your mount point
To auto mount a drive at start up edit the fstab file:
- vi /etc/fstab
A new line needs to be added to the end of the file. This should contain:
- the device location (1st column)
- the mount point (2nd column)
- the file system type (3rd column)
- other options
An example drive [hdDrive] mounted [/mnt/hdDrive] as ext3, is:
- /dev/hdDrive /mnt/hdDrive ext3 defaults 0 0
Always make sure there is a carriage return at the end of the last line or else the device will not mount
Adding a new hard disk drive (complete):
Step 1:
Physically install the hard drive and boot up the computer.
Step 2:
If the device has been recognised it will have been given a location on the system but will not be accessible until it is formatted and mounted. To find out the location of the drive, use the fdisk command (enter command AS ROOT):
- fdisk -l
Step 3:
Once you have found the location of your drive, you can use fdisk to partition it:
- fdisk /dev/hdDrive
- Note: the example shows the drive location as /dev/hdDrive but it will most probably be something like /dev/hdb
Enter p to view the partition table. Here you can check for any existing partitions (enter d and press return to delete) and check you are partitioning the correct drive by checking the size is as you expected.
Enter n and press return and then enter p to create a new primary partition. When asked for a number enter 1.
Next you will be asked which cylinder the partition should begin and then end at. You can accept the default values for both of these by just pressing return at each prompt.
Now back at the fdisk command prompt, enter w to write the partition table and exit fdisk.
Step 4:
Format the drive (as ext3):
- mkfs.ext3 -m /dev/hdDrive
Step 5:
Create a directory to mount the drive in:
- mkdir /mnt/hdDrive
Step 6:
To auto-mount the drive on boot, open the fstab file (enter command AS ROOT):
- gedit /etc/fstab
Add this line: /dev/hdDrive /mnt/hdDrive ext3 defaults 0 0 to the end of fstab (making sure there is a carriage return at the end of the last line)
Step 7:
Restart the system and the new drive should be accessible from the mount point you created in step 5.
Accessing a USB drive:
Step 1:
Insert the USB drive into an available slot.
Step 2:
If the device has been recognised it will have been given a location on the system but will not be accessible until it is formatted and mounted. To find out the location of the USB drive, use the fdisk command (enter command AS ROOT):
- fdisk -l
Step 3:
Create a directory for the USB drive:
- mkdir /mnt/usbDrive
Step 4:
Mount the USB drive:
- mount /dev/usbDrive /mnt/usbDrive
- Note: the example shows the drive location as /dev/usbDrive but it will most probably be something like /dev/sda1
Go back to the How-to Guides main page.
