A few years back, we had the problem of upgrading the OS of a fleet of systems which did not have a spare root partition.
These were autonomous machines geographically scattered around the world on very slow networks (sort of like ATMs).
The upgrade process was required to retain and restore certain amount of state from the old install to the new install (think machine identity, private keys, certificates etc.)
We ended up creating a system for im-memory reimaging of the entire hard disk.
Let’s understand this process in greater detail.
Setup the init script
Init is the first process called after kernel boots up.
We add modules for hard disk access, filesystem and a few other dependencies.
We start two terminals on tty2 and tty3 to allow diagnosis in case of errors.
Stage 1: Copy new image and files to be retained to RAM.
#!/bin/shset-eecho"Starting upgrade..."
mount /dev/sda5 /sysroot
echo"Mounted disk. Copying image to memory."cp /sysroot/rhel7.img.bz2 /
rm-rf /retentionlist ||true
mkdir-p /sysroot/tmp/retentionlist
cp /retentionlist.txt /sysroot/tmp/retentionlist/
cat chroot_commands1.sh | chroot /sysroot
cp-r /sysroot/tmp/retentionlist/ /
rm-rf /sysroot/tmp/retentionlist/
cat /retentionlist/metadata/savedperms
echo"copied rhel7 to /sysroot"
umount /sysroot
echo"Unmounted disk. Disk will be reimaged."
chroot_command1.sh - Retain files and their UID/GID
mkdir /tmp/retentionlist/data
mkdir /tmp/retentionlist/metadata
for f in`cat /tmp/retentionlist/retentionlist.txt`;do
if[-d$f];then
cp-r-v$f /tmp/retentionlist/data/
fi
if[-f$f];then
cp-v$f /tmp/retentionlist/data/
fi
if[-e$f];then
find `dirname"$f"`-path"$f"-prune-printf'%m\t%u\t%g\t%p\n'>> /tmp/retentionlist/metadata/savedperms
fi
done