Nov 5, 2009

How to add a second disk to your ubuntu box using command line

Recently I have installed ubuntu karmic (9.10) on my old box. It also had a second older disk (160Gb) with FreeBSD on it, which I wanted to use within this box. Adding another disk via command line is not that hard as you might think it is, but I don't reccomend doing it by yourself if you have just switched to ubuntu from window$. Anyways I have tested this howto on karmic koala and it worked like a breeze, but don't blame me when somthing goes wrong - use this howto at your own risk :P

At first I will post a short overview of what needs to be done:

1, Partition the disk with fdisk
2, Create the filesystem on the partition with mkfs
3, Mount the filesystem with mount and /etc/fstab file

1, 1, Check the disk with fdisk -l (this will list available disks /dev/sda - with my fresh karmic install and the second disk /dev/sdb - with my FreeBSD)


baban@brux:~$ sudo fdisk -l
[sudo] password for baban:

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x8f800000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       14219   114214086   83  Linux
/dev/sda2           14220       14593     3004155    5  Extended
/dev/sda5           14220       14593     3004123+  82  Linux swap / Solaris

Disk /dev/sdb: 160.0 GB, 160041885696 bytes
16 heads, 63 sectors/track, 310101 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk identifier: 0x406ab625

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1      310101   156290872+  a5  FreeBSD
baban@brux:~$

1, 2, Now we will delete the partition table on /dev/sdb disk and create a new linux partition on it with fdisk. Don't worry about the alert you get regarding the cylinder numbers - with ubuntu installed there is no need for dos partitions or LILO, while you are using GRUB. Just to describe what happens here:
a, after issuing the command sudo fdisk /dev/sdb we pressed "d" to "delete a partition"
b, then we pressed "p" which stands for "print the partition table" to see that FreeBSD partition has been deleted
c, we pressed "n" for "add a new partition" and selected "p" primary (you can only have 4 primary partitions max per disk) and "1" for the first partition. Then just hit enter for using default values with first and last cylinder options
d, now press "p" to print the partition table again - you should be able to see your new partition identified with Id 83 and System Linux (before repartitioning these values were Id a5  and System FreeBSD)
e, finally press "w" to "write table to disk and exit"

baban@brux:~$ sudo fdisk /dev/sdb
[sudo] password for baban:

The number of cylinders for this disk is set to 310101.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sdb: 160.0 GB, 160041885696 bytes
16 heads, 63 sectors/track, 310101 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk identifier: 0x406ab625

   Device Boot      Start         End      Blocks   Id  System

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

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

Command (m for help): p

Disk /dev/sdb: 160.0 GB, 160041885696 bytes
16 heads, 63 sectors/track, 310101 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk identifier: 0x406ab625

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      310101   156290872+  83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
baban@brux:~$

2, At this step we will create ext4fs on the partition /dev/sdb1 of the /dev/sdb disk
As Karmic comes with ext4fs by default I decided to create an ext4fs on the partition. It has numerous advantages comparing to ext3fs:
Extents
An extent is a way to improve the efficiency of on-disk file descriptors, reducing deletion times for large files, among other things.
Persistent preallocation
If an application needs to allocate disk space before actually using it, most file systems do so by writing 0s to the not-yet-used disk space. Ext4 permits preallocation without doing this, which can improve the performance of some database and multimedia tools.
Delayed allocation
Ext4 can delay allocating disk space until the last moment, which can improve performance.
More subdirectories
If you've ever felt constrained by the fact that a directory can only hold 32,000 subdirectories in ext3, you'll be relieved to know that this limit has been eliminated in ext4.
Journal checksums
Ext4 adds a checksum to the journal data, which improves reliability and performance.
Online defragmentation
Although ext3 isn't prone to excessive fragmentation, files stored on it are likely to become at least a little fragmented. Ext4 adds support for online defragmentation, which should improve overall performance.
Undelete
Although it hasn't been implemented yet, ext4 may support undelete, which, of course, is handy whenever somebody accidentally deletes a file.
Faster file system checks
Ext4 adds data structures that permit fsck to skip unused parts of the disk in its checks, thus speeding up file system checks.
Nanosecond timestamps
Most file systems, including ext3, include timestamp data that is accurate to a second. Ext4 extends the accuracy of this data to a nanosecond.

I have selected the blocksize 4096 (the -b 4096 option). So what is the blocksize good for - you may ask. Blocksize depends on what you are going to store on the partition. If there are small files, choose the small block size. If there are large files choose a large block size. I will have many large files, so I went for block size of 4096. According to man page valid block-size values are 1024, 2048 and 4096 bytes per block.

baban@brux:~$ sudo mkfs.ext4 -b 4096 /dev/sdb1
mke2fs 1.41.9 (22-Aug-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
9773056 inodes, 39072718 blocks
1953635 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
1193 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, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872

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

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
baban@brux:~$

3, Mounting the filesystem:
3, 1, I wanted to maunt my filesystem under /media/suflik so at first I needed to create a directory with that name

baban@brux:~$ cd /media;sudo mkdir suflik
baban@brux:/media$ ls -l
total 16
lrwxrwxrwx 1 root root    6 2009-10-30 18:16 cdrom -> cdrom0
drwxr-xr-x 2 root root 4096 2009-10-30 18:16 cdrom0
drwxr-xr-x 2 root root 4096 2009-10-30 18:16 cdrom1
lrwxrwxrwx 1 root root    7 2009-10-30 18:16 floppy -> floppy0
drwxr-xr-x 2 root root 4096 2009-10-30 18:16 floppy0
drwxr-xr-x 2 root root 4096 2009-11-04 01:05 suflik
baban@brux:/media$

3, 2, Now we can mount the fs under /media/suflik

baban@brux:/media$ sudo mount -t ext4 /dev/sdb1 /media/suflik
baban@brux:/media$

3, 3, If everything went according to the plan, you should be able to see the mounted fs after issuing df -h command
(notice the line /dev/sdb1             147G  188M  140G   1% /media/suflik)

baban@brux:/media$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             108G  4.8G   98G   5% /
udev                  502M  288K  501M   1% /dev
none                  502M  304K  501M   1% /dev/shm
none                  502M  192K  502M   1% /var/run
none                  502M     0  502M   0% /var/lock
none                  502M     0  502M   0% /lib/init/rw
/dev/sdb1             147G  188M  140G   1% /media/suflik
baban@brux:/media$

3, 4, Now we are going to add an entry to /etc/fstab file so the filesystem gets mounted every time you start up your box. We are going to use "uuid" of the disk, so we need to check for it using blkid command

baban@brux:~$ sudo blkid /dev/sdb1
/dev/sdb1: UUID="6b618e70-d316-4e80-9582-9adada72a2d1" TYPE="ext4"
baban@brux:~$

3, 5, Always create a backup of important files before aditing them - you can save a lot of headache :)

baban@brux:/media/suflik$ sudo cp /etc/fstab /etc/fstab_old
[sudo] password for baban:
baban@brux:/media/suflik$

3, 6, Add this entry to /etc/fstab (entry will consist of two lines - the first starting with hash character "#" followed by your description - this line is optional you can omit it if you want, the second line is important - use the uuid of your disk in the fstab file - not mine while mine would not work for you)

# suflik disk
UUID=6b618e70-d316-4e80-9582-9adada72a2d1 /media/suflik         ext4    relatime                0       2

3, 7, I also changed owner of the /media/suflik with reccursive -R flag so I can have full access to my disk without typing password all the time

baban@brux:~$ sudo chown -R baban:baban /media/suflik


3, 8, You can test if fstab entry is ok by unmounting and mounting the filesystem (when you get device is busy error while unmounting make sure you or some other user is not in /dev/suflik directory - in case you are, simply type "cd" to go to your homedir)

baban@brux:/media/suflik$ sudo umount /media/suflik/
umount: /media/suflik: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
baban@brux:/media/suflik$

After successfully unmounting the filesystem, mount it from fstab using this command

baban@brux:~$ sudo mount -a
baban@brux:~$

That is about it for this time, hope it didn't hurt :)

No comments:

Post a Comment