diff --git a/src/install_openwrt_rootfs.sh b/src/install_openwrt_rootfs.sh index 488eed6..1b0f52a 100644 --- a/src/install_openwrt_rootfs.sh +++ b/src/install_openwrt_rootfs.sh @@ -8,17 +8,22 @@ trap - ERR # Check if squashfs-combined is in volume FILE=/storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img -if [ -f "$FILE" ]; then - info "$FILE exists. Nothing to do." -else - info "$FILE does not exist. Copying squashfs-combined-${OPENWRT_VERSION}.img to storage ..." - cp /var/vm/squashfs-combined-${OPENWRT_VERSION}.img.gz /storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img.gz - gzip -d /storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img.gz - - if [ -f "/storage/current_version" ]; then - mv /storage/current_version /storage/old_version - fi +if [[ $DISABLE_OPENWRT_AUTO_UPGRADE != "true" ]]; then + if [ -f "$FILE" ]; then + info "$FILE exists. Nothing to do." + else + info "$FILE does not exist. Copying squashfs-combined-${OPENWRT_VERSION}.img to storage ..." + cp /var/vm/squashfs-combined-${OPENWRT_VERSION}.img.gz /storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img.gz + gzip -d /storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img.gz + + if [ -f "/storage/current_version" ]; then + mv /storage/current_version /storage/old_version + fi - touch /storage/current_version - echo "squashfs-combined-${OPENWRT_IMAGE_ID}.img" > /storage/current_version + touch /storage/current_version + echo "squashfs-combined-${OPENWRT_IMAGE_ID}.img" > /storage/current_version + fi +else + FILE=$(cat /storage/current_version) + info "OpenWrt upgrade check is disabled. Using "$FILE"." fi diff --git a/src/migrate_openwrt_rootfs.sh b/src/migrate_openwrt_rootfs.sh index fa89d5c..39389f6 100644 --- a/src/migrate_openwrt_rootfs.sh +++ b/src/migrate_openwrt_rootfs.sh @@ -10,101 +10,94 @@ SQUASHFS_COMBINED=/storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img CPU_ARCH=$(arch) # Migrate settings from old OpenWrt version to new one -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." - - # 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" - else - qemu_command="qemu-system-x86_64 -M pc -bios /usr/share/ovmf/bios.bin" - 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 - info "squashfs-combined-${OPENWRT_IMAGE_ID}.img is up to date. Nothing to do." - fi -else +if [ -f /storage/old_version ]; then OLD_SQUASHFS_COMBINED=`cat /storage/old_version` - info "OpenWrt upgrade check is disabled. Using "$OLD_SQUASHFS_COMBINED". Nothing to do." + + 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 + + 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 -bios /usr/share/ovmf/bios.bin" + 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 fi \ No newline at end of file diff --git a/src/run_openwrt.sh b/src/run_openwrt.sh index 2d206be..d691a04 100755 --- a/src/run_openwrt.sh +++ b/src/run_openwrt.sh @@ -13,7 +13,7 @@ trap - ERR CPU_ARCH=$(arch) VERS=$(qemu-system-"$CPU_ARCH" --version | head -n 1 | cut -d '(' -f 1) -FILE=/storage/squashfs-combined-${OPENWRT_IMAGE_ID}.img +FILE=/storage/$(cat /storage/current_version) # Attach physical interfaces to Docker container attach_eth_if () {