Instantly share code, notes, and snippets.

What would you like to do?
set -eu
_UID=$(id -u)
GID=$(id -g)
# give lxd permission to map your user/group id through
grep root:$_UID:1 /etc/subuid -qs || sudo usermod --add-subuids ${_UID}-${_UID} --add-subgids ${GID}-${GID} root
# set up a separate key to make sure we can log in automatically via ssh
# with $HOME mounted
[ -f $PUBKEY ] || ssh-keygen -f $KEY -N '' -C "key for local lxds"
# create a profile to control this, name it after $USER
lxc profile create $USER &> /dev/null || true
# configure profile
# this will rewrite the whole profile
cat << EOF | lxc profile edit $USER
name: $USER
description: allow home dir mounting for $USER
# this part maps uid/gid on the host to the same on the container
raw.idmap: |
uid $_UID 1000
gid $GID 1000
# note: user.user-data is still available
user.vendor-data: |
- name: $USER
groups: sudo
shell: $SHELL
# ensure users shell is installed
- $(dpkg -S $(readlink -m $SHELL) | cut -d: -f1)
# this section adds your \$HOME directory into the container. This is useful for vim, bash and ssh config, and such like.
type: disk
source: $HOME
path: $HOME
# to launch a container using this profile:
# lxc launch ubuntu: -p default -p $USER
# to add an additional bind mount
# lxc config device add <container> <device name> disk source=/path/on/host path=path/in/container

This comment has been minimized.

asi594 commented Aug 26, 2016

Using this script to help me grok LXD, it helps immensely.

small thing, line 23, $PROFILE should be $USER?


This comment has been minimized.


bloodearnest commented Oct 11, 2016

Hi folks

@asi594 thanks, have renamed $PROFILE to $USER, was from an older version

@bmullen never tried with sound. Which application to you want to use inside the lxd?

I suspect the best option for most apps is sharing the host's pulseaudio, a la

You may also need to expose all of /dev/snd/, perhaps. I would experiment with a privileged container first, as the user perms might be tricky for the unprivileged example above.


This comment has been minimized.


bloodearnest commented Feb 3, 2017

Have updated to support newer idmap syntax, which gets rid of the messy shell maths.


This comment has been minimized.

yen3 commented Mar 1, 2017

Thank you. It's very useful to help solve my problem.


This comment has been minimized.

argearriojas commented Mar 21, 2017

Hi @bloodearnest,

Your script has been very useful, I tested it and it works great.

However, the values provided to the raw.idmap entry need to be like this:

raw.idmap: |
uid $_UID 1000
gid $GID 1000

Or perhaps could be useful to find a way to set a custom set of uid/gid to the user in the container.


This comment has been minimized.

aaylnx commented Mar 29, 2017

This looks very interesting. Is there any way you could provide a little more information on what exactly the script does and how to use it? It just shares your home directory with LXD/LXC containers right? How does it need to be run and with what variables changes? Doesn't look like just a matter of downloading and running. Thanks!


This comment has been minimized.


bloodearnest commented May 25, 2017

@argearriojas yeah my bad, sorry. Usually, my uid is 1000 on the host, so it worked. Unfortuately, cloud init doesn't support setting uid in trusty. So have applied your change, uid is fixed at 1000 in the container.

Ideally, I'd like to use the same uid as on the host, but that proved tricky and overly complex.

@aaylnx: just run the script as your user, no extra params needed, it will create a lxd profile named after your user. Then follow the comments at the end of the script.


This comment has been minimized.

andresignacio commented Oct 19, 2017

@bloodearnest you are a godsend... i've been trying to resolve the nobody:nobody ownership of mounted folders in my lxc containers and this has really solved my headache! a big THANK YOU!

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