3

I have an operating system installation ISO. My goal is to run the second operating system on top of the kernel of Ubuntu host running LXD. I cannot find any base image for the said operating system. Is it possible for me to build a custom lxc image to run on top of ubuntu kernel and access the shell of the second system?

Thanks in advanced.

John Mayer
  • 31
  • 1
  • 1
  • 3
  • I am not totally clear on what exactly you are trying to achieve here - could you expand on your use case a little, for example by explaining what the sequence of events should be? I am struggling a little to understand what you mean precisely by `second operating system` - on the face of it, that sounds like you need (para)virtualisation, rather than containers. For building LXC base images, have you tried `lxc-create`,?Ubuntu's [LXC overview](https://help.ubuntu.com/lts/serverguide/lxc.html) gives some instructions – iwaseatenbyagrue Apr 10 '17 at 16:23

2 Answers2

2

Stéphane Graber described the procedure for creating images for LXD 2.0 and above from scratch in the article LXD 2.0: Image management (section "Manually building an image"):

Manually building an image

Building your own image is also pretty simple.

  1. Generate a container filesystem. This entirely depends on the distribution you’re using. For Ubuntu and Debian, it would be by using debootstrap.
  2. Configure anything that’s needed for the distribution to work properly in a container (if anything is needed).
  3. Make a tarball of that container filesystem, optionally compress it.
  4. Write a new metadata.yaml file based on the one described above.
  5. Create another tarball containing that metadata.yaml file.
  6. Import those two tarballs as a LXD image with:

    lxc image import <metadata tarball> <rootfs tarball> --alias some-name
    

You will probably need to go through this a few times before everything works, tweaking things here and there, possibly adding some templates and properties.

The file metadata.yaml must contain at least the following two key/value pairs (replace i686 with the correct system architecture and 1458040200 with the timestamp in Unix epoch format (e.g. date +%s):

architecture: "i686"
creation_date: 1458040200

Further information about the metadata.yaml can be found in the abovementioned article (section "Image metadata").

Ansgar Wiechers
  • 4,247
  • 2
  • 18
  • 26
  • 1
    And where do I find doco that describes ` tweaking things here and there, possibly adding some templates and properties.` ? I can't seem to find anything that tells me how to wedge my templates in there somehow. – tink Mar 18 '19 at 22:35
  • Which part of *"Further information about the `metadata.yaml` can be found in the abovementioned article (section "Image metadata")"* requires further explanation? Click the link in my answer. Go to the section "Image metadata". Scroll down a bit until you get to the sub-section "Templates". Start reading. – Ansgar Wiechers Mar 18 '19 at 23:27
  • 1
    Heh. Ta. I have read that page at least ten times in the last 7 days; doesn't mention anywhere how one ties templates into the tar-ball before importing it into lxd ... did more experimentation though, and figured it out eventually. `lxd` doco is notoriously errrrrh `terse`, `incomplete` :D – tink Mar 19 '19 at 00:36
  • The parent section of "Image metadata" ("Image formats") features a link to [this little gem](https://github.com/lxc/lxd/blob/master/doc/image-handling.md). – Ansgar Wiechers Mar 19 '19 at 08:53
0

Building custom lxd images can be done in 2022 with Infrastructure as Code principles using Packer & it's lxd plugin.

In the top level of my $PACKER_CONFIG_DIR I configured the required plugins in plugins.pkr.hcl & installed them with packer init .

In my ~/.bashrc I set:

export PACKER_CONFIG_DIR=~/path/to/config/dir
export PACKER_CACHE_DIR=/path/to/cache/on/tmpfs

Looking at this Terraform repo which uses packer to build the lxd images was helpful (the packer-plugin-lxd repo shows a json builder block) In packer 1.7.0+ you should use HCL format:

# $PACKER_CONFIG_DIR/lxd/ubuntu-2204-base.pkr.hcl
# packer build .

source "lxd" "ubuntu-2204-base" {
  image        = "images:ubuntu/22.04/amd64"
  output_image = "ubuntu-2204-base"
  publish_properties = {
    description = "Base image for Ubuntu 22.04"
  }
}

build {
  sources = ["source.lxd.ubuntu-2204-base"]

  provisioner "shell" {
    inline = [
      "apt-get install -q -y nftables iptables",
    ]
  }
}

The above info was enough to generate local LXD images:

+------------------+--------------+--------+-------------------------------------+--------------+-----------+----------+-------------------------------+
|      ALIAS       | FINGERPRINT  | PUBLIC |             DESCRIPTION             | ARCHITECTURE |   TYPE    |   SIZE   |          UPLOAD DATE          |
+------------------+--------------+--------+-------------------------------------+--------------+-----------+----------+-------------------------------+
| ubuntu-2204-base | 0e9576339070 | no     | Base image for Ubuntu 22.04         | x86_64       | CONTAINER | 198.49MB | Mar 21, 2022 at 4:06pm (UTC)  |
Stuart Cardall
  • 610
  • 5
  • 8