I’m currently busy with a project in my home lab that requires a machine on my test LAN to be issuing IP addresses. I figured I’d use an LXC container on my Ubuntu Desktop because it’s lightweight and a convenient way to contain DHCP server software without having to install it on my main OS.
The LXC container needs to interact with real machines outside of the Desktop on which it lives, so using the “normal” LXD bridge wouldn’t work. See: getting started with LXD.
I’ll first need to create the VLAN interface and then attach or launch the lxc container making sure it comes up inside of the correct network.
I’ll create a VLAN named VLAN50 ontop of enp0s25 using nmcli
dvnt@icecream:~$ sudo nmcli connection add con-name VLAN50 ifname VLAN50 type vlan dev enp0s25 id 50 autoconnect yes dvnt@icecream:~$ nmcli con NAME UUID TYPE DEVICE VLAN50 2baf554f-dcde-42e5-bb3a-71091108ca25 vlan VLAN50 Wired connection 1 550aa29b-1e3d-3ec8-bdf9-f434dcde98f2 ethernet enp0s25
Next I’ll copy the default lxc profile to a new profile named lxcVLAN50 and modify it so that the container nictype is set to a macvlan as well as set the parent to the relevant VLAN interface
dvnt@icecream:~$ lxc profile copy default lxcVLAN50 dvnt@icecream:~$ lxc profile device set lxcVLAN50 eth0 nictype macvlan dvnt@icecream:~$ lxc profile device set lxcVLAN50 eth0 parent VLAN50
Results:
dvnt@icecream:~$ lxc profile list +-------------+---------+ | NAME | USED BY | +-------------+---------+ | default | 2 | +-------------+---------+ | lxcVLAN50 | 0 | +-------------+---------+ dvnt@icecream:~$ lxc profile show lxcVLAN50 config: {} description: Default LXD profile devices: eth0: name: eth0 nictype: macvlan parent: VLAN50 type: nic root: path: / pool: default type: disk name: lxcVLAN50 used_by: []
Now all that’s left to do is launch the container using a -p flag to select the profile
dvnt@icecream:~$ lxc launch ubuntu:19.04 disco-fruitsalad -p lxcVLAN50 Creating disco-fruitsalad Starting disco-fruitsalad dvnt@icecream:~$ lxc list +------------------+---------+-------------------------+------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +------------------+---------+-------------------------+------+------------+-----------+ | cheddarfritters | STOPPED | | | PERSISTENT | | +------------------+---------+-------------------------+------+------------+-----------+ | cheesecake | STOPPED | | | PERSISTENT | | +------------------+---------+-------------------------+------+------------+-----------+ | disco-fruitsalad | RUNNING | | | PERSISTENT | | +------------------+---------+-------------------------+------+------------+-----------+