odroid-xu4:software:switching_boot_partition_sd_emmc

The Way For Switching Boot Partition Between SD And eMMC

  • Operation confirmed with testing in our ODROID XU4, 8GB MicroSD card, 32GB eMMC.
    • Used ubuntu-16.04.3-4.14-minimal-odroid-xu4-20171213.img.xz image without kernel update (4.14.5-92).
  • This guide is from a post[1] of our forum. Thanks to @emk2203.

When you want to switch boot partition between inserted MicroSD card and eMMC, just trying with boot mode switch without any setups doesn't work properly since their boot partition's UUID[2] are same each other.
Refer to the results below.

target
$ sudo blkid
# results
/dev/mmcblk0: PTUUID="3cedfd53" PTTYPE="dos"
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="52AA-6867" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="e139ce78-9841-40fe-8823-96a304a09859" TYPE="ext4" PARTUUID="3cedfd53-02"
/dev/mmcblk1: PTUUID="3cedfd53" PTTYPE="dos"
/dev/mmcblk1p1: SEC_TYPE="msdos" LABEL="boot" UUID="52AA-6867" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk1p2: LABEL="rootfs" UUID="e139ce78-9841-40fe-8823-96a304a09859" TYPE="ext4" PARTUUID="3cedfd53-02"

So you have to set another UUID for them to do that.

This guide provides you how to set another UUID for them,
and assumes that you're working with the XU4 board booted from eMMC and the MicroSD card inserted in the system.
Make sure that you've inserted MicroSD card after it is fully booted.

Install the packages below.

target
$ sudo apt install uuid-runtime mtools dosfstools

Create a file ~/.mtoolsrc and put the contents mtools_skip_check=1.

target
$ echo "mtools_skip_check=1" >> .mtoolsrc

Stop uuidd service.

target
$ sudo service uuidd stop

Generate two random 4-byte hex values for PARTUUID and vfat UUID, so run twice.
Store the output as a environment variable.

target
$ RAND_HEX1=$(od -vN 4 -An -tx1 /dev/urandom | tr -d " \n") && echo $RAND_HEX1
bd1446f4
$ RAND_HEX2=$(od -vN 4 -An -tx1 /dev/urandom | tr -d " \n") && echo $RAND_HEX2
b25924d0


Before proceed to the next, you should know that Our boot media has two partitions:

  • mmcblk#p1: (vfat) a boot partition including boot.ini, zImage, dtb…
  • mmcblk#p2: (ext4) a root file system.

Now, let's change their UUID.

Set UUID for root file system.

target
$ sudo tune2fs -U $(uuidgen) /dev/mmcblk1p2

Set UUID and add a new label to the boot partition.

target
$ sudo mlabel -N $RAND_HEX1 -i /dev/mmcblk1p1 ::
$ sudo fatlabel /dev/mmcblk1p1 boot

Ignore the warning.
For the sake of completeness, set disk identifier for boot partition.

target
$ sudo fdisk /dev/mmcblk1p1
# in the fdisk command..
x
i # FORMAT: 0xdeadbeef. $RAND_HEX2 should be here.
r
w

Now the result of blkid is..

target
$ sudo blkid
# results
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="52AA-6867" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="e139ce78-9841-40fe-8823-96a304a09859" TYPE="ext4" PARTUUID="3cedfd53-02"
/dev/mmcblk1p1: SEC_TYPE="msdos" LABEL="boot" UUID="BD14-46F4" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk1p2: LABEL="rootfs" UUID="8e21052d-2fa6-4016-9d13-ef7bf1381ea5" TYPE="ext4" PARTUUID="3cedfd53-02"
/dev/mmcblk0: PTUUID="3cedfd53" PTTYPE="dos"
/dev/mmcblk1: PTUUID="3cedfd53" PTTYPE="dos"

Now we should mirror the changes to the files below:

  • /etc/fstab
  • /media/boot/boot.ini
  • /media/boot/boot.ini.default

We will edit the MicroSD's boot.ini files at the next step to apply newly generated UUID for the /dev/mmcblk1p2.

Replace the LABEL=boot entry part on both devices for the boot partition with the UUID for the boot partition, e. g.: for eMMC for now, UUID=52AA-6867.
Do this for the eMMC now by editing /etc/fstab. Both partitions on eMMC or SD card should be booted by an UUID which lives up to the unique part.

target
$ sudo vi /etc/fstab

Edit like this.

# before
UUID=e139ce78-9841-40fe-8823-96a304a09859 / ext4 errors=remount-ro,noatime 0 1
LABEL=boot /media/boot vfat defaults 0 1
 
# after
UUID=e139ce78-9841-40fe-8823-96a304a09859 / ext4 errors=remount-ro,noatime 0 1
UUID=52AA-6867 /media/boot vfat defaults 0 1

Mount inserted MicroSD card on the /mnt directory.

target
$ sudo mount /dev/mmcblk1p2 /mnt
$ sudo mount /dev/mmcblk1p1 /mnt/media/boot


Replace BD14-46F4 to yours which in $RAND_HEX1, with following proper format (upper case alphabet, hyphen in the center).
And also you should edit UUID at the first line to the newly generated UUID. Check this by using the blkid command before editing.

target
$ sudo vi /mnt/etc/fstab

Edit like this.

# before
UUID=e139ce78-9841-40fe-8823-96a304a09859 / ext4 errors=remount-ro,noatime 0 1
LABEL=boot /media/boot vfat defaults 0 1
 
# after
UUID=8e21052d-2fa6-4016-9d13-ef7bf1381ea5 / ext4 errors=remount-ro,noatime 0 1
UUID=BD14-46F4 /media/boot vfat defaults 0 1


Now let boot.ini knows the new UUID.
Edit both two files.

target
$ sudo vi /mnt/media/boot/boot.ini
$ sudo vi /mnt/media/boot/boot.ini.default

like this.

# before
...
setenv bootrootfs "console=tty1 console=ttySAC2,115200n8 root=UUID=e139ce78-9841-40fe-8823-96a304a09859 rootwait ro fsck.repair=yes net.ifnames=0"
...
 
# after
...
setenv bootrootfs "console=tty1 console=ttySAC2,115200n8 root=UUID=8e21052d-2fa6-4016-9d13-ef7bf1381ea5 rootwait ro fsck.repair=yes net.ifnames=0"
...

Regenerate initrd and uInitrd in a chroot environment.
The $(uname -r) part assumes kernel versions on SD card and eMMC are the same. If not, replace this with the version number for the kernel on the SD card.

target
# Omitted "$" sign for the convenience
# Just copy and paste into the connected SSH shell
sudo mount -o bind /proc /mnt/proc
sudo mount -o bind /sys /mnt/sys
sudo mount -o bind /dev /mnt/dev
sudo cp -R /lib/modules/$(uname -r) /mnt/lib/modules/
 
sudo chroot /mnt
cd /boot
 
sudo update-initramfs -c -t -k $(uname -r)
sudo mkimage -A arm -O linux -T ramdisk -C none -a 0 -e 0 -n uInitrd -d /boot/initrd.img-$(uname -r) /boot/uInitrd-$(uname -r)
sudo cp /boot/uInitrd-$(uname -r) /media/boot/uInitrd

If you want a reminder which is which so you don't edit the wrong files later on, generate files thisisthesdcard in chrooted / and /media/boot with touch.

target
$ sudo touch /thisisthesdcard
$ sudo touch /media/boot/thisisthesdcard

Exit the chroot environment.

target
$ exit

Generate files thisistheemmc in / and /media/boot with touch.

target
$ sudo touch /thisistheemmc
$ sudo touch /media/boot/thisistheemmc

For the eMMC.

target
$ grep -rnw '/media/boot/' -e 'UUID'; grep -rnw '/etc/' -e 'UUID' 2>/dev/null; sudo blkid | grep mmcblk0
# results
/media/boot/boot.ini:166:setenv bootrootfs "console=tty1 console=ttySAC2,115200n8 root=UUID=e139ce78-9841-40fe-8823-96a304a09859 rootwait ro fsck.repair=yes net.ifnames=0"
/media/boot/boot.ini.default:34:#bootrootfs=console=tty1 console=ttySAC2,115200n8 root=UUID=e139ce78-9841-40fe-8823-96a304a09859 rootwait ro fsck.repair=yes net.ifnames=0
/etc/fstab:1:UUID=e139ce78-9841-40fe-8823-96a304a09859 / ext4 errors=remount-ro,noatime 0 1
/etc/fstab:2:UUID=52AA-6867  /media/boot vfat defaults 0 1
/dev/mmcblk0: PTUUID="3cedfd53" PTTYPE="dos"
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="52AA-6867" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="e139ce78-9841-40fe-8823-96a304a09859" TYPE="ext4" PARTUUID="3cedfd53-02"

For the MicroSD card.

target
$ grep -rnw '/mnt/media/boot/' -e 'UUID'; grep -rnw '/mnt/etc/' -e 'UUID' 2>/dev/null; sudo blkid | grep mmcblk1
# results 
/mnt/media/boot/boot.ini:166:setenv bootrootfs "console=tty1 console=ttySAC2,115200n8 root=UUID=8e21052d-2fa6-4016-9d13-ef7bf1381ea5 rootwait ro fsck.repair=yes net.ifnames=0"
/mnt/media/boot/boot.ini.default:34:#bootrootfs=console=tty1 console=ttySAC2,115200n8 root=UUID=8e21052d-2fa6-4016-9d13-ef7bf1381ea5 rootwait ro fsck.repair=yes net.ifnames=0
/mnt/etc/fstab:1:UUID=8e21052d-2fa6-4016-9d13-ef7bf1381ea5 / ext4 errors=remount-ro,noatime 0 1
/mnt/etc/fstab:2:UUID=BD14-46F4 /media/boot vfat defaults 0 1
/dev/mmcblk1: PTUUID="3cedfd53" PTTYPE="dos"
/dev/mmcblk1p1: SEC_TYPE="msdos" LABEL="boot" UUID="BD14-46F4" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk1p2: LABEL="rootfs" UUID="8e21052d-2fa6-4016-9d13-ef7bf1381ea5" TYPE="ext4" PARTUUID="3cedfd53-02"

The UUIDs for the

  • (mmcblk0) eMMC
  • (mmcblk1) MicroSD card

should match as well.

Maybe there's no problem that working on this state even nothing do anymore.
But to avoid from wrong behavior when you're working on kernel something or the other conditions you would work, you should adapt this procedure for the eMMC too.

Actually, the repeated procedure follows below.

  • 1. Boot from MicroSD card.
  • 2. Install the prerequisites.
  • 3. Set UUIDs for each two partitions for the eMMC.
  • 4. Edit fstab, boot.ini* for the eMMC.
  • 5. Regenerate initrd.
  • 6. Check the work.

There's nothing too hard. The only thing you keep in mind is the device name of the eMMC is /dev/mmcblk0.
Always keep in mind that which device has the current root partition and which device is mounted at the /mnt directory.
So it is recommended to enter the command df -Th before do every steps.

target
# Booted with eMMC, mount MicroSD at /mnt
 
$ sudo df -Th
# results
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  930M     0  930M   0% /dev
tmpfs          tmpfs     200M  8.4M  192M   5% /run
/dev/mmcblk0p2 ext4       29G  1.2G   28G   4% /
tmpfs          tmpfs     998M     0  998M   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     998M     0  998M   0% /sys/fs/cgroup
/dev/mmcblk0p1 vfat      128M   12M  117M   9% /media/boot
tmpfs          tmpfs     200M     0  200M   0% /run/user/0
/dev/mmcblk1p2 ext4      7.2G  1.2G  6.0G  16% /mnt
/dev/mmcblk1p1 vfat      128M   12M  117M   9% /mnt/media/boot

Finally I can get the blkid result which has no default UUIDs.

target
$ sudo blkid
# results
/dev/mmcblk1: PTUUID="3cedfd53" PTTYPE="dos"
/dev/mmcblk1p1: SEC_TYPE="msdos" LABEL="boot" UUID="BD14-46F4" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk1p2: LABEL="rootfs" UUID="8e21052d-2fa6-4016-9d13-ef7bf1381ea5" TYPE="ext4" PARTUUID="3cedfd53-02"
/dev/mmcblk0: PTUUID="3cedfd53" PTTYPE="dos"
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="BA83-051F" TYPE="vfat" PARTUUID="3cedfd53-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="d6e728b2-1f2f-4727-af9c-933aa5023006" TYPE="ext4" PARTUUID="3cedfd53-02"

And also I can upgrade the kernel respectively.
To boot with another boot media, just poweroff - switching boot mode - power on with the trigger. Now surely works.

  • If you can't boot after after upgrade the packages by enter apt full-upgrade or apt-get dist-upgrade, perhaps that's because boot.ini has been changed to which has default UUID for root partition. Boot with another boot media (not a corrupted one) and mount that into /mnt, /mnt/media/boot, then edit /mnt/media/boot/boot.ini correctly.
  • odroid-xu4/software/switching_boot_partition_sd_emmc.txt
  • Last modified: 2018/03/07 21:50
  • by odroid