lay-down-os 7.2 KB

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