SHARE
TWEET

raspbian-ua-netinst-qemu-prepare

a guest Oct 12th, 2014 529 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2.  
  3. check_tool()
  4. {
  5.         if [ -z "$(which "$1")" ]; then
  6.                 echo "The required tool \"${1}\" was not found. Abort."
  7.                 exit 1
  8.         fi     
  9. }
  10.  
  11. SCRIPT_NAME="$(basename "$0")"
  12.  
  13. DISK_IMAGE="$1"
  14.  
  15. if [ -z "$DISK_IMAGE" ]; then
  16.         echo "Usage: $SCRIPT_NAME DISK_IMAGE"
  17.         exit 1
  18. fi
  19.  
  20. if [ "$(id -u)" != 0 ]; then
  21.         sudo "$0" "$@"
  22.         exit "$?"
  23. fi
  24.  
  25. check_tool "qemu-img"
  26. check_tool "kpartx"
  27.  
  28. TEMP_DIR="$(mktemp -d ${SCRIPT_NAME}.XXXXXXXX)"
  29.  
  30. qemu-img resize "$DISK_IMAGE" 486M
  31.  
  32. LOOP_DEVICE_NAME="$(kpartx -avs "$DISK_IMAGE" | grep -o 'loop[0-9]' | head -n 1)"
  33.  
  34. mkdir -p "$TEMP_DIR/installer"
  35. mkdir -p "$TEMP_DIR/disk-image/boot"
  36.  
  37. mount "/dev/mapper/${LOOP_DEVICE_NAME}p1" "$TEMP_DIR/disk-image/boot"
  38.  
  39. (cd "$TEMP_DIR/installer" && cpio -iv < ../"disk-image/boot/installer.cpio")
  40. (cd "$TEMP_DIR/installer" && sed -i '/^bootdev=/s|/dev/mmcblk0|/dev/sda|' "etc/init.d/rcS")
  41. (cd "$TEMP_DIR/installer" && sed -i '/^bootpartition=/s|/dev/mmcblk0p1|/dev/sda1|' "etc/init.d/rcS")
  42. (cd "$TEMP_DIR/installer" && sed -i '/^rootdev=/s|/dev/mmcblk0|/dev/sda|' "etc/init.d/rcS")
  43. (cd "$TEMP_DIR/installer" && sed -i '/^rootpartition=/s|$|/dev/sda2|' "etc/init.d/rcS")
  44. (cd "$TEMP_DIR/installer" && find . | cpio -H newc -ov > ../"installer-qemu.cpio")
  45. rm -rf "$TEMP_DIR/installer"
  46.  
  47. mv "$TEMP_DIR/installer-qemu.cpio" .
  48.  
  49. cat << EOF > "$TEMP_DIR/disk-image/boot/installer-config.txt"
  50. packages=whiptail,pppoeconf
  51. bootsize=+64M
  52. EOF
  53.  
  54. cat << EOF > "$TEMP_DIR/disk-image/boot/post-install.txt"
  55. sed -i 's|/dev/sda2|/dev/mmcblk0p2|' "/rootfs/boot/cmdline.txt"
  56. sed -i 's|/dev/sda1|/dev/mmcblk0p1|' "/rootfs/etc/fstab"
  57. sed -i 's|/dev/sda2|/dev/mmcblk0p2|' "/rootfs/etc/fstab"
  58. echo "gpu_mem=16" >> "/rootfs/boot/config.txt"
  59. EOF
  60.  
  61. sync
  62.  
  63. umount "$TEMP_DIR/disk-image/boot"
  64.  
  65. kpartx -d "$DISK_IMAGE" > "/dev/null"
  66.  
  67. rm -rf "$TEMP_DIR"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top