4

I would like to install Archlinux on my RaspberryPi2, but right now it is only accessible through SSH.

It is running the standard Raspbian distro, and I was wondering if it was possible to hot-replace the system, maybe through some new partition and a reboot.

Is this possible?

Gui13
  • 103
  • 7
  • Do you have room for an extra partition already? If so, this is fairly straightforward. If not, it is somewhere between pretty complicated and impossible. – goldilocks Mar 09 '15 at 14:45
  • I do, actually. I can resize the SD card, which is 8GB. When the OS switch is done, I plan to remove the first and expand the newly created OS. But "straightforward" is not very documented. Do you have pointers? – Gui13 Mar 09 '15 at 15:35
  • I've put them into an answer. – goldilocks Mar 09 '15 at 16:37

1 Answers1

5

If you have space for an empty partition, this is fairly simple -- but first, to clarify, run

sudo fdisk /dev/mmcblk0

Then hit p (to print the partition table). You'll get a table like this:

Disk /dev/mmcblk0: 7.4 GiB, 7892631552 bytes, 15415296 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: 0x00092fac

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        8192   122879   114688   56M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      122880 15415295 15292416  7.3G 83 Linux

This is an 8GB card which does not have any such extra space. You can determine this by comparing the 15415296 sectors with the End of the last partition, which is 15415295. There are no more free sectors.

However, if you installed Raspbian from an image and never resized the root partition, you would have ~6GB of extra space at the end. In this case, you could add a new partition with n; you want a new primary partition. There is a bit of a gotcha here with pi SD cards, because there's actually 3-4 MB of empty space at the front of the card, and fdisk will assume you want to use that when it asks you for the "First sector" of the new partition ("default 2048" = before the first partition). Don't use that default. Instead, use the "End" sector of the last partition plus one. You can then make the partition as big as you want.

By default the new partition should have type 0x83, "Linux". That's what you want.

Do NOT try and use fdisk to shrink the current root partition (/dev/mmcblk0p2) if you do not have enough room.

Remember to use w to actually commit the changes, then exit.

Now:

sudo mkfs.ext4 /dev/mmcblk0p3

I won't bother indicating sudo from now on but assume all commands must be run root.

Once that's done, you can copy the contents of a new filesystem in. Raspbian style images (I'll get to Arch below) contain two filesystem partitions, you'd want the second one (if you were installing Raspbian), and there's a clue about how to find that here; related is step #4 here.

There is a possible complication because you need to download the image file onto the pi, so you will need sufficient storage to do so. If you installed Raspbian, have not resized, and all you have is the SD card, you won't have room in the root partition for the image; hopefully the new one is big enough to hold it with ~1GB to spare, because you have to copy out into that.

The Arch case is a lucky one because their image is only ~230 MB. Arch's installation page for the pi also reveals they use one partition in their image, and have you create two and copy things into them. You don't have to do that all that in this case, since you already have a first partition with most of the bits in. So, instead of creating a temporary mount directory and copying into there, just mount the new partition and use it directly:

mkdir /mnt/arch
mount /dev/mmcblk0p3 /mnt/arch

Easiest way to go is to download the image straight into /mnt/arch. Move it there if you did not. Now:

cd /mnt/arch
tar -xzf ArchLinuxARM-rpi-latest.tar.gz
cd boot

This should put you in /mnt/arch/boot. Make sure with pwd.

rename kernel.img arch.kernel
mv arch.kernel /boot/
rm *

That last command deletes everything left over, which you already have in /boot (note the absolute path, /boot is not /mnt/arch/boot).

Before you do the next couple steps, make sure you have Arch's sshd set up so you can get back in after reboot. It should run by default and it is up to you to determine how to get in. You could simply cp /etc/ssh/sshd_config /mnt/arch/etc/ssh/sshd_config, and any ~/.ssh directories as appropriate.1 That will probably ensure everything works the same way it did with Raspbian. Moving on...

Edit /boot/config.txt. Find the kernel= line; if it doesn't exist, create it:

kernel=arch.kernel

Edit /boot/cmdline.txt.

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
                                  ^^^^^^^^^^^^^^^^^^^

Yours may be more or less elaborate than this, but it will have the root= field. Change that to /dev/mmcblk0p3, i.e., the Arch partition.

reboot & exit

Wait a minute, then cross your fingers and try to ssh to the pi again.


1. This doesn't cover passwords; according to the Arch wiki the default root password is 'root'. Note there is no pre-existing user pi account on Arch.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • Following this up, is it possible to make the two system share login credentials by making Arch mount Debian somewhere using fstab, and symlink `/etc/passwd` and `/etc/shadow` files over? – Maxthon Chan Mar 10 '15 at 13:19
  • You could try it. There's the risk that you end up with inconsistencies because new system users are installed with new software. The problem will occur where explicit UID numbers are associated with them. E.g. If you install `apache` on both systems and the other one has configuration that expects a specific UID for user `httpd` -- but that would be the result of a bad practice (hardcoding UID's). The other gotcha is being sure that partition is mounted early enough for init to work properly, since some init processes use system user accounts. – goldilocks Mar 10 '15 at 13:37