Last active
February 10, 2023 00:53
-
-
Save navigaid/4b7c36d7682a9fdbc00e682ea2936eaa to your computer and use it in GitHub Desktop.
A script to install Chrome OS recovery image for Pixelbook to USB stick.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# repo: https://gist.github.com/libredot/4b7c36d7682a9fdbc00e682ea2936eaa | |
# author: [email protected] | |
set -ex | |
if [[ ! -b ${1} ]]; then | |
echo "Usage: ${0} /dev/sdx" | |
exit 1 | |
fi | |
# If we're not running as root, restart as root. | |
if [[ ${UID:-$(id -u)} -ne 0 ]]; then | |
exec sudo "${0}" "${@}" | |
fi | |
# stop auto-mounting USB devices | |
stop cros-disks || : | |
SCRIPT_FULL_PATH="$(readlink -f "${0}")" | |
SCRIPT_ROOT=$(dirname ${SCRIPT_FULL_PATH}) | |
USB="${1}" | |
STATE="${USB}1" | |
ROOT_A="${USB}3" | |
ROOT_B="${USB}5" | |
BOOT="${USB}12" | |
# Download Pixelbook recovery image at first run | |
# IMG="chromeos_9901.77.0_eve_recovery_stable-channel_mp.bin" | |
IMG="chromeos_9901.77.0_squawks_recovery_stable-channel_mp-v4.bin" | |
URL="https://dl.google.com/dl/edgedl/chromeos/recovery/${IMG}.zip" | |
# TODO: document this | |
if [[ -n "${2}" ]]; then | |
IMG="${2}" | |
fi | |
[[ -f ${SCRIPT_ROOT}/${IMG} ]] || curl ${URL} | zcat > ${IMG} | |
chromeos-install --yes --debug --skip_postinstall --skip_src_removable --skip_dst_removable --payload_image=${IMG} --dst=${USB} | |
################ grub.cfg hacks ################## | |
# - Enable dev mode. | |
# - Fix PARTUUID. | |
PARTUUID_A="$(blkid "${ROOT_A}" | rev | cut -d ' ' -f 1 | rev | tr -d \" | tr '[a-z]' '[A-Z]')" | |
PARTUUID_B="$(blkid "${ROOT_B}" | rev | cut -d ' ' -f 1 | rev | tr -d \" | tr '[a-z]' '[A-Z]')" | |
BOOT_MNT="$(mktemp -d)" | |
mount "${BOOT}" "${BOOT_MNT}" | |
cat << EOF > "${BOOT_MNT}/efi/boot/grub.cfg" | |
set timeout=3 | |
menuentry "local image A" { | |
linux /syslinux/vmlinuz.A init=/sbin/init boot=local rootwait ro noresume noswap loglevel=7 noinitrd console= i915.modeset=1 cros_efi cros_debug root=${PARTUUID_A} | |
} | |
menuentry "local image B" { | |
linux /syslinux/vmlinuz.B init=/sbin/init boot=local rootwait ro noresume noswap loglevel=7 noinitrd console= i915.modeset=1 cros_efi cros_debug root=${PARTUUID_B} | |
} | |
EOF | |
umount "${BOOT_MNT}" | |
rmdir "${BOOT_MNT}" | |
################ rootfs hacks ################## | |
# - Enable rw mount for root partition. | |
# To revert, '\000' -> '\777' | |
# To show what changes are made, run: | |
# dumpe2fs ${ROOT} | grep 'Filesystem features' | |
# You are not supposed to understand this magic ;) | |
# - pstore.conf considered harmful. Get rid of it. | |
ROOT_MNT="$(mktemp -d)" | |
for ROOT in "${ROOT_A}" "${ROOT_B}"; do | |
printf '\000' | dd of=${ROOT} seek=$((0x464+3)) conv=notrunc count=1 bs=1 status=none | |
mount "${ROOT}" "${ROOT_MNT}" | |
cp ${SCRIPT_FULL_PATH} ${ROOT_MNT} | |
rm "${ROOT_MNT}/etc/init/pstore.conf" | |
mv ${ROOT_MNT}/usr/bin/crosh{,-bak} | |
mv ${ROOT_MNT}/sbin/chromeos_startup{,-bak} | |
cat << EOF > "${ROOT_MNT}/etc/shadow" | |
root:*::::::: | |
chronos:*::::::: | |
EOF | |
cat << EOF > "${ROOT_MNT}/usr/bin/crosh" | |
#!/bin/bash | |
exec sudo su - chronos | |
EOF | |
cat << EOF > "${ROOT_MNT}/sbin/chromeos_startup" | |
#!/bin/bash | |
mount -t debugfs none /sys/kernel/debug | |
bootstat pre-startup | |
mkdir -p /dev/pts /dev/shm | |
mount -n -t tmpfs shmfs /dev/shm | |
mount -n -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts | |
sysctl -q --system | |
mount "/dev/$(lsblk -n -o pkname $(findmnt -n -o source /))1" /mnt/stateful_partition | |
mkdir -p /mnt/stateful_partition/dev_image | |
mkdir -p /mnt/stateful_partition/var_overlay | |
mkdir -p /mnt/stateful_partition/etc/docker | |
mount --bind /mnt/stateful_partition/dev_image /usr/local | |
mount --bind /mnt/stateful_partition/var_overlay /var | |
for d in home home/chronos home/root home/user unencrypted unencrypted/cache unencrypted/preserve; do | |
mkdir -p -m 0755 "/mnt/stateful_partition/${d}" | |
done | |
mount --bind /mnt/stateful_partition/home /home | |
rm -rf /var/run /var/lock | |
ln -s /run /var/run | |
ln -s /run/lock /var/lock | |
mkdir -p -m 0755 /var/cache /var/db /var/empty /var/log/metrics /var/spool /var/tmp /var/lib/misc | |
chmod 1777 /var/tmp | |
chmod 1771 /home/root | |
chown chronos:chronos /home/chronos /var/log/metrics | |
chgrp syslog /var/log | |
chmod 1775 /var/log | |
chmod 0755 /var/cache /var/db /var/empty /var/spool /var/lib /var/lib/misc | |
chattr +i /var/empty || : | |
mount --make-shared -n -t tmpfs media /media | |
bootstat post-startup | |
exit 0 | |
EOF | |
umount "${ROOT_MNT}" | |
done | |
rmdir "${ROOT_MNT}" | |
echo "OK. Now please shutdown and reboot." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment