Extracting a “golden” image from the current state of the OS¶
In many applications, it’s beneficial to prepare the operating system with pre-installed packages and specific configurations. To enable reproducibility, flashing to other devices, and sharing, creating a size-optimized replica is essential. This section explores creating such a “golden” image by extracting the current operating system state.
Considerations
Creating an image with basic tools like dd
can result in a large, inefficient image file. This is because the filesystem is often much larger than necessary. This large size limits installations on devices with smaller storage capacity. This guide aims to create an optimized, minimal copy of the OS state.
The generation and validation of the golden image must be performed carefully to avoid issues.
Prerequisites
A USB flash drive with sufficient storage (8 GB is usually enough; check disk utilization with
df -h
).A Linux-based PC or VM (this guide assumes Ubuntu 22.04) with administrative privileges. These operations should be adaptable to other OSs, but this guide won’t cover all cases.
A gateway with the desired OS state and console access.
Administrative privileges on the device.
An OS with the
cl-deploy
utility installed (check withwhich cl-deploy
).
Known Limitations
If you have multiple kernels, verify the boot order after the procedure. Disabling unused kernels and checking the GRUB configuration is recommended.
Warning
Thoroughly test each image before production use.
Use a reliable USB flash drive. Poor-quality devices can cause data corruption.
The USB flash drive’s contents will be erased. Back up any important data.
Steps
Plug a sufficiently large USB flash drive into the device (use the fastest USB interface). Zeroing out the drive’s contents is recommended but not mandatory.
Log in as root
$ sudo su
Use
lsblk
to identify the USB drive. Typically,mmcblk*
devices are internal eMMC. On Debian-based gateways, the USB drive (if only one is connected) usually appears assda
$ lsblk
...
Run
cl-deploy
to copy the filesystem
$ cl-deploy
Select the destination device using the Spacebar and press Enter (after highlighting OK)

Confirm data destruction on the destination device by pressing Enter (after highlighting Yes)

After copying,
cl-deploy
prompts for a reboot. Press Enter (after highlighting No)

Shut down the system
$ shutdown now
Unplug the USB flash drive.
Proceed to image extraction (unoptimized or optimized).
Extracting an Unoptimized Image
This can also be done on Windows (using Win32DiskImager) or macOS (using dd
).
Connect the USB flash drive to your Linux PC or VM. If using a VM, pass through the USB drive and enable port forwarding for port 22.
Create a working directory
$ mkdir image && cd image
Use
lsblk
to identify the USB drive (e.g.,/dev/sdb
)
$ lsblk
...
Use
dd
to create an image replica (ensure sufficient disk space)
$ sudo dd if=/dev/sdb of=image.img bs=1M status=progress conv=fsync
image.img
is the unoptimized image. Note its large size.Safely unmount and eject the USB flash drive.
Extracting an Optimized Image
Connect the USB flash drive (see unoptimized instructions).
Create a working directory (see unoptimized instructions).
Install
gparted
$ sudo apt install -y gparted
Identify the USB drive with
lsblk
(see unoptimized instructions).Start
gparted
$ sudo gparted /dev/sdb
Unmount all partitions in
gparted
.Resize the
rootfs
partition, minimizing used space



Close
gparted
.Determine the last block and block size using
fdisk
$ sudo fdisk -l /dev/sdb
...
Calculate the total block count (including GPT sectors).
Use
dd
with the correct block size and count:$ sudo dd if=/dev/sdb of=image.img bs=512 count=6645793 status=progress conv=fsync
Restore the GPT table using
gdisk
$ sudo gdisk image.img
... (gdisk commands)
image.img
is now the optimized image.
Preparing the Image for Sharing
Rename the image using a standard convention
$ mv image.img cthingsco-gwlight-debian-bullseye-base-20221226.img
Calculate and store the SHA-256 checksum
$ sha256sum cthingsco-gwlight-debian-bullseye-base-20221226.img
...
Install
zip
$ sudo apt install -y zip
Compress the image
$ zip -9 cthingsco-gwlight-debian-bullseye-base-20221226.zip cthingsco-gwlight-debian-bullseye-base-20221226.img
The
zip
file is ready. Validate the image thoroughly before production use.