Get guest OS flavor from host?

I’m using the following script to update all my containers:

# Set full path to bins
_apt="/usr/bin/apt"
_lxc="/snap/bin/lxc"
_awk="/usr/bin/awk"

# Get containers list
clist="$(${_lxc} list -c ns | ${_awk} '!/NAME/{ if ( $4 == "RUNNING" ) print $2}')"

function update_container() {
    container=$1
    echo "[Info] Updating $container"
    ${_lxc} exec $container ${_apt} -- update
    ${_lxc} exec $container ${_apt} -- -y full-upgrade
    ${_lxc} exec $container ${_apt} -- -y clean
    ${_lxc} exec $container ${_apt} -- -y autoclean
}

for c in $clist
do
  update_container $c
done

This worked great when all my containers were running ubuntu, but now I have some arch containers as well.

Is there a command I can use to if-else depending on the OS of the guest? I’d like to do something like:

for c in $clist
do
  if <something>
  then
    update_ubuntu_container $c
  else
    update_arch_container $c
  fi
done

You can ask for package management

for c in $clist
do
  if which apt
    then 
       update_ubuntu_container $c
    else
       update_arch_container $c
  fi
done

And add function with arch pacman update, I remember something like:
sudo pacman -syu # update y full upgrade
sudo pacman -Sc # clean cache

# Set full path to bins
_apt="/usr/bin/apt"
_pacman="/usr/bin/pacman"
_lxc="/snap/bin/lxc"
_awk="/usr/bin/awk"

# Get containers list
clist="$(${_lxc} list -c ns | ${_awk} '!/NAME/{ if ( $4 == "RUNNING" ) print $2}')"

function update_container() {
    container=$1
    echo "[Info] Updating $container"
    if ${_lxc} exec $container which apt
    then 
      ${_lxc} exec $container ${_apt} -- update
      ${_lxc} exec $container ${_apt} -- -y full-upgrade
      ${_lxc} exec $container ${_apt} -- -y clean
      ${_lxc} exec $container ${_apt} -- -y autoclean
    else
      ${_lxc} exec $container ${_pacman} -- -syyu
      ${_lxc} exec $container ${_pacman} -- -Sc
    fi
    
}

for c in $clist
do
  update_container $c
done

That’s brilliant, thanks!

You could also use lxc config get <instance> <key> to inspect the instance’s source image name, e.g.:

lxc config get v1 image.os
Ubuntu

lxc config get v1 image.description
Ubuntu focal amd64 (20210415_07:42)