-
Notifications
You must be signed in to change notification settings - Fork 5
/
posbox_create_image.sh
executable file
·169 lines (135 loc) · 5.25 KB
/
posbox_create_image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# set -o xtrace
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Arguments
# Usage: sudo ./posbox_create_image.sh <branch> <addons>
REPOSITORY="https://github.com/AwesomeFoodCoops/odoo-production"
BRANCH=${1-9.0}
if [ $# -ge 2 ]; then
ADDONS=$2
fi
file_exists() {
[[ -f $1 ]];
}
require_command () {
type "$1" &> /dev/null || { echo "Command $1 is missing. Install it e.g. with 'apt-get install $1'. Aborting." >&2; exit 1; }
}
require_command kpartx
require_command qemu-system-arm
require_command zerofree
require_command curl
__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"
if [ -d "${MOUNT_POINT}" ]; then
umount "${MOUNT_POINT}"
rm -rf "${MOUNT_POINT}"
fi
if [ ! -f kernel-qemu ] || ! file_exists *raspbian*.img ; then
./posbox_download_images.sh
fi
cp -a *raspbian*.img posbox.img
PI_DIR="${OVERWRITE_FILES_BEFORE_INIT_DIR}/home/pi"
CLONE_DIR="$PI_DIR/odoo"
#rm -rf "${CLONE_DIR}"
if [ ! -d "${CLONE_DIR}" ]; then
# Control will enter here if $DIRECTORY doesn't exist.
mkdir -p "${CLONE_DIR}"
wget ${REPOSITORY}/archive/${BRANCH}.zip -O posbox.zip
unzip posbox.zip
mkdir -p "${CLONE_DIR}/addons/"
cp -r odoo-production-$BRANCH/odoo/addons/web ${CLONE_DIR}/addons/
cp -r odoo-production-$BRANCH/odoo/addons/web_kanban ${CLONE_DIR}/addons/
cp -r odoo-production-$BRANCH/odoo/addons/hw_* ${CLONE_DIR}/addons/
mkdir -p ${CLONE_DIR}/addons/point_of_sale/tools/posbox/
cp -r odoo-production-$BRANCH/odoo/addons/point_of_sale/tools/posbox/configuration ${CLONE_DIR}/addons/point_of_sale/tools/posbox/
cp -r odoo-production-$BRANCH/odoo/openerp ${CLONE_DIR}/
cp -r odoo-production-$BRANCH/odoo/odoo.py ${CLONE_DIR}/
cp -r odoo-production-$BRANCH/extra_addons/hw_* ${CLONE_DIR}/addons/
cp -r odoo-production-$BRANCH/OCA_addons/hw_* ${CLONE_DIR}/addons/
cp -r odoo-production-$BRANCH/louve_addons/hw_* ${CLONE_DIR}/addons/
cp -r odoo-production-$BRANCH/intercoop_addons/hw_* ${CLONE_DIR}/addons/
rm -rf posbox.zip
rm -rf odoo-production-$BRANCH
fi
cd "${__dir}"
USR_BIN="${OVERWRITE_FILES_BEFORE_INIT_DIR}/usr/bin/"
if [ ! -d "${USR_BIN}" ]; then
mkdir -p "${USR_BIN}"
cd "/tmp"
curl 'https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip' > ngrok.zip
unzip ngrok.zip
rm ngrok.zip
cd "${__dir}"
mv /tmp/ngrok "${USR_BIN}"
fi
# zero pad the image to be around 3.5 GiB, by default the image is only ~1.3 GiB
dd if=/dev/zero bs=1M count=2048 >> posbox.img
# resize partition table
START_OF_ROOT_PARTITION=$(fdisk -l posbox.img | tail -n 1 | awk '{print $2}')
(echo 'p'; # print
echo 'd'; # delete
echo '2'; # second partition
echo 'n'; # create new partition
echo 'p'; # primary
echo '2'; # number 2
echo "${START_OF_ROOT_PARTITION}"; # starting at previous offset
echo ''; # ending at default (fdisk should propose max)
echo 'p'; # print
echo 'w') | fdisk posbox.img # write and quit
LOOP_MAPPER_PATH=$(kpartx -av posbox.img | tail -n 1 | cut -d ' ' -f 3)
LOOP_MAPPER_PATH="/dev/mapper/${LOOP_MAPPER_PATH}"
sleep 5
# resize filesystem
e2fsck -f "${LOOP_MAPPER_PATH}" # resize2fs requires clean fs
resize2fs "${LOOP_MAPPER_PATH}"
mkdir "${MOUNT_POINT}"
mount "${LOOP_MAPPER_PATH}" "${MOUNT_POINT}"
# 'overlay' the overwrite directory onto the mounted image filesystem
cp -a "${OVERWRITE_FILES_BEFORE_INIT_DIR}"/* "${MOUNT_POINT}"
# 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 -nodefaults -no-reboot -serial stdio -append 'root=/dev/sda2 panic=1 rootfstype=ext4 rw' -hda posbox.img -net user,hostfwd=tcp::10022-:22,hostfwd=tcp::18069-:8069,hostfwd=tcp::10443-:443,hostfwd=tcp::10080-:80 -net nic)
if [ -z ${DISPLAY:-} ] ; then
QEMU_OPTS+=(-nographic)
fi
echo "Booting qemu.."
qemu-system-arm "${QEMU_OPTS[@]}"
echo "Mounting drive.."
mount "${LOOP_MAPPER_PATH}" "${MOUNT_POINT}"
echo "Copying overwrite after init.."
cp -av "${OVERWRITE_FILES_AFTER_INIT_DIR}"/* "${MOUNT_POINT}"
# ADDONS CONFIG
# Replace --load=default,values for custom addons
if [ -z "$ADDONS" ]; then
echo "Using default addons"
else
echo "Patching rc.local with custom addons"
sed -i -E "s/--load=(.*)/--load=$ADDONS" "${MOUNT_POINT}/etc/rc.local"
fi
# cleanup
echo "Cleanup"
sleep 2
umount "${MOUNT_POINT}"
rm -r "${MOUNT_POINT}"
echo "Running zerofree..."
zerofree -v "${LOOP_MAPPER_PATH}" || true
kpartx -d posbox.img