Skip to content

Instantly share code, notes, and snippets.

@tarruda

tarruda/lxc-autostart.tcl

Last active Dec 14, 2015
Embed
What would you like to do?
Autostart unprivileged lxc containers
#!/usr/bin/expect
# Workaround to automatically start unprivileged containers on boot. This is
# required because unprivileged containers only seem to work when started from
# an interactive login session. Reference: https://github.com/lxc/lxc/issues/411#issuecomment-71414916)
#
# This script can be called from the user crontab with the @reboot
# directive(runs once at startup). One way to achieve this is to save this
# script to /home/USER/lxc-autostart.tcl and add the following entry to
# `crontab -e`:
#
# @reboot /home/USER/lxc-autostart.tcl
#
# Requires "expect" installed(sudo apt-get install expect)
# Only timeout after 5 minutes, which should be enough to start all containers.
set timeout 300
# Start an interactive ssh session connected to localhost. Assumes no password
# or confirmation prompts will be displayed, which can be achieved by
# previously connecting with an unlocked key.
set ssh_session {ssh localhost}
spawn {*}$ssh_session
send {
# Send the lxct-autostart command, which starts all unprivileged containers
# configured with lxc.start.auto = 1
lxc-autostart
# Exit the ssh session
exit
}
expect eof
@ak2766

This comment has been minimized.

Copy link

@ak2766 ak2766 commented Dec 14, 2015

I saw your post on askubuntu.com and followed your link. However, your solution did not work for me as I have a passphrase protected private key. Even with:

set ssh_session {ssh -i /dev/null localhost}

I was still prompted for a passphrase! I had to create a private key pair without a passphrase to make it work:

ssh-keygen -f /home/me/.ssh/localuseonlyprivkey
cat /home/me/.ssh/localuseonlyprivkey.pub >> /home/me/.ssh/authorized_keys

Then with the expect script, I had this:

set ssh_session {ssh -i /home/me/.ssh/localuseonlyprivkey localhost}

Thanks for your contribution.

ak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment