Skip to content

Instantly share code, notes, and snippets.

@MakiseKurisu
Last active July 5, 2022 19:36
Show Gist options
  • Save MakiseKurisu/ec21bc65bdc03cbf7aba1b58395633ce to your computer and use it in GitHub Desktop.
Save MakiseKurisu/ec21bc65bdc03cbf7aba1b58395633ce to your computer and use it in GitHub Desktop.
Install vanilla UEFI Debian on Rock Pi 4B+
# You will need a real Debian machine to perform cross debootstrap
# Debian on WSL does not work
# Enter root mode
sudo -i
# Define variables
TARGET=/dev/mmcblk0
MOUNT_POINT=/mnt
IP_ADDRESS=192.168.0.2
GATEWAY=192.168.0.1
# Install dependency
apt update
apt install -y btrfs-progs dosfstools qemu-user-static debootstrap
# Set up partition table and file systems
dd if=/dev/zero of=${TARGET} bs=1M count=8
(echo g; echo n; echo ''; echo ''; echo '+512M'; echo t; echo 1; echo n; echo ''; echo ''; echo ''; echo w) | fdisk ${TARGET}
mkfs.fat -F32 ${TARGET}p1
mkfs.btrfs -f ${TARGET}p2
# Create subvolumes
mount -o compress=zstd ${TARGET}p2 ${MOUNT_POINT}
btrfs subvolume create ${MOUNT_POINT}/@
btrfs subvolume create ${MOUNT_POINT}/@home
btrfs subvolume create ${MOUNT_POINT}/@root
umount ${MOUNT_POINT}
# Mount partitions
mount -o compress=zstd,subvol=@ ${TARGET}p2 ${MOUNT_POINT}
mkdir -p ${MOUNT_POINT}/home
mount -o compress=zstd,subvol=@home ${TARGET}p2 ${MOUNT_POINT}/home
mkdir -p ${MOUNT_POINT}/root
mount -o compress=zstd,subvol=@root ${TARGET}p2 ${MOUNT_POINT}/root
mkdir -p ${MOUNT_POINT}/boot/efi
mount ${TARGET}p1 ${MOUNT_POINT}/boot/efi
# First stage debootstrap
debootstrap --arch=arm64 --foreign --components=main,non-free,contrib --include=locales bullseye ${MOUNT_POINT} https://mirrors.tuna.tsinghua.edu.cn/debian
# Second stage debootstrap
cp /usr/bin/qemu-aarch64-static ${MOUNT_POINT}/usr/bin/
LANG=C.UTF-8 chroot ${MOUNT_POINT} /usr/bin/qemu-aarch64-static /bin/bash /debootstrap/debootstrap --second-stage
# Set up fstab
echo "UUID=$(blkid -s UUID -o value ${TARGET}p2) / btrfs defaults,subvol=@,compress=zstd 0 0" >> ${MOUNT_POINT}/etc/fstab
echo "UUID=$(blkid -s UUID -o value ${TARGET}p2) /home btrfs defaults,subvol=@home,compress=zstd 0 0" >> ${MOUNT_POINT}/etc/fstab
echo "UUID=$(blkid -s UUID -o value ${TARGET}p2) /root btrfs defaults,subvol=@root,compress=zstd 0 0" >> ${MOUNT_POINT}/etc/fstab
echo "UUID=$(blkid -s UUID -o value ${TARGET}p1) /boot/efi vfat defaults 1 2" >> ${MOUNT_POINT}/etc/fstab
# Update source.list
echo "deb [signed-by=/usr/share/keyrings/armbian-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/armbian/ bullseye main bullseye-desktop bullseye-utils" > ${MOUNT_POINT}/etc/apt/sources.list.d/armbian.list
echo "deb [signed-by=/usr/share/keyrings/pimox-archive-keyring.gpg] https://raw.fastgit.org/pimox/pimox7/master/ dev/" > ${MOUNT_POINT}/etc/apt/sources.list.d/pimox.list
curl https://mirrors.tuna.tsinghua.edu.cn/armbian/armbian.key | gpg --dearmour -o ${MOUNT_POINT}/usr/share/keyrings/armbian-archive-keyring.gpg
curl https://raw.fastgit.org/pimox/pimox7/master/KEY.gpg | gpg --dearmour -o ${MOUNT_POINT}/usr/share/keyrings/pimox-archive-keyring.gpg
# Add kernel hook for copying dtbs, which is required for U-Boot to boot grub
mkdir -p ${MOUNT_POINT}/boot/efi/dtb
cat << 'EOF' > ${MOUNT_POINT}/etc/kernel/postinst.d/copy-dtb
#!/bin/sh
set -e
version="$1"
if [ -e /usr/lib/linux-image-${version}/* ]; then
echo Copying current dtb files to /boot/efi/dtb....;
cp -r /usr/lib/linux-image-${version}/* /boot/efi/dtb/
fi;
EOF
chmod +x ${MOUNT_POINT}/etc/kernel/postinst.d/copy-dtb
# Configure network
cat << EOF > ${MOUNT_POINT}/etc/network/interfaces
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address ${IP_ADDRESS}/24
gateway ${GATEWAY}}
EOF
# Enter chroot
for i in /dev /dev/pts /proc /sys /run; do mount -B $i ${MOUNT_POINT}$i; done
LANG=en_US.UTF-8 chroot ${MOUNT_POINT} /usr/bin/qemu-aarch64-static /bin/bash
# Define variables
USER=user
# Configure locale and timezone
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
echo 'LANG="en_US.UTF-8"'> /etc/default/locale
dpkg-reconfigure -f noninteractive locales
update-locale LANG=en_US.UTF-8
echo "Asia/Shanghai" > /etc/timezone
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
dpkg-reconfigure -f noninteractive tzdata
# Install additional packages
apt update
apt install -y linux-image-arm64 grub-efi openssh-server sudo haveged zram-tools tmux bash-completion armbian-firmware wpasupplicant snapper btrfs-progs
printf "SIZE=2048\nPRIORITY=100\nALGO=zstd\n" >> /etc/default/zramswap
systemctl enable ssh haveged zramswap
# Install grub
grub-install --target=arm64-efi --efi-directory=/boot/efi --removable
sed -i "s/GRUB_CMDLINE_LINUX_DEFAULT=\"quiet\"/GRUB_CMDLINE_LINUX_DEFAULT=\"console=ttyS2,1500000 console=tty1 consoleblank=0 loglevel=1 usb-storage.quirks=0x2537:0x1066:u,0x2537:0x1068:u cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1\"/" /etc/default/grub
GRUB_DISABLE_OS_PROBER=true update-grub
# Clean up
apt-get clean
# Change root password
passwd
# Create new user for SSH login
useradd -m -s /bin/bash ${USER}
usermod -a -G sudo ${USER}
passwd ${USER}
# Exit chroot
exit
rm ${MOUNT_POINT}/usr/bin/qemu-aarch64-static
sync
umount -R ${MOUNT_POINT}
# After the installation, you need to upgrade your U-Boot to the latest mainline,
# which can be extracted from Armbian package linux-u-boot-espressobin-edge.
# Once you flash the U-Boot, you need to backup your mac address,
# and then run `env default -f -a` to clear preexisting congfiguration.
# If you do not backup your mac address you will get a random new one on every boot.
# See Final steps in https://marcin.juszkiewicz.com.pl/2021/02/15/ebbr-on-espressobin/
# Then you will need to add an entry to boot grubaa64.efi as a workaround,
# since the default bootaa64.efi cannot boot the device.
# Something like (check your existing bootcmd first with `env print bootcmd):
# env set grub_bootcmd "load mmc 0:1 ${fdt_addr_r} /dtb/marvell/armada-3720-espressobin.dtb;load mmc 0:1 ${kernel_addr_r} /efi/boot/grubaa64.efi;bootefi ${kernel_addr_r} ${fdt_addr_r};"
# env set bootcmd "un grub_bootcmd;run distro_bootcmd"
# env save
# For more info on U-Boot please visit: https://www.armbian.com/espressobin/
# In some cases your U-Boot will enter a reset loop, with the final output like this:
# BootOrder not defined
# EFI boot manager: Cannot load any image
# 856064 bytes read in 44 ms (18.6 MiB/s)
# Booting /efi\boot\bootaa64.efi
# Reset System
# resetting ...
# In this case you have to boot your board manually:
# load mmc 0:1 ${kernel_addr_r} /EFI/BOOT/grubaa64.efi
# bootefi ${kernel_addr_r}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment