Initial setup – 1: Secure, Re-Partition & Swap

October 2012:

Note: These pages are now rather old and possibly out of date with the new Raspbian release, however I’ll keep them online as they may be of help to someone…


Note: These pages have been prepared for Debian Squeeze with some additions for the Debian testing release; Wheezy. Other Linux distributions may benefit from some of the information here, but you may need to work out some of the differences for yourself.

So first things first.

I obtained the latest Debian (The image from http://www.raspberrypi.org/downloads) and wrote it to my SD card. My SD card is 4GB but the image is 2GB… No matter for now. Put this in the Pi and booted it. All OK. As part of it’s initial setup, it reboots itself, but other than that no real surprises.

Update: I’ve just used the Wheezy install from http://www.raspberrypi.org/archives/1435 so will make some changes for this too.

Before doing anything else – are you in the UK? If so, then good. If not, then you might find that the keyboard layout is somewhat strange… If typing Shitf+3 gives you a £ sign instead of what you were expecting, then login (pi;raspberry) and issue the command:

sudo dpkg-reconfigure keyboard-configuration

Go through the menus for the keyboard layout that you need.

Carrying on… Lets make a little more secure…

If not already, login; pi:raspberry. Change your password with the passwd command. Just do it. If you don’t do it and your Raspberry Pi is ever connected to a LAN, then anyone will be able to login and delete all your files, or worse.

Now that you have a password, enable sshd (so that you can connect in remotely – if you never want to connect in remotely, don’t do this)

sudo /boot/boot_enable_ssh.rc

(You may not need to do this in Wheezy)

Now lets reclaim the unused space on the SD card. 2 ways – one would be to simply create a partition for it, then format and mount it, the other way might be to extend the existing partition. I chose the 2nd way and I’ll regret it when they produce another release, however I’m hoping it’s just a kernel release…

As supplied, the Debian image has 3 partitions. (Wheezy has 2) The first is a FAT32 formatted image which the GPU reads to boot itself and the ARM. The next is an EXT4 partition which is the main bulk of the system, and the last is a small swap partition which isn’t used in the initial setup.

We need to delete the swap and ext4 partition, then re-create the ext4 partition the full size of the device, then extend the filing system.

This is not an operation to be undertaken lightly – however the worst possible scenario is a total loss of all data on the SD card, and as that can be re-written from scratch, as long as you don’t have any valuable data on it, you’ll be fine.

sudo fdisk -uc /dev/mmcblk0

(Minor note here, this was done on the Debian Squeeze install, other Linuxes, and Debian Wheezy no-longer need the -uc flags, so if you get an error, just try again without the -uc)

Type ‘p’ it will print the partition table and look like this:

Disk /dev/mmcblk0: 3999 MB, 3999793152 bytes
4 heads, 32 sectors/track, 61032 cylinders, total 7812096 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee283

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048      155647       76800    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          157696     3414015     1628160   83  Linux
/dev/mmcblk0p3         3416064     3807231      195584   82  Linux swap / Solaris

 We need to delete partition 3, then partition 2. At this point, we’re deleting the partitions, not the data! The command is ‘d‘, then it will ask for the partition, 3 then ‘d‘ and 2.

(And again, of using Wheezy, then there is no partition 3, but you still need to delete partition 2 – do remember to take note of the starting sector number – the wheezy install I’ve just done has it set to 122880 and not the 157696 that you see above!)

Now, create a new Primary partition; ‘n‘ and enter 2 as the partition number. It will not prompt for the start and here, you must not use the default, but instead use the number at the start of the /dev/mmcblk0p2 line. In this case it’s 157696 but do check as your may be different!

Let it pick the ending sector number and use the default.

Write it to the SD device ‘w‘ and you’re almost there. Because this partition is still in-use, you can’t do anything at this stage other than reboot the Pi. Exit fdisk and sudo reboot will do it.

You can try to use the partprobe command to force the system to re-read the partition, but rebooting is safer at this point.

Login again and now we have to re-size the filing system.

sudo resize2fs /dev/mmcblk0p2

This may take a minute or the, and then your done. Now the output of df -h / should show the additional space:

Filesystem            Size  Used Avail Use% Mounted on
rootfs                3.6G  1.3G  2.2G  38% /

Now a little bit of swap is good, a lot isn’t, so we should add in a little bit of swap, however if running Wheezy, then note that you don’t need to do this as it is already using the dphys-swapfile’ package.

So to create a small swapfile:

cd /var
sudo dd if=/dev/zero of=swapfile bs=1M count=128
sudo mkswap /var/swapfile
sudo swapon /var/swapfile

The dd command will take a few seconds to a minute depending on SD card speed. To make the swapfile work at boot time, you’ll need to edit the /etc/fstab file. Use the command

sudo nano /etc/fstab

and look at the last line – change:

#/dev/mmcblk0p3  none            swap    sw              0       0

into

/var/swapfile  none            swap    sw              0       0

and the next time you boot it will be configured automatically.

While we’re editing /etc/fstab, we should tidy it up and insert a new line to re-mount the root partition is a slightly more efficient manner. This is what my /etc/fstab looks like:

proc            /proc   proc    defaults             0 0
/dev/mmcblk0p1  /boot   vfat    defaults             0 0
/dev/mmcblk0p2  /       ext4    defaults,noatime     0 0
/var/swapfile   none    swap    sw                   0 0

The addition of the root device (mmcblk0p2) and the extra parameters of noatime will speed things up somewhat.

Now would be a good time to reboot again. Check the swap is enabled with the top command, and check the root mount flags with the mount command.