Ubuntu Server: Adding A USB Hard Drive


Update: As per gourgi’s comment you can simply install a package called usbmount and this will automatically mount usb drives for you after boot.

sudo apt-get install usbmount

Never-the-less the remainder of this blog post might still be useful to you if you experience formatting or permission issues.

Today I needed to add an external USB hard drive to an Ubuntu Server virtual machine (VM) in VMware vSphere 4.1.

The process of passing through a USB device from vSphere to a VM is quite straightforward (as long as the device is supported) and is previously discussed here.

Once the drive was connected the Ubuntu terminal was kind enough to give me some information about my USB hard drive:

[...] sd 3:0:0:0:0: [sbd] ...

This was helpful because now I knew that my drive was located at /dev/sdb. Otherwise this command will list mounted and un-mounted drives:

sudo fdisk -l

I formatted the drive with the following command:

sudo  mkfs -t ext3 /dev/sdb

Note: I chose ext3 under the assumption that should be able to remove the drive and read the contents on a Windows 7 computer without too much difficulty.

If your drive has existing partitions Ubuntu will warn you and prompt you to continue:

/dev/sdb is an entire device not just one partition! Proceed anyway? (y,n)

I found that mkfs did not format the drive though – maybe because I had existing partitions on the drive?

Disk /dev/sdb doesn't contain a valid partition table

I proceeded to mount the drive and look for a work-around to the issue (here username refers to the username that you are logged in with):

sudo mkdir /media/sdb
su username
sudo mount /dev/sdb /media/sdb
sudo chown -R username /dev/sdb
sudo chown -R username /media/sdb

Now that the drive was mounted I used cfdisk to format the drive:

sudo cfdisk /dev/sdb

I accepted the defaults presented to me and the disk was now successfully formated.

All that was left for me to do was create a folder and set the permissions that I needed for my server application to access the drive:

cd /media/sdb
sudo mkdir shared
sudo chmod 777 shared

I could now point my server application to /media/sdb/shared to get things going!

Because this is a USB device I decided not to auto mount it during boot – I just created a simple script mount-usb-drive.sh that I could run when I needed:

#!/bin/bash
sudo mount /dev/sdb /media/sdb

Use chmod +x mount-usb-drive.sh to make the script executable and run the script as follows: ./mount-usb-drive.sh.

Sources:

http://ubuntuforums.org/archive/index.php/t-575230.html

http://ubuntuforums.org/showthread.php?t=283131

About these ads

2 thoughts on “Ubuntu Server: Adding A USB Hard Drive

  1. $ sudo apt-cache show usbmount
    Package: usbmount
    Priority: extra
    Section: universe/admin
    Installed-Size: 120
    Maintainer: Ubuntu Developers 
    Original-Maintainer: Rogério Brito 
    Architecture: all
    Version: 0.0.19.1ubuntu1
    Depends: udev (>= 136-1), lockfile-progs, util-linux (>> 2.16)
    Recommends: pmount
    Filename: pool/universe/u/usbmount/usbmount_0.0.19.1ubuntu1_all.deb
    Size: 16104
    MD5sum: e981b41638a432f7f1e332600f22212a
    SHA1: 1a808713af3d2d8fd480448523d7605f8767a1ec
    SHA256: 724b6e76c9eb2163849fe412a75fad9e0ace648d1994c17a6a9908297167881e
    Description: automatically mount and unmount USB mass storage devices
     This package automatically mounts USB mass storage devices (typically
     USB pens) when they are plugged in, and unmounts them when they are
     removed. The mountpoints (/media/usb[0-7] by default), filesystem types
     to consider, and mount options are configurable. When multiple devices
     are plugged in, the first available mountpoint is automatically
     selected. If the device provides a model name, a symbolic link
     /var/run/usbmount/MODELNAME pointing to the mountpoint is automatically
     created.
     .
     The script that does the (un)mounting is called by the udev daemon.
     Therefore, USBmount requires a 2.6 (or newer) kernel.
     .
     Firewire devices are also supported by USBmount.
     .
     USBmount is intended as a lightweight solution which is independent of
     a desktop environment. Users which would like an icon to appear when an
     USB device is plugged in should use the pmount and hal packages
     instead.
    Homepage: http://usbmount.alioth.debian.org/
    Bugs: https://bugs.launchpad.net/ubuntu/+filebug
    Origin: Ubuntu
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s