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:
commit
30637be86d
|
@ -1,4 +1,4 @@
|
||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
trap true HUP INT QUIT TSTP
|
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
|
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=$(tty)
|
||||||
tty=$(echo ${tty#/dev/} | tr '/' '-')
|
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
|
mkdir -p /run/console-conf
|
||||||
/usr/share/subiquity/console-conf-write-login-details > /run/console-conf/login-details-${tty}.txt.tmp
|
set +e
|
||||||
mv /run/console-conf/login-details-${tty}.txt.tmp /run/console-conf/login-details-${tty}.txt
|
/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
|
fi
|
||||||
cat /run/console-conf/login-details-${tty}.txt
|
fi
|
||||||
read REPLY
|
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
|
else
|
||||||
touch /var/lib/console-conf/complete
|
touch /var/lib/console-conf/complete
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -18,4 +18,8 @@ import sys
|
||||||
|
|
||||||
from console_conf.controllers.identity import write_login_details_standalone
|
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())
|
sys.exit(write_login_details_standalone())
|
||||||
|
|
|
@ -106,13 +106,27 @@ To login:
|
||||||
Personalize your account at https://login.ubuntu.com.
|
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):
|
def write_login_details(fp, username, ips):
|
||||||
sshcommands = "\n"
|
sshcommands = "\n"
|
||||||
for ip in ips:
|
for ip in ips:
|
||||||
sshcommands += " ssh %s@%s\n"%(username, ip)
|
sshcommands += " ssh %s@%s\n"%(username, ip)
|
||||||
tty_name = os.ttyname(0)[5:] # strip off the /dev/
|
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(
|
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():
|
def write_login_details_standalone():
|
||||||
owner = get_device_owner()
|
owner = get_device_owner()
|
||||||
|
@ -133,6 +147,13 @@ def write_login_details_standalone():
|
||||||
for _, addr in sorted(l.addresses.items()):
|
for _, addr in sorted(l.addresses.items()):
|
||||||
if addr.scope == "global":
|
if addr.scope == "global":
|
||||||
ips.append(addr.ip)
|
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)
|
write_login_details(sys.stdout, owner['username'], ips)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue