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