Advertisement
Guest User

Untitled

a guest
May 27th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4. set -u
  5.  
  6. usage() {
  7. echo "Usage: $0 DEVICE IMAGE HOSTNAME"
  8. echo "Example: $0 /dev/mmcblkX ArchLinuxARM-rpi-2-latest.tar.gz myrpi"
  9. exit 1
  10. }
  11.  
  12. [ $# -ne 3 ] && { usage; }
  13.  
  14. DEVICE="$1"
  15. IMAGE="$2"
  16. HOSTNAME="$3"
  17.  
  18. if [ ! -e "$DEVICE" ]; then
  19. echo "Device $DEVICE not found!"
  20. echo "Give a valid device (e.g. /dev/sdX)"
  21. exit 2
  22. fi
  23.  
  24. echo "Flashing device $DEVICE and setting hostname to $HOSTNAME"
  25.  
  26. TMP_DIR=$(mktemp -d)
  27. echo "Temp directory: $TMP_DIR"
  28. cd "$TMP_DIR"
  29.  
  30. cleanup() {
  31. echo "Cleaning up $TMP_DIR"
  32. rm -rf "$TMP_DIR"
  33. }
  34.  
  35. trap cleanup 0
  36.  
  37. echo "Creating partitions"
  38. echo "o
  39. p
  40. n
  41. p
  42. 1
  43.  
  44. +100M
  45. p
  46. t
  47. c
  48. n
  49. p
  50. 2
  51.  
  52.  
  53. p
  54. w
  55. w
  56. " | fdisk "$DEVICE"
  57.  
  58. echo "Creating boot filesystem"
  59. mkfs.vfat "$DEVICE"p1
  60. mkdir boot
  61. mount "$DEVICE"p1 boot
  62.  
  63. echo "Creating root filesystem"
  64. mkfs.ext4 "$DEVICE"p2
  65. mkdir root
  66. mount "$DEVICE"p2 root
  67.  
  68. echo "Copying base image"
  69. tar -xf "$IMAGE" -C root
  70. sync
  71.  
  72. echo "Copying boot files to boot partition"
  73. mv root/boot/* boot
  74.  
  75. echo "Setting hostname to $HOSTNAME"
  76. echo "$HOSTNAME" > root/etc/hostname
  77.  
  78. echo "Unmounting partitions"
  79. sync
  80. umount boot root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement