Manually set a built image as cached?

Currently if you launch an image from remote, the cached bit and update_source is set:

# lxc image list --format json | jq -r .[0]
{
  "auto_update": true,
  "properties": {
    "architecture": "amd64",
    "description": "ubuntu 22.04 LTS amd64 (release) (20220506)",
    "label": "release",
    "os": "ubuntu",
    "release": "jammy",
    "serial": "20220506",
    "type": "squashfs",
    "version": "22.04"
  },
  "public": false,
  "expires_at": "2027-04-21T00:00:00Z",
  "profiles": [
    "default"
  ],
  "aliases": [],
  "architecture": "x86_64",
  "cached": true,
  "filename": "ubuntu-22.04-server-cloudimg-amd64-lxd.tar.xz",
  "fingerprint": "dda8ea8622eab3df7f71b274a436ee972610efc85d66e0fee14bdb3b4d492072",
  "size": 419463580,
  "update_source": {
    "alias": "jammy",
    "certificate": "",
    "protocol": "simplestreams",
    "server": "https://cloud-images.ubuntu.com/releases",
    "image_type": ""
  },
  "type": "container",
  "created_at": "2022-05-06T00:00:00Z",
  "last_used_at": "2022-05-11T09:21:55.855062894Z",
  "uploaded_at": "2022-05-11T09:21:53.832923079Z"
}

When you build an image, obviously those two fields are not set:

# lxc image list --format json | jq -r '.[] | select((.aliases | length) > 0) | select(.aliases[0].name | contains("test"))'
{
  "auto_update": false,
  "properties": {
    "architecture": "x86_64",
    "description": "Ubuntu 22.04 LTS server (20220506)",
    "os": "ubuntu",
    "release": "jammy"
  },
  "public": false,
  "expires_at": "0001-01-01T00:00:00Z",
  "profiles": [
    "default"
  ],
  "aliases": [
    {
      "name": "some/test",
      "description": ""
    }
  ],
  "architecture": "x86_64",
  "cached": false,
  "filename": "",
  "fingerprint": "b3be360d8237e22a0391ff04447f456a43e3ce18b823ee775310d562eebbc6dc",
  "size": 466438908,
  "type": "container",
  "created_at": "2022-05-11T10:25:37.409638232Z",
  "last_used_at": "2022-05-11T10:30:28.815326346Z",
  "uploaded_at": "2022-05-11T10:25:37.430059168Z"
}

You’ve uploaded this image to your registry (same alias) and use it later on the same machine using lxc launch registry-1:some/test test2. Since LXD sees that this exact image already exists locally, it does not download the image, but it also doesn’t set the cached and update_source field. Which is a good default, since a created image should never automatically expire.

Now I want this image to expire according to images.remote_cache_expiry setting. I could delete this image right after uploading, but at the cost that it has to be downloaded again at the first use. This costs time and bandwidth.

Wouldn’t it be useful to set those fields in an easy way? Since I’m sure I’m not the only one who doesn’t want stale images laying around and never get deleted.

I’ll look into the raw lxd commands, hopefully this allows me to set those fields.

A workaround could be filtering images regularly by last_used_at field, but then I’m kinda making a duplicate to LXD’s own cache expiry feature.

I’ve reported Add easier `set`ting of image properties (e.g. description) · Issue #7450 · lxc/lxd · GitHub a good while ago, got implemented, but it only allows setting of the properties map, the cached and update_source field are separate.

Yeah, those aren’t editable properties. You can certainly cheat your way around it through lxd sql global to edit the DB entry and set cached to 1 though.

1 Like

For the sake of completeness if anyone is finding this in the future:

$ lxc image info 717 | grep -E "(Fingerprint|Cached)"
Fingerprint: 71706d0a517fa2231111f439d5c2f9963324bd287bbad4de3c17cfa1f600b0d0
Cached: no
$ lxd sql global 'UPDATE images SET cached=1 WHERE fingerprint="71706d0a517fa2231111f439d5c2f9963324bd287bbad4de3c17cfa1f600b0d0"'
Rows affected: 1
$ lxc image info 717 | grep -E "(Fingerprint|Cached)"
Fingerprint: 71706d0a517fa2231111f439d5c2f9963324bd287bbad4de3c17cfa1f600b0d0
Cached: yes

Bear in mind that this is not a field intended to be manipulated like that, use with caution!

Oh, and there’s also an --expire argument for various image commands, e.g. for publish: Image expiration on Publish by masnax · Pull Request #8831 · lxc/lxd · GitHub