Basic Raspberry Pi Setup
Earlier this week I took delivery of my Raspberry Pi, and I’ve been setting a few basic things up prior to properly playing around with it - I thought I’d document this to help others who were looking to do the same thing.
I’m using the Debian Squeeze image, using the instructions here (I’m using OSX). When I DD’ed it across to the SD card I got the error “dd: /dev/rdisk1: Invalid argument” but this didn’t stop it working.
The following is how I resized the Raspberry Pi Debian image to the full size of my SD card, enabling the ssh server, and changing the hostname.
Resizing the SD card partition and creation of a swapfile.
The Debian image is sized to fit on a 2GB disk, if your card is larger than this you will have to resize the partitions manually. Note if this works with no problems you won’t loose any data, but just incase it doesn’t go well I did it straight after booting my Pi for the first time, and didn’t have anything on it that I couldn’t afford to loose.
This involves, firing up fdisk, rewriting the partition table, resizing the filesystem to the new partitions, and the creation and mounting of a swapfile.
If you like videos, this one carries out the exact same thing :)
To start, run fdisk. “mmcblk0” is where my /boot was mounted (IE the SD card), yours might be different- check with “
sudo fdisk -uc /dev/mmcblk0
(Whilst running fdisk):
“p” (to print the partition table)
“d” (to delete a partition)
“2” (to delete /dev/mmcblk0p2)
“d” (to delete another partition)
“3” (to delete the existing swap partition)
“n” (for creating a new partition)
“p” (for new primary partition)
“2” (for creation of partition number 2)
“157696” (in my case, to create a partition at the same location as the ones just deleted, enter in the old start number of /dev/mmcblk0p2 printed earlier).
Just hitting enter next will use the remaining space of the card, or you can manually specify a size yourself.
“w” (to write the changes)
Reboot the system, (
sudo reboot) and the we can resize the filesystem into the new partitions with:
sudo resize2fs /dev/mmcblk0p2 (this will take a while if you have a large card)
Reboot afterwards, and “
df -h” will show the new filesystem using all the space of your card!
To create the swapfile;
sudo dd if=/dev/zero of=/var/swapfile bs=1M count=128
This creates a 128MB file filled with zeros called /var/swapfile, and might take a little while depending on the speed of your card.
sudo mkswap /var/swapfile (to setup the file as swap space)
sudo swapon /var/swapfile (to enable the swap space, this also might take a little while.)
All thats left now is to edit
/etc/fstab and tell the system to mount the file as swap every time it starts up;
sudo nano /etc/fstab
Whilst in nano, delete the line starting with a hash (#), and paste or type in:
/dev/mmcblk0p2 / ext4 defaults,noatime,nodiratime 0 0
/var/swapfile none swap sw 0 0
This tells the system to mount the 2nd partition at / in the filesystem, and to mount it with “noatime,nodiratime”, meaning the system will not write when files and directories were last accessed, speed things up a bit. The next line also tells the system to mount the swapfile we made. This file is read at boot time, and tells the system to mount the stated devices automatically each time the system is booted.
“CTRL+W” then “Y” to exit nano and save changes, and then a final reboot is all that’s needed.
I also changed the hostname of my Pi, to do this:
sudo nano /etc/hostname (and enter the desired name, “CTRL+W” then “Y” to quit)
sudo nano /etc/hosts and replace raspberry with the hostname you chose above
sudo /etc/init.d/hostname.sh start (to enable the changes).
ssh-keygen (I hit enter for all three options to accept defaults and no passphrase)
sudo service ssh start (to start sshd)
sudo update-rc.d ssh defaults (to run the ssh server on startup by default)