Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration management #17

Merged
merged 5 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ Tests on one orange pi zero show that the time from power on until a login
prompt on the serial console is about 1 minute.


Applying persistent configurations
----------------------------------

Custom configurations are saved as tar.gz archives in conf.d at the root of the
fat16 file system. After the start of userspace and before systemd is launched,
content in each archive is extracted in alphabetical order to /etc of the ramdisk,
so files in an archive towards the end of the list overwrite those from an earlier
archive in case of a filename conflict, and systemd will only see the final
(highest priority) configurations. For example:

/conf.d/00-tomesh-base.tar.gz
10-tomesh-wlan-mesh-top-gs07-rt5572.tar.gz
11-tomesh-wlan-hostap-tplink-tl-wn722n.tar.gz
50-node-config-save.tar.gz
90-user-custom-configs.tar.gz

This allows local mesh communities to customize nodes by distributing archives
of systemd.network configuration files, or any files that are to be read from
/etc. The user simply mounts the fat filesystem like a USB key and drops tar.gz
files into conf.d and backs them up across software updates.

If the node needs to save persistent configurations while running, such as
remembering a MAC address, it can save the current /etc directory into a
50-node-config-save.tar.gz archive by running `config_save`. The user may
also manually add configurations with higher priority archive names.

Test the Debian image using Qemu
--------------------------------

Expand Down
21 changes: 18 additions & 3 deletions boards/common-uboot.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,40 @@ endif
# - mtools config file built for us
# - the filesystem label and disk id are fixed as "boot" and "1"

# Add a boot partition to an existing image, format it and copy the lists of
# files
# Create and write files to boot partition
#
# $1 is the mtools config file
# $2 is the mtools drive letter
# $3 is the start sector for the partition
# $4 is the list of files for the /boot dir
# $5 is the list of files for the /boot/dtb dir
define uboot_bootdir
define uboot_dirs
$(call uboot_part,$1,$2,$3,$4,$5)
$(call uboot_bootdir,$1,$2,$3,$4,$5)
$(call uboot_confdir,$1,$2,$3,$4,$5)
endef

# Add a boot partition to an existing image and format it
define uboot_part
MTOOLSRC=$1 mpartition -I $2
MTOOLSRC=$1 mpartition -c -b $3 $2
MTOOLSRC=$1 mpartition -a $2
MTOOLSRC=$1 mformat -v boot -N 1 $2
endef

# Create boot directory and copy the lists of files
define uboot_bootdir
MTOOLSRC=$1 mmd $2boot
MTOOLSRC=$1 mmd $2boot/dtb
MTOOLSRC=$1 mcopy $4 $2boot
MTOOLSRC=$1 mcopy $5 $2boot/dtb
endef

# Create an empty configuration directory
define uboot_confdir
MTOOLSRC=$1 mmd $2conf.d
endef

BUILD_DEPENDS += mtools


Expand Down
1 change: 1 addition & 0 deletions boards/raspberrypi2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,5 @@ $(DISK_IMAGE): $(BUILD)/mtoolsrc $(BOOT_FILES)
truncate --size=1025K [email protected] # ensure the FAT bootblock is mapped
MTOOLSRC=$(BUILD)/mtoolsrc mformat -v boot -N 1 z:
MTOOLSRC=$(BUILD)/mtoolsrc mcopy $(BOOT_FILES) z:
MTOOLSRC=$(BUILD)/mtoolsrc mmd z:conf.d
mv [email protected] $@
2 changes: 1 addition & 1 deletion boards/sun4i-a10-cubieboard/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ $(BUILD)/mtoolsrc: Makefile
$(DISK_IMAGE): $(SRC_SPL) $(BUILD)/mtoolsrc $(BOOT_FILES) $(BOOT_DTB_FILES)
$(call allwinner_spl,$(SRC_SPL),[email protected])
truncate --size=1025K [email protected] # ensure the FAT bootblock is mapped
$(call uboot_bootdir,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
$(call uboot_dirs,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
mv [email protected] $@
2 changes: 1 addition & 1 deletion boards/sun7i-a20-bananapi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ $(DISK_IMAGE): $(SRC_SPL) $(BUILD)/mtoolsrc $(BOOT_FILES) $(BOOT_DTB_FILES) env
$(call allwinner_spl,$(SRC_SPL),[email protected])
truncate --size=1025K [email protected] # ensure the FAT bootblock is mapped
dd if=env [email protected] conv=notrunc bs=$$((0x088000)) seek=1
$(call uboot_bootdir,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
$(call uboot_dirs,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
mv [email protected] $@
2 changes: 1 addition & 1 deletion boards/sun8i-h2-plus-orangepi-zero/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ $(BUILD)/mtoolsrc: Makefile
$(DISK_IMAGE): $(SRC_SPL) $(BUILD)/mtoolsrc $(BOOT_FILES) $(BOOT_DTB_FILES)
$(call allwinner_spl,$(SRC_SPL),[email protected])
truncate --size=1025K [email protected] # ensure the FAT bootblock is mapped
$(call uboot_bootdir,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
$(call uboot_dirs,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
mv [email protected] $@

2 changes: 1 addition & 1 deletion boards/sun8i-h3-orangepi-lite/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ $(BUILD)/mtoolsrc: Makefile
$(DISK_IMAGE): $(SRC_SPL) $(BUILD)/mtoolsrc $(BOOT_FILES) $(BOOT_DTB_FILES) $(BOOT_DTBO_FILES)
$(call allwinner_spl,$(SRC_SPL),[email protected])
truncate --size=1025K [email protected] # ensure the FAT bootblock is mapped
$(call uboot_bootdir,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
$(call uboot_dirs,$(BUILD)/mtoolsrc,z:,$$((0x100000/512)),$(BOOT_FILES),$(BOOT_DTB_FILES))
MTOOLSRC=$(BUILD)/mtoolsrc mmd z:boot/dtb/overlay
MTOOLSRC=$(BUILD)/mtoolsrc mcopy $(BOOT_DTBO_FILES) z:boot/dtb/overlay
mv [email protected] $@
2 changes: 1 addition & 1 deletion boards/sun8i-v3s-licheepi-zero/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ $(BUILD)/mtoolsrc: Makefile
$(DISK_IMAGE): $(SRC_SPL) $(BUILD)/mtoolsrc $(BOOT_FILES) $(BOOT_DTB_FILES) $(BUILD)/root.fs
$(call allwinner_spl,$(SRC_SPL),[email protected])
truncate --size=1025K [email protected] # ensure the FAT bootblock is mapped
$(call uboot_bootdir,$(BUILD)/mtoolsrc,z:,$(PART1_BEGIN_SEC),$(BOOT_FILES),$(BOOT_DTB_FILES))
$(call uboot_dirs,$(BUILD)/mtoolsrc,z:,$(PART1_BEGIN_SEC),$(BOOT_FILES),$(BOOT_DTB_FILES))
MTOOLSRC=$(BUILD)/mtoolsrc mpartition -c -T0x82 -b $(PART2_BEGIN_SEC) y:
MTOOLSRC=$(BUILD)/mtoolsrc mpartition -c -T0x83 -b $(PART3_BEGIN_SEC) x:
truncate --size=$$(( $(PART3_BEGIN_SEC)*512 )) [email protected]
Expand Down
2 changes: 1 addition & 1 deletion debian/packages.d/README
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ customisations. These customisations are applied in several phases,
and each package that is found installed in the rescue system will
attempt to run its matching script during each phase.

Curently the phases are:
Currently the phases are:

"minimise"
During this phase, anything that can be removed to minimise the
Expand Down
38 changes: 38 additions & 0 deletions debian/packages.d/systemd.customise.add/init.d/20confd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Apply configurations from conf.d
#
# Copyright (C) 2018 Benedict Lau <[email protected]>

CONFDIR=conf.d

try_partition() {
# Create file system node from partition
mknod /dev/$1 b $2 $3 2>/dev/null

# Mount partition as read-only
mount /dev/$1 /mnt -o ro

# Find conf.d in root of partition and extract in alphabetical order each
# tar.gz archive into the ramdisk /etc, overwriting any existing file
if [ -d "/mnt/$CONFDIR" ]; then
for conf in /mnt/$CONFDIR/*.tar.gz; do
echo Applying configurations from /dev/$1: $conf
tar --extract -f $conf -C /etc
done
fi

# Unmount partition
umount /mnt
}

# Mount proc
mount -t proc proc /proc

cat /proc/partitions | while read major minor size name; do
# Check each partition matching sd*[0-9] (e.g. sda1) or mmcblk*p* (e.g. mmcblk0p1)
case $name in
sd*[0-9]|mmcblk*p*)
echo Checking for configuration files on $name
try_partition $name $major $minor
;;
esac
done
43 changes: 43 additions & 0 deletions debian/packages.d/systemd.customise.add/usr/local/sbin/config_save
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Save current configurations to partition containing conf.d
#
# Copyright (C) 2018 Benedict Lau <[email protected]>

CONFDIR=conf.d

try_partition() {
retval=1

# Mount partition as read-write
mount /dev/$1 /mnt

# Find conf.d in root of partition and save current configurations in /etc
if [ -d "/mnt/$CONFDIR" ]; then
echo Saving configurations to /dev/$1: "/mnt/$CONFDIR/50-node-config-save.tar.gz"
tar --create --gzip -f "/mnt/$CONFDIR/50-node-config-save.tar.gz" -C /etc .

# Set function return value to successful
echo Configurations saved to /dev/$1: "/mnt/$CONFDIR/50-node-config-save.tar.gz"
retval=0
fi

# Unmount partition
umount /mnt

return $retval
}

cat /proc/partitions | while read major minor size name; do
# Check each partition matching sd*[0-9] (e.g. sda1) or mmcblk*p* (e.g. mmcblk0p1)
case $name in
sd*[0-9]|mmcblk*p*)
echo Checking for configuration directory on $name
try_partition $name $major $minor
S=$?

# Save only to the first partition found with conf.d
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hamishcoleman Break like this after first successful save?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems reasonable

if [ $S -eq 0 ]; then
break
fi
;;
esac
done