| 1 | #!/bin/shscip-shell filesystem . . `/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.oncescip-shell filesystem . . `/var/lib/cloud/sem/uncloud-init.once`. |
| 14 | ROOT_RW="" |
| 15 | |
| 16 | doexecscip-shell shell shell-project 0.0.0 doexec().() { |
| 17 | if [scip-shell system . . `[`. -n "$ROOT_RW" ]; then |
| 18 | mkdirscip-shell system . . mkdir. -p "${MARK%/*}" |
| 19 | datescip-shell system . . date. > "${MARK}" |
| 20 | fi |
| 21 | cleanupscip-shell shell shell-project 0.0.0 cleanup(). |
| 22 | logscip-shell shell shell-project 0.0.0 log(). "invoking /sbin/init $*" |
| 23 | exec /sbin/initscip-shell filesystem . . `/sbin/init`. "$@" |
| 24 | } |
| 25 | logscip-shell shell shell-project 0.0.0 log().() { echoscip-shell system . . echo. "::${0##*/}:" "$@"; } |
| 26 | cleanupscip-shell shell shell-project 0.0.0 cleanup().() { |
| 27 | [scip-shell system . . `[`. -z "${UMOUNT}" ] || { umountscip-shell system . . umount. "${UMOUNT}" && unset UMOUNT; } |
| 28 | [scip-shell system . . `[`. -z "${RMDIR}" ] || { rmscip-shell system . . rm. -Rf "${RMDIR}" && unset RMDIR; } |
| 29 | [scip-shell system . . `[`. -z "${ROOT_RW}" ] || { |
| 30 | mountscip-shell system . . mount. -o remount,ro /scip-shell filesystem . . `/`. |
| 31 | unset ROOT_RW |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | updateFromscip-shell shell shell-project 0.0.0 updateFrom().() { |
| 36 | local dev=$1 fmt=$2 |
| 37 | local mp="" |
| 38 | |
| 39 | [scip-shell system . . `[`. "${fmt}" = "tar" -o "${fmt}" = "mnt" ] || |
| 40 | { |
| 41 | logscip-shell shell shell-project 0.0.0 log(). FAIL "unknown format ${fmt}" |
| 42 | return 1 |
| 43 | } |
| 44 | |
| 45 | logscip-shell shell shell-project 0.0.0 log(). INFO "updating from ${dev} format ${fmt}" |
| 46 | [scip-shell system . . `[`. ! -e "${dev}" -a -e "/dev/${dev}" ] && dev="/dev/${dev}" |
| 47 | [scip-shell system . . `[`. -e "${dev}" ] || { |
| 48 | echoscip-shell system . . echo. "no file $dev" |
| 49 | return 2 |
| 50 | } |
| 51 | |
| 52 | mp=$(mktempscip-shell system . . mktemp. -d "${TEMPDIR:-/tmp}/update.XXXXXX") && |
| 53 | RMDIR="${mp}" || |
| 54 | { |
| 55 | logscip-shell shell shell-project 0.0.0 log(). FAIL "failed to mktemp" |
| 56 | return 1 |
| 57 | } |
| 58 | |
| 59 | if [scip-shell system . . `[`. "$fmt" = "tar" ]; then |
| 60 | ddscip-shell system . . dd. "if=${dev}" | (tarscip-shell system . . tar. -C "${mp}" -xf -) || |
| 61 | { |
| 62 | logscip-shell shell shell-project 0.0.0 log(). FAIL "failed to extract ${dev}" |
| 63 | return 1 |
| 64 | } |
| 65 | elif [scip-shell system . . `[`. "$fmt" = "mnt" ]; then |
| 66 | mountscip-shell system . . mount. -o ro "${dev}" "${mp}" && UMOUNT=${mp} || |
| 67 | { |
| 68 | logscip-shell shell shell-project 0.0.0 log(). FAIL "failed mount ${mp}" |
| 69 | return 1 |
| 70 | } |
| 71 | else |
| 72 | logscip-shell shell shell-project 0.0.0 log(). FAIL "unknown format ${fmt}" |
| 73 | return 1 |
| 74 | fi |
| 75 | |
| 76 | if [scip-shell system . . `[`. -d "${mp}/updates" ]; then |
| 77 | rsyncscip-shell system . . rsync. -av "${mp}/updates/" "/scip-shell filesystem . . `/`." || |
| 78 | { |
| 79 | logscip-shell shell shell-project 0.0.0 log(). FAIL "failed rsync updates/ /" |
| 80 | return 1 |
| 81 | } |
| 82 | fi |
| 83 | if [scip-shell system . . `[`. -f "${mp}/updates.tar" ]; then |
| 84 | tarscip-shell system . . tar. -C /scip-shell filesystem . . `/`. -xvf "${mp}/updates.tar" || |
| 85 | { |
| 86 | logscip-shell shell shell-project 0.0.0 log(). FAIL "failed tar -C / -xvf ${mp}/updates.tar" |
| 87 | return 1 |
| 88 | } |
| 89 | fi |
| 90 | script="${mp}/updates.script" |
| 91 | if [scip-shell system . . `[`. -f "${script}" -a -x "${script}" ]; then |
| 92 | MP_DIR=${mp} "${mp}/updates.script" || |
| 93 | { |
| 94 | logscip-shell shell shell-project 0.0.0 log(). FAIL "failed to run updates.script" |
| 95 | return 1 |
| 96 | } |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | failscip-shell shell shell-project 0.0.0 fail().() { |
| 101 | { [scip-shell system . . `[`. $# -eq 0 ] && logscip-shell shell shell-project 0.0.0 log(). "FAILING"; } || logscip-shell shell shell-project 0.0.0 log(). "$@" |
| 102 | exit 1 |
| 103 | } |
| 104 | |
| 105 | [scip-shell system . . `[`. -s "$MARK" ] && { |
| 106 | logscip-shell shell shell-project 0.0.0 log(). "already updated" |
| 107 | doexecscip-shell shell shell-project 0.0.0 doexec(). "$@" |
| 108 | } |
| 109 | |
| 110 | mountscip-shell system . . mount. -o remount,rw /scip-shell filesystem . . `/`. || failscip-shell shell shell-project 0.0.0 fail(). "failed to mount rw" |
| 111 | ROOT_RW=1 |
| 112 | |
| 113 | if [scip-shell system . . `[`. ! -e /proc/cmdlinescip-shell filesystem . . `/proc/cmdline`. ]; then |
| 114 | mountscip-shell system . . mount. -t proc /procscip-shell filesystem . . `/proc`. /procscip-shell filesystem . . `/proc`. |
| 115 | read cmdline < /proc/cmdlinescip-shell filesystem . . `/proc/cmdline`. |
| 116 | umountscip-shell system . . umount. /procscip-shell filesystem . . `/proc`. |
| 117 | else |
| 118 | read cmdline < /proc/cmdlinescip-shell filesystem . . `/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 | [scip-shell system . . `[`. "${dev}" = "${val}" ] && fmt="" || fmt=${val#${dev}:} |
| 129 | logscip-shell shell shell-project 0.0.0 log(). "update from ${dev},${fmt}" |
| 130 | updateFromscip-shell shell shell-project 0.0.0 updateFrom(). "${dev}" "${fmt}" || failscip-shell shell shell-project 0.0.0 fail(). "update failed" |
| 131 | logscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. "${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 | logscip-shell shell shell-project 0.0.0 log(). "setting ubuntu pass = ${ubuntu_pass}" |
| 143 | printfscip-shell system . . printf. "\n===\nubuntu_pass = %s\n===\n" "${ubuntu_pass}" > /dev/ttyS0scip-shell filesystem . . `/dev/ttyS0`. |
| 144 | fi |
| 145 | |
| 146 | [scip-shell system . . `[`. -z "${ubuntu_pass}" ] || |
| 147 | printfscip-shell system . . printf. "ubuntu:%s\n" "${ubuntu_pass}" > /root/ubuntu-user-passscip-shell filesystem . . `/root/ubuntu-user-pass`. |
| 148 | |
| 149 | if [scip-shell system . . `[`. -e /root/ubuntu-user-passscip-shell filesystem . . `/root/ubuntu-user-pass`. ]; then |
| 150 | logscip-shell shell shell-project 0.0.0 log(). "changing ubuntu user's password!" |
| 151 | chpasswdscip-shell system . . chpasswd. < /root/ubuntu-user-passscip-shell filesystem . . `/root/ubuntu-user-pass`. || |
| 152 | logscip-shell shell shell-project 0.0.0 log(). "FAIL: failed changing pass" |
| 153 | fi |
| 154 | |
| 155 | cpscip-shell system . . cp. /etc/init/tty2.confscip-shell filesystem . . `/etc/init/tty2.conf`. /etc/init/ttyS0.confscip-shell filesystem . . `/etc/init/ttyS0.conf`. && |
| 156 | sedscip-shell system . . sed. -i s,tty2,ttyS0,g /etc/init/ttyS0.confscip-shell filesystem . . `/etc/init/ttyS0.conf`. 2> /dev/nullscip-shell filesystem . . `/dev/null`. && |
| 157 | logscip-shell shell shell-project 0.0.0 log(). "enabled console on ttyS0" |
| 158 | |
| 159 | pa=PasswordAuthentication |
| 160 | sedscip-shell system . . sed. -i "s,${pa} no,${pa} yes," /etc/ssh/sshd_configscip-shell filesystem . . `/etc/ssh/sshd_config`. 2> /dev/nullscip-shell filesystem . . `/dev/null`. && |
| 161 | logscip-shell shell shell-project 0.0.0 log(). "enabled passwd auth in ssh" || |
| 162 | logscip-shell shell shell-project 0.0.0 log(). "failed to enable passwd ssh" |
| 163 | |
| 164 | grepscip-shell system . . grep. -q vga16fb /etc/modprobe.d/blacklist.confscip-shell filesystem . . `/etc/modprobe.d/blacklist.conf`. || { |
| 165 | echoscip-shell system . . echo. "blacklist vga16fb" >> /etc/modprobe.d/blacklist.confscip-shell filesystem . . `/etc/modprobe.d/blacklist.conf`. && |
| 166 | logscip-shell shell shell-project 0.0.0 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 | doexecscip-shell shell shell-project 0.0.0 doexec(). "$@" |