Raspberry Pi Home Server: Part 7, Sharing Files With Samba

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Note: This article is part of a series. See the Index for more information.

Self-promotion: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!

Updates: Everything in this post works just fine under the Jessie release of Raspbian, so there are no changes to report at this time.


Now that the Raspberry Pi has the space to hold your stuff, it’s about time it started doing something useful around the house. Its first task will be to share files from the hard drive. In the previous post we added a hard drive with two partitions, one ext4 partition for the operating system, and one NTFS partition to hold data.

Prerequisites

  • A Raspberry Pi
  • A mounted, USB hard drive
    Mine is the second hard drive partition, mounted at /mnt/data

Install Samba

The “Samba” package creates network file shares in a way that Mac and Windows computers on the network will understand. Install Samba as follows:

sudo apt-get install samba samba-common-bin

Set up shares

Just for fun, we’ll use Webmin to do this part. Get to a browser on your primary computer (or the Pi, if you like waiting), and navigate to the Raspberry Pi’s IP address, port 10000 (e.g. https://192.168.1.123:10000).

Webmin has a Samba administration module, which should currently be listed under the “Un-used Modules” section because Samba wasn’t installed before. Since you’ve just installed Samba, it would be nice if this module weren’t hidden like this. Click “Refresh Modules” near the bottom of Webmin’s left-hand menu, and Webmin will go through its inventory, and figure out which modules can actually be used. This can take a little while, so be patient.

When it is finished, the “Samba Windows File Sharing” module should have moved to the “Servers” section at the top.

Click on the “Samba Windows File Sharing” module, and then on the “Windows Networking” icon under “Global Configuration”. Change “Workgroup” to match your home network if it is different (it’s probably just “WORKGROUP”, though, unless you specifically changed it). Select the “Be a WINS server” radio button on the second row.

image

Click “Save” to save the settings and return to the main Samba page.

Click on the “Create a new file share” link near the top of the page. Give the new share a name. I called mine “Data” to avoid confusion, and type “/mnt/data” (or whatever you called your mount point) in the “Directory to share” textbox. Your screen should look like this:

image

Click “Create” to create the share, and you will be taken back to the main Samba page. Your new share should now appear in the list. Click on it to go to the “Edit File Share” page. From here, click the “Security and Access Control” icon.

Set “Writeable” and “Guest Access” to “Yes”.

image

Click “Save” to save your settings and return to the “Edit File Share” form.

Click the “Return to share list” link at the bottom of the page to return to the main Samba page.

Click on the “Restart Samba Servers” button near the bottom of the page. You should now be able to view the share from other computers on your network.

image

Disclaimer

This is a wide-open file share. Anyone on the local network can add, edit, or delete the files it contains. I am not a Linux security expert, and somewhere someone is screaming at me for doing it all wrong. I’m sorry, it’s nothing personal, but we’re talking about a home network, here. I’m more interested in having a place to put stuff than in controlling access to it.

You can certainly do that, of course, but the intricacies of Linux filesystem security are beyond the scope of this series.

Make some folders

Create some file folders in the Data share to hold your stuff, and copy it in. Music, Pictures, Videos, anything you want. For this server, I’ve created the following folder structure.

  • Documents
    • Music
    • Pictures
    • Videos
  • Downloads

You don’t have to use the same structure as me, of course, but this is the one I’ll be using in future posts, so you’ll need to adjust accordingly if you do something else.

Load it up

When it comes to copying the files in, you can save some time, and skip the network bottleneck by moving the external hard drive to the computer that currently has the data. Because the data partition was formatted as NTFS in the previous post, when you plug it in to your Windows or Mac computer, you’ll just see a drive called “Data”, or whatever you named it.

Remember to shut down the Raspberry Pi before removing the hard drive, of course. Just put it back when you’re done copying files, and start up the Raspberry Pi as usual.

What’s next?

In the next post, we’ll get a media server up and running so that you can stream music and movies all over your house from the hard drive share.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 47 Comments

Raspberry Pi Home Server: Part 6, Adding a hard drive

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Note: This article is part of a series. See the Index for more information.

Self-promotion: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!

Updates: If you didn’t change the boot option using raspi-config so that the Pi boots to the command-line, the hard drive will auto-mount each time you boot up, and copying the root filesystem to the external drive will fail. Make sure you’ve configured the Pi to boot to the command line, and everything should work as per the original post.


Running the Raspberry Pi off of an SD card is simple, affordable, and very convenient. SD cards are also very small compared to hard drives, though, and they can only be written to a finite number of times. That number is ridiculously large, but some have said that it can still be used up faster than you think by things like virtual memory swap files which are written and re-written constantly.

Whether you believe that or not, one thing is certain; In order to be a useful home server, the Raspberry Pi is going to need access to more storage space than you get from a simple SD card. The Raspberry Pi Home Server is going to need a proper hard drive, and as long as you’re adding a hard drive, you may as well take advantage of its speed and boot the Raspberry Pi from it.

Okay, I’ll level with you. You can’t actually boot entirely from the hard drive, but you can boot mostly from the hard drive. When the Raspberry Pi boots up, it looks to the first partition on the SD card for instructions on what to do next. That boot partition contains just a tiny piece of the boot process, but at least for now, its location is non-negotiable. That’s just the way it is. That’s not to say things couldn’t change in the future. I saw someone on a forum claiming to have done away with the SD card altogether, but I’ll believe it when I see it.

What you can do is to move all the stuff that comes after that first step to a different device, such as the external hard drive. All that stays behind on the SD card is a very small boot partition, everything else moves to the hard drive.

The resulting system should run faster and smoother, and you won’t be using up your SD card’s limited number of write cycles on high-frequency stuff like a virtual memory swap file.

Acknowledgements

This article would not have been possible if not for others that went before me. In particular, Ted Hale’s article at http://raspberrypihobbyist.blogspot.com/2013/07/running-from-external-hard-drive.html is pretty much the blueprint I started from. In turn, that article was based on a forum post by “Rattus” at http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=10914&p=129474&hilit=resizefs#p122476.

I’ve added my own bits, such as consulting the boot/cmdline.txt file rather than simply assuming the root file system’s location, using UUIDs to identify the partitions, and expanding the existing swap file rather than creating a separate swap partition.

I’ve turned these resources into a walkthrough that matches the rest of this series, but I wanted to at least point out where my source material originated.

Gather your tools

This post uses some software packages that are not included by default in the Raspbian distribution. You’re going to need NTFS filesystem support, and the gdisk tool. Install them before continuing.

sudo apt-get install ntfs-3g ntfs-config gdisk

Pick a hard drive

Pretty much any external USB hard drive will do. For my own server at home, I’m using a dual-drive 2TB RAID enclosure (Cavalry CADA-SA2). This might seem like overkill for a Raspberry Pi, but one of my home server’s jobs is to centralize and share my music collection. It took me four solid weeks to rip every CD I own, and I do not plan to repeat that process. My family photos and videos are on that drive as well. Basically, this is all stuff I don’t want to lose due to a hard drive failure.

Note: Yes, I have an offline backup, too, but swapping out a dead drive in a RAID is easier than rebuilding the whole thing by hand.

Linux device names

Before we start, lets take a moment to discuss the way in which Linux refers to devices in general, and more specifically the way it refers to hard drives. Windows users are used to seeing multiple drives show up with different letters. You have your “C” drive, where Windows and all of your programs are installed. Maybe you also have a second hard drive called “D”, or maybe “D” is CD or DVD drive on your computer. “A” and “B” used to refer to floppy drives, and are reserved for historical purposes. Each of these devices is completely separate, and has its own individual root.

Linux has no drive letters. Linux uses what’s called a “unified file system”, meaning that files, folders, and even entire drives appear together under one root. You can see all of these different devices in the “/dev” folder. Take a look:

ls /dev

image

I won’t go into what all of these entries mean, but you can see that there are a lot of them. If I were to plug in an external hard drive, or perhaps a flash drive, I would see new entries pop up in the list. Here’s another screen shot after I’ve plugged in a flash drive.

image

Look about halfway down the third column, and you’ll see new entries called “sda” and “sda1”. These represent the flash drive, and the first partition it contains, respectively.

Storage devices like a flash drive or a hard drive can be divided up into multiple pieces called partitions that can make it appear to be multiple drives. This list of where the partitions begin and end is called the “partition table”, and every drive has one. If there were more partitions on the flash drive, there would be more entries in /dev called “sda2”, “sda3”, etc.

On a computer where drives are connected via SCSI (Small Computer System Interface), “a” would be the first device, “b” would be the second, etc. Drives connected via IDE (Integrated Drive Electronics), or SATA (Serial AT Attachment) have similar concepts of “first”, “second”, etc.

This is not the case with USB. USB devices have no concept of “first” or “second” and so their names are assigned on a first-come, first-served basis. The first drive to be ready gets to be “sda”, the next one “sdb” and so on.

If you only have one drive, then it will obviously always be called “sda”, and there’s no problem, but since you’re building a server, it’s conceivable, and even likely that you will eventually end up with more than one hard drive attached to it. Perhaps one drive will house videos, and another one will have music on it.

Even if you don’t see a need for that yet, you’ll end up filling up a drive eventually, and you’ll want to add another one. As soon as you add more USB drives, however, you can’t count on their names being the same every time the machine starts up. As a result, drives may end up mounted in the wrong places, and programs won’t find their files where they expect them to be because the drive that used to be “sda” was a little slower off the line one morning and got stuck with the name “sdb” instead.

Let’s take care of that particular problem before going any further.

Format the hard drive

If the drive you’re planning to use already has stuff on it, then back its contents up somewhere. and restore them when you’re done. This approach calls for nuking and repaving the whole hard drive.

Caution: Even if you wanted to live dangerously and manipulate the partitions in place, I wouldn’t do it without a backup copy first. There are just too many things that could go wrong. What if the power went out partway through the process? Don’t risk it. Just copy your stuff to another drive, and copy it back when you’re done.

The key to fixing the device naming problem requires us to have a specific kind of partition table called a GUID Partition Table. This is different than the older Master Boot Record which is used by most Windows machines.

In order to create a GPT, you’ll need to use Linux’s Partition Editor (parted) application. Raspbian already includes parted, so there’s nothing to install. Run it with the following command:

sudo parted

You should see something almost, but not quite entirely unlike a command line. To see a list of all the known devices and partitions, type “print all”. Parted should print out a table with two rows similar to this:

image

The rows in the table represent the partitions. The columns will tell you where each partition begins and ends, how large it is, and what kind of filesystem it contains.

If you see more than one table at this point, then you probably have another drive plugged in somewhere. I strongly advise shutting down and removing all drives other than the SD card before continuing.

The SD card in the image above was created using the Raspbian image file, and has two partitions on it, a small “boot” partition, and the root filesystem partition. If you started by using NOOBS, then you probably have five partitions. Future releases could divide things up differently, so don’t worry if your output doesn’t look like the example.

Attach the hard drive

Plug your external drive into a free USB port on the Raspberry Pi, and give the OS a few seconds to notice it. List the known partitions by typing “print all” again.

image

This time, there are two tables. In my example, the second one is the external drive, although the order is not guaranteed here.

Notice the headers above the tables. The header tells you information about the drive in general. In this case, the header tells us that the hard drive was assigned the name “/dev/sda”, that it’s 2TB in size, and that its partition table type is “msdos”. That means it’s using the old MBR-style partition table, and that’s not going to work for this server.

Select the external drive by typing “select /dev/sda”. Type “print” all by itself to make sure. You should see the second table from above again. Read the header carefully, and make sure it’s referring to the external drive, because you’re about to blow away the partition table.

Create a new partition table with the command “mklabel gpt”. Read the warning carefully, and make sure it refers to “/dev/sda”.

image

Answer “y” to the prompt, and in a few seconds, you will have a nice blank slate to work with. Check the results with another “print”. The header should now say that the partition table type is “gpt”, and there should be no partitions on it.

image

Create new partitions

Next, you’ll need to create two new partitions; one to hold the root filesystem, and one to hold general data. You could make one big partition, combining the root filesystem and data storage in one, but there are some benefits to keeping the data partition separate. The root filesystem must be formatted using Linux’s native filesystem, ext4. The data partition, however, can be any type you want, which means you can pick something other, non-Linux computers will understand.

Root Partition

This is where Linux and your programs will go. You can think of it as Windows’ C: drive. The size is up to you, but 16GB is probably fine for the simple server described in this series. If your existing SD card is larger, or you are planning to install a lot of programs, go bigger. Just make sure the partition is at least as large as the SD card you are currently using.

Create a new, 16GB partition starting at the beginning of the drive:

mkpart primary 0GB 16GB

Data Partition

This is where you’ll put your “stuff”, and it should take up the rest of the available drive space. The mkpart command can interpret the beginning and ending parameters in a variety of different formats. Use this ability to create a second partition that begins where the first partition ends, and takes up all the rest of the space on the drive:

mkpart primary 16GB 100%

Finally, use the “print” command again to see the results.

image

That’s it for the partitions, so type “q” and press enter to exit parted. You can ignore the warning about updating /etc/fstab for now. We’ll get to that soon enough. You now have two partitions, but they’re completely blank at this point.

Copy the old root partition to the new drive

The file /boot/cmdline.txt specifies the location of the root filesystem. You’ll need to edit this file later in order to boot from the hard drive, but for now, you just need to know for sure where the root filesystem currently resides. Show the cmdline file’s contents like this:

cat /boot/cmdline.txt

image

The part immediately to the right of “root=” specifies the device and partition that holds the root filesystem, and that’s the partition whose contents you need to move to the hard drive. In my case, it says “mmcblk0p2”. If you started from the raw Raspbian image, then yours probably says the same thing. If you started from NOOBS, then it’s probably “mmcblk0p6”

Copy the existing root partition’s contents to the new 16GB partition (sda1) on the hard drive, changing the highlighted part to match the current root filesystem path from above.

sudo dd if=/dev/mmcblk0p2 of=/dev/sda1 bs=32M conv=noerror,sync

This can run for quite a while, depending on how large your SD card is. Unfortunately, the dd command doesn’t give you any kind of feedback. In this case, no news is good news. As long as the drive appears to be busy, then keep waiting.

Here’s what that all means:

  • “dd” copies things
  • “if” specifies the input file, in this case an entire partitions
  • “of” specifies the output file
  • “bs” specifies how many bytes to copy in one chunk, here it’s 32 MB
  • “conv” specifies how to convert things as they are copied
    • noerror says to continue if anything goes wrong
    • sync does some padding during the copy

When the copy process eventually finishes, check the target partition on the hard drive for errors.

sudo e2fsck -f /dev/sda1

Press enter at the prompts to fix any errors that it finds. You can probably expect one about the free block count, and one about the free inodes count.

Since the image that you just copied over from the SD card is almost definitely smaller than the partition you copied it to, you’ll want to expand it to fill up the available space:

sudo resize2fs /dev/sda1

Format the data partition

That takes care of the root filesystem. Now on to the data partition. Choosing NTFS as the filesystem will allow you to simply plug the drive into any Windows, MAC, or Linux desktop computer and manipulate the drive contents. This can be especially helpful for loading up large amounts of data, such as the family’s music and photo collection, without having to squeeze it all through the network connection.

Format the second partition as follows, filling in whatever name you want the partition to have. I’ve called mine “Data”.

sudo mkfs.ntfs -Q -L Data /dev/sda2

Note: Don’t forget the –Q (Quick) or you’ll be waiting around a while.

Uniquely identifying a drive

Warning: Because the next section involves copying around large strings of unmemorizable data, I recommend performing the steps through a remote SSH window, or from a terminal on the X desktop. The Ids you’ll need to copy need to be copied verbatim, letter for letter, or you’ll find yourself unable to boot, and you’ll have to start over again from your last backup.

Currently, the cmdline.txt file specifies that the root file system is on the second partition of the internal SD card (/dev/mmcblk0p2). We’d like that to say “/dev/sda1” instead, but there’s a problem. As I mentioned earlier, we don’t have any guarantee that this particular drive will be called “sda” in the future. What we need is a way to uniquely refer to this drive no matter what letter it get assigned on any given day.

This was why we built the drive with a GUID Partition Table. Each partition on a GPT device is assigned a universally-unique identifier (UUID).

Note: The difference between GUID and UUID is not important here, they are, for all intents and purposes, the same thing; a very long, randomly-assigned number.

Instead of identifying the root filesystem’s partition by location, we want to refer to it by Id so that it can be found no matter what order the drives were discovered in. Use the “gdisk” package you installed earlier to find out more about /dev/sda1:

sudo gdisk /dev/sda

You should see another not-quite-a-command-prompt like this:

image

Type “i” and press enter to show information about partitons, then type “1” (one) to identify the partition you are interested in. The partition’s details will be displayed like this:

image

The piece of information we’re interested is the “Partition unique GUID” on the second line, not the “Partition GUID code”. Take a picture, write it down very carefully, or select and copy the text if you are doing this through a remote terminal connection like I am.

Type “q” and press enter to exit the gdisk tool.

Boot Configuration

The new root filesystem’s partition is now uniquely identifiable, but the Raspberry Pi doesn’t know to use it. Edit the /boot/cmdline file to change where the bootloader will look for the root filesystem.

sudo nano /boot/cmdline.txt

Find the part that says “root=/dev/mmcblk0p…” and change it to “root=PARTUUID=” and whatever your “Partition unique GUID” was from above. Also add the string “rootdelay=5” at the end. This will give the Raspberry Pi time to discover the USB drive before it tries booting from it.

The result should look like this (sorry for the small picture):

image

Exit Nano, saving your changes (ctrl-x,y,enter)

Reboot (Important, do not skip this step)

Before the next set of changes will “stick”, you’ll need to reboot so that the Raspberry Pi uses the hard drive for the initial load. If you don’t reboot now, nothing you’re about to do will count, and you’ll just have to do it all again. You’ve been warned.

sudo reboot

If everything goes well, you should find yourself back at a login, and you can continue. If something went wrong, go back to your most recent backup and try again.

Mounting filesystems

Partitions and filesystems are not the same thing. Linux now knows what partition to load the OS from, and that’s great, but as things stand right now, it’s still going to mount the root filesystem from the SD card.

To complete the transition to the hard drive, you’ll need to edit the filesystem table. This file controls what gets mounted where, and in what order, and it needs to know where the root filesystem is. Take a look at the current contents.

cat /etc/fstab

image

That third line is the root filesystem, which you can tell by the “/” in the second column. Unfortunately, and it’s still loading from the 2nd partition on the SD card (mmcblk0p2). You could change this to say “/dev/sda1”, but that would only work as long as the drive continues to get the name “sda”.

Fortunately, you can use a very similar UUID-based trick here to uniquely identify the filesystem no matter what letter the device gets. Filesystems have UUIDs too, and you can see them all with this command:

sudo blkid

image

If you look carefully, you may notice a problem. both /dev/mmblk0p2 and /dev/sda1 have the same UUID. So much for being unique, right? This is because of the way we cloned the old root filesystem into a new location. It brought the whole filesystem over, including its UUID. Before you can use a unique Id to identify the drive, you’ll need to make sure it’s actually unique.

You need to give /dev/sda1 a new UUID. You can hand-assign your favorite UUID, or just let the computer pick a random one. Use the following command to assign anew UUID to the first partition on the hard drive:

sudo tune2fs /dev/sda1 -U random

Note: At the time of this edit, there seems to be an issue with the newer “Jessie” release of Raspbian, and it may prevent you from changing the UUID on the partition. If you see an error that says “The UUID may only be changed when the filesystem is unmounted.”, even though it’s NOT mounted, then you’ll need to perform one more tweak. The magic words are

sudo tune2fs -O ^uninit_bg /dev/sda1

I can’t take credit for this one at all. I just found it here. After reading the man page on tune2fs, I still don’t honestly even understand what this has to do with anything, but it does seem to do the trick, so if you get the above error, give this command a shot, then try the “-U random” command again and it should succeed.

Either way, display the device Ids again, just to be sure

sudo blkid

image

Copy down the UUIDs for /dev/sda1 and /dev/sda2, you’ll need them both in a minute. Then open the filesystem table in an editor.

sudo nano /etc/fstab

Change “/dev/mmcblk0p2” on the third line to “/dev/disk/by-uuid/” and the new UUID you just assigned to /dev/sda1. Add another line for /sda2, substituting its UUID, and mounting it as /mnt/data, with a filesystem type of “ntfs”.

The end result should look like this:

image

Don’t worry about the columns lining up, it doesn’t matter, but I’m presenting mine to the public, so I’ve gone ahead and made it pretty

Exit Nano, saving your changes (ctrl-x,y,enter)

The system should be just about ready to boot from the hard drive, but there’s one more thing to take card of before we do that. Currently, the filesystem table says to mount the second partition at /mnt/data. Before that can succeed, you need to create a folder in that location. You can think of it as a placeholder where the actual drive will appear later on.

sudo mkdir /mnt/data

That’s it. you’re ready to reboot again, and this time, everything should be faster.

sudo reboot

You’ll notice that the “ACT” light on the Raspberry Pi will not blink much anymore. That’s because the SD card is no longer being accessed for anything other than the boot partition. The hard drive’s activity light will now blink where the Raspberry Pi’s activity light used to.

When the system has rebooted, double-check your filesystem table to make sure your changes are still there.

cat /etc/fstab

If you see /dev/mmcblk0p2, and no line for your data partition, it’s because you skipped that “Reboot” step above. See? I told you it was important. Redo everything in the “Mounting filesystems” section, and reboot again.

Swap configuration

One thing you may have noticed if you’ve looked at other walkthroughs for booting from the hard drive is that they usually create a “swap” partition on the hard drive to be used as virtual memory.

If you look back at the filesystem table from the beginning of this post, though, you’ll notice that Raspbian never had a swap partition in the first place. That’s because Raspbian is set up to use swap files instead of swap partitions. Raspbian’s swap file lives at /var/swap, and since we just moved the whole root filesystem onto the hard drive, the swap file came along for the ride.

At this point, you’re already running your swap file from the hard drive, and you didn’t even have to do anything. Check it out with the “swapon” command:

sudo swapon –s

This shows the swap summary, which will tell you what swaps are in use. It should have one entry in it (/var/swap). It’s pretty small, though; only 100MB:

image

The partition we created for the root filesystem is 16GB (or more if you so chose). There’s plenty of space left to expand the swap file to something roomier.

Edit the swap file configuration:

sudo nano /etc/dphys-swapfile

The configuration file that came with Raspbian only has a single line in it which says “CONF_SWAPSIZE=100”. This is the size of the swap file in megabytes. Change the value to 2048, which will create a 2GB swap file.

image

Close Nano, saving your work (ctrl-x,y,enter).

Reboot one more time to apply the changes, and check them by typing “swapon –s” again. You should see a table similar to the first time you ran this command, only now the swap file is much larger:

image

Wrapping up

You now have a Raspberry Pi that boots (mostly) from an external USB drive. It also uses this drive for its virtual memory swap file. The whole system should run more smoothly now, and you won’t have to worry about using up your SD card, if that’s the sort of thing you worry about.

Although most of the important stuff is on the hard drive, you should probably make at least one more backup of the SD card for safety. I may cover backing up the root filesystem from the hard drive later on in another post, or update this one with the changes.

What’s next?

In the next post, we’ll put the new drive to work sharing files over the local network.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 291 Comments

Raspberry Pi Home Server: Part 4, Web Administration

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Note: This article is part of a series. See the Index for more information.

Self-promotion: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!

Updates: Everything in this post seems to work just fine under Jessie, so there are no changes to report.


SSH is a simple way to remotely log in to your machine’s command-line interface, but that’s not always the most convenient way to work. There is a wonderful web administration system called Webmin that can handle a lot of the “magic” of system configuration. Webmin can take care of a lot of the tasks you’d normally do from the command line, but in a much friendlier way. In addition, you can add Webmin modules for many of the features we’ll be adding to the Raspberry Pi in this series.

It’s pretty easy to install, so let’s get started.

You can’t just install Webmin through apt-get like the other software packages so far because apt-get doesn’t know about Webmin, or at least it doesn’t know about it yet. There are several approaches to a Webmin installation, but I’ve found that the easiest is to simply tell apt-get where to get the packages it needs for Webmin. Edit apt-get’s list of sources like this:

sudo nano /etc/apt/sources.list

When the editor appears, add the following two lines to the end of the file:

deb http://download.webmin.com/download/repository sarge contrib
deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib

Note: Your browser may wrap these lines, but make sure they go in the sources.list as two lines like this:

image

Press Ctrl-x,y,enter to exit nano, saving the file. Next you’ll need to import the signing key that verifies the packages coming from the new repository. These next few commands need to be run as the actual root user of the machine. This is the first time this series has done this, so I’ll break it down for you. Type the following to temporarily become the root user:

sudo su

Your command prompt will change, losing all of its color, and becoming more sinister, dark, and dangerous looking like this:

image

You are now operating as the root user of the machine. The root user can do pretty much anything. Unlike the Windows world, Linux users try to spend as little time in “God-mode” as possible.

Type the following commands to import the Webmin repository’s signing key:

cd /root
wget http://www.webmin.com/jcameron-key.asc
apt-key add jcameron-key.asc
exit

That last “exit” tells the system that you want to stop being the root user now, and go back to being “pi”. The final result should look like this:

image

Now that you’ve added Webmin’s repository to the list of places apt-get will look for stuff, update the list of available packages again.

sudo apt-get update

Note: You may or may not see an error during this process. I presume that’s why the Webmin folks had you add two repositories, in case one of them is down. If you get an error, try the next steps anyway. If it still fails, double-check that you got the repository addresses exactly right, and try again.

Now that apt-get knows where to get Webmin, you can install it just like you have everything else so far.

sudo apt-get install webmin

When the installation is complete, you can test it by opening a browser, and navigating to the IP address of your server, but specifying https and port 10000 rather than the default http port of 80. If you forget the “https” part, you’ll see a friendly page that offers to redirect you. Either way, you’ll probably also get a warning about the site not having a valid certificate, which it doesn’t. Proceed to the page anyway, and the result should look like this:

image

Log in as “pi”, and you’ll see the main Webmin interface, which looks like this:

image

Poke around a bit, and see what Webmin is all about. You can monitor storage and memory usage from here, be notified about updates, apply them, manage user accounts, and a lot more.

Look inside the “Un-used Modules” section to see all the things that Webmin could help you manage, if you had those packages installed. It’s a pretty impressive list.

Wrapping up

You’ve reached another milestone, and I strongly recommend politely shutting down the Raspberry Pi (sudo shutdown –h now), and taking another backup of the SD card.

What’s next?

In the next post, we’ll add the ability to remote desktop into the Raspberry Pi.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 27 Comments

Raspberry Pi Home Server: Part 3, Configuring the OS

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Note: This article is part of a series. See the Index for more information.

Self-Promotion: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!

Updates: Everything in this post seems to work the same with the new Jessie release of Raspbian.


Now that you’ve turned your inert Raspberry Pi into a working computer, it’s time to get moved in.

Prerequisites

  • A Raspberry Pi with the Raspbian Linux distribution installed as described in Part 2 of this series.
  • You should be logged in as the “pi” user account.

Assigning a static IP address

Since this series is about turning the Raspberry Pi into a server, it’s important that it have a consistent IP address so that you can tell your router where to redirect traffic for things like web traffic or other services. There are two ways to accomplish this.

Method 1

You can use your router’s configuration to assign a permanent “lease” to individual devices. In other words, you tell your router to always hand out the same IP address to a certain device when asked. There are definite advantages to letting your router handle the IP assignments. It centralizes the management of network resources in one place, rather than distributing that configuration across the various devices on the network. Also, there are certain UPnP functions that work better if the router is responsible for handling out the IP addresses.

This is the method I use, personally. Instructions for how to configure an individual router vary by manufacturer and model, so I won’t cover them in detail here, but the gist of it is that you assign an IP address to the hardware MAC address of the network card built into the Raspberry Pi.

To get the MAC address, type the following command:

ifconfig

The result should look something like this:

image

The MAC address appears on the first line, next to the label “HWaddr”. Follow the directions for your specific router, and assign a permanent IP lease to this MAC address. Make sure it’s not in the range that the DHCP server uses as it’s “pool”. For my router, everything from 100-255 is the DHCP pool, so anything below 100 is fair game for leasing or static assignment.

Method 2

The second method is to configure the IP address directly on the Raspberry Pi itself. Here is where you will first start to see the difference in attitude from the Windows world to the Linux world. Windows is about convenience and ease of use for the average non-technical user. Linux is all about highly-knowledgeable users tweaking things exactly the way they want them. Linux settings are stored in plain text files in ordinary folders. The trick is mostly in knowing which settings go in which file, and where it lives.

Type the following command to edit the network configuration file.

sudo nano /etc/network/interfaces

“nano” is a text editor that is included in the Raspbian image, and is convenient for making configuration changes. You need “sudo” to run it with elevated privileges because regular users aren’t allowed to edit configuration files, and while it’s certainly above average, the “pi” user is just a regular user at the end of the day.

The default network interfaces will open in the editor, and should look like this.

image

There are a lot of magic words in this file, but the only part you need to be concerned with is the line that says “iface eth0 inet dhcp”.

Let’s break that down:

  • iface” means “interface”.
  • eth0” refers to the first (and only) ethernet port the Raspberry Pi has
    (computers like to start counting things at 0)
  • inet” specifies TCP/IP networking, which is what this interface uses
  • dhcp” stands for “Dynamic Host Configuration Protocol”, which essentially means that someone else (your router) is responsible for assigning this computer’s IP address.

To assign a static IP address, you need to change “dhcp” to “static” and then add some lines that explicitly say what address should be used. Change the “iface” line to read as follows, substituting the highlighted values for your own network where appropriate.

iface eth0 inet static
address 192.168.1.x
netmask 255.255.255.0
gateway 192.168.1.1

If you don’t know what values to use for “netmask” and “gateway”, you can imitate what your primary computer is using. From a Windows command prompt on your primary computer, type “ipconfig”, and take the “Subnnet Mask” and “Default Gateway” values.

Hold down Ctrl, and press “x” to exit nano. It will prompt you to save your changes, press “y” to save them, and accept the default filename, which is the same as the file you loaded to begin with.

Before these changes will take effect, you’ll need to reboot the Raspberry Pi. Type the following and press enter to restart the machine.

sudo reboot

Note: If you had just typed “reboot” without the “sudo” you would have been denied. Regular users are not allowed to reboot a Linux machine.

Once the machine has rebooted, type “ifconfig” to see the current interface configuration. The configuration for eth0 should have the IP address you assigned listed as the first item on the second row.

image

Going headless

You are now at a point where you could disconnect the Raspberry Pi from its monitor and keyboard and do everything remotely. Actually, you could have done this a few steps ago, but it’s easier to connect when you know the IP address.

Find an SSH client program that you like, and use it to connect to the raspberry Pi. The particulars of configuring a connection will vary by program, but in Putty, all I need to do is provide the IP address of the computer I want to connect to and click “Open”.

image

At this point, you could disconnect the Raspberry Pi from your monitor and keyboard, hide it in a closet somewhere, and do the rest of your configuration remotely. I’d recommend leaving it hooked up, though, at least for now. You’ll be setting up a remote desktop a little later on, and it’s nice to have the visual feedback on the monitor to let you know things are working.

Update all the things

Now that the Raspberry Pi is situated on the network, it might be a good time to check for any updates to software that came as part of the Raspbian image.

Raspbian uses a program called “Advanced Packaging Tool” (APT) to install software packages by downloading them over the internet from well-known public repositories. Packages are aware of their own dependencies, so that installing a package will automatically install any other packages that it requires.

Type the following commands to update any currently-installed packages to their latest versions.

sudo apt-get update
sudo apt-get upgrade

Let’s break that down:

  • sudo” runs with elevated privileges, which are required when installing or updating packages.
  • apt-get” is just one program in the apt suite, and the one you’ll use the most. It installs or updates software packages.
  • update” tells apt-get to update its list of available packages from the repositories it knows about.
  • upgrade” tells apt-get to upgrade currently-installed packages to their latest versions

The upgrade step will summarize what it’s going to change, and prompt you to confirm that this is what you want.

image

You can simply hit enter at this prompt, since “yes” is the default answer as indicated by the “Y” being capitalized in the prompt, and the “n” being lower-case. You’ll see this pattern a lot in Linux prompts.

New versions of the listed software packages will be downloaded and installed, which might take a while, depending on how many of them have been updated since the Raspbian image was last published.

image

When this is finished, type the following to update the Raspberry Pi’s own firmware and Linux kernel to the latest versions:

sudo rpi-update

Note: I’ve seen some tutorials that do this in the opposite order, updating the firmware and then the packages. Here’s the thing, though. Look at the screenshot above, near the bottom. In this example, apt-get upgraded rpi-update itself. I’d rather make sure I have the latest rpi-update before running it.

You’ll get a progress report as the update proceeds, which should look like the this:

image

Reboot one more time to complete the update.

sudo reboot

When this is complete, you will have a completely up-to-date Raspberry Pi.

Wrapping up

You’ve reached another milestone, and I strongly recommend politely shutting down the Raspberry Pi (sudo shutdown –h now), and taking another backup image of the SD card, especially if the update/upgrade cycle took a long time. If you want to start over later, restoring from this image will skip over those updates, and save you some time.

You’ll probably want to repeat the update/upgrade/rpi-update cycle every now and then as part of regular maintenance. Packages get updated all the time, so you need to stay up to date.

What’s next?

In the next post, we’ll add web administration capabilities.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 9 Comments

Raspberry Pi Home Server: Part 2, Installing the OS

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Note: This article is part of a series. See the Index for more information.

Updates: Raspbian Jessie has been released. The installation process is still the same, but some minor adjustments will need to be made. Most importantly, Jessie boots straight to the desktop by default. You can run the new windowed version or raspi-config by clicking “Menu”, then “Preferences”, and then “Raspberry Pi Configuration”. You’ll want to change the “Boot” option to “CLI” and then click OK.

You can also exit the desktop by clicking “Menu”, then “Shutdown”, selecting “Exit to command line”, and then clicking “OK”. Once at the desktop, run raspi-config as usual and change the boot configuration so that the Pi boots to the command line.

Everything else should be more or less the same.

Self-Promotion: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!


The first thing any computer needs is an operating system. There are several Linux distributions to choose from. You can think of a distribution as an “Edition” of Linux. Just like Windows comes in Home, Business, and Enterprise editions (or whatever they’re called this week), each Linux distribution consists of the same core OS, but with different combinations of additional features added. In the Linux world, these “features” could include things like the entire windowing system, so it’s a little more dramatic than Windows editions, but you get the point.

In this series we’ll be building on top of Raspbian, which is a Raspberry Pi variation on the Debian Linux distribution. It is available from the Raspberry Pi Foundation’s download page as an image, or as part of the “New Out Of Box Software” (NOOBS) package.

As mentioned in Part 1 of this series, NOOBS lets you pick more than one distribution and multi-boot between them. This can be useful for a Raspberry Pi that stays permanently attached to a television in your living room, allowing you to use it as both a media center and a gaming station. For a dedicated home server like this, though, it’s unnecessary.

Note: At this point, you can follow the directions at the Raspberry Pi Foundation’s own help pages. The tutorial there is very complete, and has videos to explain the process in greater detail than I’ll be going into here. In the interest of being complete, I’m going to summarize the instructions here as part of the series.

Prerequisites

  • A Raspberry Pi, power supply, etc.
  • An HDMI or composite monitor, keyboard, and a mouse (optional)
  • A wired network connection
  • An SD card of at least 4GB
    If you plan to boot from a hard drive, as covered later in the series, then you don’t need anything bigger than this.

Create the SD card

Decide whether you want to go with NOOBS or with the Raspbian image, and follow the appropriate instructions below.

Using an image file (recommended)

Go to the Raspberry Pi Foundation’s download page, and get the current Raspbian image. At the time of this post, that is the 6/20/2014 release. You can download this directly, or you can save the foundation some bandwidth costs and download via BitTorrent.

Get a copy of Win32DiskImager, and use that to write the Raspbian image to your SD card. More detailed instructions for multiple operating systems are available from the Raspberry Pi Foundation’s site. When you’re done, eject or “safely remove” the SD card from your computer.

Hook the Raspberry Pi up to your monitor, network, and keyboard (and mouse if you have one). Insert the SD card you prepared above. Finally, plug the power supply into the Raspberry Pi, and skip ahead to the “First Boot” section.

Using NOOBS (meh)

There are two versions of NOOBS. The Full version includes all the files needed to install any of the supported Linux distributions. The Lite version is much smaller, and will download the needed pieces at installation time. If you’re only planning to do this once, then the Lite version could save you some time. If you imagine that you’ll start over again several times before you’re happy with the result, then having the Full version around will cut out a lot of time spent re-downloading things again in the future.

Download your preferred version of NOOBS, and save it off somewhere. Format your SD card as a regular, Fat32 disk, preferably by using the SD Association’s SD Formatter tool. Copy the files from the NOOBS archive to the newly-formatted SD card, and “eject” or “safely remove” the SD card from your computer.

Note: If you have an SD card that gets corrupted, and even SD Formatter won’t bring it back to life, see my supplemental post “SD Cards Gone Bad” for some steps that might help you get your card back.

Hook the Raspberry Pi up to your monitor, network, and keyboard (and mouse if you have one). Insert the SD card you prepared above. Finally, plug the power supply into the Raspberry Pi, and allow it to boot to the NOOBS menu.

Note: In this screenshot, multiple operating systems have been selected for illustration.

You should see a list of operating systems, and some drop-downs for selecting your language and keyboard layout. I’m not exactly sure what the language options affect, since you’ll just have to set them again later on, but it wouldn’t be there if it weren’t important, right?

Pick your language settings, and check “Raspbian” in the list of distributions at the top. You could select multiple OSs, and get a menu at boot time, but for this server we’re only interested in Raspbian. Click “Install” at the top of the window, and go make lunch. This part is going to take a while, and even longer if you chose the “Lite” version of NOOBS, since it will have to download the OS at this point.

Eventually, NOOBS will finish setting up the OS, and you’ll be prompted to restart the Raspberry Pi. Clicking OK will reboot, and after a lot of scrolling start-up messages that may frighten users who are not used to seeing behind the curtain, you are ready to proceed.

First Boot

Note: The screenshots that follow are taken from a remote, SSH session running through Putty for Windows. If you don’t know what that means, that’s fine, it’s just that my screenshots are going to have a window frame around them, and I thought you should know why.

image

This program allows you to make changes to the configuration of the OS itself, and certain hardware settings. Perform the following operations to configure the Raspberry Pi:

Update raspi-config

Go to “Advanced Options” from the main menu and then select “Update”

image

This will make sure that you’re using the latest version of raspi-config. It only takes a few minutes, and you never know what improvements they might have made since the last image was published. If you just downloaded the image, then you’re probably already up to date, but it doesn’t hurt to check anyway.

Expand Filesystem

If you used NOOBS to create your SD card, then this was taken care of this for you. If you installed from an image file, though, you’ll want to select this option. Image files are typically shrunken down to take up less room, so if you put a 4GB image on a 16GB SD card, you’ll need to expand the file system to get the other 12GB of space on the card back.

Change password

From the main menu, select “Change User Password”

image

This will set the password for the user named “pi”. The default password is “raspberry”, but since “pi” is the all-powerful administrative user, you’ll want it to have a different password than everyone else’s, right?

Boot to Console

Go to “Enable Boot to Desktop/Scratch” from the main menu and choose “Console Text console, requiring login”.

image

Since this will be a server, you don’t want to waste resources displaying a desktop that no-one ever looks at. At the time of this writing, this is the default option, but I like to check just to be sure.

Set up language and locale options

Go to “Internationalization Options”

image

Set up all three options (Locale, Timezone, and Keyboard Layout) to match where you live. For the Locale, make sure that only the locale that matches your country is checked off.

image

Overclocking

In previous versions of raspi-config, overclocking was available, but voided your warranty. In the current version, this is no longer the case, and the Raspberry Pi is now capable of dynamically governing its own clock, turning it up when needed, and turning it down when the computer is idle or if it gets too hot. It does such a good job, that I usually give the Raspberry Pi free reign to go as fast as it wants whenever it wants.

CAUTION: The ability to overclock varies from board to board, and is affected by your power supply as well. If you are using some old leftover cell-phone charger you happen to have lying around in a drawer, this may not work for you. If you’ve invested extra money in a high-quality power supply, you’ll probably have better luck.

Until you know how your particular combination will behave, it’s probably safest to leave it running at the stock 700MHz. You can always come back to raspi-config later on and change it once you have everything stabilized.

image

Name that Pi

Go to “Advanced Options” from the main menu and then select “Hostname”.

image

Give your new Raspberry Pi a unique name. For this series, I am calling mine “RPHS” for “Raspberry Pi Home Server”.

Memory Split

Go to “Advanced Options” from the main menu and then select “Memory Split”

image

The 512MB of memory in the Raspberry Pi Model B can be used for graphics or by the CPU. During the initial setup steps, it can be useful to dedicate more memory to graphics to make the X windows desktop more responsive. Once the setup is complete, you’ll get better performance by dedicating less of the RAM to graphics processing. I set mine to 128MB during the initial setup phase, and then back down to 32GB when I’m done.

Enable SSH

Go to “Advanced Options” from the main menu and then select “SSH”

image

Since this will eventually be a “headless” server with no monitor or keyboard, you’ll need a way to communicate with it. Enabling the SSH server will allow us to “terminal” into it using an SSH client program like like Putty for Windows. This will be just like sitting at the command like in front of the computer. It’s also how I’m getting the screen-shots for this series.

Finish

From the main menu, select “Finish”, and answer “Yes” when prompted to reboot.

image

The new settings will be applied, and the Raspberry Pi will reboot.

Second Boot

When the Raspberry Pi reboots this time, it will be more or less a working computer. It doesn’t have a lot of programs installed so far, but it’s a starting point. You should see a login prompt similar to the following.

RPHS login:

Log in using the user name “pi”, and the password you set up earlier (or “raspberry” if you skipped that step). It’s not particularly good form to log in as the “pi” user all the time, since “pi” is an administrative user, not meant for daily use. One of the first orders of business will be to create a user account for yourself.

Type the following commands, substituting your name for the highlighted “USERNAME” where appropriate, and filling in the relevant information when prompted. You can just press enter to accept the defaults for things like room #, phone #, etc.

sudo adduser USERNAME
sudo usermod -a -G users USERNAME
sudo usermod -a -G sudo USERNAME

image

Let’s break down what that all means.

  • sudo” lets you run things with elevated privileges.
    It’s actually a little more complicated than that, but you can think of it as the equivalent of Windows’ “run as administrator”. There is a lot more to the sudo command than that, but this definition is adequate for our purposes.
  • adduser” does just what it says, it adds a user with the specified name (USERNAME).
  • usermod” modifies the named user (USERNAME). Here, you are appending (-a) the user to the group (-G) “users”, and to the group “sudo”. The “sudo” group is the group of users who are allowed to use the sudo command. Not everyone can run things with elevated privileges. The “Pi” user had this ability out of the box, and now you’re granting it to your own account.

You now have a user account that is specific to you, but with the necessary rights and privileges to perform maintenance tasks on the machine.

Test out your new user by typing “logout”, and pressing enter. Enter your new username and password when prompted in order to log on as yourself. your prompt should now reflect your new user name as well as the new name of the machine. At this point, you can play around with all the Linux-ey things for a while, and realize just how much you’ve forgotten about command-line interfaces. It’s all right, we’ve all forgotten.

You can also type “startx” to start up the desktop, and poke around with a mouse. Go ahead and play. When you’re done, click the red “power” button in the lower-right corner and click “Logout”. You’ll be back at the command prompt, which is where we’ll spend most of our time in this series.

Note: While you’re installing and configuring software, it’s probably still a good idea to continue logging on as “pi”. There are certain steps where this is more important than others, but it’s simpler for this series of articles if you just keep using “pi” for installation and configuration tasks.

Wrapping up

You’ve reached a milestone. The Raspberry Pi is now a fully-functioning computer. It has been localized, and somewhat personalized. Now is the ideal time to take a backup.

Shut down the Raspberry Pi “politely” with the following command

sudo shutdown –h now

This tells the compute to shut down “hard”, and to do it now. There are a few different shutdown options, including the ability to schedule a shutdown for a specified time and date in the future. This is the command you’ll use most often, though. After the screen turns off, wait a few more seconds for the Raspberry Pi to finish up any last-minute tasks, make sure all the lights have stopped blinking, unplug the power, and transfer the SD card to your primary computer.

Using Win32 Disk Imager, “Read” the contents of the SD card into a backup file for safekeeping.

image

I’ll recommend making these checkpoint saves after pretty much each post in this series.

What’s next?

In the next post, we’ll configure the OS and set up remote connectivity so that the Raspberry Pi can be removed from the monitor and keyboard and transplanted to a more permanent home away from your desktop.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 15 Comments

Raspberry Pi Home Server: Part 1, Meet the Raspberry Pi

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Important: This article is part of a series (see index here). Each post builds on the previous ones. I cannot make any promises as to your results if you have skipped something important, or have decided to build off of a completely different OS distribution.

Self-Promotion: I have recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!


A couple of years or so ago, I started looking into replacing the aging laptop that was acting as my home server; sharing files, media, and doing backups. Its fan was starting to sound less and less happy as time went by, and I knew it wouldn’t be long before it stopped working completely. Laptops use a lot less power than a desktop computer, so you’d think they’d make great servers, but they’re not really engineered to be on 24/7.

I’d seen a few posts about the Raspberry Pi and it seemed that it was capable of all the things my old laptop was doing while using even less power than a night light, and making no noise at all. I picked up my first Raspberry Pi at my local MicroCenter, and started putting it to work. It now spends its life mounted to a floor joist in my basement, right next to the wireless router.

What’s a Raspberry Pi?

So what is this thing, exactly? You can go to its website if you want the full rundown, but the gist is that it’s a Linux computer the size of a deck of cards (or a deck of Uno cards if you put it in a case). It’s not the prettiest computer in the world. The earlier models didn’t even really have a “front”. Because it’s so small, there were ports sticking off every side of the board.

Note: The newer models (B+, 2B, Zero) have consolidated the ports so that cables are only sticking out of one or two sides now.

The Raspberry Pi was designed as an affordable computer to teach students computer programming on. It has since caught on in the “maker” community for its ability to act as a cheap brain for a wide variety of electronics projects.

There are several different models currently for sale. The $25 model A has 256 MB of RAM, and 1 USB port. The $35 Model B has 512 MB of RAM, 2 USB ports, and a built-in Ethernet port. The B+ builds on the B and while it doesn’t change any of the key attributes of the system like RAM or CPU speed, it doubles the number of USB ports to four, moves some of the connectors around into a little nicer arrangement, and uses a bit less power. There was an earlier, 256 MB version of the Model B as well, but that was completely superseded by the 512 MB version.

Finally, there’s the Raspberry Pi Zero, which only costs $5, but has some serious limitations in both memory, speed, and connectivity. It’s a great choice for building small projects, or for fulfilling the Raspberry Pi’s original goal of teaching children to code, but it isn’t quite suitable for this project.

But I don’t know Linux

Yeah… that’s pretty normal. Let’s face it, Linux is not an OS you’d put in front of your grandmother or non-technical spouse. For people used to Windows or Mac machines, Linux is going to be very unfamiliar. You don’t get to just right-click on things and pick “Properties” to set stuff up. When you plug in new hardware, Linux doesn’t automatically download drivers from the internet and set them up for you.

For those of us that were around for the first wave of home computers in the 80’s, and remember tweaking out the settings in autoexec.bat to get the memory settings just right so that we could play a certain game, this will be like a wave of nostalgia. For the younger folks, this will seem very strange and foreign.

Linux is an enchanted land full of magic words that you have to get exactly right in order for them to work. You have to stand in the right spot, at the right time of night, under exactly the right moon, and you’d better be sure that the chicken you’re about to sacrifice is a lefty, or you’ll bring doom and destruction on all the land.

Okay, it’s not quite that bad, but if you are uncomfortable with the idea of following instructions that tell you to type an obscure string of commands and parameters with little or no explanation as to what they mean, you’ll need to get over that fear. The internet is your friend, and you can look up how to do just about anything, but there are a lot of things to configure, and people aren’t always going to tell you why something has to be phrased the way it is.

In this series, I’ll try my best to explain the meaning of the obscure commands as best as I can, at least the ones that I understand.

What hardware do I need?

Unless you bought some kind of bundle, your Raspberry Pi will need a few accessories before you can do anything with it. The Raspberry Pi doesn’t come with a power supply, or any kind of storage. When you buy a Pi, that’s all you get. A single-board computer, and nothing else.

You’ll need to supply your own monitor, keyboard, and mouse. The monitor can be anything with an HDMI or composite video input. There are adapters available to go from HDMI to VGA if that’s all you have available, but they can be pricey. Any USB keyboard will do, and if you’re planning to stick to the command-line interface, you won’t even need a mouse, but without one you won’t get to play with the X desktop, and it does come in handy at a few points in this series.

The power supply can be almost any Micro-USB phone charger, although it must provide at least 700mA (1000mA for the Pi 2). Since the quality of the power supply can directly affect the stability of the system, I’d recommend buying a high quality supply like the Raspberry Pi branded 1-Amp power supply. They’re only about $10, and will save you frustration later. I have some leftover chargers from old cell phones, and some of them claim to be full 1-Amp supplies, but can’t even power the caps-lock light on my keyboard. The better your supply, the better your experience will be. Trust me, it’s worth it.

Note: In general, you should try to avoid plugging things into the Raspberry Pi while it is running. Linux is more than capable of hot-plugging peripherals, but with such a small power supply, the Raspberry Pi has tendency to “brown out” and reboot when you plug things in. Your basic flash drive is probably safe, but plugging in a game controller might power-cycle the computer. You’ve been warned.

Storage comes in the form of an SD card. As part of this series, I’ll show you how to boot the Raspberry Pi from an external hard drive, so an SD card over 8GB is probably overkill. I used to say 4GB, but the newer Raspbian “Jessie” release needs a little more space. Once you’re booting from the hard drive, the speed of the card won’t matter anymore, either, but faster cards will make some of the initial setup noticeably faster. Larger cards will take longer to back up though, so I usually stick to an 8GB card while I do the build.

Finally, you may want to buy a case of some kind. There are plenty to choose from, and there are even plans you can print out and fold up to make a case out of card paper. Many people seem to like making cases from Lego bricks as well. I’d recommend a decent protective plastic case with keyhole slots on the back so that the whole thing can be mounted somewhere when you’re finished. You could also Velcro the Raspberry Pi to the side of the hard drive if you want.

What software do I need?

The Raspberry Pi has no built-in operating system of any kind. It expects to find its OS on the SD card when it powers up, so you’ll need to get an OS onto the SD card before it’s going to do anything.

Note: Older versions of these posts talked about using the Raspberry Pi Foundation’s “NOOBS” image, but that really does cause more problems than it solves when building a server, so I have removed that information. If you really want to read that stuff, then you can use the Internet Archive’s Wayback Machine:

https://web.archive.org/web/*/melgrubb.wordpress.com

 

These days, I only recommend getting a plain, Raspbian image from the Raspberry Pi Foundation’s download page, and writing it directly to the card using a program like Win32 Disk Imager. It will boot a little faster because there’s no pause for the recovery mode that you’re probably not going to use anyway, and the resulting SD card’s layout will be much simpler, with fewer partitions.

You’ll also want to go get the SD Association’s “SD Formatter” tool. This will help you reclaim the unallocated space on your SD card in the event that you need to start over again. When you write a 4GB image to a 16GM SD card, you can’t just stick it in a Windows machine, reformat it and expect to get all the space back. Windows is going to see an SD card with a 4GB partition, and 12GB of unallocated space which it will totally ignore. When you tell Windows to format the drive, it’s just going to format the 4GB partition, and leave the other 12GB alone. SD Formatter will format the whole card and get all your space back.

You can accomplish the same effect using something like diskpart on Windows or fdisk on Linux, and you’ll need them if things go really wrong, but in most cases, SD Formatter is all you need. See the supplemental post “SD Cards Gone Bad” for more information on using diskpart to recover corrupted SD cards.

What’s next?

That’s it for the introduction. In the next post, we’ll start building the initial OS image, and get the machine up and running.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 5 Comments

Raspberry Pi Home Server: Index

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Update: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!


This series of posts is about using a Raspberry Pi as a low-cost, always-on home server. These posts started off as my own notes about what worked and what didn’t, and the entire series has seen a few updates in the short time since I first published it.

Hopefully, readers will find this series to be well organized, and I hope it can help others who are trying to build a Raspberry Pi Home Server get past some of the hurdles that I ran into. Things like:

  • Dead SD cards
  • Bad permissions
  • 403: Permission Denied when trying to use Transmission
  • Broken database connections
  • Broken WordPress themes

Meet my home server

WP_20140731_21_05_41_Pro

It consists of the following

  • Raspberry Pi Model B
  • 5V, 1Amp power supply for the Raspberry Pi
  • 8GB, Class 10, MicroSD card with adapter
  • Cavalry CADA-SA2 USB Raid enclosure with a pair of 2TB drives in it
  • USB cable for the hard drive

That’s it. That’s the whole thing. That is my home server. It uses less electricity than a night light, less than a penny a day (not counting the external drives), and it does all of the following for me.

  • Home network file share
  • DLNA-compliant home media server
    Currently sharing every CD in the entire house
  • Transmission BitTorrent client
    Mostly used to get new Raspberry Pi images, actually
  • CrashPlan Backup Server
    Backs up all the household computers whether they are at home or not
  • OpenVPN Server
    I can VPN to my home network from anywhere with a connection
  • BitTorrent Sync
    Synchronizes files across all of my devices

There’s plenty more it can do. I’ve only just started.

Sound good? Want one? Well go buy yourself a Raspberry Pi, an external drive, and follow along with this series, and you’ll have a fully-functioning home server before you know it. You can build the entire thing in four to five hours from beginning to end. I’m not kidding.

Acknowledgements

Virtually everything in the series is available elsewhere on the internet, in bits and pieces on other blogs and in forum discussions, but I never found anything that really put it all together for me in a logical order, so I decided to write my own.

A lot of credit goes to the Instructables article that got me started: http://www.instructables.com/id/Ultimate-Pi-Based-Home-Server

It’s a good article, but it seems to be quite out of date. Also, I wanted to start from a standard Raspbian image rather than TurnKey, and I didn’t agree with some of the choices and ordering. For instance, I felt that basic file sharing should be tackled before building out a private cloud, and that the LAMP stack should be installed all at once before any other components that depend on it to prevent their installations from jumping the gun and installing bits of it on their own.

Articles

These are the steps to building a Raspberry Pi Home Server like the one I have at home.

  1. Installing the OS
  2. Configuring the OS
  3. Web Administration
  4. Remote Desktop
  5. Adding a Hard Drive
  6. Sharing Files With Samba
  7. Sharing Media With MiniDLNA
  8. Transmission BitTorrent Client
  9. CrashPlan Backup Server
  10. OpenVPN Server
  11. The LAMP Stack
  12. WordPress Blog Engine
  13. BitTorrent Sync
  14. Extra: SD Cards Gone Bad
  15. Power Failures

Thanks for reading, and good luck.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 52 Comments

Raspberry Pi Home Server: Part 10, CrashPlan

Note: This article is part of a series. See the Index for more information.

Self-promotion: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!

Updates: It is no longer necessary to replace the Java Runtime Engine since the newer installer doesn’t seem to include it anymore, so skip removing /usr/local/crashplan/jre, which doesn’t even exist, and don’t create the symbolic link.

There is a newer version of the standard widget toolkit (SWT) in the current installer, so after installing it, take a look in your /usr/lib/java folder to see what version you have. My most recent version was swt-gtk-3.8.2.jar, rather than the …3.8.0 from the original installation. Adjust the copy command accordingly.

Initially my service wouldn’t start, but after a reboot it worked just fine. If you run into trouble, reboot and try again.


IMPORTANT UPDATE 2017-10-07:

I’m leaving this here for posterity, but the simple fact is that CrashPlan is no longer a viable option on the Pi. It is no longer written in pure Java, some key libraries are only available for Linux running on x86 architectures, and Code42 has eliminated the free home-user product from their offerings meaning that you’ll have to pay for a subscription in order to keep using CrashPlan at all. This no longer fits with the “free if you do it yourself” theme of this blog series, so I am an throwing in the towel.

See “CrashPlan is dead


IMPORTANT UPDATE 2016/10/02:

I’ve gotten a number of comments about CrashPlan not working lately. It looks like some recent update has possibly broken CrashPlan on the Raspberry Pi to an extent we’ve not seen before.

Code42 wrote a great backup program, but the Pi isn’t an officially supported platform, and never has been, so you can understand why they don’t really have any incentive to come rushing to our rescue. They never meant CrashPlan to run on ARM devices, and that’s what the Raspberry Pi is.

Some users have reported being able to get the engine running, but not the UI, and others have said that even the engine isn’t working anymore. My own CrashPi stopped working as well, and I haven’t had a chance to see if I can find a way around it. Until further notice, I can’t really recommend running CrashPlan on the Raspberry Pi. I’m open to suggestions if anyone knows a way to get it running and keep it that way, but I haven’t found one myself yet. I am looking into it, though.

Update 2016/05/17

If you tried recently to install CrashPlan and it failed, or wouldn’t stay running for very long, it looks like the most recent installer has fixed whatever was broken. The current download from Code42’s site should be version 4.7 or newer, and readers are reporting that all is well again.

If, like me, a previous auto-update broke your existing installation, then I suggest uninstalling CrashPlan like this (From memory, forgive me if I get something wrong)

cd /mnt/data/public/downloads/crashplan-install
sudo ./uninstall -i /usr/local/crashplan

Then download and install the latest version from Code42. You’ll need to repeat all of the steps in this article, but as long as you point the backups to the same directory you were using before, CrashPlan should pick them up and continue using them, so you don’t have to start completely over. You will need to reinstall and re-patch the UI, and then sign in to your account again, just like the first time around.


Updated 2015/02/07:

Recently, Code42 (CrashPlan’s developer) updated their installation files, and the instructions in this article stopped working. Thanks to the work of several readers (see the comments), I’ve been able to revisit this article and update the instructions so that they work once again.

See the end of this article for additional troubleshooting information if you find that your installation mysteriously stops working one day.

Overview

One of the main jobs of a proper “home server” is to back up the other computers on the network. You could accomplish this through the file shares I covered earlier in the series, but I’d like to try something a little more ambitious.

There’s a great product out there called “CrashPlan” (www.code42.com/crashplan). The company has subscription offerings that will store your backups in their “cloud”, similar to other products like Carbonite (www.carbonite.com), but if you can supply your own storage, CrashPlan is free.

The free version of CrashPlan can back up to a local drive on the same computer, or to another computer that is also running CrashPlan. Your Raspberry Pi Home Server can be that “other computer”, and handle the backups for all of the computers in your house.

Note: It is possible to get CrashPlan to backup to a share on your local network, too, but it involves jumping through some symbolically-linked hoops, and won’t be covered here.

Acknowledgements

I didn’t figure all this stuff out on my own, not by a long shot. I am standing on the shoulders of other geeks here. In 2012, Jon Rogers wrote a blog post about getting CrashPlan running on the Raspberry Pi. That article is largely the basis for this one.

In February of 2013, Bion Ren posted an updated version of Jon’s instructions, replacing OpenJDK with Oracle’s Java 8 preview. He also called attention to a post by Torbjörn that solves a major configuration issue, and a comment by Brad Peterson that addresses a rather serious stability issue with regards to swap file use.

I am posting this because I wanted to gather together a complete, updated set of end-to-end instructions for adding CrashPlan to the Raspberry Pi Home Server I’ve been describing in this series. As I have pointed out before, I am by no means a Linux expert. I could not have done this without the work of those that went before me.

Before you start

CrashPlan was not written with tiny computers like the Raspberry Pi in mind. It expects to be running on a computer with more resources in general, and more memory in particular. When it doesn’t have enough memory, it will start paging to the swap file. Whether or not this will wear out your SD card is a rather contentious subject, and I’m not going to pretend that I know the answer to that one. One thing is for sure, though, SD card-based swap files are slow.

As a result, I cannot recommend running CrashPlan on a Raspberry Pi unless you have moved your swap file (and I recommend your root filesystem as well) onto a proper hard drive as covered in my “Adding a hard drive” post. If you skipped that article, this isn’t going to work for you. The SD card just doesn’t have enough space to hold backups. In order to serve as a useful CrashPlan backup destination, you’re going to have to have a big hard drive anyway, so you may as well take full advantage of it.

In addition, CrashPlan does some pretty heavy lifting in the background, compressing and pruning backups, and doing general housekeeping. This can use a lot of CPU, and make the rest of your server’s functions slow to respond. As I’m writing this, my own CrashPlan instance is using up about 60% of my CPU time compressing backups from my primary computer. CrashPlan is usually pretty good at getting out of the way when other programs need to think, at least on the desktop.

YMMV

Java

CrashPlan runs on Java, which means it can run on anything with a Java runtime. Current Raspbian images (2015-01-31 at the time of this update) already include the Java Runtime, version 8. You can check this by typing the following at the command line:

java –version

If it says “1.8.0” or above, you’re all set to go. If not, you may need to install the Java Runtime Engine (JRE) yourself. Unfortunately, I can’t find a simple JRE installer for the raspberry pi at this time, so you’ll just have to install the whole Java Development Kit (JDK). Only do this if you don’t already have JRE8, as checked in the last step.

sudo apt-get install oracle-java8-jdk

There is an additional piece you’ll need in order for CrashPlan to work. According to a user called Torbjörn on the CrashPlan forums, you’ll also need the Java Native Access (JNA) library. Install them both at once like this:

sudo apt-get install libjna-java

When the Java installation completes, you are ready to install CrashPlan itself.

Get the CrashPlan installer

Since there are new versions being released all the time, I can’t give you a simple url to “wget”. Go to http://www.code42.com/crashplan/thankyou/?os=linux, from a browser on the Pi itself, and the download should start automatically. I used the new “Web” browser that’s part of the Raspbian image, so the file ended up in /home/pi/Downloads. Depending on your browser, your downloads may go somewhere else. If you can’t figure out where they are going, just download the package from your primary computer, and copy it over on a flash drive.

When the download is complete, find the .tgz file and extract it. Since I was already in the desktop environment to run the browser, I took advantage of the modern amenities, right-clicked the file and picked “Extract here” from the context menu. Sometimes it’s nice to live in the future, if only for a moment.

If you want to do it from the command line, go to the folder where your .tgz file is:

cd /mnt/data/Downloads

…and type the following:

tar zxvf FILENAME.tgz

Install CrashPlan

Navigate to the folder where you just extracted the installer (Note: your folder may be different):

cd ~/Downloads/CrashPlan-install

Execute the installer from the command line.

sudo ./install.sh

The install script will prompt you for anything it needs, including accepting a really long EULA that you probably won’t really read (q exits the reader). You can accept most of the defaults and let the installer create any missing folders, but one directory you should probably change is the backup location.

Unless you are backing up something very very small, like another Raspberry Pi, the root partition is not going to be big enough to hold the files. Change the backup location to somewhere on the data partition. I set mine to “/mnt/data/CrashPlan”, and let the installer create the folder. You’ll get a summary and a confirmation prompt like this:

image

Double check all of your values, and then let the installer go. It’s a surprisingly fast installation. If there were no problems, you should see a confirmation like this:

image

At this point, you have CrashPlan installed, but it’s not very happy. Although the installer says the engine is installed and running, it’s actually not. You can check on the status of the CrashPlan service like this:

service crashplan status

The answer will probably say “CrashPlan Engine is stopped.”, and even if you try to start is manually, checking its status will still say “stopped”.

The trouble is that the installer downloaded it’s own private copy of the Java Runtime Engine, version 7 (JRE7). Normally, this would just be an irritating waste of storage space, having multiple copies of the JRE around, but the one CrashPlan installed isn’t even the right architecture. The CrashPlan installer has installed a JRE that assumes it’s on a desktop processor, specifically something i586-ish (Pentium, Core, etc.)

Don’t worry about it if you don’t know what that means. All it means to us is that we’re going to have to trick CrashPlan into running on the JRE that’s already on the Pi, instead of the one it installed itself. Thanks to Rainer Boehme and Matt for showing me this in the comments.

The first order of business is to blow away the useless x86 version of JRE7.

sudo rm -r /usr/local/crashplan/jre

Next, we’ll create a “symbolic link”, which is a kind of shortcut on your hard drive. Symbolic links make things in one place appear to be somewhere else, or even in multiple places. You can use them to keep one physical copy of a file, but have it appear in many different places. Here, we’re going to use one to make the raspberry pi-friendly JRE8 appear where CrashPlan put its raspberry-unfriendly JRE7.

sudo ln -s /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt /usr/local/crashplan/jre

Patch CrashPlan to work on the Pi

CrashPlan is now installed, but it’s not very happy. If you were to look in the /usr/local/crashplan/log/engine_error.log file right now, you’d see a complaint about the /usr/local/crashplan/libjtux.so file being the wrong version. Luckily for us, Jon Rogers already fixed that problem.

Return to the terminal, and go to the /usr/local/crashplan folder where you will find the file in question.

image Remove the bad libjtux.so file, and replace it with the one that Jon Rogers patched.

cd /usr/local/crashplan
sudo rm libjtux.so
sudo wget http://www.jonrogers.co.uk/wp-content/uploads/2012/05/libjtux.so

image There’s also something wrong with the libmd5.s0 file, so replace that one the same way.

sudo rm libmd5.so
sudo wget http://www.jonrogers.co.uk/wp-content/uploads/2012/05/libmd5.so -O ./libmd5.so

image

These two files should allow the CrashPlan engine to start up.

Next, edit the CrashPlanEngine file.

sudo nano /usr/local/crashplan/bin/CrashPlanEngine

Find (ctrl-w) the line that starts with “FULL_CP=”, and add “/usr/share/java/jna.jar:” (don’t forget the colon) to the beginning of the string value so that the result looks like this:

image

Exit nano, saving the file (ctrl-x,y,enter)

Start CrashPlan

To check whether everything is working, try launching CrashPlan manually.

sudo service crashplan start
sudo service crashplan status

If everything went well, you should get a message indicating that the CrashPlan service is running.

image

Configure CrashPlan to auto-start

Having a backup system like CrashPlan won’t do you much good if it doesn’t start back up again after your power goes out. Type the following command to automatically configure CrashPlan to auto-start after a reboot.

sudo update-rc.d crashplan defaults

To make sure that it worked, reboot the Raspberry Pi, log back in, and check the status of the service. It should be running.

sudo reboot
…
sudo service crashplan status

image

Patch the CrashPlan UI

At this point, you have a working CrashPlan server, but no way to communicate with it. You need to get a GUI talking to the CrashPlan engine so that you can configure backups, change preferences, and generally fiddle with the knobs.

LiquidState has a very detailed walkthrough of configuring CrashPlan to answer to a UI running on a different computer. If that’s how you want to do things, then go ahead, but it’s not that much harder to get the UI running on the Pi’s own X desktop.

If you were to try the CrashPlan icon that the installer has placed on your X desktop right now, it wouldn’t work because the version of the Standard Widget Toolkit (SWT) library that CrashPlan installed won’t work on the Raspberry Pi. You can double-click that icon all day and it will never get you anywhere until we fix this problem.

Install a compatible version of the SWT library like this:

sudo apt-get install libswt-gtk-4-java libswt-cairo-gtk-4-jni

Next, replace the SWT library that CrashPlan installed with the compatible one. You’ll notice that we’re totally lying about the filename. CrashPlan isn’t meant to run on ARM processors, and in 4.8 they started choosing between x86 and x64 versions of the libraries dynamically at runtime. We’re renaming the ARM-compatible version of the toolkit so that it appears to be the x86 file that CrashPlan will end up trying to run on the Pi.

sudo cp /usr/lib/java/swt-gtk-4.3.2.jar /usr/local/crashplan/lib/org.eclipse.swt.gtk.linux.x86.jar

Note: Thanks to Jeremy Nauta for identifying newer versions of these libraries being used by CrashPlan 4.8

The exact version of this file is subject to change, so if the command complains, use tab-completion to help find the correct filename. Type up to “/usr/lib/java/swt” and then hit the Tab key to fill in the rest. This file has changed more than once since this article was first published.

You can now run the CrashPlan UI directly from the shortcut the installer put on your X desktop. The first run can be pretty slow, so give it some time.

You’ll either need to connect to an existing CrashPlan account, or create a new one. It’s been so long since I signed up, that I honestly couldn’t walk you through the steps anymore, but all you’re going to need is what’s included in the free version.

Back up your stuff

Now that you have a CrashPlan server, it’s time to start backing things up. Install the CrashPlan software on the computer you want to back up, and open the UI. Sign in using the same account information you used above.

From here on, it’s just business as usual. Choose what you want to back up, and select “another computer” for the destination. If you signed in with the same account on the client and server, they should see each other in the list, and you can pick the Raspberry Pi as your backup location.

If your computers have trouble seeing each other, I’d suggest looking in the support section of the CrashPlan site. CrashPlan does a pretty good job of opening its own firewall holes via UPnP, but perhaps your router doesn’t support that, or maybe you’ve turned it off on purpose. Such networking issues are beyond the scope of this article. Your mileage may vary. Google is your friend.

Warning: The first time you back up another computer it will take a very long time, and it’s going to peg the Raspberry Pi’s CPU, just like when MiniDLNA was indexing your media. Don’t worry, you’re not going to hurt the Pi. Mine was at 100% CPU usage for more than a day, and the CPU temperature never rose above 64°C. As long as the temperature stays below 80°C, you have nothing to worry about. Subsequent backups will also run up the CPU, but not for nearly as long.

The CrashPi: Secure, off-site backups

Now we come to the real payoff, and the part that’s going to keep your stuff safe in the event of a fire or flood. When you use CrashPlan to back up to another computer, that other computer does not have to be on the same local network as the computer it’s backing up. It can be anywhere on the internet. It could be on the other side of town, or in a different state.

A Raspberry Pi is cheap enough that you could just Velcro one to the side of a big USB drive and leave the whole thing at a buddy’s house. If your buddy does the same, and parks a CrashPi (as I am now calling it) at your house, then you’ll both be protected in the event of a disaster… unless, of course, you live in the same apartment building. You should choose a location for your off-site backups that is far enough away that both sites are unlikely to fall victim to the same disaster.

To build a CrashPi, all you need to do is build another Raspberry Pi Home Server, leaving out all the parts that CrashPlan doesn’t need, which is actually most of them. Follow this subset of the Raspberry Pi Home Server blog series, and you’ll have made a CrashPi.

  1. Installing the OS:
    Start with a small, cheap SD card. You only need 4GB.
  2. Configuring the OS:
    If you want SSH access to maintain the CrashPi remotely, your buddy will have to punch a hole in the firewall, or put the CrashPi in his/her DMZ.
  3. Web Administration: (Optional)
    This will let you check in on your CrashPi to perform maintenance, install updates, etc.
    Your buddy will have to punch a hole in the firewall, or put the CrashPi in his/her DMZ.
  4. This post:
    Obviously

You may have noticed something CrashPlan calls a “backup code”. This is a unique code that identifies a specific CrashPlan installation. You can give this code to a friend to let them back things up to your server, even though you have different accounts. For instance, when the kids head off to college, you might want to let them use your Raspberry Pi Home Server as a backup destination. All they’ll need is that particular computer’s “backup code”, which you can get from “Friends” tab in the CrashPlan UI.

What’s next?

As far as core home server functionality goes, the Raspberry Pi is already doing a lot. It’s sharing files and media on the local network, downloading things for you, and backing up your computers, even over the internet when you’re away from home.

What more could you possibly want?

How about Virtual Private Networking (VPN) access to your home network when you’re away? Sound good? In the next post, we’ll set up OpenVPN so that you can have all the safety and security of your home network, even from the sketchiest of airport coffee shop networks.

Troubleshooting

I have a sneaking suspicion that this article is going to see more updates than most, so I’m starting a section here at the bottom to catch any updates.

CrashPlan UI has stopped working!

If you have already installed CrashPlan in the past, and find that the UI doesn’t work anymore after performing an apt-get update/upgrade, you’ll need to repeat the final step of the article once again to put things back to normal. Simply run the following commands to replace the SWT library that CrashPlan installs with one that is compatible with the Pi, and you should be up and running again.

sudo cp /usr/lib/java/swt-gtk-3.8.0.jar /usr/local/crashplan/lib/swt.jar

You may need to do this each time you do an apt-get update/upgrade, unfortunately. I haven’t quite figured out what causes the problem just yet, but since CrashPlan wasn’t installed through apt-get, that shouldn’t be it. Just remember this in case your UI stops working one day.

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged , | 296 Comments

Raspberry Pi Home Server: Part 5, Remote Desktop

Article Obsolete

A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.


Note: This article is part of a series. See the Index for more information.

Self-promotion: I’ve recorded this series as a screencast for Pluralsight:
(http://www.pluralsight.com/courses/raspberry-pi-home-server)
If you have a Pluralsight subscription, please consider watching it. Thanks!


Update – 2016/10/04

The newest release of Raspbian (the one with the “Pixel” desktop) already has RealVNC built it. I was using x11VNC in this article, and I had good reasons for that, but with RealVNC built directly into the Raspbian image now, there’s no reason to not use it. All you have to do is go to the “Interfaces” tab on the system preferences dialog, and enable VNC.

I was also using UltraVNC as my viewer before, and I’m sure it can be made to work with RealVNC, but it’s much simpler to just download the RealVNC viewer. It’s a portable application, and doesn’t need to be installed. You just download it, point it at your Pi’s address, and you can connect in no time.

Basically, this article is no longer needed. I’m leaving it here for posterity, but there’s no need to do any of this by hand anymore.


Other Updates: Since the Jessie release of Raspbian initially booted straight to the desktop, you won’t need to manually run the desktop once using “startx” just to create the “Desktop” directory. It should already be there.

I have noticed one other configuration change under Jessie that you’ll have to deal with. You’ll need to edit the /etc/X11/Xwrapper.config file and change allowd_users from “console” to “anybody” in order to start the desktop from an SSH session.

Other than that, nothing changes under Jessie.


So far in this series, we’ve set up the Raspberry Pi for remote login via SSH, and for web administration through Webmin. Sometimes, though, you just want to see a desktop, and that’s not easy when the computer you’re trying to see has no monitor, keyboard, or mouse.

You can easily add remote desktop ability to the Raspberry Pi pretty at this point through a program call X11VNC. There are other VNC (Virtual Network Computing) packages out there, but this one has worked pretty painlessly in my experience.

Installing X11VNC

Type the following to install the X11VNC package.

sudo apt-get install x11vnc

That’s it, a single apt package, and X11VNC is installed.

Configuring X11VNC

There’s a bit of configuration to be done before your remote desktop will be usable. Firstly, if you haven’t run the desktop while logged in as “pi” already, you’ll want to do that now. You’re about to need a certain folder that gets created automatically by the X desktop when it is first run. So, if you haven’t run the desktop yet, make sure your Raspberry Pi is hooked up to a monitor and go to the desktop by typing the following:

startx

Give the desktop a minute or so to complete whatever startup tasks it has, and then exit the desktop by clicking on the red “power” icon in the lower-right corner, and clicking “Logout”.

You’ll need to establish a password used to secure the remote desktop connection itself. Type the following command to do this:

x11vnc -storepasswd

You will be prompted to assign a VNC password. This doesn’t have to be the same password as your regular password, but you may want to keep the passwords in sync so you’ll have fewer to remember. When prompted whether to store the password, you can just hit enter to accept the default answer of “yes”.

Next, create a folder to hold the X11VNC desktop configuration file.

mkdir /home/pi/.config/autostart

Note: If you get an error that a folder doesn’t exist, it’s probably because you didn’t run the desktop at least once while logged in as “pi”. See the first configuration step above and try again after having started the desktop at least once.

Create and edit a new desktop configuration file by typing the following:

nano /home/pi/.config/autostart/x11vnc.desktop

Copy the following lines into the new file. Without going into too much detail, you are specifying that x11vnc should start running whenever the x desktop is shown.

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=X11VNC
Exec=x11vnc -forever -usepw -display :0 -ultrafilexfer
StartupNotify=false
Terminal=false
Hidden=false

Close nano, saving your changes (ctrl-x,y,enter), and reboot the Raspberry Pi to force it to pick up on the changes.

sudo reboot

To test this out, log back in as “pi”, and start up the desktop by typing “startx” at the command prompt. The desktop should start up the same as it did before.

Open your favorite VNC viewer application. I have used both TightVNC and UltraVNC. Which one you prefer is a personal choice. Connect to the Raspberry Pi by IP address or by name (RPHS), and you should be prompted for the password you created above. Type in the password, and you should be looking at a remote desktop that mirrors what’s currently on your monitor.

image

The real magic is that you can start the desktop remotely via SSH. This is how you’ll get to a remote desktop once the Raspberry Pi goes “headless” (loses its monitor). There’s a slight hiccup, though. If you unplug the Raspberry Pi from any kind of monitor, it won’t know how to pick a resolution. In fact, it won’t even know if it’s supposed to be using an HDMI monitor, or composite. As a result, you’ll get a uselessly tiny screen when you connect via VNC.

By editing the boot configuration file, we can take the choice of resolution and output off autopilot an tell the Raspberry Pi what resolution to use.

sudo nano /boot/config.txt

Look for the line that says “hdmi_force_hotplug=1”, and uncomment it by removing the pound sign from the beginning of the line. Do the same for the “hdmi_group” and “hdmi_mode” lines. You’ll need to provide values for them, though. You can find a pretty exhaustive list on the eLinux site. Pick something that fits comfortable on your primary computer’s monitor.

Log in via SSH and start up the desktop by typing “startx”. The command prompt will show something like this:

image

Don’t worry about that “fatal” error. It does that to everybody. It’s not important.

Connect your VNC viewer, and you’re all set to go.

image

Remember to log out of the desktop when you are done so the server doesn’t waste CPU cycles running a desktop that no-one is looking at. At that point, the command prompt in the SSH window will wake back up, and you can log out of the Raspberry Pi completely.

image

Problems?

I’ve sometimes had problems with the remote desktop only working once per reboot. Once I’ve logged out of the desktop, it doesn’t seem to want to connect again until I reboot. I’m not sure why, but if I figure it out, I’ll update this post. For now, I just reboot the pi when the remote desktop doesn’t work. I’m not running a fortune 500 company’s critical infrastructure on the thing, just some simple tasks around the house, so the occasional reboot doesn’t bother me.

Other users?

If you want to log on to a remote desktop session as a user other than “pi”, you’ll need to repeat the configuration steps (starting the desktop once, creating the autostart file, etc.) while logged in as the user you want to have remote desktop abilities. Make sure to SSH in as the user whose desktop you want to see, and manually start up the desktop in the same way as you did for “pi”.

I have not personally done this, since the only reason I would want to see the remote desktop would be to do administrative tasks, which I always perform as “pi” anyway.

Wrapping up

You’ve reached another milestone, and I strongly recommend politely shutting down the Raspberry Pi (sudo shutdown –h now), and taking another backup of the SD card.

What’s next?

That’s it for all the different ways we can remotely connect and administer the server. In the next post, we’ll add a hard drive, and get the Raspberry Pi to boot from it (mostly).

Posted in Computers and Internet, Home Server, Raspberry Pi | Tagged | 42 Comments

Urinal Code Kata

While I was at CodeMash 2012, I had an idea for a programming Kata. I remembered an online “Urinal Quiz” that I had seen in the past. Basically, the idea is that for any given set of urinals, occupied and unoccupied, there is a single “best” one to pick. The rules for choosing the “correct” stall were simple enough to be memorized, but complicated enough to have some exceptions. This is similar to what we experience all the time at work as programmers. There’s a simple rule such as “Claims are processed the last Thursday of every month” and a bunch of exceptions “Unless the last Thursday is the last day of the month, in which case they are processed the week before”. It seems we’re always confronted with something that should have been very simple, but is made complicated by some set of arcane edge cases.

The rule set in this case is actually quite simple, so it makes for a good Kata. It also had the advantage of being somewhat humorous, and so I turned it into an entry in the Thursday night Pecha Kucha talks (20 slides * 20 seconds each). The video is available here (Codemash 2012 Urinal Code Kata)

There are a lot of ways to solve this particular problem. For instance, I considered assigning point values to different rules, and evaluating them for each position. I didn’t like this approach because it’s not the sort of thing you can do in your head easily. Instead, I decided to approach this using Linq as a sorting and ordering problem. This is closer to the way we think in our heads “Find the stall furthest from the door that has no neighbors”. The whole problem can be expressed with three very simple rules.

  1. Get as far away from the door as possible.
  2. Try not to stand next to another dude.
  3. Really try not to stand next to two other dudes.

The exceptions are not that complicated either. Rules 2 and 3 can be violated in one of two cases.

  1. There is a line
  2. Someone else has already broken that particular rule.

The first exception is simple to implement. The second one requires a bit of analysis of the existing data, and also some explanation. If someone else has already broken a rule, and you continue to follow it, you are unintentionally calling attention to their infraction, and that is clearly a violation of the Bro-code.

Like all good Katas, don’t try to over-analyze the problem up front. The rules can be further simplified, but that will happen as part of the Kata. The key with Katas is that you’re trying to train your mind to recognize patterns so that you can pick up on them when they happen in the real world.

I like to write tests for Katas in a style similar to many of the “Koans” you may have seen. It’s the opposite of “One assert per test”. In fact, there is only one test with many many assertions in it. This means that at any given time, running the unit tests will only tell you one thing that’s wrong with your code. Only when you have addressed that problem are you allowed to know the next one. It’s an approach that stops you from thinking ahead and trying to solve the whole puzzle at once.

So, lets see how the Kata progresses. First we’ll need a function to evaluate the scenario. I’ve refactored a little bit since the first presentation of the Kata during the CodeMash 2012 Pecha Kucha sessions. The function takes a boolean array representing whether the stalls are occupied or not, and returns a nullable integer representing the correct stall to choose. The array is taken to be in order of closest to farthest from the door. For instance, the array { false, false, true } represents three stalls, with the farthest one from the door being occupied. There’s an additional boolean parameter that indicates whether or not there is a line.

The function will first do some manipulation of the data, compiling neighbor counts and such, before proceeding to evaluate the rules. The version presented at CodeMash used the Neighbors method repeatedly during rule evaluation. Later, a coworker, Jeff Walker, suggested a way to move all the counting up-front so that it only happens once. It’s a good optimization, and makes the query much easier to understand.

The Method
  1. public class UrinalRules
  2. {
  3.     public static int? Solve(bool[] stalls, bool queued)
  4.     {
  5.         var positions = stalls
  6.             .Select((occupied, index) => new { index, occupied, neighbors = Neighbors(stalls, index) })
  7.             .ToList();
  8.         var result = (int?)null;
  9.         return result;
  10.     }
  11.     private static int Neighbors(bool[] stalls, int index)
  12.     {
  13.         return (index > 0 && stalls[index – 1] ? 1 : 0)
  14.             + (index + 1 < stalls.Length && stalls[index + 1] ? 1 : 0);
  15.     }
  16. }

Implementing Rule 1: “Get as far away from the door as possible”
It is imperative that you pick a position as far from the door as possible for a variety of reasons, not the least of which is reducing the amount of traffic passing behind you while you’re “occupied”. Call it a survival instinct. This is just a matter of adding a Linq query to select an unoccupied stall, and a simple ordering clause to pick the one furthest from the door.

Rule 1
  1. public static int? Solve(bool[] stalls, bool queued)
  2. {
  3.     var positions = stalls
  4.         .Select((occupied, index) => new { index, occupied, neighbors = Neighbors(stalls, index) })
  5.         .ToList();
  6.     var result = positions
  7.         .Where(x => !x.occupied)
  8.         .OrderByDescending(x => x.index)
  9.         .Select(x => x.index)
  10.         .FirstOrDefault();
  11.     return result;
  12. }

Rule 2: “Never stand next to another dude”
I’ll just highlight the differences in the query from this point forward. All we need for this rule is another condition in the where clause to limit the choices to positions with no neighbors.

Rule 2
  1. var result = positions
  2.     .Where(x => !x.occupied && x.neighbors == 0)
  3.     .OrderByDescending(x => x.index)
  4.     .Select(x => x.index)
  5.     .FirstOrDefault();

Rule 2, Exception 1: There is a line at the door
Now we start getting into the exceptions, and things start getting more interesting. If there’s a line at the door, you must take a stall. You still want to be as far from the door as possible, though. That part does not change.

Rule 2, Exception 1
  1. var result = positions
  2.     .Where(x => !x.occupied && x.neighbors == 0
  3.         || (queued && x.neighbors == 1))
  4.     .OrderByDescending(x => x.index)
  5.     .Select(x => x.index)
  6.     .FirstOrDefault();

Rule 2, Exception 2: Someone else has already broken Rule 2
This gets into complex social behavior that’s harder to explain, but suffice to say that continuing to uphold a rule when someone else has already violated it would be “calling them out”, a clear-cut violation of the bro-code. So, if someone already has a neighbor, then it’s okay to have one too. We’ll just expand the exception to encompass both of the conditions. We’ll also need another OrderBy clause because, while it’s now acceptable to have a neighbor, it is still to be avoided if at all possible.

Rule 2, Exception 2
  1. var result = positions
  2.     .Where(x => !x.occupied && (x.neighbors == 0
  3.         || (x.neighbors == 1 && (queued || positions.Any(y => y.occupied && y.neighbors == 1)))
  4.         ))
  5.     .OrderBy(x => x.neighbors)
  6.     .ThenByDescending(x => x.index)
  7.     .Select(x => (int?)x.index)
  8.     .FirstOrDefault();

Rule 3: Never stand next to two other dudes
This is kind of like Rule 2, except more important. If having one neighbor is bad, then it goes without saying that having two neighbors is even worse. This rule has similar exceptions to Rule 2 (There’s a line, or someone already broke the rule), so I’ll include them here as well.

Rule 3
  1. var result = positions
  2.     .Where(x => !x.occupied && (x.neighbors == 0
  3.         || (x.neighbors == 1 && (queued || positions.Any(y => y.occupied && y.neighbors == 1)))
  4.         || (x.neighbors == 2 && (queued || positions.Any(y => y.occupied && y.neighbors == 2)))))
  5.     .OrderBy(x => x.neighbors)
  6.     .ThenByDescending(x => x.index)
  7.     .Select(x => (int?)x.index)
  8.     .FirstOrDefault();

By now, a pattern should be apparent. I can’t have a neighbor if no-one else does. I can have exactly one neighbor if someone else does. I can have two neighbors if somebody else does, as well. What we’re really saying is that you should never have more neighbors than anyone else. Of course, if you can get away with fewer neighbors than everyone else then you win.

To refactor the query, we’ll use Linq’s Max extension method to select the highest neighbor count from all the occupied positions. Unfortunately, in the case that none of the stalls are occupied, this result set would be empty, and Max will throw an exception. We can work around this by changing the type of the Neighbors method to be nullable, and coalescing the result of Max with 0. It’s kind of a cheat, but it works. For more information on this particular problem, see http://www.interact-sw.co.uk/iangblog/2007/09/10/linq-aggregates.

The final version of the class looks like this:

Final Version
  1. public class UrinalRules
  2. {
  3.     public static int? Solve(bool[] stalls, bool queued)
  4.     {
  5.         var positions = stalls
  6.             .Select((occupied, index) => new { index, occupied, neighbors = Neighbors(stalls, index) })
  7.             .ToList();
  8.         var result = positions
  9.             .Where(x => !x.occupied
  10.                 && (queued || x.neighbors <= (positions.Where(y => y.occupied).Max(y => y.neighbors) ?? 0)))
  11.             .OrderBy(x => x.neighbors)
  12.             .ThenByDescending(x => x.index)
  13.             .Select(x => (int?)x.index)
  14.             .FirstOrDefault();
  15.         return result;
  16.     }
  17.     private static int? Neighbors(bool[] stalls, int index)
  18.     {
  19.         return (index > 0 && stalls[index – 1] ? 1 : 0)
  20.             + (index + 1 < stalls.Length && stalls[index + 1] ? 1 : 0);
  21.     }
  22. }

From here, you can explore some of the more esoteric rules on your own. The example I gave as extra credit in the Pecha Kucha talk was the “No Pairing” rule which states that it is preferable to join a pre-existing pair of neighbors than to start a new “couple” with a singleton “loner”. I haven’t given this one much thought, myself, and it was only included in the talk as a kind of punch line, since it’s so odd and obscure that no-one would guess it corectly.

You could also explore the simpler problem of what to do with the short, or “kids” stall. The ruling on this is actually very straightforward. The short stall is ignored by everybody until it is the last stall left to choose from, and even then it’s only used if there is a line.

If you want to work through this particular Kata yourself, here are the tests to exercise the rules presented above. They are presented in MsTest format simply because it’s built into Visual Studio and makes for a good lowest common denominator.

Unit Tests
  1. [TestClass]
  2. public class UrinalRulesTests
  3. {
  4.     [TestMethod]
  5.     public void UrinalRules_Test()
  6.     {
  7.         Assert.IsNull(UrinalRules.Solve(new[] { true, true, true, true, true }, false), “All stalls are occupied. You should return a null (Stalemate).”);
  8.         Assert.AreEqual(4, UrinalRules.Solve(new[] { false, false, false, false, false }, false), “Rule 1: Get as far away from the door as possible.”);
  9.         Assert.AreEqual(2, UrinalRules.Solve(new[] { true, false, false, false, true }, false), “Rule 2: Avoid standing next to another dude.”);
  10.         Assert.IsNull(UrinalRules.Solve(new[] { true, false, true, false, true }, false), “Rule 2: Avoid standing next to another dude.”);
  11.         Assert.AreEqual(4, UrinalRules.Solve(new[] { false, true, false, true, false }, true), “Rule 2, Exception 1: You can have a neighbor if there is a line.”);
  12.         Assert.AreEqual(3, UrinalRules.Solve(new[] { true, true, false, false, true }, false), “Rule 2, Exception 2: You can have a neighbor if someone else already does.”);
  13.         Assert.IsNull(UrinalRules.Solve(new[] { true, false, true, false, true }, false), “Rule 3: Really avoid standing next to TWO other dudes.” );
  14.         Assert.AreEqual(3, UrinalRules.Solve(new[] { true, false, true, false, true }, true), “Rule 3, Exception 1: You can have two neighbors if there is a line.”);
  15.         Assert.AreEqual(3, UrinalRules.Solve(new[] { true, true, true, false, true }, false), “Rule 3, Exception 1: You can have two neighbors if someone else already does.”);
  16.     }
  17. }

So, what started as a funny idea, and turned into an impromptu talk, has quickly evolved into a useful coding Kata. It is representative of real-world business code, which always seems to have nearly as many arcane exceptions as rules. It also gives us an opportunity for a simple refactoring, and to exercise some Linq goodness. The Linq part may not help the Java folks much, but as I said at the beginning of this post. There are many ways to solve this particular puzzle. Even the .Net folks should try doing it without using Linq. You could attack it by writing your own sort, or by assigning point values to different rules.

Remember… Stick to the code.

Posted in Computers and Internet | 2 Comments