22 October 2016

Moving a Rpi root partition

The Raspberry Pi uses a MicroSD card as its primary storage, Unfortunately it is also prone to corruption. The following is the process I've used to move the root partition from the SD card onto a USB storage device.

I first tried it on a Maxtor OneTouch II external hard disk that I had siting around. It gave me an error when I tried to write the partition table to the disk, so it may not work on all devices. I also tried it on a Sandisk Cruiser Switch (thumb drive) and lastly on a Seagate Expansion external hard disk. The last two worked fine. As they say your mileage might vary.

The Pi isn't very good at powering USB devices like hard disks so use a powered hard disk (ie one that comes with it own power pack) or you could use the PiDrive adaptor cable that supplies power to both the Pi and the external hard disk. Thumb drives should be fine as they require minimal power.

I can't take credit for the instructions, I got them from a Raspberry Pi forum post from 2013. It can be found at https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=44177
The instruction below are done in a terminal window.


Start-up Pi
Start off with a clean Jessie or Jessie Lite image on the micro-SD card. Boot up and change a few settings via Raspi-Config (sudo raspi-config). The HDD or thumb drive should not be plugged into the Pi at this point.

- Change_password
- Change locale as appropriate
- Change the timezone as appropriate
- Change memory split to 16Mb
- Exit and reboot


Find the device name
Plug in HDD or thumb drive and type “tail /var/log/messages” (without the quotes). The log should looks something like this:

Oct 19 20:43:53 pie39 kernel: [512215.189155] usb 1-1.2: SerialNumber: L2069VYG
Oct 19 20:43:53 pie39 kernel: [512215.190065] usb-storage 1-1.2:1.0: USB Mass Storage device detected
Oct 19 20:43:53 pie39 kernel: [512215.208124] scsi host0: usb-storage 1-1.2:1.0
Oct 19 20:43:54 pie39 kernel: [512216.272762] scsi 0:0:0:0: Direct-Access     Maxtor   OneTouch II      023g PQ: 0 ANSI: 4
Oct 19 20:43:54 pie39 kernel: [512216.282521] sd 0:0:0:0: Attached scsi generic sg0 type 0
Oct 19 20:43:54 pie39 kernel: [512216.328166] sd 0:0:0:0: [sda] 195813072 512-byte logical blocks: (100 GB/93.4 GiB)
Oct 19 20:43:54 pie39 kernel: [512216.383210] sd 0:0:0:0: [sda] Write Protect is off
Oct 19 20:43:54 pie39 kernel: [512216.438164] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
Oct 19 20:43:55 pie39 kernel: [512216.629457]  sda: sda1
Oct 19 20:43:55 pie39 kernel: [512216.849474] sd 0:0:0:0: [sda] Attached SCSI disk

We can see that device sda has been assigned. If you got another device name (eg sdb) then change the commands below to refer to /dev/sdb instead.


Partition disk
Start fdisk so we can see the partitions and create one as needed. Type “sudo fdisk /dev/sda”. The command p will display the partitions:

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p

Disk /dev/sda: 93.4 GiB, 100256292864 bytes, 195813072 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
Disklabel type: dos
Disk identifier: 0x0b2461bf


Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1        2048 195809279 195807232 93.4G  7 HPFS/NTFS/exFAT


Lets delete it (D command) and create a new primary partition (N command). We'll use the defaults for everything. Once done we’ll write it to disk (W command). If you need to exit without writing it to disk then use the Q command.

My Seagate Expansion had 4 partitions. They are shown as extra lines as above with /dev/sda2, /dev/sda3 and /dev/sda4 under the Device column. I had to repeat the delete command for each partition.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-195813071, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-195813071, default 195813071):

Created a new partition 1 of type 'Linux' and of size 93.4 GiB

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.


If you get an error message after that ioctl line then you know your device is not compatible like my OneTouch II.


Format disk
We now need to format the partition in the ext4 format with the following command “sudo mke2fs -t ext4 -L OneTouchII /dev/sda1”. The word after the -L parameter is the volume label. You might want to name it something other than OneTouchII.

mke2fs 1.42.12 (29-Aug-2014)
/dev/sda1 contains a ext4 file system labelled 'OneTouchII'
        last mounted on Sat Oct  1 22:35:25 2016
Proceed anyway? (y,n) y
Creating filesystem with 24476378 4k blocks and 6119424 inodes
Filesystem UUID: d335ad29-9708-478a-80aa-9dabf0e3cdeb
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done


Lets mount the disk by typing “sudo mount /dev/sda1 /mnt”

Check we can see it by typing “df –h” and we should get something like this:

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  1.2G   13G   9% /
devtmpfs        483M     0  483M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M  6.5M  481M   2% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/mmcblk0p1   63M   21M   43M  33% /boot
/dev/sda1        92G   60M   88G   1% /mnt



Copy root across
Lets get rsync installed so we can copy all the files. Type “sudo apt-get install rsync". This should install the rsync package.

Copy root partition across by typing “sudo rsync -axv / /mnt”. This can take around 15 minutes.


Point root to the HDD
Backup the boot.txt file by typing “sudo cp /boot/cmdline.txt /boot/cmdline.orig”

Display it by typing "cat /boot/cmdline.txt". It should look like this:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Edit the file (sudo nano /boot/cmdline.txt) and change the root= to our new partition and add a rootdelay of 5 seconds. After we're done it should look like this:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait rootdelay=5

Edit /mnt/etc/fstab (sudo nano /mnt/etc/fstab) and add the following:

/dev/sda1    /   ext4    defaults,noatime  0       1

And then comment out the memory card entry (put a # symbol at the front)

#/dev/mmcblk0p2  /               ext4    defaults,noatime       0       1

It should now look like:

proc            /proc           proc    defaults          0       0
/dev/sda1       /               ext4    defaults,noatime  0       1
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
#/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that


Reboot (sudo reboot)

No comments: