Sunday, March 23, 2008

WD's 'My Book' Product & Linux

note: This article is intended for a technical audience -- you should use extreme caution when modifying a production system, as your data will be nearly impossible to recover if you use this command incorrectly -- caveat emptor.

Today I picked up one of the new 'essential edition' 750 GiB USB External Hard Drives -- very nice piece of kit, it looks like the volume of a book and fits on the bookshelf (albeit if one runs the power supply and related cords down the back of said bookshelf first) as advertised on the box.

To make it work under Linux wasn't hard either.

It comes with a FAT32 (vfat) filesystem by default and includes Windows and MacOSX versions of Google Desktop, Skype and a few other things.

The procedure here was run on OpenSuSE 10.3, but any semi-recent Linux distribution will do -- it uses one command, mke2fs -- which is present on all Linux distributions back to the dark ages:

First, unmount the drive (either from the right-click desktop option, or from the command line)

Second, open a terminal and sudo to root.

Third, format the drive -- I use ext3 filesystems, so my command line was:

mke2fs -j -L "My Book" -m 1 -O dir_index,sparse_super -T largefile /dev/sdX1

Where sdX is the WD device, I bought two of them and they were sdd and sde respectively. (note: this is never usually /dev/hda or /dev/sda -- please make sure you're writing to the correct device before pressing ENTER, type once, look twice!).

As for options, I used those to get the most storage and speed from my new drives:

-j -- Adds an ext3 journal to the filesystem.
-L "My Book" -- Adds a Label to the new drive (by default, the drive mounts out of the box with a label of 'My Book', so I use that, but you could call yours anything as long as it is less than 16 characters long, just remember to put it in double quotes on the command line.
-m 1 -- Uses 1% of the total drive space (750MiB) as reserved blocks for the root user, which is handy to have when the drive dies and one needs to run recovery tools to get back their data.
-O dir_index,sparse_super -- Two options, seperated by commas -- one (dir_index) to speed up lookups in large directories (say, one with the contents of your digital camera or music collection) and the other (sparse_super)to gain a few extra megabytes of usable space by not creating as many superblock backups (on a 750GiB drive, this option creates 24, rather than 80-odd due to the sheer size)
-T largefile -- Creates a filesystem suitable for medium to large files (a few megabytes or more per file) rather than one suited to a standard home directory (lots of files worth a few kilobytes, intermixed with some larger ones).

Once you have pressed ENTER, the mke2fs will format your drive using the parameters specified on the command line, then return you to the prompt.

Fourth, you will need to give your user account permission to write to the drive (at the moment, only root can do this) -- as root, type:

chown -R username:users

Where username is your username, mine's paul, yours probably isn't.

Finally, remove the USB cable and plug it back in -- if you're running a recent Linux distribution (2006 onwards) it should re-appear on your desktop as 'My Book' with an unreadable directory called lost+found.

Now you can use your drive as normal, with a native filesystem, access rights and all the rest.