20120107

How do you install/mount a new drive in Ubuntu?


These instructions focuses on Ubuntu, but it maybe possible to work on other distributions.

1. Physically install the drive into the machine (internally/externally).

Once this is done, use the following command:
# Scan the hardware, displays only disk class.
> sudo lshw -C disk
This will produce quite a few entries. Focus only on the ‘*-disk’ entries below to find the correct hardware (Note: only *-disk, not *-disk:0 or *-disk:1).
*-disk
 description: ATA Disk
 product: WDC WD2500JB-00G
 vendor: Western Digital
 physical id: 0
 bus info: scsi@0:0.0.0
 logical name: /dev/sda
 version: 08.0
 serial: WD-WCAL77000695
 size: 232GiB (250GB)
 capabilities: partitioned partitioned:dos
 configuration: ansiversion=5 signature=7b56ae05
After verifying that this is the correct disk (in this example, it’s the WD 250GB IDE disk), the item of interest is the logical name (e.g. /dev/sda).

2. Setup partitions

If there is no need to setup partitions/format the disk, skip the step #4.
Fdisk is the proper tool to create partitions on the disk. Type the following command, replacing the example with the correct logical name:
# Ready to format the disk
> sudo fdisk /dev/sda
This will bring up a menu with several options:
Command (m for help): m
Command action
 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)
To create a new partition, press ‘n’. If you want the format the whole thing, it’s probably better to type ‘o’ first to erase the whole partition table.
Once n is selected, it’ll ask you for primary or extended partition. There can only be 4 partitions in a disk; this is a hardware limitation. Extended partitions is a workaround since through one extended partition multiple partitions can be created. For simplicity sake, create only one partition here (select p):
Command action
e   extended
p   primary partition (1-4)
> p
Follow through with the instruction, and select 1.
Partition number (1-4): 1
Now comes the cylinder information. This determines how big the disk should be. For simplicity, select the default values (it will try to choose the largest size possible)
First cylinder (1-30401, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-30401, default 30401):
Using default value 30401
The partition table is now created. To view the newly created partition, type ‘p’
Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x24b47c92

 Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1       30401   244196001   83  Linux
Type ‘w’ to save the settings. (q to quit without saving changes)
The partition table has been altered!

3. Format disk

If there is no need for formatting the disk, skip to step #4.
‘mkfs’ is the command used for formatting. It is possible to choose a specific filesystem (ext2, ntfs, fat32, etc.) when formatting. For Ubuntu, ext3/2 is probably the best, though ntfs should be better if the disk shares with Windows.
# Formats the disk (mkfs -t )
> sudo mkfs -t ext3 /dev/sda1

4a. Mount the drive (manually)

The simpliest way to mount the drive is to use the ‘mount’ command:
# Mount drive (mount )
> sudo mount /dev/sda1 /backup
When done, unmount the drive using ‘umount’ command:
# Unmount the drive
> sudo umount /backup
Note: /backup needs to exist for the mount to work; otherwise, it will complain.

4b. Mount the drive (bootup)

Most likely, the manual option is useless for almost all situations, especially internal hard drives. Another method is to mount it during bootup time. For that, /etc/fstab needs to be editted. Ubuntu uses UUID instead of logical names to mount the drives (though logical names are certainly supported). This is slightly better, since it is possible for logical names to associate to different hardware if the system’s hardware changes. UUID sticks to its hardware and does not change.
To determine the UUID of a drive, type the following command:
# Find the UUID of the WD disk.
> sudo vol_id -u /dev/sda1
6486a213-45ad-2aa3-5c23-0d712a3465ds
Now, add that entry into the /etc/fstab file:
# /etc/fstab: static file system information.
#
#                
proc            /proc           proc    defaults        0       0
# /dev/sdc1
UUID=58a8346d-471a-4751-8fea-7456234bcdac /       ext3 defaults 0 1
# Backup media (IDE disk)
UUID=6486a213-45ad-2aa3-5c23-0d712a3465ds /backup ext3 defaults 0 1
When the system reboots, it should automatically mount the disk.
Note: the directory that the device is mounting to needs to exist for this to work. In this case, /backup had to be created manually first before it will mount. Otherwise, it will give an error and refuse to mount.

4c. Mount the drive (automatically)

The best way to mount a drive automatically other than bootup time is to still use /etc/fstab. If it’s a harddrive, the UUID method is still the best. However, each storage devices requires its UUID to be added into /etc/fstab, and this can become a tiresome hassle to do.
There are scripts to handle the auto-mounting (Ubuntu does a decent job if you brought the machine up to GUI level), but explaining it goes beyond the scope of this post.

References:

        More articles  

No comments:

Post a Comment