Shell script to update all lxd container hypervisor

A simple shell script wrapper to update all lxd container hypervisor images eitehr running Debian or Ubuntu Linux.

#!/bin/bash
# A simple shell script to update all lxd container hypervisor
# URL: https://bash.cyberciti.biz/virtualization/shell-script-to-update-all-lxd-container-hypervisor/
# Tested on : Ubuntu 16.04 LTS lxd server 
# Tested on : Ubuntu/Debian lxd container hypervisor only 
# ----------------------------------------------------------------------------
# Author: nixCraft 
# Copyright: 2016 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated 14 Aug 2016
# ----------------------------------------------------------------------------
# Set full path to bins 
_apt="/usr/bin/apt-get"
_lxc="/usr/bin/lxc"
_awk="/usr/bin/awk"
 
# Get containers list
clist="$(${_lxc} list -c ns | ${_awk} '!/NAME/{ if ( $4 == "RUNNING" ) print $2}')"
 
# Use bash for loop and update all container hypervisor powered by Debian or Ubuntu
# NOTE: for CentOS use yum command instead of apt-get
for c in $clist
do
	echo "Updating Debian/Ubuntu container hypervisor \"$c\"..."
	${_lxc} exec $c ${_apt} -- -qq update
	${_lxc} exec $c ${_apt} -- -qq -y upgrade
	${_lxc} exec $c ${_apt} -- -qq -y clean
	${_lxc} exec $c ${_apt} -- -qq -y autoclean
done

Run script as follows:
$ /path/to/update.lxd.vms
Sample outputs:

Updating Debian/Ubuntu container hypervisor "wwwapp1"...
Updating Debian/Ubuntu container hypervisor "msyqldb1"...
Updating Debian/Ubuntu container hypervisor "memcache1"...
Updating Debian/Ubuntu container hypervisor "nginx1"...
Updating Debian/Ubuntu container hypervisor "apachephp1"...
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • poxin Dec 10, 2016 @ 23:45

    Cheers for this, really didn’t feel like writing one :)

  • deepthought Aug 21, 2016 @ 9:00

    Better and automated approach would be to install and configure unattended-upgrades on the containers

    • 🐧 Vivek Gite Aug 21, 2016 @ 20:05

      You can just call this script from a host with cron and all containers will get updated:

      @daily /path/to/this/lxc-update.sh
  • KEM Aug 14, 2016 @ 12:38

    Thank you for publishing this, very helpful. For those of us learning bash scripts, could you break down this line? I think I understand what it does, but having the explanation would be great.
    Thanks
    clist="$(${_lxc} list -c ns | ${_awk} '!/NAME/{ if ( $4 == "RUNNING" ) print $2}')"

    • 🐧 Vivek Gite Aug 20, 2016 @ 9:03

      1. Find list of running container lxc with list -c ns
      2. Use awk to print a list of container which are running but exclude header NAME with awk command

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.