lay-down-os 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #!/bin/bash
  2. set -e -x
  3. SCRIPTS_DIR=$(dirname ${0})
  4. VERSION=${VERSION:?"VERSION not set"}
  5. while getopts "i:f:c:d:t:r:o:p:ka:" OPTION
  6. do
  7. case ${OPTION} in
  8. i) DIST="$OPTARG" ;;
  9. f) FILES="$OPTARG" ;;
  10. c) CLOUD_CONFIG="$OPTARG" ;;
  11. d) DEVICE="$OPTARG" ;;
  12. o) OEM="$OPTARG" ;;
  13. p) PARTITION="$OPTARG" ;;
  14. r) ROLLBACK_VERSION="$OPTARG" ;;
  15. k) KEXEC=y ;;
  16. a) APPEND="$OPTARG" ;;
  17. t) ENV="$OPTARG" ;;
  18. *) exit 1 ;;
  19. esac
  20. done
  21. [[ "$ARCH" == "arm" && "$ENV" != "rancher-upgrade" ]] && ENV=arm
  22. DIST=${DIST:-/dist}
  23. CLOUD_CONFIG=${CLOUD_CONFIG:-"${SCRIPTS_DIR}/conf/empty.yml"}
  24. CONSOLE=tty0
  25. BASE_DIR="/mnt/new_img"
  26. BOOT=boot/
  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. KERNEL_ARGS="rancher.state.dev=LABEL=RANCHER_STATE rancher.state.wait console=${CONSOLE}"
  31. device_defined()
  32. {
  33. if [[ -z "$1" ]]; then
  34. echo "Need to Pass a device name -d <dev>." 1>&2
  35. exit 1
  36. fi
  37. }
  38. format_device()
  39. {
  40. device_defined ${DEVICE}
  41. mkfs.ext4 -F -i 4096 -L RANCHER_STATE ${PARTITION}
  42. }
  43. get_dev()
  44. {
  45. if [ -z "$(which ros)" ]; then
  46. lsblk -n -o label $1
  47. else
  48. ros dev LABEL=${1}
  49. fi
  50. }
  51. mount_device()
  52. {
  53. LABEL=RANCHER_STATE
  54. local raw="${1:-false}"
  55. mkdir -p ${BASE_DIR}
  56. if [ -n "$(get_dev RANCHER_BOOT)" ]; then
  57. LABEL=RANCHER_BOOT
  58. BOOT=
  59. fi
  60. local mount_opts="-L ${LABEL}"
  61. if [ "${raw}" == "true" ]; then
  62. device_defined ${DEVICE}
  63. mount_opts=${PARTITION}
  64. fi
  65. mount ${mount_opts} ${BASE_DIR}
  66. trap "umount ${BASE_DIR}" EXIT
  67. }
  68. create_boot_dirs()
  69. {
  70. mkdir -p ${BASE_DIR}/${BOOT}grub
  71. mkdir -p ${BASE_DIR}/${BOOT}syslinux
  72. }
  73. install_syslinux() {
  74. dd bs=440 count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=${DEVICE}
  75. cp /usr/lib/syslinux/modules/bios/* ${BASE_DIR}/${BOOT}syslinux
  76. extlinux --install ${BASE_DIR}/${BOOT}syslinux
  77. }
  78. install_syslinux_raid() {
  79. dd bs=440 count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sda
  80. dd bs=440 count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sdb
  81. cp /usr/lib/syslinux/modules/bios/* ${BASE_DIR}/${BOOT}syslinux
  82. extlinux --install --raid ${BASE_DIR}/${BOOT}syslinux
  83. }
  84. install_grub() {
  85. grub-install --boot-directory=${BASE_DIR}/boot ${DEVICE}
  86. }
  87. grub2_config(){
  88. local grub_cfg=${BASE_DIR}/${BOOT}grub/grub.cfg
  89. local append_line="${1}"
  90. cat >${grub_cfg} <<EOF
  91. set default="0"
  92. set timeout="1"
  93. #set fallback=1
  94. menuentry "RancherOS-current" {
  95. set root=(hd0,msdos1)
  96. linux /${BOOT}vmlinuz-${VERSION}-rancheros ${KERNEL_ARGS} ${append_line}
  97. initrd /${BOOT}initrd-${VERSION}-rancheros
  98. }
  99. EOF
  100. if [ ! -z ${ROLLBACK_VERSION} ]; then
  101. sed -i 's/^#set/set/' ${grub_cfg}
  102. cat >>${grub_cfg} <<EOF
  103. menuentry "RancherOS-rollback" {
  104. set root=(hd0,msdos1)
  105. linux /${BOOT}vmlinuz-${ROLLBACK_VERSION}-rancheros ${KERNEL_ARGS} ${append_line}
  106. initrd /${BOOT}initrd-${ROLLBACK_VERSION}-rancheros
  107. }
  108. EOF
  109. fi
  110. }
  111. syslinux_config(){
  112. local syslinux_cfg=${BASE_DIR}/${BOOT}syslinux/syslinux.cfg
  113. local append_line="${1}"
  114. cat >${syslinux_cfg} <<EOF
  115. DEFAULT RancherOS-current
  116. LABEL RancherOS-current
  117. LINUX ../vmlinuz-${VERSION}-rancheros
  118. APPEND ${KERNEL_ARGS} ${append_line}
  119. INITRD ../initrd-${VERSION}-rancheros
  120. EOF
  121. if [ ! -z ${ROLLBACK_VERSION} ]; then
  122. cat >>${syslinux_cfg} <<EOF
  123. LABEL RancherOS-rollback
  124. LINUX ../vmlinuz-${ROLLBACK_VERSION}-rancheros
  125. APPEND ${KERNEL_ARGS} ${append_line}
  126. INITRD ../initrd-${ROLLBACK_VERSION}-rancheros
  127. EOF
  128. fi
  129. }
  130. install_rancher()
  131. {
  132. cp ${DIST}/initrd ${BASE_DIR}/${BOOT}initrd-${VERSION}-rancheros
  133. cp ${DIST}/vmlinuz ${BASE_DIR}/${BOOT}vmlinuz-${VERSION}-rancheros
  134. }
  135. pvgrub_config()
  136. {
  137. local grub_file=${BASE_DIR}/${BOOT}grub/menu.lst
  138. local append_line="${1}"
  139. cat > ${grub_file}<<EOF
  140. default 0
  141. timeout 0
  142. #fallback 1
  143. hiddenmenu
  144. title RancherOS ${VERSION}-(current)
  145. root (hd0)
  146. kernel /${BOOT}vmlinuz-${VERSION}-rancheros ${KERNEL_ARGS} ${append_line}
  147. initrd /${BOOT}initrd-${VERSION}-rancheros
  148. EOF
  149. if [ ! -z ${ROLLBACK_VERSION} ]; then
  150. sed -i 's/^#\(fallback\)/\1/' ${grub_file}
  151. cat >> ${grub_file}<<EOF
  152. title RancherOS ${ROLLBACK_VERSION}-(rollback)
  153. root (hd0)
  154. kernel /${BOOT}/vmlinuz-${ROLLBACK_VERSION}-rancheros ${KERNEL_ARGS} ${append_line}
  155. initrd /${BOOT}initrd-${ROLLBACK_VERSION}-rancheros
  156. EOF
  157. fi
  158. }
  159. format_and_mount()
  160. {
  161. format_device
  162. mount_device
  163. create_boot_dirs
  164. }
  165. if [ -n ${ENV} ]; then
  166. case ${ENV} in
  167. "generic")
  168. format_and_mount
  169. install_grub
  170. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  171. ;;
  172. "arm")
  173. format_and_mount
  174. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  175. ;;
  176. "amazon-ebs-pv"|"amazon-ebs-hvm")
  177. CONSOLE=ttyS0
  178. format_and_mount
  179. if [ "${ENV}" == "amazon-ebs-hvm" ]; then
  180. install_grub
  181. fi
  182. # AWS Networking recommends disabling.
  183. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  184. ;;
  185. "googlecompute")
  186. CONSOLE=ttyS0
  187. format_and_mount
  188. install_grub
  189. "${SCRIPTS_DIR}/seed-data" ${BASE_DIR} ${CLOUD_CONFIG} ${FILES}
  190. ;;
  191. "noformat")
  192. mount_device
  193. create_boot_dirs
  194. install_syslinux
  195. ;;
  196. "raid")
  197. mount_device
  198. create_boot_dirs
  199. install_syslinux_raid
  200. ;;
  201. "bootstrap")
  202. CONSOLE=ttyS0
  203. mount_device true
  204. create_boot_dirs
  205. KERNEL_ARGS="${KERNEL_ARGS} rancher.cloud_init.datasources=[ec2,gce]"
  206. ;;
  207. "rancher-upgrade")
  208. mount_device
  209. create_boot_dirs
  210. ;;
  211. *)
  212. echo "$ENV is not a valid environment" 1>&2
  213. exit 1
  214. ;;
  215. esac
  216. fi
  217. if [ -e ${BASE_DIR}/${BOOT}append ]; then
  218. PRESERVED_APPEND=$(cat ${BASE_DIR}/${BOOT}append)
  219. fi
  220. if [ "${APPEND}" = "" ]; then
  221. APPEND="${PRESERVED_APPEND}"
  222. fi
  223. echo "${APPEND}" > ${BASE_DIR}/${BOOT}append
  224. grub2_config "${APPEND}"
  225. syslinux_config "${APPEND}"
  226. pvgrub_config "${APPEND}"
  227. install_rancher
  228. seusers=${BASE_DIR}/etc/selinux/ros/seusers
  229. failsafe_context=${BASE_DIR}/etc/selinux/ros/contexts/failsafe_context
  230. if [ -f "${seusers}" ]; then
  231. echo "__default__:unconfined_u:s0-s0:c0.c1023" > ${seusers}
  232. fi
  233. if [ -f "${failsafe_context}" ]; then
  234. echo "unconfined_r:unconfined_t:s0" > ${failsafe_context}
  235. fi
  236. if [ "$KEXEC" = "y" ]; then
  237. kexec -l ${DIST}/vmlinuz --initrd=${DIST}/initrd --append="${KERNEL_ARGS} ${APPEND}" -f
  238. fi