Thursday 2 May 2013

adding usb drives to raspberry pi

Powered USB Hub Required

In order to expand storage capacity, you will need to connect a powered USB hub to one of your Pi USB jacks, and then connect the keyboard, mouse/trackpad (if any), and USB drive to the hub. A USB hard drive can either have its own AC/mains power supply or it can have two USB plugs, and get a USB 2.0 hub with at least four expansion ports - more is better, within a reasonable cost. A four-port USB 2.0 hub will cost about US$7 - $15, and don't bother with a USB 3.0 hub, as the Pi only has a USB 2.0 interface.

Mounting Drive Partitions


You can plug in a USB flash or hard disk drive while the Pi is running without any problem, but, be sure to unmount a drive before unplugging it from the Pi (covered below). The USB device will be sensed by the Pi, however, it probably won't be added to the operating system's file system unless you are running an X window desktop such as LXDE, where USB drives should appear in the file manager. Running at just the command line will require some manual intervention to "mount" the device onto the file system. A flash or hard drive may be configured with one, or more, partitions, which you can create yourself in the unlikely event that the drive doesn't already have any. Creating partitions is covered in a separate tutorial.

USB Drive Hierarchy


Linux hardware devices are organized and identified by letters under the file system's device hierarchy, /dev. USB drives attached to the Pi are found under the file system as /dev/sdX, where "X" starts with the letter "a" representing the first USB drive, "b" for the second drive, "c" for the third, etc. The individual partitions on each drive are represented by incremented numbers, starting with "1". So, the first partition on the first USB drive is located in the file system as /dev/sda1, the second partition on that drive is /dev/sda2, the third partition on a third drive would be /dev/sdc3, etc.

Mount Points


In order to make a partition accessible to the file system, you need to use the Linux "mount" command, referencing the hardware (e.g., /dev/hda1) and a "mount point" in the file system, which is any empty directory, usually created for the purpose by the user. There is a canonical (i.e., standard) directory path where mount points are usually created: the /mnt directory. You can create a new directory under /mnt for each partition to be mounted, and it can be named anything you want as long as it doesn't contain any spaces. You might want to name it to correspond to the drive's physical characteristics, e.g., /mnt/1GB_USB_flash, or /mnt/120GB_USB_hard_disk.

Mounting a Partition


After creating the mount point for a partition, the only thing left to do is to actually mount the drive partition, e.g.:
sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/1GB_USB_flash
where:
-o (lowercase letter "o", not the number zero) specifies that an option string follows
uid=pi,gid=pi specifies the user ID is user "pi" and the group ID is "pi" (note there are no spaces allowed between these terms)
/dev/sda1 is the first partition on the first USB drive
/mnt/1GB_USB_flash is the mount point directory

Operations on a Mounted Partition


If all goes well, you will see nothing after pressing the Return key other than another command line prompt, which means there were no errors encountered during the mount process. You can then access the partition by referring it via /mnt/1GB_USB_flash/... where the "..." would be the file system path to the directories and files on which you want to perform an operation. For example, to list the details about the files at the top of the partition, you would simply type:
ls -l /mnt/1GB_USB_flash
To copy a file named "file1.txt" to another file named "file2.txt" in the directory "my_directory", you would type:
cp /mnt/1GB_USB_flash/my_directory/file.txt /mnt/1GB_USB_flash/my_directory/file2.txt
Of course, you can make this a lot simpler by just changing the current directory to my_directory like this:
cd /mnt/1GB_USB_flash/my_directory
To run an executable file "my_program.bin" while in that directory, you would type:
./my_program.bin
You can copy or move files and directories to or from any directory in the mounted partition by just referring to the appropriate path to that directory in the partition.

Unmounting a Partition Before Drive Disconnection


When you're done with a drive and want to disconnect it from the Pi, you should unmount the drive by using the "umount" command (notice there is no letter "n" between the "u" and the "m") and referring to the mount point:
sudo umount /mnt/1GB_USB_flash

Other mount and umount Features


You can learn more about mount and umount features by typing mount -h or mount --help,umount -h or umount --help, or via their "man" (manual) pages by typing man mount or man umount.

No comments:

Post a Comment

Amazon

Donate

Donate Towards More Raspberry PI's for Projects