LXC container for lab environment.

Power of the containers (mainly based on docker) has been proved many, many times. But what in the scenario you have very, very limited resources in your virtual lab and lets say, small debian installation with very few GB of RAM and 1 CPU. With LXC you can use this resources in very convenient and efficient way, to build few separated environment that will serve services for you like DNS, DHCP or web server to provide easy file access in your VMware lab environment. All done in a way you like, as LXC provides full Linux OS in those separated environment. For every of them you can use apt to install and then configure application whatever you like.

I have found such very nice to use, especially with VMware environment, as such one VM (with many containers) can be with little configuration change cloned for many environments and adjusted in a way that is needed – in that sense, it can be duplicated and used several times without huge time effort.

At home lab, my favorite distribution to use is Debian, and so this configuration example is based on it.

  • Download and install Debian. I have found that net installation iso is very nice approach as it speeds up task to create USB bootable drive (350MB). Also installation time is short as Debian installer needs to download only small amount of packages (choose just CLI environment and SSH service, no GUI like gnome we do not really need)
  • Hopefully installed system has working IP address (DHCP). At this point, do not modify the network, just use it download necessary packages plus your favorite one.
    Packages you will need for Debian 11: apt-get install lxc libvirt0 libpam-cgfs bridge-utils uidmap libvirt-clients libvirt-daemon-system iptables ebtables dnsmasq-base libxml2-utils iproute2 — not necessary all needed, but better to be on safe site.
  • Lets configure bridge interface for communication containers with the word.
    Use ip a command to check network interfaces name (eno0 in this example)
    then edit:
    /etc/network/interfaces
    and set the file accordingly:
l@host:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

#auto eno0
#iface eno0 inet static
#	address 192.168.1.19
#	netmask 255.255.255.0
#	gateway 192.168.1.1

auto br0
iface br0 inet static
      bridge_ports eno0
      bridge_fd 0
      address 192.168.1.19
      netmask 255.255.255.0
      network 192.168.1.0
      broadcast 192.168.1.255
      gateway 192.168.1.1
      # dns-* options are implemented by the resolvconf package, if installed
      #dns-nameservers 8.8.8.8 1.1.1.1
      #dns-search your.search.domain.here

Change /etc/default/lxc-net to disable lxc bridge as we don’t really need it:

#USE_LXC_BRIDGE="true"
USE_LXC_BRIDGE="false"

LXC can create own bridge to use simply use NAT for containers.

Also edit /etc/lxc/default.conf, with the following:

cat /etc/lxc/default.conf 
lxc.net.0.type = veth
#lxc.net.0.link = lxcbr0
lxc.net.0.link = virbr0
lxc.net.0.flags = up

lxc.apparmor.profile = generated
lxc.apparmor.allow_nesting = 1

Restart network (/etc/init.d/networking restart). While you are doing that, it would be good to have console access to VM, there are many changes and it is likely that network won’t work at the first try. It would be good also to restart whole server and verify how it is working after.

Verify if bridge has created with command ip a
Try to ping gateway, other networks and verify also if br0 interface is reachable.

  • create first container and configure it:
lxc-create -n lxdeb1 -t debian -- -r stretch
lxc-start -n lxdeb1
lxc-attach lxdeb1  //check if you have access and if it is working fine; verify the network
cat /etc/network/interfaces
auto lo
iface lo inet loopback

#auto eth0
#iface eth0 inet dhcp

network/interfaces we are changing inside the container as we don’t really need that configuration, network interface will be configured by the container configuration (see below)

lxc-stop lxdeb1

Reconfigure container with our new br0 interface, file: /var/lib/lxc/lxdeb1/config: //modify lxdeb1 with yours container name.

## Network
lxc.uts.name = lxdeb1
lxc.net.0.type = veth
lxc.net.0.flags = up

## Network
lxc.utsname = containershostname
lxc.network.type = veth
lxc.network.flags = up

# that's the interface defined above in host's interfaces file
lxc.network.link = br0

# name of network device inside the container,
# defaults to eth0, you could choose a name freely
# lxc.network.name = lxcnet0 

lxc.network.hwaddr = 00:FF:AA:00:00:01

# the ip may be set to 0.0.0.0/24 or skip this line
# if you like to use a dhcp client inside the container
lxc.network.ipv4 = 192.168.1.110/24

# define a gateway to have access to the internet
lxc.network.ipv4.gateway = 192.168.1.1

lxc.apparmor.profile = generated
lxc.apparmor.allow_nesting = 1
lxc.rootfs.path = dir:/var/lib/lxc/unfiy/rootfs

# Common configuration
lxc.include = /usr/share/lxc/config/debian.common.conf

# Container specific configuration
lxc.tty.max = 4
lxc.uts.name = unfiy
lxc.arch = amd64
lxc.pty.max = 1024

Above example from: https://wiki.debian.org/LXC/SimpleBridge#Host_device_as_bridge

Update config for our container

sudo lxc-update-config -c /var/lib/lxc/lxdeb1/config

If not error here, you can start container, attach to it and verify your network configuration, verify if container is reachable from your lab network.

That’s it. Now you can create few more containers and use it in a way you need in your lab.

Other necessary command:

sudo lxc-info lxdeb1

lxc-autostart lxdeb1

If autostart won’t work for you as expected (didn’t work for me) then just add to the container config file the following: lxc.start.auto = 1

Images list can be found here: https://uk.lxd.images.canonical.com/

Hope it was helpful, comments are very welcome.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Free(BSD)
yt-dlp – download your data from youtube

Hi, this short article only to mention that there is a wonderful application that exists for a years for the purpose to download YOUR or maybe some other movies from youtube. I don’t want to go in to legal aspect too much, what can be downloaded from youtube, what not …

Linux
Migrate WordPress site to another hosting service.

IntroductionThis article details the migration of WordPress site (exactly this site you are now on) from one service provider to Amazon Lightsail. There might be various reason to do that (mine is outlined below) but in general I hope to share the message that especially with WordPress, migration can be …

VMware
VMware Workstation and Fusion can be installed and use for free (even for the enterprise)

For a while now, the VMware Workstation (and Fusion for MacOS) can be used without any additional fee for Personal use. That was a great Broadcom news and nice gesture from that software vendor. Recently Broadcom announced that the software will be available for all, even the commercial sector. This …