package playne

imports "programmer"

Mounting a folder from the host inside an LXC container

sharing folder with host on LXC

1. Create profile (makes life easier)

Take this script (a slightly modified version from here), save it and make it executable and save it.

#!/bin/bash 
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 
KEY=$HOME/.ssh/id_lxd_$USER 
PUBKEY=$KEY.pub 
AUTHORIZED_KEYS=$HOME/.ssh/authorized_keys 
[ -f $PUBKEY ] || ssh-keygen -f $KEY -N '' -C "key for local lxds" 
grep "$(cat $PUBKEY)" $AUTHORIZED_KEYS -qs || cat $PUBKEY >> $AUTHORIZED_KEYS 
 
# 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 
config: 
  # 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: | 
    #cloud-config 
    users: 
      - name: $USER 
        groups: sudo 
        shell: $SHELL 
        sudo: ['ALL=(ALL) NOPASSWD:ALL'] 
    # ensure users shell is installed 
    packages: 
      - $(dpkg -S $(readlink -m $SHELL) | cut -d: -f1) 
EOF

So – this will take care of mapping your UIDs GIDs so you can edit the files in your mount point to your newly added user

2. Create your container

lxc launch ubuntu:18.04 <YOUR-CONTAINER> -p $USER -p default

3. Add you shared folder as a device and mount it

lxc config device add YOUR-CONTAINER YOUR-DEVICE-NAME disk source=/home/<user>/<projects-folder> path=/home/<user>/<projects-folder>

Congratulations – you now have a shiny Ubuntu 18.04 container to do your dev work in.

You  might want to jump in and install some important stuff

lxc exec YOUR-CONTAINER /bin/bash
$ apt install build-essential ....

Posted

in

by

Tags: