1

I'd like to implement one of the answers on How to make sure, that certain functionality (like Alt+Ctrl+F1 switching into tty) is available in havily swapping system?, and for that I'll need a way to login a user on a virtual console. Is it possible in a non-interactive manner at all?

Adam Ryczkowski
  • 4,403
  • 9
  • 40
  • 65

1 Answers1

1

Here is a not fully working solution. Please - if you have some time - correct the problem.

Step 1. Create a file /etc/init/autologtty.conf

# Get the user to guess the number. If they get it right, let them
# login.

start on runlevel [23]
stop on runlevel [!23]

env VLOCK_PRESENT=1
env VLOCK_CURRENT_MESSAGE=
env TTY=tty9

respawn
respawn limit 10 5


# XXX: Ensure job is connected to the terminal device
console output

script
    [ -f /etc/default/autologtty ] && . /etc/default/autologtty

    # XXX: Ensure all standard streams are connected to the console
    exec 0</dev/$TTY >/dev/$TTY 2>&1
    clear
    if [ "$DEBUG" -eq 1 ]; then
        set -x 
    fi
    dpkg -s vlock>/dev/null
    if [ $? -ne 0 ]; then
        VLOCK_PRESENT=0
    fi  

    trap '' INT TERM HUP #Prevent Ctrl+C

    rcfile=`mktemp`

    if [ "$VLOCK_PRESENT" -eq "1" ]; then
        echo "[ -f ~/.bashrc ] && . ~/.bashrc">>$rcfile
        if [ -n "$VLOCK_CURRENT_MESSAGE" ]; then
            echo "echo \"$VLOCK_CURRENT_MESSAGE\"" >>$rcfile
            vlock=/usr/sbin/vlock-main
        else
            vlock=vlock
        fi
        echo $vlock >>$rcfile
        if [ -n "$USER" ]; then
            chown $USER $rcfile
            sudo -u $USER /bin/bash --rcfile $rcfile && stop
        else
            bash -l --rcfile $rcfile && stop
        fi
    else
        if [ -n "$USER" ]; then
            su -l $USER -c "bash -l" && stop
        else
            "bash -l" && stop
        fi
    fi
#   exec /sbin/getty -8 38400 $TTY
end script

Step 2. Create a file /etc/default/autologtty

VLOCK_PRESENT=1
USER=adam
VLOCK_CURRENT_MESSAGE="Welcome user $USER! Please hit <ENTER> and enter your password to unlock the tty"
TTY=tty9
DEBUG=0

Make sure that the file is only root-writable, because all commands will be executed by root.

  1. Under the specified tty (9 by default) there will be a working shell. If you have vlock installed, there will be first a question about a password.

You can test it by sudo service autologtty restart

Well, this would work, if I was able to run the fully working bash. If I only get answer to the question https://unix.stackexchange.com/questions/166326/why-does-sudo-su-l-user-c-bash-fails-to-load-users-environment the script will be ready.


EDIT:

The upstart script work ok now.

Adam Ryczkowski
  • 4,403
  • 9
  • 40
  • 65