Parts
- Description
- Notes
- Recipe
- Example
Description
This recipe describes a method of manually mounting a USB flash drive on a computer filesystem.
Notes
An earlier version of this recipe was published in the article Storing bitcoin on an offline Raspberry Pi, in the section Recipe For Manually Mounting A USB Memory Stick. The recipe shown here has been rewritten and retested for republication.
This recipe was originally derived from work published in the article How I bought and stored some bitcoin [paywalled].
Several points concerning filesystems and mounting:
- A filesystem can be considered to be a tree. Directories are branches and may contain subdirectories (subbranches). Files are leaves on a branch. The filesystem tree begins at the root directory
/
. - Another filesystem (e.g. the one on a flash drive storage device), which is also a tree, can be attached to the existing filesystem on a computer. This allows tools on the computer to access data on the secondary filesystem as if it were simply a normal branch of the primary filesystem. The process of attachment, called "mounting", connects the root of the secondary filesystem to an existing directory (henceforth called the "mountpoint" directory) on the primary filesystem. One can refer to this as "mounting a device to a mountpoint directory".
- The reverse process of detachment is called "unmounting". Once unmounted, a storage device can be physically unplugged without causing any problems.
Recipe
0) Copy any data that you wish to transfer to the destination computer into a directory named "transfer", located on the top level of the flash drive filesystem.
1) If you are using a command-line interface on the destination computer, skip this step. If you are using a graphical user interface on the destination computer, open a terminal emulator program.
2) Use the following command to: Make a new directory to be the mountpoint for the flash drive. Making this new directory in the /mnt directory requires root permissions (the "root" user owns the /mnt directory), so "sudo" is used.
sudo mkdir /mnt/mountpoint_directory
3) Insert the flash drive into a USB port on the destination computer.
4) Use the following command to: Mount the flash drive located at the device file /dev/sda1 to the directory /mnt/mountpoint_directory.
sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/mountpoint_directory
[
Note that:
-o (lowercase letter "o", not the number zero) specifies that an option string follows.
uid=pi,gid=pi specifies that the user ID is "pi" and the group ID is "pi" (note there are no spaces allowed between these two terms).
/dev/sda1 is the first partition on the first USB flash drive that the computer has detected. [Future: Investigate whether this is accurate.]
]
5) Use the following command to: Make a new directory to hold a copy of the contents of the flash drive.
mkdir flash_drive_contents
6) Use the following command to: Copy the contents of the the "transfer" directory on the flash drive to the directory "flash_drive_contents".
cp -r /mnt/mountpoint_directory/transfer/* flash_drive_contents/
7) Use the following command to: Copy a file named "work.log" from the current working directory to the flash drive. This demonstrates how to transfer information from the destination computer onto the flash drive.
cp work.log /mnt/mountpoint_directory/
Note: The trailing slash after "mountpoint_directory" is a useful habit that prevents the accidental overwriting of a file. Without a trailing slash, if "mountpoint_directory" were actually a file (but you thought it was a directory), this command would overwrite it with the contents of work.log. With the trailing slash, an error is raised instead.
8) Use the following command to: Unmount the flash drive. Making changes to the /mnt directory requires root permissions, so "sudo" is used. Once unmounted, the flash drive can be physically unplugged without causing any problems.
sudo umount /mnt/mountpoint_directory
9) Unplug the flash drive from the destination computer.
Example
The destination computer: A Raspberry Pi, Model B+ V1.2 [circa 2014]. It has various ports, including 4 USB ports (which allows simultaneous use of a mouse, a keyboard, and a flash drive) and an HDMI port. It does not have an onboard WiFi chip. I have various peripherals for the Raspberry Pi. These are: An 8GB micro SD card with an installed operating system, a Raspberry Pi power cable, a screen with a DVI port, a screen power cable, a DVI male-male connector cable, a DVI female to HDMI male converter, a USB keyboard, a USB mouse, and a plastic case customised for the Raspberry Pi Model B+. The installed operating system is Raspbian GNU/Linux 7 (wheezy) and the Linux kernel version is 4.1.13+. Tools: nano 2.2.6, tee 8.13, Python 2.7.3.
USB flash drive: 8 GB, FAT32 format, name = "NO NAME"
The FAT32 filesystem is very portable (i.e. supported by many operating systems).
Step 0) Copy any data that you wish to transfer to the destination computer into a directory named "transfer".
Using a work computer (a Macbook running Mac OS X 10.6.8 Snow Leopard), I have created a directory on the flash drive named "transfer". I have created two files in this directory.
File 1 is named "test.txt" and contains the string "hello".
File 2 is named "test2.txt" and contains the string "world".
Extra Step) Boot and log into the Raspberry Pi.
Insert the power cable into the Raspberry Pi. Wait for it to boot up.
Log into the Raspberry Pi using the default credentials (username = "pi", password = "raspberry"). Note that the password is not printed to the screen.
raspberrypi login: pi
Password:
Last login: Sun Mar 28 19:13:20 UTC 2016 on tty1
Linux raspberrypi 4.1.13+ #826 PREEMPT Fri Nov 13 20:13:22 GMT 2015 armv61
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
pi@raspberrypi ~ $
Step 1) If you are using a command-line interface on the destination computer, skip this step. If you are using a graphical user interface on the destination computer, open a terminal emulator program.
The Raspberry Pi booted into a command-line interface.
Step 2) Make a new directory to be the mountpoint for the flash drive.
[Make a new directory to be the mountpoint for the flash drive. Making this new directory in the /mnt directory requires root permissions (the "root" user owns the /mnt directory), so "sudo" is used.]
[List items in the directory "/mnt"]
mountpoint_directory
pi@raspberrypi ~ $ sudo mkdir /mnt/mountpoint_directory
[List items in the directory "/mnt"]
pi@raspberrypi ~ $ ls -1 /mnt
mountpoint_directory
Step 3) Insert the flash drive into a USB port on the Raspberry Pi.
Step 4) Mount the flash drive located at the device file /dev/sda1 to the directory /mnt/mountpoint_directory.
[List items in the directory "/dev" that begin with "sda"]
/dev/sda
/dev/sda1
[Mount the flash drive located at the device file /dev/sda1 to the directory /mnt/mountpoint_directory]
[
Note that:
-o (lowercase letter "o", not the number zero) specifies that an option string follows.
uid=pi,gid=pi specifies that the user ID is "pi" and the group ID is "pi" (note there are no spaces allowed between these two terms).
/dev/sda1 is the first partition on the first USB flash drive that the computer has detected. [Future: Investigate whether this is accurate.]
]
[List the contents of the top directory in the flash drive filesystem]
transfer
[List the contents of the "transfer" directory in the flash drive filesystem]
test2.txt
test.txt
pi@raspberrypi ~ $ ls -1 /dev/sda*
/dev/sda
/dev/sda1
[Mount the flash drive located at the device file /dev/sda1 to the directory /mnt/mountpoint_directory]
pi@raspberrypi ~ $ sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/mountpoint_directory
[
Note that:
-o (lowercase letter "o", not the number zero) specifies that an option string follows.
uid=pi,gid=pi specifies that the user ID is "pi" and the group ID is "pi" (note there are no spaces allowed between these two terms).
/dev/sda1 is the first partition on the first USB flash drive that the computer has detected. [Future: Investigate whether this is accurate.]
]
[List the contents of the top directory in the flash drive filesystem]
pi@raspberrypi ~ $ ls -1 /mnt/mountpoint_directory
transfer
[List the contents of the "transfer" directory in the flash drive filesystem]
pi@raspberrypi ~ $ ls -1 /mnt/mountpoint_directory/transfer
test2.txt
test.txt
Step 5) Make a new directory to hold a copy of the contents of the flash drive.
[List the contents of the current working directory]
Desktop
Documents
Downloads
indiecity
python_games
Scratch
[Make a new directory to hold a copy of the contents of the flash drive]
[List the contents of the current working directory]
Desktop
Documents
Downloads
flash_drive_contents
indiecity
python_games
Scratch
pi@raspberrypi ~ $ ls -1
Desktop
Documents
Downloads
indiecity
python_games
Scratch
[Make a new directory to hold a copy of the contents of the flash drive]
pi@raspberrypi ~ $ mkdir flash_drive_contents
[List the contents of the current working directory]
pi@raspberrypi ~ $ ls -1
Desktop
Documents
Downloads
flash_drive_contents
indiecity
python_games
Scratch
Step 6) Copy the contents of the the "transfer" directory on the flash drive to the directory "flash_drive_contents".
[Copy the contents of the the "transfer" directory on the flash drive to the directory "flash_drive_contents"]
[List the items in the flash_drive_contents directory]
test2.txt
test.txt
[Check the type of each item]
flash_drive_contents/test.txt: ASCII text, with no line terminators
flash_drive_contents/test2.txt: ASCII text, with no line terminators
[Print the contents of each item]
hello
world
[Note: The files test.txt and test2.txt lack final newline bytes, so the next command prompt starts immediately after the printed text ("hello" or "world"). Newlines have been added here in order to make this output easier to read.]
pi@raspberrypi ~ $ cp -r /mnt/mountpoint_directory/transfer/* flash_drive_contents/
[List the items in the flash_drive_contents directory]
pi@raspberrypi ~ $ ls -1 flash_drive_contents
test2.txt
test.txt
[Check the type of each item]
pi@raspberrypi ~ $ file flash_drive_contents/test.txt
flash_drive_contents/test.txt: ASCII text, with no line terminators
pi@raspberrypi ~ $ file flash_drive_contents/test2.txt
flash_drive_contents/test2.txt: ASCII text, with no line terminators
[Print the contents of each item]
pi@raspberrypi ~ $ cat flash_drive_contents/test.txt
hello
pi@raspberrypi ~ $ cat flash_drive_contents/test2.txt
world
[Note: The files test.txt and test2.txt lack final newline bytes, so the next command prompt starts immediately after the printed text ("hello" or "world"). Newlines have been added here in order to make this output easier to read.]
Step 7) Copy a file named "work.log" from the current working directory to the flash drive.
[Print the path of the current working directory (pwd = "print working directory")]
/home/pi
[Create a file named "work.log"]
[Write "testing123" to work.log. Note: The
[List the contents of the current working directory]
Desktop
Documents
Downloads
flash_drive_contents
indiecity
python_games
Scratch
work.log
[Copy work.log from the current working directory to the flash drive]
[Note: The trailing slash after "mountpoint_directory" is a useful habit that prevents the accidental overwriting of a file. Without a trailing slash, if "mountpoint_directory" were actually a file (but you thought it was a directory), this command would overwrite it with the contents of work.log. With the trailing slash, an error is raised instead.]
[List the contents of the top directory in the flash drive filesystem]
transfer
work.log
[Print the contents of the resulting file on the flash drive]
testing123
pi@raspberrypi ~ $ pwd
/home/pi
[Create a file named "work.log"]
pi@raspberrypi ~ $ touch work.log
[Write "testing123" to work.log. Note: The
echo
command automatically adds a final newline byte.]
pi@raspberrypi ~ $ echo "testing123" > work.log
[List the contents of the current working directory]
pi@raspberrypi ~ $ ls -1
Desktop
Documents
Downloads
flash_drive_contents
indiecity
python_games
Scratch
work.log
[Copy work.log from the current working directory to the flash drive]
pi@raspberrypi ~ $ cp work.log /mnt/mountpoint_directory/
[Note: The trailing slash after "mountpoint_directory" is a useful habit that prevents the accidental overwriting of a file. Without a trailing slash, if "mountpoint_directory" were actually a file (but you thought it was a directory), this command would overwrite it with the contents of work.log. With the trailing slash, an error is raised instead.]
[List the contents of the top directory in the flash drive filesystem]
pi@raspberrypi ~ $ ls -1 /mnt/mountpoint_directory
transfer
work.log
[Print the contents of the resulting file on the flash drive]
pi@raspberrypi ~ $ cat /mnt/mountpoint_directory/work.log
testing123
Step 8) Unmount the flash drive.
[Unmount the flash drive. Making changes to the /mnt directory requires root permissions, so "sudo" is used. Once unmounted, the flash drive can be physically unplugged without causing any problems.]
[Confirm that the flash drive has been unmounted. This command should produce no output.]
pi@raspberrypi ~ $ sudo umount /mnt/mountpoint_directory
[Confirm that the flash drive has been unmounted. This command should produce no output.]
pi@raspberrypi ~ $ ls -1 /mnt/mountpoint_directory
Step 9) Unplug the flash drive from the destination computer.
Extra Step) Shut down the Raspberry Pi.
pi@raspberrypi ~ $ sudo halt
[a variety of output is printed, then the Raspberry Pi shuts down]
Finally, unplug the power cable from the Raspberry Pi.