diff --git a/docker-compose.yml b/docker-compose.yml index 42e8287..89751e9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -75,6 +75,17 @@ services: #OPENWRT_AFTER_BOOT_CMD: "a=$$((1+1)); echo The result of 1+1 is $${a}" # Shell script example #OPENWRT_AFTER_BOOT_CMD: '[ -f /etc/first_boot_over ] || { touch /etc/first_boot_over; echo "Crazy first boot"; }' # Example to run a command only once + # When a user pulls a new OpenWrt container image most likely also a new OpenWrt image is included. + # During the first container startup the new OpenWrt image will be automatically used. + # The settings data from the previous OpenWrt image will be automatically copied. This is done by the OpenWrt sysupgrade command. + # With this option this can be disabled. + # It is useful for the following use cases + # * To upgrade OpenWrt internally with owut https://openwrt.org/docs/guide-user/installation/sysupgrade.owut + # * To upgrade OpenWrt internally with sysupgrade (CLI or LuCI) + # * To stick to previous used OpenWrt image and version + # If unset -> disabled (OpenWrt auto upgrade is enabled) + #DISABLE_OPENWRT_AUTO_UPGRADE: "true" + # Enable debugging mode. # Showing the final qemu command # If unset -> disabled diff --git a/src/helpers.sh b/src/helpers.sh index d386aa1..80e360b 100644 --- a/src/helpers.sh +++ b/src/helpers.sh @@ -17,5 +17,6 @@ trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR : "${PCI_1:=""}" # PCI 1 slot (first number in lspci) : "${FORWARD_LUCI:=""}" # Make LuCI OpenWrt web interface accessible via host LAN : "${OPENWRT_AFTER_BOOT_CMD:=""}" # User command or script to run after OpenWrt is booted +: "${DISABLE_OPENWRT_AUTO_UPGRADE:=""}" # Disables the OpenWrt upgrade check every container startup : "${IS_U_OS_APP:=""}" # By default this container is not a u-OS app : "${DEBUG:=""}" # Disable debugging diff --git a/src/migrate_openwrt_rootfs.sh b/src/migrate_openwrt_rootfs.sh index f0b7b14..001653f 100644 --- a/src/migrate_openwrt_rootfs.sh +++ b/src/migrate_openwrt_rootfs.sh @@ -10,96 +10,100 @@ SQUASHFS_COMBINED=/storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img CPU_ARCH=$(arch) # Migrate settings from old OpenWrt version to new one -if [ -f /storage/old_version ]; then - OLD_SQUASHFS_COMBINED=`cat /storage/old_version` +if [[ $DISABLE_OPENWRT_AUTO_UPGRADE != "true" ]]; then + if [ -f /storage/old_version ]; then + OLD_SQUASHFS_COMBINED=`cat /storage/old_version` - info "Migrate settings from $OLD_SQUASHFS_COMBINED to squashfs-combined-${OPENWRT_IMAGE_ID}.img." + info "Migrate settings from $OLD_SQUASHFS_COMBINED to squashfs-combined-${OPENWRT_IMAGE_ID}.img." - # Because we do not know if the host system is supporting the filesystems squashfs or f2fs we simple boot a intermediated OpenWrt - # Make a copy from the original OpenWrt image - cp $SQUASHFS_COMBINED /tmp/squashfs-combined-tmp.img + # Because we do not know if the host system is supporting the filesystems squashfs or f2fs we simple boot a intermediated OpenWrt + # Make a copy from the original OpenWrt image + cp $SQUASHFS_COMBINED /tmp/squashfs-combined-tmp.img - if [ $CPU_ARCH = "aarch64" ]; then - qemu_command="qemu-system-aarch64 -M virt -bios /usr/share/qemu/edk2-aarch64-code.fd" + if [ $CPU_ARCH = "aarch64" ]; then + qemu_command="qemu-system-aarch64 -M virt -bios /usr/share/qemu/edk2-aarch64-code.fd" + else + qemu_command="qemu-system-x86_64 -M pc" + fi + + # Boot a temporary OpenWrt + $qemu_command -cpu host --enable-kvm -smp 2 -nographic -nodefaults -m 256 \ + -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/tmp/squashfs-combined-tmp.img \ + -device virtio-blk-pci,drive=hd0 \ + -blockdev driver=raw,node-name=hd1,cache.direct=on,file.driver=file,file.filename=/storage/$OLD_SQUASHFS_COMBINED \ + -device virtio-blk-pci,drive=hd1 \ + -blockdev driver=raw,node-name=hd2,cache.direct=on,file.driver=file,file.filename=$SQUASHFS_COMBINED \ + -device virtio-blk-pci,drive=hd2 \ + -device virtio-net,netdev=qlan0 -netdev user,id=qlan0,net=172.31.1.0/24,hostfwd=tcp::1022-172.31.1.1:22 \ + -device virtio-net,netdev=qwan0 -netdev user,id=qwan0 \ + & QEMU_PID=$! + echo "QEMU started with PID $QEMU_PID" + + # Wait until OpenWrt has booted + until ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new root@localhost -p 1022 "cat /etc/banner"; do echo "Waiting for OpenWrt boot ..."; sleep 1; done + ssh root@localhost -p 1022 "mkdir -p /tmp/squashfs_dir" + ssh root@localhost -p 1022 "mkdir -p /tmp/userdata_dir" + + # Create config backup in previous image + if [[ $OLD_SQUASHFS_COMBINED == *"rootfs"* ]]; then + warn "Using rootfs-.img images is deprecated!" + ssh root@localhost -p 1022 "mount /dev/vdb /mnt" + ssh root@localhost -p 1022 "chroot /mnt/ mkdir -p /var/lock" + ssh root@localhost -p 1022 "chroot /mnt /sbin/sysupgrade -k -b /tmp/openwrt_config.tar.gz" + ssh root@localhost -p 1022 "cp /mnt/tmp/openwrt_config.tar.gz /tmp" + ssh root@localhost -p 1022 "umount /mnt" + else + FILESYSTEM_OFFSETS=$(/run/mount_openwrt_squashfs_combined.sh -o /storage/$OLD_SQUASHFS_COMBINED) + WRITEABLE_FS_OFFSET=$(echo $FILESYSTEM_OFFSETS | cut -d',' -f2) + ssh root@localhost -p 1022 "mount /dev/vdb2 /tmp/squashfs_dir" + ssh root@localhost -p 1022 "losetup -o $WRITEABLE_FS_OFFSET /dev/loop1 /dev/vdb" + ssh root@localhost -p 1022 "mount /dev/loop1 /tmp/userdata_dir" + ssh root@localhost -p 1022 "mount -t overlay -o lowerdir=/tmp/squashfs_dir,upperdir=/tmp/userdata_dir/upper,workdir=/tmp/userdata_dir/work overlay /mnt" + ssh root@localhost -p 1022 "mount -o bind /tmp/userdata_dir /mnt/overlay/" + ssh root@localhost -p 1022 "mount -o bind /tmp/squashfs_dir /mnt/rom/" + ssh root@localhost -p 1022 "mount -o bind /dev /mnt/dev" + ssh root@localhost -p 1022 "chroot /mnt/ mkdir -p /var/lock" + ssh root@localhost -p 1022 "chroot /mnt /sbin/sysupgrade -k -b /tmp/openwrt_config.tar.gz" + ssh root@localhost -p 1022 "cp /mnt/tmp/openwrt_config.tar.gz /tmp" + ssh root@localhost -p 1022 "umount /mnt/overlay" + ssh root@localhost -p 1022 "umount /mnt/rom" + ssh root@localhost -p 1022 "umount /mnt/dev" + ssh root@localhost -p 1022 "umount /mnt" + ssh root@localhost -p 1022 "umount /tmp/userdata_dir" + ssh root@localhost -p 1022 "umount /tmp/squashfs_dir" + ssh root@localhost -p 1022 "losetup -d /dev/loop1" + fi + + # Put config backup to new squashfs-combined + FILESYSTEM_OFFSETS=$(/run/mount_openwrt_squashfs_combined.sh -o $SQUASHFS_COMBINED) + WRITEABLE_FS_OFFSET=$(echo $FILESYSTEM_OFFSETS | cut -d',' -f2) + ssh root@localhost -p 1022 "mount /dev/vdc2 /tmp/squashfs_dir" + ssh root@localhost -p 1022 "losetup -o $WRITEABLE_FS_OFFSET /dev/loop1 /dev/vdc" + ssh root@localhost -p 1022 "mount /dev/loop1 /tmp/userdata_dir" + ssh root@localhost -p 1022 "mount -t overlay -o lowerdir=/tmp/squashfs_dir,upperdir=/tmp/userdata_dir/upper,workdir=/tmp/userdata_dir/work overlay /mnt" + ssh root@localhost -p 1022 "mount -o bind /tmp/userdata_dir /mnt/overlay/" + ssh root@localhost -p 1022 "mount -o bind /tmp/squashfs_dir /mnt/rom/" + ssh root@localhost -p 1022 "mount -o bind /dev /mnt/dev" + ssh root@localhost -p 1022 "chroot /mnt/ mkdir -p /var/lock" + ssh root@localhost -p 1022 "cp /tmp/openwrt_config.tar.gz /mnt/tmp" + ssh root@localhost -p 1022 "chroot /mnt /sbin/sysupgrade -r /tmp/openwrt_config.tar.gz" + + # Stop VM + ssh root@localhost -p 1022 'sync; halt' + while kill -0 $QEMU_PID 2>/dev/null; do echo "Waiting for qemu exit ..."; sleep 1; done; + + # Delete temporary OpenWrt image + rm /tmp/squashfs-combined-tmp.img + + # Finally remove old squashfs-combined and old files + rm /storage/$OLD_SQUASHFS_COMBINED + rm /storage/old_version + if [ -f storage/config-`basename $OLD_SQUASHFS_COMBINED.img`.tar.gz ]; then + rm /storage/config-`basename $OLD_SQUASHFS_COMBINED.img`.tar.gz + fi else - qemu_command="qemu-system-x86_64 -M pc" - fi - - # Boot a temporary OpenWrt - $qemu_command -cpu host --enable-kvm -smp 2 -nographic -nodefaults -m 256 \ - -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/tmp/squashfs-combined-tmp.img \ - -device virtio-blk-pci,drive=hd0 \ - -blockdev driver=raw,node-name=hd1,cache.direct=on,file.driver=file,file.filename=/storage/$OLD_SQUASHFS_COMBINED \ - -device virtio-blk-pci,drive=hd1 \ - -blockdev driver=raw,node-name=hd2,cache.direct=on,file.driver=file,file.filename=$SQUASHFS_COMBINED \ - -device virtio-blk-pci,drive=hd2 \ - -device virtio-net,netdev=qlan0 -netdev user,id=qlan0,net=172.31.1.0/24,hostfwd=tcp::1022-172.31.1.1:22 \ - -device virtio-net,netdev=qwan0 -netdev user,id=qwan0 \ - & QEMU_PID=$! - echo "QEMU started with PID $QEMU_PID" - - # Wait until OpenWrt has booted - until ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new root@localhost -p 1022 "cat /etc/banner"; do echo "Waiting for OpenWrt boot ..."; sleep 1; done - ssh root@localhost -p 1022 "mkdir -p /tmp/squashfs_dir" - ssh root@localhost -p 1022 "mkdir -p /tmp/userdata_dir" - - # Create config backup in previous image - if [[ $OLD_SQUASHFS_COMBINED == *"rootfs"* ]]; then - warn "Using rootfs-.img images is deprecated!" - ssh root@localhost -p 1022 "mount /dev/vdb /mnt" - ssh root@localhost -p 1022 "chroot /mnt/ mkdir -p /var/lock" - ssh root@localhost -p 1022 "chroot /mnt /sbin/sysupgrade -k -b /tmp/openwrt_config.tar.gz" - ssh root@localhost -p 1022 "cp /mnt/tmp/openwrt_config.tar.gz /tmp" - ssh root@localhost -p 1022 "umount /mnt" - else - FILESYSTEM_OFFSETS=$(/run/mount_openwrt_squashfs_combined.sh -o /storage/$OLD_SQUASHFS_COMBINED) - WRITEABLE_FS_OFFSET=$(echo $FILESYSTEM_OFFSETS | cut -d',' -f2) - ssh root@localhost -p 1022 "mount /dev/vdb2 /tmp/squashfs_dir" - ssh root@localhost -p 1022 "losetup -o $WRITEABLE_FS_OFFSET /dev/loop1 /dev/vdb" - ssh root@localhost -p 1022 "mount /dev/loop1 /tmp/userdata_dir" - ssh root@localhost -p 1022 "mount -t overlay -o lowerdir=/tmp/squashfs_dir,upperdir=/tmp/userdata_dir/upper,workdir=/tmp/userdata_dir/work overlay /mnt" - ssh root@localhost -p 1022 "mount -o bind /tmp/userdata_dir /mnt/overlay/" - ssh root@localhost -p 1022 "mount -o bind /tmp/squashfs_dir /mnt/rom/" - ssh root@localhost -p 1022 "mount -o bind /dev /mnt/dev" - ssh root@localhost -p 1022 "chroot /mnt/ mkdir -p /var/lock" - ssh root@localhost -p 1022 "chroot /mnt /sbin/sysupgrade -k -b /tmp/openwrt_config.tar.gz" - ssh root@localhost -p 1022 "cp /mnt/tmp/openwrt_config.tar.gz /tmp" - ssh root@localhost -p 1022 "umount /mnt/overlay" - ssh root@localhost -p 1022 "umount /mnt/rom" - ssh root@localhost -p 1022 "umount /mnt/dev" - ssh root@localhost -p 1022 "umount /mnt" - ssh root@localhost -p 1022 "umount /tmp/userdata_dir" - ssh root@localhost -p 1022 "umount /tmp/squashfs_dir" - ssh root@localhost -p 1022 "losetup -d /dev/loop1" - fi - - # Put config backup to new squashfs-combined - FILESYSTEM_OFFSETS=$(/run/mount_openwrt_squashfs_combined.sh -o $SQUASHFS_COMBINED) - WRITEABLE_FS_OFFSET=$(echo $FILESYSTEM_OFFSETS | cut -d',' -f2) - ssh root@localhost -p 1022 "mount /dev/vdc2 /tmp/squashfs_dir" - ssh root@localhost -p 1022 "losetup -o $WRITEABLE_FS_OFFSET /dev/loop1 /dev/vdc" - ssh root@localhost -p 1022 "mount /dev/loop1 /tmp/userdata_dir" - ssh root@localhost -p 1022 "mount -t overlay -o lowerdir=/tmp/squashfs_dir,upperdir=/tmp/userdata_dir/upper,workdir=/tmp/userdata_dir/work overlay /mnt" - ssh root@localhost -p 1022 "mount -o bind /tmp/userdata_dir /mnt/overlay/" - ssh root@localhost -p 1022 "mount -o bind /tmp/squashfs_dir /mnt/rom/" - ssh root@localhost -p 1022 "mount -o bind /dev /mnt/dev" - ssh root@localhost -p 1022 "chroot /mnt/ mkdir -p /var/lock" - ssh root@localhost -p 1022 "cp /tmp/openwrt_config.tar.gz /mnt/tmp" - ssh root@localhost -p 1022 "chroot /mnt /sbin/sysupgrade -r /tmp/openwrt_config.tar.gz" - - # Stop VM - ssh root@localhost -p 1022 'sync; halt' - while kill -0 $QEMU_PID 2>/dev/null; do echo "Waiting for qemu exit ..."; sleep 1; done; - - # Delete temporary OpenWrt image - rm /tmp/squashfs-combined-tmp.img - - # Finally remove old squashfs-combined and old files - rm /storage/$OLD_SQUASHFS_COMBINED - rm /storage/old_version - if [ -f storage/config-`basename $OLD_SQUASHFS_COMBINED.img`.tar.gz ]; then - rm /storage/config-`basename $OLD_SQUASHFS_COMBINED.img`.tar.gz + info "squashfs-combined-${OPENWRT_IMAGE_ID}.img is up to date. Nothing to do." fi else - info "squashfs-combined-${OPENWRT_IMAGE_ID}.img is up to date. Nothing to do." + info "OpenWrt upgrade check is disabled. Using squashfs-combined-${OPENWRT_IMAGE_ID}.img. Nothing to do." fi \ No newline at end of file diff --git a/test/tests/openwrt_docker_test.py b/test/tests/openwrt_docker_test.py index 89a26bc..4ccd8e4 100644 --- a/test/tests/openwrt_docker_test.py +++ b/test/tests/openwrt_docker_test.py @@ -517,4 +517,13 @@ def test_pci_passthrough_fail(docker_services): assert ('Loading vfio-pci kernel module' in get_logs()) == True # It has to fail because IOMMU is not available in multipass (run via Github Actions) - assert ('ERROR: Failed to bind PCI device 0000:00:05.0 to vfio-pci.' in get_logs()) == True \ No newline at end of file + assert ('ERROR: Failed to bind PCI device 0000:00:05.0 to vfio-pci.' in get_logs()) == True + + +@pytest.mark.parametrize("parameter", + [('DISABLE_OPENWRT_AUTO_UPGRADE','true')], indirect=True, + ids=['DISABLE_OPENWRT_AUTO_UPGRADE="true"']) +def test_openwrt_migrate_existing_volume(docker_services): + wait_for_specific_log(docker_services, 'Booting image using QEMU emulator') + + assert ('OpenWrt upgrade check is disabled' in get_logs()) == True