Add disk image cleanup script
This commit is contained in:
commit
28cb1fcd57
|
@ -0,0 +1,53 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# dd bs=256K if=/dev/mmcblk0 of=2022-11-17-pilpil.img status=progress oflag=dsync
|
||||||
|
set -o errexit
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
# Display help if -h(elpf) used
|
||||||
|
if [[ "${1-}" =~ ^-*h(elp)?$ ]] || [[ -z "${1-}" ]]; then
|
||||||
|
echo -e "Usage: ./cleanup_pilpil_img.sh path_to_disk_image"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! -e "${1}" ]];then
|
||||||
|
echo -e "File not found !"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
GIT_URL="https://forge.chapril.org/pilpil/pilpil-client.git"
|
||||||
|
ROOTFS="./rootfs"
|
||||||
|
|
||||||
|
# Mount disk image
|
||||||
|
LOOP_DEV="$(bash mount_img.sh "${1}")"
|
||||||
|
|
||||||
|
# dhcpcd.conf : Remove 2 last lines
|
||||||
|
sudo sed -i "$(( $(wc -l < $ROOTFS/etc/dhcpcd.conf) - 2 +1 )),$ d" "$ROOTFS"/etc/dhcpcd.conf
|
||||||
|
# sshd_config : Remove 3 last lines
|
||||||
|
sudo sed -i "$(( $(wc -l < $ROOTFS/etc/ssh/sshd_config) - 3 +1 )),$ d" "$ROOTFS"/etc/ssh/sshd_config
|
||||||
|
# wpa_supplicant.conf : subs SSID and PW
|
||||||
|
sudo sed -i 's/\(ssid=\"\).*/\1pilpil\"/' "$ROOTFS"/etc/wpa_supplicant/wpa_supplicant.conf
|
||||||
|
sudo sed -i 's/\(psk=\"\).*/\1poilpoil\"/' "$ROOTFS"/etc/wpa_supplicant/wpa_supplicant.conf
|
||||||
|
# Clear git/ssh credentials
|
||||||
|
#cd "$ROOTFS"/home/pil/pilpil-client/
|
||||||
|
#git config --global --unset credential.helper
|
||||||
|
#git config --local --unset credential.helper
|
||||||
|
#git config --unset credential.helper
|
||||||
|
#sudo rm "$ROOTFS"/home/pil/.ssh/*
|
||||||
|
#sudo sed -i "s:localgit:$GIT_URL:g" "$ROOTFS"/home/pil/pilpil-client/.git/config
|
||||||
|
# Empty a few folders
|
||||||
|
sudo rm -rf "$ROOTFS"/home/pil/.ssh/authorized_keys "$ROOTFS"/home/pil/Videos/* "$ROOTFS"/etc/ssl/private/nginx-selfsigned.key "$ROOTFS"/etc/ssl/certs/nginx-selfsigned.crt
|
||||||
|
#sudo rm -rf "$ROOTFS"/home/pil/*.log "$ROOTFS"/home/pil/.bash_history "$ROOTFS"/home/pil/.python_history
|
||||||
|
# Create dummy file in ~/Videos to reserve some space
|
||||||
|
sudo dd if=/dev/zero of="$ROOTFS"/home/pil/Videos/remove_me bs=4MB count=100
|
||||||
|
# hostname : Change hostname
|
||||||
|
echo "pilpil" | sudo tee "$ROOTFS"/etc/hostname
|
||||||
|
# hosts : Replace with hostname
|
||||||
|
sudo sed -i 's/\(127.0.1.1\t\).*/\1pilpil/' "$ROOTFS"/etc/hosts
|
||||||
|
|
||||||
|
sudo awk '{print $0" consoleblank=1"}' "$ROOTFS"/boot/cmdline.txt
|
||||||
|
|
||||||
|
# Unmount disk image
|
||||||
|
bash umount_img.sh "$LOOP_DEV"
|
||||||
|
|
||||||
|
# sudo pishrink.sh -Zpa 2022-10-21-pilpil.img
|
|
@ -0,0 +1,26 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -o errexit
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
if [[ -z "${1-}" ]] || [[ ! -e "${1}" ]]; then
|
||||||
|
echo "No file provided or file not found."
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! -d ./rootfs ]];then
|
||||||
|
echo "Creating missing mount point directory."
|
||||||
|
mkdir ./rootfs
|
||||||
|
fi
|
||||||
|
|
||||||
|
LOOP_DEV="$(sudo losetup -f -P --show "$1")"
|
||||||
|
|
||||||
|
sudo mount "$LOOP_DEV"p2 -o rw ./rootfs/
|
||||||
|
sudo mount "$LOOP_DEV"p1 ./rootfs/boot/
|
||||||
|
sudo mount --bind /sys ./rootfs/sys/
|
||||||
|
sudo mount --bind /dev ./rootfs/dev/
|
||||||
|
sudo mount --bind /dev/pts ./rootfs/dev/pts
|
||||||
|
sudo mount --bind /proc ./rootfs/proc/
|
||||||
|
|
||||||
|
echo "${LOOP_DEV}"
|
|
@ -0,0 +1,26 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -o errexit
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
if [[ -z "${1-}" ]] || [[ ! -e "${1}" ]]; then
|
||||||
|
echo "Missing or non-existent loop device path."
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! $(findmnt -M "./rootfs") ]]; then
|
||||||
|
echo "Not mounted, aborting."
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Unmounting FSes..."
|
||||||
|
sudo umount ./rootfs/boot/
|
||||||
|
sudo umount ./rootfs/sys/
|
||||||
|
sudo umount ./rootfs/dev/pts
|
||||||
|
sudo umount ./rootfs/dev/
|
||||||
|
sudo umount ./rootfs/proc/
|
||||||
|
sudo umount ./rootfs/
|
||||||
|
|
||||||
|
echo "Detaching loop setup..."
|
||||||
|
sudo losetup -d "${1}"
|
Loading…
Reference in New Issue