lay-down-os 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/bash
  2. set -e -x
  3. SCRIPTS_DIR=$(dirname ${0})
  4. . "${SCRIPTS_DIR}/build.conf"
  5. VERSION=${VERSION:?"VERSION not set"}
  6. while getopts "i:f:c:d:t:r:o:p:ka:" OPTION
  7. do
  8. case ${OPTION} in
  9. i) DIST="$OPTARG" ;;
  10. f) FILES="$OPTARG" ;;
  11. c) CLOUD_CONFIG="$OPTARG" ;;
  12. d) DEVICE="$OPTARG" ;;
  13. o) OEM="$OPTARG" ;;
  14. p) PARTITION="$OPTARG" ;;
  15. r) ROLLBACK_VERSION="$OPTARG" ;;
  16. k) KEXEC=y ;;
  17. a) APPEND="$OPTARG" ;;
  18. t) ENV="$OPTARG" ;;
  19. *) exit 1 ;;
  20. esac
  21. done
  22. [[ "$ARCH" == "arm" && "$ENV" != "rancher-upgrade" ]] && ENV=arm
  23. DIST=${DIST:-/dist}
  24. CLOUD_CONFIG=${CLOUD_CONFIG:-"${SCRIPTS_DIR}/conf/empty.yml"}
  25. CONSOLE=tty0
  26. BASE_DIR="/mnt/new_img"
  27. # TODO: Change this to a number so that users can specify.
  28. # Will need to make it so that our builds and packer APIs remain consistent.
  29. PARTITION=${PARTITION:=${DEVICE}1}
  30. device_defined()
  31. {
  32. if [[ -z "$1" ]]; then
  33. echo "Need to Pass a device name -d <dev>." 1>&2
  34. exit 1
  35. fi
  36. }
  37. format_device()
  38. {
  39. device_defined ${DEVICE}
  40. mkfs.ext4 -F -i 4096 -L RANCHER_STATE ${PARTITION}
  41. }
  42. mount_device()
  43. {
  44. local label=RANCHER_STATE
  45. local raw="${1:-false}"
  46. mkdir -p ${BASE_DIR}
  47. if [ "$(lsblk -o label|grep RANCHER_BOOT | wc -l)" -gt "0" ]; then
  48. label=RANCHER_BOOT
  49. fi
  50. local mount_opts="-L ${label}"
  51. if [ "${raw}" == "true" ]; then
  52. device_defined ${DEVICE}
  53. mount_opts=${PARTITION}
  54. fi
  55. mount ${mount_opts} ${BASE_DIR}
  56. trap "umount ${BASE_DIR}" EXIT
  57. }
  58. create_boot_dirs()
  59. {
  60. mkdir -p ${BASE_DIR}/boot/grub
  61. }
  62. install_grub() {
  63. grub-install --boot-directory=${BASE_DIR}/boot ${DEVICE}
  64. }
  65. grub2_config(){
  66. local grub_cfg=${BASE_DIR}/boot/grub/grub.cfg
  67. local append_line="${1}"
  68. cat >${grub_cfg} <<EOF
  69. set default="0"
  70. set timeout="1"
  71. #set fallback=1
  72. menuentry "RancherOS-current" {
  73. set root=(hd0,msdos1)
  74. linux /boot/vmlinuz-${VERSION}-rancheros ${append_line} console=${CONSOLE}
  75. initrd /boot/initrd-${VERSION}-rancheros
  76. }
  77. EOF
  78. if [ ! -z ${ROLLBACK_VERSION} ]; then
  79. sed -i 's/^#set/set/' ${grub_cfg}
  80. cat >>${grub_cfg} <<EOF
  81. menuentry "RancherOS-rollback" {
  82. set root=(hd0,msdos1)
  83. linux /boot/vmlinuz-${ROLLBACK_VERSION}-rancheros ${append_line} console=${CONSOLE}
  84. initrd /boot/initrd-${ROLLBACK_VERSION}-rancheros
  85. }
  86. EOF
  87. fi
  88. }
  89. install_rancher()
  90. {
  91. cp ${DIST}/initrd ${BASE_DIR}/boot/initrd-${VERSION}-rancheros
  92. cp ${DIST}/vmlinuz ${BASE_DIR}/boot/vmlinuz-${VERSION}-rancheros
  93. }
  94. pvgrub_config()
  95. {
  96. local grub_file=${BASE_DIR}/boot/grub/menu.lst
  97. local append_line="${1}"
  98. cat > ${grub_file}<<EOF
  99. default 0
  100. timeout 0
  101. #fallback 1
  102. hiddenmenu
  103. title RancherOS ${VERSION}-(current)
  104. root (hd0)
  105. kernel /boot/vmlinuz-${VERSION}-rancheros ${append_line} console=${CONSOLE}
  106. initrd /boot/initrd-${VERSION}-rancheros
  107. EOF
  108. if [ ! -z ${ROLLBACK_VERSION} ]; then
  109. sed -i 's/^#\(fallback\)/\1/' ${grub_file}
  110. cat >> ${grub_file}<<EOF
  111. title RancherOS ${ROLLBACK_VERSION}-(rollback)
  112. root (hd0)
  113. kernel /boot/vmlinuz-${ROLLBACK_VERSION}-rancheros ${append_line} console=${CONSOLE}
  114. initrd /boot/initrd-${ROLLBACK_VERSION}-rancheros
  115. EOF
  116. fi
  117. }
  118. format_and_mount()
  119. {
  120. format_device
  121. mount_device
  122. create_boot_dirs
  123. }
  124. KERNEL_ARGS=${KERNEL_ARGS:-""}
  125. if [ -n ${ENV} ]; then
  126. case ${ENV} in
  127. "generic")
  128. format_and_mount
  129. install_grub
  130. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  131. ;;
  132. "arm")
  133. format_and_mount
  134. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  135. ;;
  136. "amazon-ebs-pv"|"amazon-ebs-hvm")
  137. CONSOLE=ttyS0
  138. format_and_mount
  139. if [ "${ENV}" == "amazon-ebs-hvm" ]; then
  140. install_grub
  141. fi
  142. # AWS Networking recommends disabling.
  143. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  144. ;;
  145. "googlecompute")
  146. CONSOLE=ttyS0
  147. format_and_mount
  148. install_grub
  149. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  150. ;;
  151. "bootstrap")
  152. CONSOLE=ttyS0
  153. mount_device true
  154. create_boot_dirs
  155. KERNEL_ARGS="${KERNEL_ARGS} rancher.cloud_init.datasources=[ec2,gce]"
  156. ;;
  157. "rancher-upgrade")
  158. mount_device
  159. create_boot_dirs
  160. ;;
  161. *)
  162. echo "$ENV is not a valid environment" 1>&2
  163. exit 1
  164. ;;
  165. esac
  166. fi
  167. grub2_config "${KERNEL_ARGS}"
  168. pvgrub_config "${KERNEL_ARGS}"
  169. install_rancher
  170. if [ "$KEXEC" = "y" ]; then
  171. if [ "$APPEND" = "" ]; then
  172. APPEND=$(cat /proc/cmdline)
  173. fi
  174. kexec -l ${DIST}/vmlinuz --initrd=${DIST}/initrd --append="$APPEND" -f
  175. fi