packet.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. set -e
  3. TINKERBELL_URL=$(cat /proc/cmdline | sed -e 's/^.*tinkerbell=//' -e 's/ .*$//')/phone-home
  4. INSTALLER_IMAGE=rancher/os:v0.7.0-rc3
  5. tinkerbell_post()
  6. {
  7. system-docker run rancher/curl -X POST -H "Content-Type: application/json" -d "{\"type\":\"provisioning.$1\",\"body\":\"$2\"}" ${TINKERBELL_URL}
  8. }
  9. tinkerbell_post 104 "Connected to magic install system"
  10. DEV_PREFIX=/dev/sd
  11. if [ -e /dev/vda ]; then
  12. DEV_PREFIX=/dev/vd
  13. fi
  14. BOOT=${DEV_PREFIX}a1
  15. BOOT_TYPE=83
  16. SWAP=${DEV_PREFIX}a5
  17. SWAP_TYPE=82
  18. OEM=${DEV_PREFIX}a6
  19. OEM_TYPE=83
  20. ROOT=${DEV_PREFIX}a7
  21. ROOT_TYPE=83
  22. RAID=false
  23. wait_for_dev()
  24. {
  25. for DEV; do
  26. for ((i=0;i<10;i++)); do
  27. if [ ! -e $DEV ]; then
  28. partprobe || true
  29. sleep 1
  30. else
  31. break
  32. fi
  33. done
  34. done
  35. }
  36. modprobe md || true
  37. dd if=/dev/zero of=${DEV_PREFIX}a bs=1M count=1
  38. if [ -e ${DEV_PREFIX}b ]; then
  39. dd if=/dev/zero of=${DEV_PREFIX}b bs=1M count=1
  40. RAID=true
  41. BOOT=/dev/md1
  42. BOOT_TYPE=fd
  43. SWAP=/dev/md5
  44. SWAP_TYPE=fd
  45. OEM=/dev/md6
  46. OEM_TYPE=fd
  47. ROOT=/dev/md7
  48. ROOT_TYPE=fd
  49. fi
  50. # Partition BOOT
  51. echo -e "n\np\n1\n\n+2G\nt\n${BOOT_TYPE}\nw" | fdisk ${DEV_PREFIX}a || true
  52. partprobe || true
  53. # Partition Extended
  54. echo -e "n\ne\n\n\n\nw" | fdisk ${DEV_PREFIX}a || true
  55. partprobe || true
  56. # Partition SWAP
  57. echo -e "n\nl\n\n+2G\n\nt\n5\n${SWAP_TYPE}\nw" | fdisk ${DEV_PREFIX}a || true
  58. partprobe || true
  59. # Partition OEM
  60. echo -e "n\nl\n\n+100M\n\nt\n6\n${OEM_TYPE}\nw" | fdisk ${DEV_PREFIX}a || true
  61. partprobe || true
  62. # Partition ROOT
  63. echo -e "n\nl\n\n\n\nt\n7\n${ROOT_TYPE}\nw" | fdisk ${DEV_PREFIX}a || true
  64. partprobe || true
  65. # Make boot active
  66. echo -e "a\n1\nw" | fdisk ${DEV_PREFIX}a || true
  67. partprobe || true
  68. if [ "$RAID" = "true" ]; then
  69. sfdisk --dump ${DEV_PREFIX}a | sfdisk --no-reread ${DEV_PREFIX}b
  70. wait_for_dev ${DEV_PREFIX}b1 ${DEV_PREFIX}b5 ${DEV_PREFIX}b6 ${DEV_PREFIX}b7 ${DEV_PREFIX}a1 ${DEV_PREFIX}a5 ${DEV_PREFIX}a6 ${DEV_PREFIX}a7
  71. mdadm --create $BOOT --level=1 --metadata=1.0 --raid-devices=2 ${DEV_PREFIX}a1 ${DEV_PREFIX}b1
  72. mdadm --create $SWAP --level=1 --metadata=1.2 --raid-devices=2 ${DEV_PREFIX}a5 ${DEV_PREFIX}b5
  73. mdadm --create $OEM --level=1 --metadata=1.2 --raid-devices=2 ${DEV_PREFIX}a6 ${DEV_PREFIX}b6
  74. mdadm --create $ROOT --level=1 --metadata=1.2 --raid-devices=2 ${DEV_PREFIX}a7 ${DEV_PREFIX}b7
  75. fi
  76. mkswap -L RANCHER_SWAP $SWAP
  77. system-docker run --privileged --entrypoint /bin/bash ${INSTALLER_IMAGE} -c "mkfs.ext4 -L RANCHER_BOOT $BOOT"
  78. system-docker run --privileged --entrypoint /bin/bash ${INSTALLER_IMAGE} -c "mkfs.ext4 -L RANCHER_STATE $ROOT"
  79. system-docker run --privileged --entrypoint /bin/bash ${INSTALLER_IMAGE} -c "mkfs.ext4 -L RANCHER_OEM $OEM"
  80. tinkerbell_post 105 "Server partitions created"
  81. mkdir -p /mnt/oem
  82. mount $(ros dev LABEL=RANCHER_OEM) /mnt/oem
  83. cat > /mnt/oem/oem-config.yml << EOF
  84. #cloud-config
  85. mounts:
  86. - [ LABEL=RANCHER_SWAP, "", swap, "" ]
  87. EOF
  88. umount /mnt/oem
  89. tinkerbell_post 106 "OEM drive configured"
  90. METADATA=$(system-docker run rancher/curl -sL https://metadata.packet.net/metadata)
  91. eval $(echo ${METADATA} | jq -r '.network.addresses[] | select(.address_family == 4 and .public) | "ADDRESS=\(.address)/\(.cidr)\nGATEWAY=\(.gateway)"')
  92. eval $(echo ${METADATA} | jq -r '.network.interfaces[0] | "MAC=\(.mac)"')
  93. NETWORK_ARGS="rancher.network.interfaces.bond0.address=$ADDRESS rancher.network.interfaces.bond0.gateway=$GATEWAY rancher.network.interfaces.mac:${MAC}.bond=bond0"
  94. tinkerbell_post 107 "Network interface configuration fetched from metadata"
  95. COMMON_ARGS="console=ttyS1,115200n8 rancher.autologin=ttyS1 rancher.cloud_init.datasources=[packet] ${NETWORK_ARGS}"
  96. if [ "$RAID" = "true" ]; then
  97. ros install -f -t raid -i ${INSTALLER_IMAGE} -d ${DEV_PREFIX}a -a "rancher.state.mdadm_scan ${COMMON_ARGS}" --no-reboot
  98. else
  99. ros install -f -t noformat -i ${INSTALLER_IMAGE} -d ${DEV_PREFIX}a -a "${COMMON_ARGS}" --no-reboot
  100. fi
  101. tinkerbell_post 108 "Installation finished, rebooting server"
  102. reboot