Skip to content

Instantly share code, notes, and snippets.

@hiracchi
Created September 8, 2019 02:40
Show Gist options
  • Save hiracchi/1e54c942ba7ec5a4f523f9eac811d117 to your computer and use it in GitHub Desktop.
Save hiracchi/1e54c942ba7ec5a4f523f9eac811d117 to your computer and use it in GitHub Desktop.
copy microsd data to USB memory at raspberry pi
#!/bin/bash
DEVICE="/dev/sda"
export LANG=C
sudo parted -s -a optimal ${DEVICE} -- mklabel msdos
sudo parted -s -a optimal ${DEVICE} -- mkpart primary fat32 0% 100M set 1 boot on
sudo parted -s -a optimal ${DEVICE} -- mkpart primary ext4 100M 100%
sudo parted -s -a optimal ${DEVICE} -- print
sudo mkfs.vfat -F -n BOOT -F 32 ${DEVICE}1
sudo mkfs.ext4 -F ${DEVICE}2
sudo mkdir -p /mnt/target
sudo mount ${DEVICE}2 /mnt/target/
sudo mkdir /mnt/target/boot
sudo mount ${DEVICE}1 /mnt/target/boot/
sudo rsync -ax --progress / /boot /mnt/target
# ls -l /dev/disk/by-partuuid/
PARTUUID1=`sudo blkid | grep ${DEVICE}1 | sed -e "s/.\+PARTUUID=\"\(.\+\)\"/\1/g"`
PARTUUID2=`sudo blkid | grep ${DEVICE}2 | sed -e "s/.\+PARTUUID=\"\(.\+\)\"/\1/g"`
# echo ${PARTUUID1}
# echo ${PARTUUID2}
cat /boot/cmdline.txt | \
perl -pe "s/PARTUUID=(.+?)\s/PARTUUID=${PARTUUID2} /g" | \
sudo tee /mnt/target/boot/cmdline.txt
cat /etc/fstab | \
perl -pe "s|PARTUUID=\S+\s+/boot\s+|PARTUUID=${PARTUUID1} /boot |g" | \
perl -pe "s|PARTUUID=\S+\s+/\s+|PARTUUID=${PARTUUID2} / |g" | \
sudo tee /mnt/target/etc/fstab
# make sshkeys
sudo mount --bind /dev /mnt/target/dev
sudo mount --bind /sys /mnt/target/sys
sudo mount --bind /proc /mnt/target/proc
cat << EOS | sudo tee /mnt/target/usr/local/sbin/update_sshkeys.sh
rm /etc/ssh/ssh_host*
dpkg-reconfigure openssh-server
exit
EOS
sudo chmod +x /mnt/target/usr/local/sbin/update_sshkeys.sh
cd /mnt/target
sudo chroot /mnt/target /usr/local/sbin/update_sshkeys.sh
sudo umount /mnt/target/dev
sudo umount /mnt/target/sys
sudo umount /mnt/target/proc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment