So. I've now written a script to save the Pi filesystem from an SD-Card to a tarfile and another script to partition the SD-card, format it, and write the tarfile filesystem into it. They seem to work OK on Kubuntu, and the second script also works with the Arch Linux tarfile which is the only way that Arch is supplied now.
They proved to be difficult to write with an endless stream of "gotchas" to work around. They also require a fair number of utilities to be installed.
And they only work with KDE at the moment which has its own specific way of handling udev mounting.
KDE does not automount by default but Gnome and Unity do. Not automounting means that the user must be prompted to mount from the taskbar icon, but automounting by default caused problems for partitioning and formatting. So I'd have to add umount commands in there and do another round of testing if I want it to work on other desktops. I might do that sometime, but not now.
Anyway, here's the scripts, and all I can say is that they work for me on my system as it's configured at the moment:
#!/bin/bash # Save Raspberry Pi filesystem from SD-Card to tarfile fail() { echo Error: $*; exit 1; } bak="pi-$(date +%d%b%y | tr '[A-Z]' '[a-z]')" [[ -f "$bak" ]] && fail archive already exists [[ -z $(groups $USER | grep '\bdisk\b') ]] && fail $USER not in disk group read -p "Insert SD-card then press [enter]" sleep 1 dv=/dev/$(dmesg | grep -o '\[sd.\]' | tail -1 | tr -d '\[\]') [[ "$(sfdisk -g $dv 2>&1)" != *cylinders*heads* ]] && fail re-insert SD-card [[ -z $(udevadm info $dv 2>&1 | grep ID_BUS=usb) ]] && fail not USB device labs="$(blkid ${dv}{1,2} -s LABEL -s TYPE | sed 's:/dev/sd.::')" echo "At ${dv}:" echo "$labs" if [[ "$labs" == *vfat*ext4* && "$labs" != *FAT-BOOT*ext4-main* ]]; then read -p 'Ready to set SD-card partition labels. Press [enter]' e2label ${dv}2 ext4-main || fail label setting failed fatlabel ${dv}1 FAT-BOOT || fail need to install dosfstools read -p "Remove and re-insert SD-Card. Then press [enter]" sleep 1 fi labs="$(blkid ${dv}{1,2} -s LABEL -s TYPE | sed 's:/dev/sd.::')" [[ "$labs" != *FAT-BOOT*vfat*ext4-main*ext4* ]] && fail re-insert device ras="/media/$USER/ext4-main" boot="/media/$USER/FAT-BOOT" read -p "Mount SD-Card filesytems via USB icon or when prompted. [enter]" [[ ! -d $ras || ! -d $boot ]] && fail should mount at $ras and $boot read -p "Ready to fetch filesystem [ent]" echo Copying files from boot partition to boot directory sudo cp -fa $boot/* $ras/boot/ sudo chown root:root $ras/boot/* echo "Tarring to $bak.tar" # c=create-archive z=gzip f=file numeric-owner=suppress user:group names sudo tar -C $ras --numeric-owner --one-file-system -czf $bak.tar.gz "." sudo chown $USER:$(id -ng $USER) "$bak".tar.gz # user ownership echo Erasing temporary files in /boot directory sudo rm -f $ras/boot/* echo "Done" ls -lh $bak.tar.gz
# ------------------------------------------------------------------
#!/bin/bash # Burn tarfile filesystem to SD-Card for Raspberry Pi fail() { echo Error: $*; exit 1; } PATH=$PATH:/sbin:/usr/sbin # for Debian users [[ "$1" != *.tar.gz ]] && fail tar.gz file required as parameter [[ -z $(groups $USER | grep '\bdisk\b') ]] && fail $USER not in disk group read -p "Insert SD-card then press [enter]" sleep 1 dv=/dev/$(dmesg | grep -o '\[sd.\]' | tail -1 | tr -d '\[\]') [[ "$(sfdisk -g $dv 2>&1)" != *cylinders*heads* ]] && fail re-insert SD-card [[ -z $(udevadm info $dv 2>&1 | grep ID_BUS=usb) ]] && fail not USB device mod=$(udevadm info $dv | grep 'ID_MODEL=' | sed 's/.*ID_MODEL=//') labs="$(blkid ${dv}{1,2} -s LABEL -s TYPE | sed 's:/dev/sd.::')" if [[ "$labs" != *FAT-BOOT*vfat*ext4-main*ext4* ]]; then read -p "Ready to partition SD-card in $mod at $dv. Press [enter]" echo -e "1,59,0C,*\n60\n0,0\n0,0\n" |sudo sfdisk -uM -L $dv read -p $'\n'"Remove and re-insert SD-card. Then press [enter]" sleep 1 read -p "Ready to format SD-card. [enter]" mkfs.vfat ${dv}1 -n "FAT-BOOT" || fail format failed mkfs.ext4 ${dv}2 -F -L "ext4-main" || fail format failed read -p "Done. Remove and re-insert SD-card. [enter]" fi ras="/media/$USER/ext4-main" boot="/media/$USER/FAT-BOOT" read -p "Mount SD-Card filesytems via USB icon or when prompted. [enter]" [[ ! -d $ras || ! -d $boot ]] && fail should mount at $ras and $boot read -p "Ready to write tarfile to SD-card. [enter]" echo Untarring to sd-card... sudo rm -rf /media/$USER/ext4-main/* /media/$USER/FAT-BOOT/* sudo tar --numeric-owner -xzpf "$1" -C "$ras" sync echo Copying files from boot directory to boot partition sudo cp -d --preserve=mode,timestamps $ras/boot/* $boot/ if [[ $? = 0 ]]; then echo Erasing temporary files in /boot directory sudo rm -f $ras/boot/* fi sync echo "Done."