uncloud-init 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/bin/sh
  2. # This file is part of cloud-init. See LICENSE file for license information.
  3. # This script is meant to "kvmify" an image. Its not meant to be
  4. # terribly robust, or a good idea to ever run in a "real image".
  5. # its' only intended method of invocation is from the kernel as 'init'
  6. # in which case it will then invoke /sbin/init after it is done
  7. # init=/path/to/kvmify-init
  8. KEY="xupdate"
  9. UMOUNT=""
  10. RMDIR=""
  11. MARK=/var/lib/cloud/sem/uncloud-init.once
  12. ROOT_RW=""
  13. doexec() {
  14. if [ -n "$ROOT_RW" ]; then
  15. mkdir -p "${MARK%/*}";
  16. date > "${MARK}";
  17. fi
  18. cleanup;
  19. log "invoking /sbin/init $*"
  20. exec /sbin/init "$@";
  21. }
  22. log() { echo "::${0##*/}:" "$@"; }
  23. cleanup() {
  24. [ -z "${UMOUNT}" ] || { umount "${UMOUNT}" && unset UMOUNT; }
  25. [ -z "${RMDIR}" ] || { rm -Rf "${RMDIR}" && unset RMDIR; }
  26. [ -z "${ROOT_RW}" ] || { mount -o remount,ro / ; unset ROOT_RW; }
  27. }
  28. updateFrom() {
  29. local dev=$1 fmt=$2
  30. local mp="";
  31. [ "${fmt}" = "tar" -o "${fmt}" = "mnt" ] ||
  32. { log FAIL "unknown format ${fmt}"; return 1; }
  33. log INFO "updating from ${dev} format ${fmt}"
  34. [ ! -e "${dev}" -a -e "/dev/${dev}" ] && dev="/dev/${dev}"
  35. [ -e "${dev}" ] || { echo "no file $dev"; return 2; }
  36. mp=$(mktemp -d "${TEMPDIR:-/tmp}/update.XXXXXX") &&
  37. RMDIR="${mp}" ||
  38. { log FAIL "failed to mktemp"; return 1; }
  39. if [ "$fmt" = "tar" ]; then
  40. dd "if=${dev}" | ( tar -C "${mp}" -xf - ) ||
  41. { log FAIL "failed to extract ${dev}"; return 1; }
  42. elif [ "$fmt" = "mnt" ]; then
  43. mount -o ro "${dev}" "${mp}" && UMOUNT=${mp} ||
  44. { log FAIL "failed mount ${mp}"; return 1; }
  45. else
  46. log FAIL "unknown format ${fmt}"; return 1;
  47. fi
  48. if [ -d "${mp}/updates" ]; then
  49. rsync -av "${mp}/updates/" "/" ||
  50. { log FAIL "failed rsync updates/ /"; return 1; }
  51. fi
  52. if [ -f "${mp}/updates.tar" ]; then
  53. tar -C / -xvf "${mp}/updates.tar" ||
  54. { log FAIL "failed tar -C / -xvf ${mp}/updates.tar"; return 1; }
  55. fi
  56. script="${mp}/updates.script"
  57. if [ -f "${script}" -a -x "${script}" ]; then
  58. MP_DIR=${mp} "${mp}/updates.script" ||
  59. { log FAIL "failed to run updates.script"; return 1; }
  60. fi
  61. }
  62. fail() { { [ $# -eq 0 ] && log "FAILING" ; } || log "$@"; exit 1; }
  63. [ -s "$MARK" ] && { log "already updated" ; doexec "$@"; }
  64. mount -o remount,rw / || fail "failed to mount rw"
  65. ROOT_RW=1
  66. if [ ! -e /proc/cmdline ]; then
  67. mount -t proc /proc /proc
  68. read cmdline < /proc/cmdline
  69. umount /proc
  70. else
  71. read cmdline < /proc/cmdline
  72. fi
  73. ubuntu_pass=""
  74. for x in ${cmdline}; do
  75. case "$x" in
  76. ${KEY}=*)
  77. val=${x#${KEY}=}
  78. dev=${val%:*}
  79. [ "${dev}" = "${val}" ] && fmt="" || fmt=${val#${dev}:}
  80. log "update from ${dev},${fmt}"
  81. updateFrom "${dev}" "${fmt}" || fail "update failed"
  82. log "end update ${dev},${fmt}"
  83. ;;
  84. ubuntu-pass=*|ubuntu_pass=*) ubuntu_pass=${x#*=};;
  85. helpmount) helpmount=1;;
  86. root=*) rootspec=${x#root=};;
  87. esac
  88. done
  89. if [ "${ubuntu_pass}" = "R" -o "${ubuntu_pass}" = "random" ]; then
  90. ubuntu_pass=$(python -c 'import string, random;
  91. random.seed(); print "".join(random.sample(string.letters+string.digits, 8))')
  92. log "settting ubuntu pass = ${ubuntu_pass}"
  93. printf "\n===\nubuntu_pass = %s\n===\n" "${ubuntu_pass}" >/dev/ttyS0
  94. fi
  95. [ -z "${ubuntu_pass}" ] ||
  96. printf "ubuntu:%s\n" "${ubuntu_pass}" > /root/ubuntu-user-pass
  97. if [ -e /root/ubuntu-user-pass ]; then
  98. log "changing ubuntu user's password!"
  99. chpasswd < /root/ubuntu-user-pass ||
  100. log "FAIL: failed changing pass"
  101. fi
  102. cp /etc/init/tty2.conf /etc/init/ttyS0.conf &&
  103. sed -i s,tty2,ttyS0,g /etc/init/ttyS0.conf 2>/dev/null &&
  104. log "enabled console on ttyS0"
  105. pa=PasswordAuthentication
  106. sed -i "s,${pa} no,${pa} yes," /etc/ssh/sshd_config 2>/dev/null &&
  107. log "enabled passwd auth in ssh" ||
  108. log "failed to enable passwd ssh"
  109. grep -q vga16fb /etc/modprobe.d/blacklist.conf || {
  110. echo "blacklist vga16fb" >> /etc/modprobe.d/blacklist.conf &&
  111. log "blacklisted vga16fb"
  112. }
  113. #lstr="${rootspec}"
  114. #if ! grep -q "^${lstr}[[:space:]]" /etc/fstab; then
  115. # log "changing / in /etc/ftab to agree with cmdline (${lstr}) (bug 509841)"
  116. # sed -i "s,^\([^[[:space:]#]\+\)\([[:space:]]\+\)/\([[:space:]]\+\),${lstr}\2/\3," /etc/fstab
  117. #fi
  118. doexec "$@"
  119. # vi: ts=4 noexpandtab