Adding a New Disk Drive to an Ubuntu 11.04 System

From Techotopia
Revision as of 19:43, 28 June 2011 by Neil (Talk | contribs)

Jump to: navigation, search
PreviousTable of ContentsNext
An Overview of Ubuntu OneAdding and Managing Ubuntu 11.04 Swap Space


<google>BUY_UBUNTU_11</google>


One of the first problems encountered by users these days is that they run out of disk space to store data. Fortunately disk space is now one of the cheapest of all physical technology commodities. Solutions to this problem involve either adding an external USB connected disk drive to the system or, in the case of desktop systems, adding one or more internal hard disk drives. In this chapter we will look at the steps necessary to integrate a new internal disk drive into an Ubuntu 11.04 system.


Contents


Getting Started

This tutorial assumes that the new physical hard drive has been installed on the system and is visible to the operating system. The best way to do this is to enter the system BIOS setup during the boot process and ensure that the BIOS detects the disk drive. Sometimes the BIOS will provide a menu option to scan for new drives. If the BIOS does not detect the disk drive, double check the connectors and jumper settings (if any) on the drive.

Finding the New Hard Drive in Ubuntu

Assuming the drive is visible to the BIOS it should automatically be detected by the Ubuntu operating system. Typically, the disk drives in a system are assigned device names beginning hd or sd followed by a letter to indicate the device number. For example, the first device might be /dev/sda, the second /dev/sdb and so on.

The following is output from a system with only one physical disk drive:

ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda5

This shows that the disk drive represented by /dev/sda is itself divided into three partitions, represented by /dev/sda1, /dev/sda2 and /dev/sda5.

The following output is from the same system after a second hard disk drive has been installed and detected by the operating system:

 
ls /dev/sd*
/dev/sda   /dev/sda1  /dev/sda2 /dev/sda5 /dev/sdb

As shown above, the new hard drive has been assigned to the device file /dev/sdb. At this point the drive has no partitions shown (because we have yet to create any).


Creating Linux Partitions

The next step is to create one or more Linux partitions on the new disk drive. This is achieved using the fdisk utility which takes as a command-line argument the device to be partitioned (in this case /dev/sdb):

$ sudo fdisk /dev/sdb
[sudo] password for johndoe:
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xc2fe324b.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): 

In order to view the current partitions on the disk enter the p command:

Command (m for help): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8a304bff

   Device Boot      Start         End      Blocks   Id  System

As we can see from the above fdisk output, the disk currently has no partitions because it is a previously unused disk. The next step is to create a new partition on the disk, a task which is performed by entering n (for new partition) and p (for primary partition):

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 

In this example we only plan to create one partition which will be partition 1. Next we need to specify where the partition will begin and end. Since this is the first partition we need it to start at cylinder 1 and since we want to use the entire disk we specify the last cylinder as the end. Note that if you wish to create multiple partitions you can specify the size of each partition by cylinders, bytes, kilobytes or megabytes.

Partition number (1-4): 1
First cylinder (1-1044, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044):
Using default value 1044

Now that we have specified the partition we need to write it to the disk using the w command:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

If we now look at the devices again we will see that the new partition is visible as /dev/sdb1:

$ ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda5 /dev/sdb  /dev/sdb1

Now that the disk has been successfully partitioned, the next step is to create a file system on our new partition.

Creating a Filesystem on an Ubuntu 11.04 Disk Partition

We now have a new disk installed, it is visible to Ubuntu and we have configured a Linux partition on the disk. The next step is to create a Linux file system on the partition so that the operating system can use it to store files and data. The easiest way to create a file system on a partition is to use the mkfs.ext4 utility which takes as arguments the label and the partition device:

$ sudo mkfs.ext4 -L /music /dev/sdb1
mke2fs 1.41.14 (22-Dec-2010)
Filesystem label=/music
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2096474 blocks
104823 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Mounting a File System

Now that we have created a new file system on the Linux partition of our new disk drive we need to mount it so that it is accessible. In order to do this we need to create a mount point. A mount point is simply a directory into which the file system will be mounted. For the purposes of this example we will create a /music directory to match our file system label (although it is not necessary that these values match):

sudo mkdir /music

The file system may then be manually mounted using the mount command:

sudo mount /dev/sdb1 /photos

Running the df -h shows us the space available on the all currently mounted file systems (including our new file system):

$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              11G  2.7G  7.7G  26% /
none                  495M  648K  494M   1% /dev
none                  501M  256K  501M   1% /dev/shm
none                  501M  368K  501M   1% /var/run
none                  501M     0  501M   0% /var/lock
/dev/sr0              686M  686M     0 100% /media/Ubuntu 11.04 i386
/dev/sdb1             7.9G  146M  7.4G   2% /music

Configuring Ubuntu to Automatically Mount a Filesystem

In order to set up the system so that the new file system is automatically mounted at boot time, an entry needs to be added to the /etc/fstab file. This may be edited by issuing the following command in a terminal window:

sudo gedit /etc/fstab

The following example shows an /etc/fstab file configured to automount our /music partition:

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
# /dev/sda1
UUID=4a621e4d-8c8b-4b39-8934-98ab8aa52ebc /               ext3    defaults,errors=remount-ro 0       1
# /dev/sda5
UUID=9c82bf09-c6f7-4042-8927-34e46518b224 none            swap    sw              0       0
/dev/scd0       /media/cdrom0   udf,iso9660 user,noauto,exec 0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec 0       0
/dev/sdb1       /photos         auto    defaults        0       0