Raspberry Pi: How to mount a USB hard disk

After connecting the drive to the Pi, we first need to find out where it is located. One way to do this is via the blkid command, which lists the block devices on the PI. Below is the output I get when entering this command.

$ sudo blkid
/dev/mmcblk0p1: LABEL="boot" UUID="3725-1C05" TYPE="vfat" PARTUUID="80ea9ce7-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="fd695ef5-f047-44bd-b159-2a78c53af20a" TYPE="ext4" PARTUUID="80ea9ce7-02"
/dev/mmcblk0: PTUUID="80ea9ce7" PTTYPE="dos"
/dev/sda1: LABEL="Elements SE" UUID="2E1C78611C78264D" TYPE="ntfs" PARTLABEL="Elements SE" PARTUUID="0f520684-2030-4069-8d8e-45b4197ba26d"

Having found the device you could now mount it manually with the following command.

$ sudo mount /dev/sda1 /mnt

I’m going to make the assumption that you want the drive to mount automatically however. To accomplish this we first need to modify the /etc/fstab file. To do this I’m going to use nano as shown below.

$ sudo nano /etc/fstab

Now enter the details for your drive. Mine is shown in the last line of the file. When you’ve finished type <ctrl>X to exit and save, then follow the on screen instructions. Make sure you set the location of the drive according to the location you determined above, /dev/sda1 in my case.

proc                  /proc           proc    defaults                 0       0
PARTUUID=80ea9ce7-01  /boot           vfat    defaults                 0       2
PARTUUID=80ea9ce7-02  /               ext4    defaults,noatime         0       1
/dev/sda1             /mnt            ntfs    auto,uid=1000,pid=1000   0       0

You now need to tell Linux to mount all the file system mentioned in the /etc/fstab file. You do this by typing the following.

$ sudo mount -a

If you now try to list the conents of the drive you may receive a message telling you that you do not have permission to open the /mnt directory. If that is the case then you can change the directory permissions via the chmod command.

$ sudo chmod 777 /mnt

Which should allow you to finally see the contents of the disk.

$ ls /mnt
Movies  $RECYCLE.BIN  System Volume Information  TV Shows

To gain the most use from mounting the disk you probably need to install the ntfs-3g package. This is required if you want to write as well as read the drive. You can do this with the following command.

$ sudo apt-get install ntfs-3g

Linux: A Basic Guide to Processes and Threads

Processes form the base on which programs are run. They hold data relating to which files are open, what signals are pending, the processors state and address space, together with a list of what threads are executing.

Whilst the process describes the environment in which a program is executed, the specifics of that programs execution is detailed in a thread, which holds information relating to program position and stack.

A process begins with a call to fork() which creates a new process by duplicating the old one. The new process begins by executing code at the line after the call to fork(). To create a new address space and begin the execution of a new program a call to exec() is needed.

A program executes until it reaches the exit() call which terminates the process and frees its resources. A parent process can request the state of a child by calling the wait() routine, which allows it to wait for the termination of the child. If the child ends before the parent makes the wait() call then the child enters the zombie state and remains there until the parent makes the call to wait().

Threads

Threads provide a means of executing multiple code paths within a shared address space with shared open files and other resources.

Linux treats threads as processes, but sets their process descriptors up in such a way that memory, files and resources can be shared between multiple instances. The Call to create a thread for example results in a call to clone() similar to that shown below

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0)

meaning that parent and child share address space, file system information, open files and signal handlers.

A standard call to fork on the other had results in the more compact

clone(SIGCHLD, 0)

Linux Commands: tree

The tree command produces a recursive, depth indented directory/file listing, followed by a count of the number of files and directories found.

By default the tree command is not installed. Type one of the following according to the type of Linux installation you have.

$ sudo apt-get install tree
$ yum install tree

To list the contents of the directory test, descending all paths, type the following

$ tree test
tree
|-- foo1.txt
|-- foo2.txt
'-- foodir
    '-- foo3.txt

To limit the directory level descended into append the option -L to the tree command followed by the number of levels you wish to limit to.

$ tree test -L 1
tree
|-- foo1.txt
|-- foo2.txt
'-- foodir

Linux Commands: How to use wc

The wc command is used to count newlines, words, or bytes in a given file. Used with a pipe it can also be untilised for general counting operations. The following sections give a few examples of how it can be used.

To count the number of bytes in a file:

$ cat 02-Test.m3u | wc -c

To find the length of the longest line in a file. NB calling wc with the name of a file supplied results in output which contains both the answer and the file name., whilst piping to wc results only in the answer.

$ wc -L 02-Test.m3u
71 02-Test.m3u

$ cat 02-Test.m3u | wc -L
71

To count the number of files plus directories at the current location

$ ls -1 | wc -l