packet.sh 4.4 KB

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