Skip to content

Instantly share code, notes, and snippets.

@devmfc
Last active April 17, 2023 20:15
Show Gist options
  • Save devmfc/d6b3a5cfbb2802b61bea5b799a03498b to your computer and use it in GitHub Desktop.
Save devmfc/d6b3a5cfbb2802b61bea5b799a03498b to your computer and use it in GitHub Desktop.
Install to emmc experimental
#!/bin/bash
# 2020 DEVMFC
exit_with_error() {
echo "Error occured $1 [$?], exitting..."
umount -f /tmp/${target_boot_part}
umount -f /tmp/${target_root_part}
exit 1
}
exit_on_error() {
if [ $? -ne 0 ]; then
echo "Error occured, exitting..."
umount -f /tmp/${target_boot_part}
umount -f /tmp/${target_root_part}
exit 1
fi
}
create_partition_table(){
echo "Backup vendor u-boot"
dd if="${target_disk}" of=/root/vendor-aml-uboot-backup.img bs=1M count=4
exit_on_error
# Amlogic u-boot has its environment safed om emmc offset 0x4d400000, after that
# a recovery partition exists, so skip that space.
parted -s "${target_disk}" mklabel msdos
exit_on_error
parted -s "${target_disk}" mkpart primary fat32 1536M 2047M # boot
exit_on_error
parted -s "${target_disk}" mkpart primary ext4 2048M 100% # rootfs
exit_on_error
echo "Start restoring vendor u-boot"
dd if=/root/vendor-aml-uboot-backup.img of="${target_disk}" conv=fsync bs=442 count=1
exit_on_error
dd if=/root/vendor-aml-uboot-backup.img of="${target_disk}" conv=fsync bs=512 skip=1 seek=1
exit_on_error
sync
}
create_fs() {
#return 0
local fs_type=${1:-ext4}
local part=$2
local label=$3
echo -n "Formatting BOOT partition... "
mkfs.${fs_type} ${part}
exit_on_error
[ "${fs_type}" = "ext4" ] && e2label ${part} "${label}"
[ "${fs_type}" = "vfat" ] && exfatlabel ${part} "${label}"
echo "done."
}
'grep' -l MMC /sys/bus/mmc/devices/*/type >/dev/null || exit_with_error "No eMMC found"
target_disk="/dev/"$(basename $(dirname $('grep' -l MMC /sys/bus/mmc/devices/*/type|head -n1))/block/*)
target_boot_part=${target_disk}p1
target_root_part=${target_disk}p2
host_root_part=$(findmnt --target / -n -o SOURCE)
host_boot_part=$(findmnt --target /boot -n -o SOURCE)
host_disk=/dev/$(lsblk -ro PATH,KNAME,PKNAME,MOUNTPOINT | grep -E '/$'|cut -d' ' -f3)
echo "Host disk: $host_disk"
echo "Current boot part dev: $host_boot_part"
echo "Current root part dev: $host_root_part"
echo "target emmc dev: $target_disk"
if [[ "${host_disk}" = "${target_disk}"* ]]; then
echo "System already running from target disk. Exit"
exit 1
fi
echo "Start create dos partiton table omn emmc"
echo "This will erase all existing partitions on that disk!"
read -p "Very sure? y/N" answer;
if [[ ! ${answer} =~ ^[yY]$ ]]; then
echo "Bye"
exit 0
fi
echo "Ok, proceeding"
apt-get -yyqq update &>/dev/null
if ! command -v fdisk >/dev/null; then
echo -n "Installing fdisk... "
apt-get install fdisk -yy || exit_with_error "Installing fdisk"
echo "[Ok]"
fi
if ! command -v parted >/dev/null; then
echo -n "Installing parted... "
apt-get install parted -yy || exit_with_error "Installing parted"
echo "[Ok]"
fi
if ! command -v mkfs.fat >/dev/null; then
echo -n "Installing dosfstools... "
apt-get install dosfstools -yy || exit_with_error "Installing dosfstools"
echo "[Ok]"
fi
if ! command -v rsync >/dev/null; then
echo -n "Installing rsync... "
apt-get install rsync -yy || exit_with_error "Installing rsync"
echo "[Ok]"
fi
create_partition_table
create_fs vfat ${target_boot_part} "BOOT"
create_fs ext4 ${target_root_part} "ROOTFS"
#create_fs ext4 ${target_disk}p3 "ROOTFS2"
#create_fs ext4 ${target_disk}p4 "HOME"
mkdir -p /tmp${target_boot_part}
mkdir -p /tmp${target_root_part}
mount -o rw ${target_boot_part} /tmp/${target_boot_part}
exit_on_error
mount -o rw ${target_root_part} /tmp/${target_root_part}
exit_on_error
echo -n "Copy /boot..."
cp --archive /boot/* /tmp${target_boot_part} && sync
echo "done."
echo "Creating rootfs..."
mkdir -p /tmp${target_root_part}/{dev,etc,home,mnt,opt,proc,root,run,selinux,srv,sys,tmp,usr,var}
mkdir -p /tmp${target_root_part}/usr/{bin,lib,sbin,local,share,src}
ln -s usr/bin /tmp${target_root_part}/bin
ln -s usr/lib /tmp${target_root_part}/lib
ln -s usr/sbin /tmp${target_root_part}/sbin
echo "copy rootfs files..."
#find / -maxdepth 1 -not -path "/" -not -name "dev" -not -name "sys" -not -name "proc" -not -name "run" -not -name "var" -not -name "mnt" -not -name "tmp" -not -name "lost+found" -exec cp -v --archive {} ./tmp${target_root_part} \;
# also copy var?
find / -maxdepth 1 -not -path "/" \
-not -name "dev" \
-not -name "media" \
-not -name "sys" \
-not -name "proc" \
-not -name "run" \
-not -name "mnt" \
-not -name "tmp" \
-not -name "lost+found" \
-not -name "System Volume Information" \
-not -name "Android" \
-exec /bin/bash -c "echo \"Copying {}\"; rsync -a --info=progress2 {} /tmp${target_root_part}; echo Ok." \;
sync
echo "done"
echo -n "Creating new fstab... "
cat <<EOF > /tmp${target_root_part}/etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
UUID=$(lsblk -ro PATH,UUID|grep ${target_root_part}|cut -d' ' -f2) / ext4 defaults,noatime,nodiratime,commit=60,errors=remount-ro 0 1
UUID=$(lsblk -ro PATH,UUID|grep ${target_boot_part}|cut -d' ' -f2) /boot vfat defaults 0 2
tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
echo "done."
echo -n "Config boot... "
[ -f /tmp/${target_boot_part}/boot.config ] && \
grep "^root=" "/tmp${target_boot_part}/boot.config" && \
sed -i "s|^root=.*|root=${target_root_part}|" /tmp${target_boot_part}/boot.config || \
echo "root=${target_root_part}" >> /tmp${target_boot_part}/boot.config
echo "done."
echo "Cleaning up"
sync
umount -f /tmp/${target_boot_part}
umount -f /tmp/${target_root_part}
echo "ready, you can now reboot. Or not, whatever you want."
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment