Merge pull request #209 from CanonicalLtd/mwhudson/console-conf-no-ip-no-crash

fix crash on startup with no ip address
This commit is contained in:
Michael Hudson-Doyle 2017-03-29 11:07:07 +13:00 committed by GitHub
commit 30637be86d
3 changed files with 63 additions and 8 deletions

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -e
trap true HUP INT QUIT TSTP
@ -15,13 +15,43 @@ if [ "$(snap managed)" = "true" ]; then
if grep -qE '^[-a-z0-9+.-_]+:x:' /var/lib/extrausers/passwd && ! grep -qE '^[-a-z0-9+.-_]+:\$[0-9]+\$.*:' /var/lib/extrausers/shadow; then
tty=$(tty)
tty=$(echo ${tty#/dev/} | tr '/' '-')
if [ ! -f /run/console-conf/login-details-${tty}.txt ]; then
readargs=()
filepath="/run/console-conf/login-details-${tty}.txt"
if [ ! -f ${filepath} ]; then
mkdir -p /run/console-conf
/usr/share/subiquity/console-conf-write-login-details > /run/console-conf/login-details-${tty}.txt.tmp
mv /run/console-conf/login-details-${tty}.txt.tmp /run/console-conf/login-details-${tty}.txt
set +e
/usr/share/subiquity/console-conf-write-login-details > ${filepath}.tmp
rval=$?
set -e
# A exit code of 2 from console-conf-write-login-details
# means there are no scope global IP addresses. It will
# have printed a message saying that you can't log in
# until the device gets an IP address so we display that
# but check every 5 seconds if an ip address has appeared.
if [ $rval -eq 0 ]; then
mv ${filepath}.tmp ${filepath}
elif [ $rval -eq 2 ]; then
mv ${filepath}.tmp ${filepath}.noip
filepath=${filepath}.noip
readargs=(-t 5)
else
exit $rval
fi
cat /run/console-conf/login-details-${tty}.txt
read REPLY
fi
cat $filepath
set +e
while :; do
read "${readargs[@]}" REPLY
if [ $? -le 128 ]; then
# If we didn't time out, re-display everything.
exit 0
fi
if ip addr show | grep -qE "scope global"; then
# If we timed out, but it appears that we may now have
# an IP address, re-display everything.
exit 0
fi
done
else
touch /var/lib/console-conf/complete
fi

View File

@ -18,4 +18,8 @@ import sys
from console_conf.controllers.identity import write_login_details_standalone
from subiquitycore.log import setup_logger
setup_logger(dir='/var/log/console-conf')
sys.exit(write_login_details_standalone())

View File

@ -106,13 +106,27 @@ To login:
Personalize your account at https://login.ubuntu.com.
"""
login_details_tmpl_no_ip = """\
Ubuntu Core 16 on <no ip address> ({tty_name})
You cannot log in until the system has an IP address. (Is there
supposed to be a DHCP server running on your network?)
Personalize your account at https://login.ubuntu.com.
"""
def write_login_details(fp, username, ips):
sshcommands = "\n"
for ip in ips:
sshcommands += " ssh %s@%s\n"%(username, ip)
tty_name = os.ttyname(0)[5:] # strip off the /dev/
if len(ips) == 0:
fp.write(login_details_tmpl_no_ip.format(
sshcommands=sshcommands, tty_name=tty_name))
else:
first_ip = ips[0]
fp.write(login_details_tmpl.format(
sshcommands=sshcommands, host_key_info=host_key_info(), tty_name=tty_name, first_ip=ips[0]))
sshcommands=sshcommands, host_key_info=host_key_info(), tty_name=tty_name, first_ip=first_ip))
def write_login_details_standalone():
owner = get_device_owner()
@ -133,6 +147,13 @@ def write_login_details_standalone():
for _, addr in sorted(l.addresses.items()):
if addr.scope == "global":
ips.append(addr.ip)
if len(ips) == 0:
tty_name = os.ttyname(0)[5:]
print("Ubuntu Core 16 on <no ip address> ({})".format(tty_name))
print()
print("You cannot log in until the system has an IP address.")
print("(Is there supposed to be a DHCP server running on your network?)")
return 2
write_login_details(sys.stdout, owner['username'], ips)
return 0