9. Thanks for the memories
For this chapter you’ll need a memory stick or a pre-formatted external hard drive with a USB connection. As we are going to be putting videos on it, the larger the better. Got one? Good. Stuff it up your Pi’s USB port. There should be whirrings and flashings (subject to lights or motors being present) as something happens. If we were using Windows, the external storage would appear as a drive letter. Linux, however, lets you choose where to make it appear. Linux has a single directory tree under which external storage and internal storage is placed, or “mounted” in Linux terminology. Remember when I said to change the “Download to: ” directory setting in Transmission to /mnt/downloads”? That’s where we will be mounting your external storage. You might have questions at this point, but let’s get your external storage mounted before we come to them. A while ago I was blethering on about how bad it was to be root and how it was a good idea that you couldn’t log in as root. Yes, well, I was probably slightly exaggerating to reinforce a point. Sometimes you have to do lots of stuff as root and all that sudoing starts getting boring. You can make yourself root until you exit, by doing sudo -s. Try it:
naich@raspberrypi ~ $ sudo -s root@raspberrypi:/home/naich#
Notice a few things here – the “$” has changed to “‘#” to indicate you are now root, the “~” has changed to “/home/your_name because it is no longer your home directory and you are now “root@raspberrypi”. You now have absolute power over the entire operating system. Let it go to your head for a while, cackle like Emperor Palpatine and let the hate flow through you. I like to do this occasionally when I become root and I find it best get it out of my system before I do anything else. Back to normal again? Good. First we need to create the directory to mount the external storage on. The mount location has to already exist in order to mount something on it. Type in the commands in bold:
root@raspberrypi:/home/naich# cd /mnt root@raspberrypi:/mnt# ls -l total 0 root@raspberrypi:/mnt# mkdir downloads root@raspberrypi:/mnt# ls -l total 4 drwxr-xr-x 2 root root 4096 Aug 21 13:24 downloads root@raspberrypi:/mnt#
You change your working directory to /mnt (this is traditionally where you mount things), see what is already there (nothing), create a directory called “downloads” with “mkdir” and then you list it. Quick aside here – see that “drwxr-xr-x” bit? That shows you what sort of file it is (“d” means directory) and the permissions for it, showing who is allowed to do what to it. Read the this page up to and including section 2.1.1to understand permissions or try and battle through my very short explanation here:
There are 3 lots of “rwx”s, the first for the owner of the file, the second for the specified group (it is the second “root” in the illustration above, shown here in italics) and the third is for “others”, i.e. everyone else who is not the owner or a member of the group. In each “rwx”, “r” means “read”, “w” means “write” and “x” means “execute” if it is a program or “enter” if it is a directory. A dash (“-“) means a lack of permission for that function, so in the above example, the first 3 letters after “d” (“rwx”) mean the owner (root) can “r”ead, “w”rite and “x”enter the directory. A member of the “root” group (the next 3 letters) can “r”ead and “x”enter the directory but not “w”rite any files to it, as the “w” is missing. The same “r-x” permissions apply to everyone else, as shown in the last 3 letters.
Now we need to find the external storage. The Linux kernal (the core of the operating system) will have interrogated your external storage to find out what it was when you plugged it in, and then set it up as a block device, making it available in the /dev directory as if it were a file. As it is, it is an interface that you can read and write to it but not in any way that is useful to you. It is now up to you to mount that block device on the filesystem so you can treat it as a branch on the directory tree. Have a look in /dev and you’ll see a whole load of files that are interfaces to devices. One of those files is your external storage, but which one? There are a few ways to find it, but the easiest is with the command fdisk -l:
root@raspberrypi:/mnt# fdisk -l Disk /dev/mmcblk0: 3965 MB, 3965190144 bytes 4 heads, 16 sectors/track, 121008 cylinders, total 7744512 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000714e9 Device Boot Start End Blocks Id System /dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA) /dev/mmcblk0p2 122880 7744511 3810816 83 Linux Disk /dev/sda: 8019 MB, 8019509248 bytes 20 heads, 16 sectors/track, 48947 cylinders, total 15663104 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x665e2cdf Device Boot Start End Blocks Id System /dev/sda1 * 80 15663103 7831512 b W95 FAT32 root@raspberrypi:/mnt#
There’s a lot of information there, but the important bits are where it says “Disk /dev/…”, showing the size of the hardware and the column under “Device” which shows the names of the block devices each partition has been assigned. “Disk /dev/mmcblk0” is the SD card – see how it is split into two partitions, one large (3810816 blocks, each block being 1K making it about 3.8GB) and one small (57344 blocks)? The other “Disk” (it will probably be “/dev/sda”) is your external storage, with its single partition appearing as “/dev/sda<number>”. In my case it’s “sda1 “and 8019 MB big – it’s an 8GB memory stick with its partition having 7831512 1K blocks. Yours might have a different number – “sda4”, for example, in which case use that rather than “sda1”. Now let’s mount the /dev/sda1 device to /mnt/downloads with mount /dev/sda1 /mnt/downloads:
root@raspberrypi:/mnt# mount /dev/sda1 /mnt/downloads root@raspberrypi:/mnt# ls -l downloads total 188 drwxr-xr-x 3 root root 4096 Jul 12 17:07 Acad Course -rwxr-xr-x 1 root root 181574 Jul 12 16:44 Chamber Assembly.wmv drwxr-xr-x 2 root root 4096 Aug 15 19:31 vids root@raspberrypi:/mnt#
The contents of the memory stick may vary. Note how the owner and group are “root”? That is because it was root who mounted the device. Now we know it works, we can make it so the drive is automatically mounted at boot time, writeable by the user “debian-transmission”, who runs the transmission-daemon program. We need to find out his user ID and group ID though – type id -u debian-transmission and make a note of the user ID:
root@raspberrypi:/mnt# id -u debian-transmission 106 root@raspberrypi:/mnt#
It’s 106 for me. Then do the same for the group ID with id -g debian-transmission and make a note of that number – 109 for me. We need to unmount it before we remount it for real. This is the equivalent of safely removing it – unwritten data will be flushed and no more can be sent to it. The command is umount /mnt/downloads – that’s umount, not unmount. If you are doing this as a normal user later on, remember to sudo the command.
root@raspberrypi:/mnt# umount /mnt/downloads/ root@raspberrypi:/mnt# ls -l downloads/ total 0 root@raspberrypi:/mnt#
Never remove a storage device without unmounting it first. If you do, a boxing glove on a spring will shoot out of the top of your Pi and punch you in the nose. Well, it should. You can lose data and corrupt the file system by yanking it out without unmounting. You might also be wondering what would happen to anything that was already in the /mnt/downloads directory when your device was mounted. The contents are preserved but are not accessible while the device is mounted over the top of them.
Now let’s automate the mounting of the external storage. At boot time the file /etc/fstab is read to see what needs to be mounted. Edit that file now with nano /etc/fstab and you will see this:
proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 0 /dev/mmcblk0p2 / ext4 defaults,noatime 0 0 # a swapfile is not a swap partition, so no using swapon|off from here on, use $
Any line starting with “#” is a comment for the benefit of humans and ignored by the computer. This goes for most configuration files. Have a guess at what the first two columns are. If you are really keen, try reading the man page for fstab with man fstab. The first is the block device name, the second is where it is mounted, the third is what sort of filesystem it is and the fourth is options. The other two are used for maintenance and can be left out or set to 0. You will want to add this line at the end in order to automatically mount your external storage:
/dev/sda1 /mnt/downloads auto defaults,uid=106,gid=109 0 0
Replace the “uid=106” and “gid=109” with the numbers you found earlier for user ID and group ID. Save and exit with CTRL-X and then answering “Y” and hit return to save it with the same file name. Now mount the drive with mount -a. This mounts all the devices in /etc/fstab in the same manner as if it was booting up. Finally, check everything is hunky-dory:
root@raspberrypi:/mnt# mount -a root@raspberrypi:/mnt# ls -l total 4 drwxr-xr-x 4 debian-transmission debian-transmission 4096 Jan 1 1970 downloads
Smashing. I should also point out that Linux distributions like Ubuntu do all this automatically, popping up a file manager window showing the contents of the memory stick as soon as it is plugged in. But we are operating at a lower level than that.
Now stop being root by tying exit:
root@raspberrypi:/mnt# exit exit naich@raspberrypi ~ $
Now we are back to normal. You can stop cackling. Your Pi is set up to mount the memory stick whenever it boots, so you don’t need to do anything else other than remember to sudo umount /mnt/downloads if you take the stick out and sudo mount -a when you put it back in.
10. Torrent something!
Now open up the Transmission web interface by pointing your browser at http://your_pi_ip_address:9091 and click on the “Open” icon. Put this URL for a perfectly legal video torent in it:
Put it in the “URL” box and click open. Magnet links also work in this box. If you have found a Magnet link from a perfectly respectable web site, right click and choose “copy link location” and paste it into the “URL” box in Transmission. If you have downloaded the .torrent file, upload it to Transmission.
You can check on the status of the torrent by clicking on it and then clicking the “i” icon for more info on it. I got a “unregistered torrent” error, but DHT found enough peers to get it all.
Sometimes, with a popular torrent with lots of fast peers, the poor little Pi can get a bit overwhelmed with all the traffic hammering its ports. It’s like a DOS attack and can bring down the Pi if it is bad enough. So I like to limit the number of connections by going to the web panel, opening up settings by clicking on the spanner, going to the “Peers” tab and setting a limit of 20 peers per torrent and 100 peers overall. I also set speed limits of 400kB/s for up and downloads in the “Speed” tab.
If you are having trouble connecting to any peers, make sure that “Use port forwarding from my router” is checked in “Network” tab of the settings. This page gives some more info about setting up your router. If your internet connection is fast you should have a high definition video file on your memory stick before you know it. I’ll leave you to find out more about how Transmission works by the age-old method of clicking things to see what happens.
Most tellies these days have USB ports on them which you can lob a stick into. If yours does, unmount and lob your stick. Our telly also has an ethernet port on it, for streaming video from a uPNP media server. Wouldn’t it be good to have a uPNP video server on the Pi? I think we could probably manage that in the next chapter.