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.