Preparing an SD card for Raspberry Pi using the GNU/Linux command line

For the purpose of reminding myself, and possible others, here is a quick post describing the steps needed to prepare an SD card with with the NOOBS system installer for Raspberry Pi using the GNU/Linux command line.

I’m using Ubuntu, but it should work on any GNU/Linux system.

Download NOOBS

NOOBS is the user friendly installer for the Raspbian GNU/Linux distribution for the Raspberry Pi.

Download a compressed .zip file containing the latest version of NOOBS. At the time of writing, it’s version 1.5.0. The URL below, should should be an alias to the latest version at any given time. I’m using wget to download the file.

wget https://downloads.raspberrypi.org/NOOBS_latest

Note, that you can also download the .zip file using BitTorrent.

The file I downloaded, is called: NOOBS_v1_5_0.zip

Locate the SD card

Insert the SD card in to your machine.

Having inserted the card. Run the command dmesg to see how the machine responded to the SD card beeing inserted. Here are two lines from the response on my machine.

_…. _[206339.307192] mmc0: new high speed SDHC card at address 0007 [206339.307494] mmcblk0: mmc0:0007 SD8GB 7.42 GiB ….

The device representing my card is mmcblk0. Confirm by listing it with the command ls /dev/mmcblk0.

If the card allready has partitions on it, they were probably mounted when you inserted it. Let’s see if any thing was mounted. Look at the list of mounted devices, and print any lines matching our device.

mount | grep mmcblk0

/dev/mmcblk0p5 on /media/tbp/SETTINGS type ext4 (rw,nosuid,nodev,relatime,data=ordered,uhelper=udisks2) /dev/mmcblk0p7 on /media/tbp/root type ext4 (rw,nosuid,nodev,relatime,data=ordered,uhelper=udisks2) /dev/mmcblk0p6 on /media/tbp/boot type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)

Three partitions on my card are mounted. The output is a bit difficult to read, so let’s limit the output to just the mount points by only printing the 3rd entry of every line delimited by the space character denoted by ' ‘.

mount | grep mmcblk0 | cut -d ' ' -f 3

/media/tbp/SETTINGS /media/tbp/root /media/tbp/boot

Let’s unmount the partitions.

umount /media/tbp/SETTINGS /media/tbp/root /media/tbp/boot

Create a partition on the SD card

We now need to create a single partition on the SD card. We’ll do it with cfdisk. You will need to run the command with super user privileges by prepending the command with sudo (super user do).

sudo cfdisk /dev/mmcblk0

If you have any partitions on your card (Like me), you need to delete them. Select a partition by highligting it, and delete it by choosing [ Delete ].

Having deleted all partitions, we need to create a new one. With no partitions left on the card, the Free space should be selected. Select [ New ] to create a new partition. Press enter at each question to accept the defaults of a Primary partition of maximum size.

Select [ Type ] and set the  type of the partition to W95 FAT32 (LBA).

Save the new partition table to the SD card  by selecting [ Write ], and then Yes.

Exit cfdisk by selecting [ Quit ].

Create a file system

We need to create a FAT32 filsystem on the partition we created on the card. The partition is presented as a device in /dev, like the SD card itself. The name of the device is mmcblk0p1. Notice, this is the name of the SD card device with at suffix of p1 (Partition 1).

Use mkfs.vfat to create the filesystem. Prepend with sudo to get the needed priviliges.

sudo mkfs.vfat /dev/mmcblk0p1

Copy NOOBS to the SD card

Take the SD card out of your machine. Insert it again. It should be mounted automatically. We need to know where the partition is mounted. Let’s get the the lines containing the partition’s device from the list of mounted filesystems.

mount | grep mmcblk0p1 | cut -d ' ' -f 3

_/media/tbp/7BA7-C44B _

The partion is mounted at /media/tbp/7BA7-C44B.

I can now unzip the files making up NOOBS and put them on the SD card.

unzip NOOBS_v1_5_0.zip -d /media/tbp/7BA7-C44B/

Archive: NOOBS_v1_5_0.zip _ inflating: /media/tbp/7BA7-C44B/bcm2708-rpi-b.dtb_ _ inflating: /media/tbp/7BA7-C44B/bcm2708-rpi-b-plus.dtb_ _ inflating: /media/tbp/7BA7-C44B/bcm2709-rpi-2-b.dtb_ _ inflating: /media/tbp/7BA7-C44B/bootcode.bin_ _ inflating: /media/tbp/7BA7-C44B/BUILD-DATA_ _ creating: /media/tbp/7BA7-C44B/defaults/_ _ ………_ _ ………_ _ extracting: /media/tbp/7BA7-C44B/RECOVERY_FILES_DO_NOT_EDIT_ _ inflating: /media/tbp/7BA7-C44B/recovery.img_ _ inflating: /media/tbp/7BA7-C44B/recovery.rfs_ _ inflating: /media/tbp/7BA7-C44B/riscos-boot.bin_

As the last step, let’s unmount the partition.

umount /media/tbp/7BA7-C44B

Eject the SC card, and insert it in to your Raspberry Pi. Booting the machine, should present you with the graphical NOOBS installation menu.