From 9c4e23a656bfd7b2f0c40feafadbce1b8d651aa8 Mon Sep 17 00:00:00 2001
From: lejeune quentin <qle@odoo.com>
Date: Tue, 26 Nov 2019 08:33:13 +0000
Subject: [PATCH] [IMP] point_of_sale: Refactoring the "build" of the IoT Box

The use of an emulator (qemu) for the IoT Box build posed compatibility and performance issues.
So we have the Raspbian filesystem to install and configure packages.

closes odoo/odoo#40839

Task: 2145445
Signed-off-by: Quentin Lejeune (qle) <qle@odoo.com>
---
 .../etc/init_posbox_image.sh                  | 42 +++++--------------
 .../tools/posbox/posbox_create_image.sh       | 41 ++++++++----------
 .../tools/posbox/posbox_download_images.sh    |  7 ----
 3 files changed, 27 insertions(+), 63 deletions(-)
 delete mode 100755 addons/point_of_sale/tools/posbox/posbox_download_images.sh

diff --git a/addons/point_of_sale/tools/posbox/overwrite_before_init/etc/init_posbox_image.sh b/addons/point_of_sale/tools/posbox/overwrite_before_init/etc/init_posbox_image.sh
index 95358c9652c3..9f2e84387702 100755
--- a/addons/point_of_sale/tools/posbox/overwrite_before_init/etc/init_posbox_image.sh
+++ b/addons/point_of_sale/tools/posbox/overwrite_before_init/etc/init_posbox_image.sh
@@ -8,14 +8,14 @@ __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
 __base="$(basename ${__file} .sh)"
 
-# Since we are emulating, the real /boot is not mounted, 
-# leading to mismatch between kernel image and modules.
-mount /dev/sda1 /boot
-
 # Recommends: antiword, graphviz, ghostscript, python-gevent, poppler-utils
 export DEBIAN_FRONTEND=noninteractive
 echo "nameserver 8.8.8.8" >> /etc/resolv.conf
 
+echo "export LANGUAGE=en_US.UTF-8" >> ~/.bashrc
+echo "export LANG=en_US.UTF-8" >> ~/.bashrc
+echo "export LC_ALL=en_US.UTF-8" >> ~/.bashrc
+source ~/.bashrc
 
 apt-get update && apt-get -y upgrade
 # Do not be too fast to upgrade to more recent firmware and kernel than 4.38
@@ -142,30 +142,15 @@ update-rc.d -f nginx remove
 update-rc.d -f dnsmasq remove
 update-rc.d timesyncd defaults
 
-systemctl daemon-reload
 systemctl enable ramdisks.service
 systemctl disable dphys-swapfile.service
 systemctl enable ssh
-
-# USER PI AUTO LOGIN (from nano raspi-config)
-# We take the whole algorithm from raspi-config in order to stay compatible with raspbian infrastructure
-if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
-        SYSTEMD=1
-elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
-        SYSTEMD=0
-else
-        echo "Unrecognised init system"
-        return 1
-fi
-if [ $SYSTEMD -eq 1 ]; then
-    systemctl set-default graphical.target
-    systemctl disable getty@tty1.service
-    systemctl enable autologin@.service
-    systemctl disable systemd-timesyncd.service
-    systemctl disable hostapd.service
-else
-    update-rc.d lightdm enable 2
-fi
+systemctl set-default graphical.target
+systemctl disable getty@tty1.service
+systemctl enable autologin@.service
+systemctl disable systemd-timesyncd.service
+systemctl unmask hostapd.service
+systemctl disable hostapd.service
 
 # disable overscan in /boot/config.txt, we can't use
 # overwrite_after_init because it's on a different device
@@ -174,10 +159,6 @@ fi
 # cf: https://www.raspberrypi.org/documentation/configuration/raspi-config.md
 echo "disable_overscan=1" >> /boot/config.txt
 
-# https://www.raspberrypi.org/forums/viewtopic.php?p=79249
-# to not have "setting up console font and keymap" during boot take ages
-setupcon
-
 # exclude /drivers folder from git info to be able to load specific drivers
 echo "addons/hw_drivers/drivers/" > /home/pi/odoo/.git/info/exclude
 
@@ -190,6 +171,3 @@ create_ramdisk_dir "/var"
 create_ramdisk_dir "/etc"
 create_ramdisk_dir "/tmp"
 mkdir /root_bypass_ramdisks
-umount /dev/sda1
-
-reboot
diff --git a/addons/point_of_sale/tools/posbox/posbox_create_image.sh b/addons/point_of_sale/tools/posbox/posbox_create_image.sh
index 1a5b6e322d3a..d71393c061e7 100755
--- a/addons/point_of_sale/tools/posbox/posbox_create_image.sh
+++ b/addons/point_of_sale/tools/posbox/posbox_create_image.sh
@@ -18,22 +18,22 @@ require_command () {
 }
 
 require_command kpartx
-require_command qemu-system-arm
+require_command qemu-arm-static
 require_command zerofree
 
 __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
 __base="$(basename ${__file} .sh)"
 
-
 MOUNT_POINT="${__dir}/root_mount"
 OVERWRITE_FILES_BEFORE_INIT_DIR="${__dir}/overwrite_before_init"
 OVERWRITE_FILES_AFTER_INIT_DIR="${__dir}/overwrite_after_init"
 VERSION=13.0
 REPO=https://github.com/odoo/odoo.git
 
-if [ ! -f kernel-qemu ] || ! file_exists *raspbian*.img ; then
-    ./posbox_download_images.sh
+if ! file_exists *raspbian*.img ; then
+    wget 'http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30/2019-09-26-raspbian-buster-lite.zip' -O raspbian.img.zip
+    unzip raspbian.img.zip
 fi
 
 cp -a *raspbian*.img posbox.img
@@ -84,8 +84,12 @@ START_OF_ROOT_PARTITION=$(fdisk -l posbox.img | tail -n 1 | awk '{print $2}')
  echo 'p';                          # print
  echo 'w') | fdisk posbox.img       # write and quit
 
-LOOP_MAPPER_PATH=$(kpartx -avs posbox.img | tail -n 1 | cut -d ' ' -f 3)
+LOOP=$(kpartx -avs posbox.img)
+LOOP_MAPPER_PATH=$(echo "${LOOP}" | tail -n 1 | awk '{print $3}')
 LOOP_MAPPER_PATH="/dev/mapper/${LOOP_MAPPER_PATH}"
+LOOP_MAPPER_BOOT=$(echo "${LOOP}" | tail -n 2 | awk 'NR==1 {print $3}')
+LOOP_MAPPER_BOOT="/dev/mapper/${LOOP_MAPPER_BOOT}"
+
 sleep 5
 
 # resize filesystem
@@ -94,30 +98,19 @@ resize2fs "${LOOP_MAPPER_PATH}"
 
 mkdir -p "${MOUNT_POINT}" #-p: no error if existing
 mount "${LOOP_MAPPER_PATH}" "${MOUNT_POINT}"
+mount "${LOOP_MAPPER_BOOT}" "${MOUNT_POINT}/boot/"
+
+QEMU_ARM_STATIC="/usr/bin/qemu-arm-static"
+cp "${QEMU_ARM_STATIC}" "${MOUNT_POINT}/usr/bin/"
 
 # 'overlay' the overwrite directory onto the mounted image filesystem
 cp -a "${OVERWRITE_FILES_BEFORE_INIT_DIR}"/* "${MOUNT_POINT}"
+chroot "${MOUNT_POINT}" /bin/bash -c "sudo /etc/init_posbox_image.sh"
 
 # get rid of the git clone
 rm -rf "${CLONE_DIR}"
 # and the ngrok usr/bin
 rm -rf "${OVERWRITE_FILES_BEFORE_INIT_DIR}/usr"
-
-# get rid of the mount, we have to remount it anyway because we have
-# to "refresh" the filesystem after qemu modified it
-sleep 2
-umount "${MOUNT_POINT}"
-
-# from http://paulscott.co.za/blog/full-raspberry-pi-raspbian-emulation-with-qemu/
-# ssh pi@localhost -p10022
-# as of stretch with newer kernels, the versatile-pb.dtb file is necessary
-QEMU_OPTS=(-kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -dtb versatile-pb.dtb -no-reboot -serial stdio -append 'root=/dev/sda2 rootfstype=ext4 rw' -hda posbox.img -net user,hostfwd=tcp::10022-:22,hostfwd=tcp::18069-:8069 -net nic)
-if [ -z ${DISPLAY:-} ] ; then
-    QEMU_OPTS+=(-nographic)
-fi
-qemu-system-arm "${QEMU_OPTS[@]}"
-
-mount "${LOOP_MAPPER_PATH}" "${MOUNT_POINT}"
 cp -av "${OVERWRITE_FILES_AFTER_INIT_DIR}"/* "${MOUNT_POINT}"
 
 find "${MOUNT_POINT}"/ -type f -name "*.iotpatch"|while read iotpatch; do
@@ -129,11 +122,11 @@ find "${MOUNT_POINT}"/ -type f -name "*.iotpatch"|while read iotpatch; do
 done
 
 # cleanup
-sleep 2
-umount "${MOUNT_POINT}"
-rm -r "${MOUNT_POINT}"
+umount -f "${MOUNT_POINT}"/boot
+umount -f "${MOUNT_POINT}"
 
 echo "Running zerofree..."
 zerofree -v "${LOOP_MAPPER_PATH}" || true
 
 kpartx -d posbox.img
+rm -rf "${MOUNT_POINT}"
diff --git a/addons/point_of_sale/tools/posbox/posbox_download_images.sh b/addons/point_of_sale/tools/posbox/posbox_download_images.sh
deleted file mode 100755
index e810d0f91331..000000000000
--- a/addons/point_of_sale/tools/posbox/posbox_download_images.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/sh
-
-wget 'http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30/2019-09-26-raspbian-buster-lite.zip' -O raspbian.img.zip
-unzip raspbian.img.zip
-wget 'https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.14.79-stretch' -O kernel-qemu
-wget 'https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb'
-
-- 
GitLab