mirror of
https://github.com/AlbrechtL/openwrt-docker.git
synced 2026-10-14 00:01:26 +00:00
Add qemu guest agend to image. Do migration via chroot instead of qemu. Add grateful shutdown and reboot.
This commit is contained in:
+18
-3
@@ -31,17 +31,32 @@ RUN apk add --no-cache \
|
||||
|
||||
# Get OpenWrt images
|
||||
RUN mkdir /var/vm \
|
||||
mkdir /var/vm/packages \
|
||||
&& if [ "$OPENWRT_VERSION" = "master" ] ; then \
|
||||
wget "https://downloads.openwrt.org/snapshots/targets/armsr/armv8/openwrt-armsr-armv8-generic-ext4-rootfs.img.gz" \
|
||||
-O /var/vm/rootfs-${OPENWRT_VERSION}.img.gz \
|
||||
&& wget "https://downloads.openwrt.org/snapshots/targets/armsr/armv8/openwrt-armsr-armv8-generic-kernel.bin" \
|
||||
-O /var/vm/kernel.bin ;\
|
||||
-O /var/vm/kernel.bin \
|
||||
&& wget "https://downloads.openwrt.org/snapshots/targets/armsr/armv8/openwrt-armsr-armv8-rootfs.tar.gz" \
|
||||
-O /tmp/rootfs-${OPENWRT_VERSION}.tar.gz ; \
|
||||
else \
|
||||
wget "https://archive.openwrt.org/releases/${OPENWRT_VERSION}/targets/armsr/armv8/openwrt-${OPENWRT_VERSION}-armsr-armv8-generic-ext4-rootfs.img.gz" \
|
||||
-O /var/vm/rootfs-${OPENWRT_VERSION}.img.gz \
|
||||
&& wget "https://archive.openwrt.org/releases/${OPENWRT_VERSION}/targets/armsr/armv8/openwrt-${OPENWRT_VERSION}-armsr-armv8-generic-kernel.bin" \
|
||||
-O /var/vm/kernel.bin ;\
|
||||
fi
|
||||
-O /var/vm/kernel.bin \
|
||||
&& wget "https://archive.openwrt.org/releases/${OPENWRT_VERSION}/targets/armsr/armv8/openwrt-${OPENWRT_VERSION}-armsr-armv8-rootfs.tar.gz" \
|
||||
-O /tmp/rootfs-${OPENWRT_VERSION}.tar.gz ; \
|
||||
fi \
|
||||
&& mkdir /tmp/openwrt-rootfs \
|
||||
&& tar -xzf /tmp/rootfs-${OPENWRT_VERSION}.tar.gz -C /tmp/openwrt-rootfs \
|
||||
&& cp /etc/resolv.conf /tmp/openwrt-rootfs/etc/resolv.conf \
|
||||
&& chroot /tmp/openwrt-rootfs mkdir -p /var/lock \
|
||||
&& chroot /tmp/openwrt-rootfs opkg update \
|
||||
&& chroot /tmp/openwrt-rootfs opkg install qemu-ga luci --download-only \
|
||||
&& cp /tmp/openwrt-rootfs/*.ipk /var/vm/packages \
|
||||
&& rm -rf /tmp/openwrt-rootfs \
|
||||
&& rm /tmp/rootfs-${OPENWRT_VERSION}.tar.gz
|
||||
|
||||
ENV OPENWRT_VERSION=${OPENWRT_VERSION}
|
||||
|
||||
COPY supervisord.conf /etc/supervisord.conf
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
|
||||
echo "Backup configuration"
|
||||
mkdir -p /root/backup
|
||||
sysupgrade -k -b /root/backup/config.tar.gz
|
||||
sync
|
||||
|
||||
echo "Shutdown system"
|
||||
halt
|
||||
|
||||
exit 0
|
||||
@@ -1,9 +0,0 @@
|
||||
|
||||
echo "Restore configuration"
|
||||
sysupgrade -r /root/backup/config.tar.gz
|
||||
sync
|
||||
|
||||
echo "Shutdown system"
|
||||
halt
|
||||
|
||||
exit 0
|
||||
@@ -1,9 +1,8 @@
|
||||
{
|
||||
"title": "Openwrt control",
|
||||
"title": "OpenWrt control",
|
||||
"port": "5000",
|
||||
|
||||
"access": {
|
||||
"allowed_users": "*",
|
||||
"admin_users": "*"
|
||||
"allowed_users": "*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "98 Hard reboot",
|
||||
"description": "Hard reboot of OpenWrt VM",
|
||||
"script_path": "/run/qemu_qmp.sh -r"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "6 Reboot",
|
||||
"description": "Reboot OpenWrt VM",
|
||||
"script_path": "/run/qemu_qmp.sh -R"
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "98 Reset",
|
||||
"description": "Hard reset of OpenWrt VM",
|
||||
"script_path": "/run/qemu_qmp.sh -r"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "5 Stop",
|
||||
"description": "Stop OpenWrt VM",
|
||||
"script_path": "supervisorctl stop openwrt"
|
||||
"script_path": "/run/qemu_qmp.sh -S"
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,13 @@ trap - ERR
|
||||
CURRENT_VERSION_ROOTFS=`cat /storage/current_version`
|
||||
|
||||
# Stop openwrt
|
||||
echo "****** Stop openwrt ******"
|
||||
supervisorctl stop openwrt
|
||||
echo "****** shutdown openwrt ******"
|
||||
/run/qemu_qmp.sh -S
|
||||
|
||||
until ! supervisorctl status openwrt | grep RUNNING
|
||||
do
|
||||
sleep 1
|
||||
done
|
||||
|
||||
# Get config
|
||||
echo "****** Mount $CURRENT_VERSION_ROOTFS ******"
|
||||
|
||||
@@ -5,8 +5,6 @@ trap - ERR
|
||||
|
||||
. /run/helpers.sh
|
||||
|
||||
CURRENT_VERSION_ROOTFS=`cat /storage/current_version`
|
||||
|
||||
# Stop openwrt
|
||||
echo "****** Stop openwrt ******"
|
||||
supervisorctl stop openwrt
|
||||
|
||||
@@ -17,6 +17,7 @@ else
|
||||
info "Inject some additional files into the image"
|
||||
mount /storage/rootfs-${OPENWRT_VERSION}.img /mnt
|
||||
# mount -o offset=$((512*262656)) /storage/disk.img /mnt # combined image ext4 partition starts at offset 262656
|
||||
|
||||
chmod +x /var/vm/openwrt_additional/bin/*
|
||||
cp /var/vm/openwrt_additional/bin/* /mnt/usr/bin/
|
||||
|
||||
@@ -24,6 +25,14 @@ else
|
||||
cp /var/vm/openwrt_additional/rc.local /mnt/etc/rc.local
|
||||
chmod +x /mnt/etc/rc.local
|
||||
|
||||
info "Install additional IPKs into the image"
|
||||
mkdir /mnt/var/offline_packages
|
||||
cp /var/vm/packages/*.ipk /mnt/var/offline_packages/
|
||||
chroot /mnt/ mkdir -p /var/lock
|
||||
chroot /mnt/ sh -c 'cd /var/offline_packages/ && opkg install *.ipk'
|
||||
rm -rf /mnt/var/offline_packages/
|
||||
rm -rf /mnt/var/lock
|
||||
|
||||
umount /mnt
|
||||
|
||||
if [ -f "/storage/current_version" ]; then
|
||||
|
||||
@@ -5,70 +5,26 @@ trap - ERR
|
||||
|
||||
. /run/helpers.sh
|
||||
|
||||
FILE=/storage/rootfs-${OPENWRT_VERSION}.img
|
||||
|
||||
# Migrate settings from old OpenWrt version to new one
|
||||
if [ -f /storage/old_version ]; then
|
||||
OLD_VERSION_ROOTFS=`cat /storage/old_version`
|
||||
|
||||
info "Migrate settings from $OLD_VERSION_ROOTFS to rootfs-${OPENWRT_VERSION}.img. This can take some time because we need to boot openwrt multiple times."
|
||||
info "Migrate settings from $OLD_VERSION_ROOTFS to rootfs-${OPENWRT_VERSION}.img."
|
||||
|
||||
mount /storage/$OLD_VERSION_ROOTFS /mnt
|
||||
mv /mnt/etc/rc.local /mnt/etc/rc.local.disabled
|
||||
cp /var/vm/openwrt_additional/do_backup_rc.local /mnt/etc/rc.local
|
||||
chmod +x /mnt/etc/rc.local
|
||||
umount /mnt
|
||||
|
||||
info "Boot previous $OLD_VERSION_ROOTFS to get config"
|
||||
qemu-system-aarch64 -M virt \
|
||||
-m 128 \
|
||||
-nodefaults \
|
||||
$CPU_ARGS -smp $CPU_COUNT \
|
||||
-bios /usr/share/qemu/edk2-aarch64-code.fd \
|
||||
-display vnc=:0,websocket=5700 \
|
||||
-vga none -device ramfb \
|
||||
-kernel /var/vm/kernel.bin -append "root=fe00 console=tty0" \
|
||||
-blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/storage/${OLD_VERSION_ROOTFS} \
|
||||
-device virtio-blk-pci,drive=hd0 \
|
||||
-device qemu-xhci -device usb-kbd
|
||||
|
||||
# Get config backup from old rootfs
|
||||
mount /storage/$OLD_VERSION_ROOTFS /mnt
|
||||
rm /mnt/etc/rc.local
|
||||
mv /mnt/etc/rc.local.disabled /mnt/etc/rc.local
|
||||
|
||||
if [ -f /mnt/root/backup/config.tar.gz ]; then
|
||||
cp /mnt/root/backup/config.tar.gz /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz
|
||||
else
|
||||
error "No config.tar.gz in $OLD_VERSION_ROOTFS found"
|
||||
fi
|
||||
chroot /mnt/ mkdir -p /var/lock
|
||||
chroot /mnt /sbin/sysupgrade -k -b /tmp/openwrt_config.tar.gz
|
||||
mv /mnt/tmp/openwrt_config.tar.gz /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz
|
||||
umount /mnt
|
||||
|
||||
# Put config backup to new rootfs
|
||||
mount $FILE /mnt
|
||||
mkdir -p /mnt/root/backup
|
||||
cp /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz /mnt/root/backup/config.tar.gz
|
||||
|
||||
mv /mnt/etc/rc.local /mnt/etc/rc.local.disabled
|
||||
cp /var/vm/openwrt_additional/restore_backup_rc.local /mnt/etc/rc.local
|
||||
chmod +x /mnt/etc/rc.local
|
||||
umount /mnt
|
||||
|
||||
info "Boot current rootfs-${OPENWRT_VERSION}.img to install config"
|
||||
qemu-system-aarch64 -M virt \
|
||||
-m 128 \
|
||||
-nodefaults \
|
||||
$CPU_ARGS -smp $CPU_COUNT \
|
||||
-bios /usr/share/qemu/edk2-aarch64-code.fd \
|
||||
-display vnc=:0,websocket=5700 \
|
||||
-vga none -device ramfb \
|
||||
-kernel /var/vm/kernel.bin -append "root=fe00 console=tty0" \
|
||||
-blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=${FILE} \
|
||||
-device virtio-blk-pci,drive=hd0 \
|
||||
-device qemu-xhci -device usb-kbd
|
||||
|
||||
mount $FILE /mnt
|
||||
rm /mnt/etc/rc.local
|
||||
mv /mnt/etc/rc.local.disabled /mnt/etc/rc.local
|
||||
mv /mnt/root/backup/config.tar.gz /mnt/root/backup/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz
|
||||
chroot /mnt/ mkdir -p /var/lock
|
||||
chroot /mnt /sbin/sysupgrade -r /root/backup/config.tar.gz
|
||||
umount /mnt
|
||||
|
||||
# Finally remove old rootfs and old files
|
||||
|
||||
+11
-2
@@ -28,7 +28,7 @@ Help()
|
||||
# Process the input options. Add options as needed. #
|
||||
############################################################
|
||||
# Get the options
|
||||
while getopts ":hvrqs" option; do
|
||||
while getopts ":hvrqsSVR" option; do
|
||||
case $option in
|
||||
h) # display Help
|
||||
Help
|
||||
@@ -45,10 +45,19 @@ while getopts ":hvrqs" option; do
|
||||
s) # Shutdown VM
|
||||
echo -ne '{ "execute": "qmp_capabilities" } { "execute": "system_powerdown" }' | nc -w 1 -U /run/qmp-sock
|
||||
exit;;
|
||||
S) # Shutdown VM grateful, package qemu-ga needs to be installed
|
||||
echo -ne '{"execute":"guest-shutdown"}' | nc -w 1 -U /run/qga.sock
|
||||
exit;;
|
||||
V) # Get VM infos, package qemu-ga needs to be installed
|
||||
echo -ne '{"execute":"guest-get-osinfo"}' | nc -w 1 -U /run/qga.sock
|
||||
exit;;
|
||||
R) # Reboot, package qemu-ga needs to be installed
|
||||
echo -ne '{"execute": "guest-exec", "arguments": { "path": "reboot"}}' | nc -w 1 -U /run/qga.sock
|
||||
exit;;
|
||||
\?) # Invalid option
|
||||
echo "Error: Invalid option"
|
||||
Help
|
||||
exit;;
|
||||
esac
|
||||
esac
|
||||
done
|
||||
|
||||
|
||||
@@ -13,8 +13,13 @@ fi
|
||||
CURRENT_VERSION_ROOTFS=`cat /storage/current_version`
|
||||
|
||||
# Stop openwrt
|
||||
echo "****** Stop openwrt ******"
|
||||
supervisorctl stop openwrt
|
||||
echo "****** shutdown openwrt ******"
|
||||
/run/qemu_qmp.sh -S
|
||||
|
||||
until ! supervisorctl status openwrt | grep RUNNING
|
||||
do
|
||||
sleep 1
|
||||
done
|
||||
|
||||
# Get config
|
||||
echo "****** Mount $CURRENT_VERSION_ROOTFS ******"
|
||||
@@ -22,6 +27,7 @@ mount /storage/$CURRENT_VERSION_ROOTFS /mnt
|
||||
|
||||
echo "****** Restore $2 ******"
|
||||
cp $2 /mnt/tmp/openwrt_config.tar.gz
|
||||
chroot /mnt/ mkdir -p /var/lock
|
||||
chroot /mnt /sbin/sysupgrade -r /tmp/openwrt_config.tar.gz
|
||||
|
||||
echo "****** umount $CURRENT_VERSION_ROOTFS ******"
|
||||
|
||||
+6
-1
@@ -9,6 +9,7 @@ trap - ERR
|
||||
. /run/migrate_openwrt_rootfs.sh
|
||||
|
||||
VERS=$(qemu-system-aarch64 --version | head -n 1 | cut -d '(' -f 1)
|
||||
FILE=/storage/rootfs-${OPENWRT_VERSION}.img
|
||||
|
||||
attach_eth_if () {
|
||||
HOST_IF=$1
|
||||
@@ -120,7 +121,11 @@ qemu-system-aarch64 -M virt \
|
||||
$LAN_ARGS \
|
||||
$WAN_ARGS \
|
||||
$USB_ARGS \
|
||||
-qmp unix:/run/qmp-sock,server=on,wait=off
|
||||
-qmp unix:/run/qmp-sock,server=on,wait=off \
|
||||
-chardev socket,path=/run/qga.sock,server=on,wait=off,id=qga0 \
|
||||
-device virtio-serial \
|
||||
-device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0
|
||||
|
||||
|
||||
# -device virtio-net,netdev=qlan1 -netdev user,id=qlan1,net=192.168.1.0/24,hostfwd=tcp::8000-192.168.1.1:80 \
|
||||
# -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/var/vm/openwrt-armsr-armv8-generic-ext4-combined.img \
|
||||
|
||||
Reference in New Issue
Block a user