ubuntu orange logoThe other day we needed to pull data off a restored VM running an older LTS version of Ubuntu Server. My IT contact helpfully attached a small 500 MB drive to the VMware vSphere server and then left me to do the rest. This is what was needed to be done:

I first ran the fdisk utility to get a listing of devices currently seen by the system.

sudo fdisk -l

This gave me a list of all devices, from which I was able to determine the size and logical name assigned to the newly added disk, in this case a 500mb drive located as /dev/sdb.

Unfortunately for me, this time around the drive wasn’t actually picked up as being formatted, and as such I needed to first partition the disk using fdisk:

sudo fdisk /dev/sdb 

fdisk will display the following menu:

  Command (m for help): m <enter>
  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)

  Command (m for help):

We want to add a new partition. Type “n” and press enter.

  Command action
   e   extended
   p   primary partition (1-4)

We want a primary partition. Enter “p” and enter.

  Partition number (1-4):

Since this will be the only partition on the drive, number 1. Enter “1” and enter.

If it asks about the first cylinder, just type “1” and enter. (We are making 1 partition to use the whole disk, so it should start at the beginning.)

Now that the partition is entered, choose option “w” to write the partition table to the disk. Type “w” and enter.

If all went well (i.e. “The partition table has been altered!” appeared), you now have a properly partitioned hard drive that’s ready to be formatted. Since this is the first partition, Linux will recognize it as /dev/sdb1, while the disk that the partition is on is still /dev/sdb.

Next up, I needed to format the new partition.

There are a variety of options you could choose from, but seeing as I was working with Ubuntu server, ext3 made sense for this particular use case:

sudo mkfs -t ext3 /dev/sdb1

Of course, if you prefer to use something that would work under both Ubuntu and Windows, then FAT32 remains a good option:

sudo mkfs -t fat32 /dev/sdb1

(Note I had to do the whole partition and format thing because of the nature of drive plugged in. If this had not been the case, the system would have recognised the drive appropriately – as seen in the fdisk listing – and you would be able to skip straight to this mount part of the walkthrough).

With the disk partitioned, formatted and now ready for use, the next step is to actually make it usable on the system, in other words mount it.

First up, create a mount point, basically the path through which you will access it. (I would recommend using a mount point with “/media”, as it is the default used by Ubuntu.)

sudo mkdir /media/external

Now to actually mount the drive:

sudo mount /dev/sdb1 /media/external

Note, depending on the filesystem of your attached drive, you might have to alter the mount string accordingly. If the filesystem is FAT16 or FAT32 (like it is for most USB flash drives), and we want to mount it at /media/external (having already created the mount point):

sudo mount -t vfat /dev/sdb1 /media/external -o uid=1000,gid=1000,utf8,dmask=027,fmask=137

The options following the “-o” allow your user to have ownership of the drive, and the masks allow for extra security for file system permissions. If you don’t use those extra options you may not be able to read and write the drive with your regular username.

Otherwise if the device is formatted with NTFS, run:

sudo mount -t ntfs-3g /dev/sdb1 /media/external

And you should be good to go, happily copying data to and from /media/external. When you are done and need to plug out the device, first unmount it using:

sudo umount /media/external


sudo umount /dev/sdb1

Note that the unmount command will fail if your current working directory is in the /media/external path. You need to step out of the disk if you want to unmount it! :)

Related Links: https://help.ubuntu.com/community/Mount/USB, https://help.ubuntu.com/community/InstallingANewHardDrive