[Tutorial] - Install Ubuntu Mate for Raspberry Pi on an external USB drive and active SWAP partition

WARNING : DOING THIS TUTORIAL YOU CAN LOOSE ALL YOUR DATA !

What is this tutorial about ?

This tutorial is for raspberry pi 2 or 3.
This tutorial is to do a clean install of ubuntu mate (or other ubuntu pi flavors like lubuntu or xubuntu) on an external USB harddrive or USB key.
Because the system will be installed on a USB device we will activate a swap partiton and that is really cool performance boost because rapsberry pi 2 or 3 have only 1GB of RAM :slight_smile:

What do you need ?

You need both an empty SDCARD with size more or equal 64MB (you can use a very old SDcard of 1GB for example :slight_smile: ) and a empty USB storage device (harddrive or key) with size more or equal 16 GB.
You need a computer running a version of ubuntu (can be a raspberry pi :slight_smile: ).

YOU NEED TO BE FAMILIAR USING GPARTED !
IF NOT YOU CAN LOOSE ALL YOUR COMPUTER DATA !
I WILL NOT BE RESPONSIBLE OF IT YOU ARE WARN !

For raspberry pi 3 you can maybe don’t have to use an sdcard but it is not compatible with all USB storage device.
But it is not what this tutorial is about !
I will write another tutorial about it but I didn’t have a compatible USB storage device for now.
For some information about it see : https://www.raspberrypi.org/blog/pi-3-booting-part-i-usb-mass-storage-boot/

Let’s go !

Step 1 - Install some software we will need

On your linux computer, open a terminal then type :

sudo apt update
sudo apt install xz-utils gparted kpartx

Step 2 - Download ubuntu mate for rapsberry pi

Download ubuntu mate for rapsberry pi and unzip it, in a terminal type :

unxz ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img.xz

You will have a file name ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img now.

Step 3 - Inspect partition size of ubuntu mate image file

We will inspect partition size with fdisk, in the terminal type :

fdisk -l ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img

You will have something like that (yes I am french :)) :

Disque ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img : 7,5 GiB, 8053063680 octets, 15728640 secteurs
Unités : sectors of 1 * 512 = 512 octets
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x580a66ff

Périphérique Amorçage Start Fin Secteurs Size Id Type
ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img1 * 2048 133119 131072 64M c W95 FAT32 (LBA)
ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img2 133120 15728639 15595520 7,4G 83 Linux

Ok, now we need to do some maths, there is two partitions on the image :

  • ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img1
  • ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img2

We need the size of both partition in Mebibyte !

For ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img1 we have 131072 sectors and a sector have a size of 512 bytes.
So 131072 * 512 = 67108864 bytes
67108864 / (1024*1024) = 64 MB

The SIZE_BOOT is 64 MB, we will need it later.

For ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img2 we have 15595520 sectors and a sector have a size of 512 bytes.
So 15595520 * 512 = 7984906240 bytes
7984906240 / (1024*1024) = 7615 MB

The SIZE_ROOT is 7615 MB, we will need it later.

The result of sizes could be different with other version than ubuntu mate 16.04 !
You must calculate it if you use another ubuntu flavor like lubuntu or xubuntu or a another version of ubuntu mate than 16.04 !

Step 4 - Configuring the SDCARD

Plug your SDCARD on your computer.
Open a terminal and lauch gparted :

sudo gparted

Once gparted is launch select your SDCARD device /dev/sdX (X could be a,b,c,d …) on the top right corner.

IF YOU SELECT THE BAD DEVICE THERE YOU WILL LOOSE ALL YOUR DATA IN NEXT STEP SO BE SURE IT IS THE GOOD DEVICE !
YOU SDCARD WILL BE ERASE TOO WITH THE NEXT STEPS SO BACKUP YOUR DATA IF YOU HAVE ANY !

Erase all the partitions of the SDCARD and apply.
Create a partition table from the menu.
Create a new primary partition with type fat16 and size SIZE_BOOT MB (SIZE_BOOT=64 for ubuntu mate 16.04).

– Optional step –
If your SDCARD is big and you want to use the space remaning you can :
Create an extended partition and then create a fat32 partition with the remaning space.
You will can use that partition for data storage if you guess to :wink:
– END Optional step –

Close gparted.

Now we need to create some device for the image of ubuntu mate, open a terminal then type :

sudo kpartx -av ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img

You will have an output like that :

add map loop0p1 (252:0): 0 131072 linear 7:0 2048
add map loop0p2 (252:1): 0 7546880 linear 7:0 133120

In the terminal type (replacing X of sdX1 with the good letter of your SDCARD) :
IF YOU MISTAKE HERE YOU CAN LOOSE ALL YOUR DATA !

sudo dd if=/dev/mapper/loop0p1 of=/dev/sdX1 bs=512 conv=notrunc,noerror

We need to launch gparted again :

sudo gparted

Select your SDCARD device and right click on the first partition and select manage flags.
Then select boot and lba then apply.

Close gparted.

Now, you must have a PI_BOOT device mounted in your computer, if not unplug and plug again your SDCARD.
Open a terminal go to the PI_BOOT directory of the mounted SDCARD then edit cmdline.txt file :

sudo nano cmdline.txt

You must have a file like this :

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait quiet splash

Change root=/dev/mmcblk0p2 to root=/dev/sda1

You must have now a file like this :

dwc_otg.lpm_enable=0 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait quiet splash

Save & Quit.

– Optinal step for raspberry pi 2 –
If you have a raspberry pi 2 and an USB drive without power supply you must edit file config.txt too.

sudo nano config.txt

Go to the end of the file then add :

max_usb_current=1

Save & Quit.

– End Optinal step for raspberry pi 2 –

Umount and unplug the SDCARD.

SDCARD IS CONFIGURED !

Step 5 - Configuring the USB device

Plug your USB device on your computer.
Open a terminal and lauch gparted :

sudo gparted

Once gparted is launch select your USB device /dev/sdX (X could be a,b,c,d …) on the top right corner.
IF YOU SELECT THE BAD DEVICE THERE YOU WILL LOOSE ALL YOUR DATA IN NEXT STEP SO BE SURE IT IS THE GOOD DEVICE !
YOU USB DEVICE WILL BE ERASE TOO WITH THE NEXT STEPS SO BACKUP YOUR DATA IF YOU HAVE ANY !
Erase all the partitions of the USB device and apply.
Create a partition table from the menu.
Create a new primary partition with type ext4 and size SIZE_ROOT MB (SIZE_ROOT=7615 for ubuntu mate 16.04).

Close gparted.

In the terminal type (replacing X of sdX1 with the good letter of your USB DEVICE) :
IF YOU MISTAKE HERE YOU CAN LOOSE ALL YOUR DATA !

sudo dd if=/dev/mapper/loop0p2 of=/dev/sdX1 bs=512 conv=notrunc,noerror

We need to launch gparted again :

sudo gparted

Select your USB device and right click on the first partition and move/resize.
Resize the partion to almost all the disk, let free 2 to 3 GB.
Apply.
Create a extended partition.
Create a new partiton with the remaning space with linux-swap type.
Apply then close gparted.

Now, you must have PI_ROOT mounted if not unplug and plug your USB device.
Open a terminal then type (replacing X for sdX5 with your USB device letter) :

sudo blkid /dev/sdX5

You will have an output like :

/dev/sda5: UUID=“5e972f47-c5f2-4d96-9b21-57ff4eb2043a” TYPE=“swap” PARTUUID=“defa51a9-05”

Save somewhere the UUID number for me : 5e972f47-c5f2-4d96-9b21-57ff4eb2043a

now go to the PI_ROOT directory in the terminal with cd.
then type :

sudo nano etc/fstab

change the line :

/dev/mmcblk0p2 / ext4 defaults,noatime 0 1

By the line :

/dev/sda1 / ext4 defaults,noatime 0 1

At the end of file add a line like this with your own UUID number you save before.

UUID=5e972f47-c5f2-4d96-9b21-57ff4eb2043a none swap sw 0 0

Save & Quit.

Umount and unplug the USB Device.

USB DEVIVE IS CONFIGURED !

Step 6 - Run the raspberry pi

Plug the SDCARD and the USB device in your rapsberry pi then plug power cable !

Step - If it don’t work :slight_smile:

If it don’t work some USB drives need more wait delay to start, if it don’t work try the following.
We will add 5 seconds more delay to USB drive could response.
Take the SDCARD and put it in your computer and edit cmdline.txt :

sudo nano cmdline.txt

At the end of the line add :

rootdelay=5

So you must have a file like this :

dwc_otg.lpm_enable=0 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait quiet splash rootdelay=5

Save & Quit.

Step - Verify swap is running

After configuring the system on your raspberry pi, launch a terminal and type :

swapon

If swap partition is active you must have an output like this :

NAME TYPE SIZE USED PRIO
/dev/sda5 partition 3,5G 0B -1

Enjoy !

Aworan

1 Like

Great tutorial, i have a little M.2 SSD connecting through USB that i’m going to have a go at moving MATE onto tonight, the SSD is overkill for the PI’s USB 2.0, but i#m hoping the read/write of the SSD will work for VM

Hi,

I’m a french newby using rapsberry 3 B+ and I found the raspbian os very very slow.
I tried to get ubuntu mate but I didn’t make it work.
Do you have a good tutorial for it please ?
FI, I’m using a vga screen (I have a hmdi-vga cable)

Thanks in advance