@@ -1,115 +1,115 | |||||
1 | #!/bin/sh |
|
1 | #!/bin/sh | |
2 | # This file contains utility functions used by rpi23-gen-image.sh |
|
2 | # This file contains utility functions used by rpi23-gen-image.sh | |
3 |
|
3 | |||
4 | cleanup (){ |
|
4 | cleanup (){ | |
5 | set +x |
|
5 | set +x | |
6 | set +e |
|
6 | set +e | |
7 |
|
7 | |||
8 | # Remove exports from nexmon |
|
8 | # Remove exports from nexmon | |
9 | unset KERNEL |
|
9 | unset KERNEL | |
10 | unset ARCH |
|
10 | unset ARCH | |
11 | unset SUBARCH |
|
11 | unset SUBARCH | |
12 | unset CCPLUGIN |
|
12 | unset CCPLUGIN | |
13 | unset ZLIBFLATE |
|
13 | unset ZLIBFLATE | |
14 | unset Q |
|
14 | unset Q | |
15 | unset NEXMON_SETUP_ENV |
|
15 | unset NEXMON_SETUP_ENV | |
16 | unset HOSTUNAME |
|
16 | unset HOSTUNAME | |
17 | unset PLATFORMUNAME |
|
17 | unset PLATFORMUNAME | |
18 |
|
18 | |||
19 | # Identify and kill all processes still using files |
|
19 | # Identify and kill all processes still using files | |
20 | echo "killing processes using mount point ..." |
|
20 | echo "killing processes using mount point ..." | |
21 | fuser -k "${R}" |
|
21 | fuser -k "${R}" | |
22 | sleep 3 |
|
22 | sleep 3 | |
23 | fuser -9 -k -v "${R}" |
|
23 | fuser -9 -k -v "${R}" | |
24 |
|
24 | |||
25 | # Clean up temporary .password file |
|
25 | # Clean up temporary .password file | |
26 | if [ -r ".password" ] ; then |
|
26 | if [ -r ".password" ] ; then | |
27 | shred -zu .password |
|
27 | shred -zu .password | |
28 | fi |
|
28 | fi | |
29 |
|
29 | |||
30 | # Clean up all temporary mount points |
|
30 | # Clean up all temporary mount points | |
31 | echo "removing temporary mount points ..." |
|
31 | echo "removing temporary mount points ..." | |
32 | umount -l "${R}/proc" 2> /dev/null |
|
32 | umount -l "${R}/proc" 2> /dev/null | |
33 | umount -l "${R}/sys" 2> /dev/null |
|
33 | umount -l "${R}/sys" 2> /dev/null | |
34 | umount -l "${R}/dev/pts" 2> /dev/null |
|
34 | umount -l "${R}/dev/pts" 2> /dev/null | |
35 | umount "$BUILDDIR/mount/boot/firmware" 2> /dev/null |
|
35 | umount "$BUILDDIR/mount/boot/firmware" 2> /dev/null | |
36 | umount "$BUILDDIR/mount" 2> /dev/null |
|
36 | umount "$BUILDDIR/mount" 2> /dev/null | |
37 | cryptsetup close "${CRYPTFS_MAPPING}" 2> /dev/null |
|
37 | cryptsetup close "${CRYPTFS_MAPPING}" 2> /dev/null | |
38 | losetup -d "$ROOT_LOOP" 2> /dev/null |
|
38 | losetup -d "$ROOT_LOOP" 2> /dev/null | |
39 | losetup -d "$FRMW_LOOP" 2> /dev/null |
|
39 | losetup -d "$FRMW_LOOP" 2> /dev/null | |
40 | trap - 0 1 2 3 6 |
|
40 | trap - 0 1 2 3 6 | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | chroot_exec() { |
|
43 | chroot_exec() { | |
44 | # Exec command in chroot |
|
44 | # Exec command in chroot | |
45 | LANG=C LC_ALL=C DEBIAN_FRONTEND=noninteractive chroot "${R}" "$@" |
|
45 | LANG=C LC_ALL=C DEBIAN_FRONTEND=noninteractive chroot "${R}" "$@" | |
46 | } |
|
46 | } | |
47 |
|
47 | |||
48 | as_nobody() { |
|
48 | as_nobody() { | |
49 | # Exec command as user nobody |
|
49 | # Exec command as user nobody | |
50 | sudo -E -u nobody LANG=C LC_ALL=C "$@" |
|
50 | sudo -E -u nobody LANG=C LC_ALL=C "$@" | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | install_readonly() { |
|
53 | install_readonly() { | |
54 | # Install file with user read-only permissions |
|
54 | # Install file with user read-only permissions | |
55 | install -o root -g root -m 644 "$@" |
|
55 | install -o root -g root -m 644 "$@" | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | install_exec() { |
|
58 | install_exec() { | |
59 | # Install file with root exec permissions |
|
59 | # Install file with root exec permissions | |
60 | install -o root -g root -m 744 "$@" |
|
60 | install -o root -g root -m 744 "$@" | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | use_template () { |
|
63 | use_template () { | |
64 | # Test if configuration template file exists |
|
64 | # Test if configuration template file exists | |
65 | if [ ! -r "./templates/${CONFIG_TEMPLATE}" ] ; then |
|
65 | if [ ! -r "./templates/${CONFIG_TEMPLATE}" ] ; then | |
66 | echo "error: configuration template ${CONFIG_TEMPLATE} not found" |
|
66 | echo "error: configuration template ${CONFIG_TEMPLATE} not found" | |
67 | exit 1 |
|
67 | exit 1 | |
68 | fi |
|
68 | fi | |
69 |
|
69 | |||
70 | # Load template configuration parameters |
|
70 | # Load template configuration parameters | |
71 | . "./templates/${CONFIG_TEMPLATE}" |
|
71 | . "./templates/${CONFIG_TEMPLATE}" | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | chroot_install_cc() { |
|
74 | chroot_install_cc() { | |
75 | # Install c/c++ build environment inside the chroot |
|
75 | # Install c/c++ build environment inside the chroot | |
76 | if [ -z "${COMPILER_PACKAGES}" ] ; then |
|
76 | if [ -z "${COMPILER_PACKAGES}" ] ; then | |
77 | COMPILER_PACKAGES=$(chroot_exec apt-get -s install g++ make bc | grep "^Inst " | awk -v ORS=" " '{ print $2 }') |
|
77 | COMPILER_PACKAGES=$(chroot_exec apt-get -s install g++ make bc | grep "^Inst " | awk -v ORS=" " '{ print $2 }') | |
78 | # Install COMPILER_PACKAGES in chroot |
|
78 | # Install COMPILER_PACKAGES in chroot | |
79 |
chroot_exec apt-get -q -y --allow-unauthenticated --no-install-recommends install |
|
79 | chroot_exec apt-get -q -y --allow-unauthenticated --no-install-recommends install ${COMPILER_PACKAGES} | |
80 | fi |
|
80 | fi | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | chroot_remove_cc() { |
|
83 | chroot_remove_cc() { | |
84 | # Remove c/c++ build environment from the chroot |
|
84 | # Remove c/c++ build environment from the chroot | |
85 | if [ -n "${COMPILER_PACKAGES}" ] ; then |
|
85 | if [ -n "${COMPILER_PACKAGES}" ] ; then | |
86 | chroot_exec apt-get -qq -y --auto-remove purge "${COMPILER_PACKAGES}" |
|
86 | chroot_exec apt-get -qq -y --auto-remove purge "${COMPILER_PACKAGES}" | |
87 | COMPILER_PACKAGES="" |
|
87 | COMPILER_PACKAGES="" | |
88 | fi |
|
88 | fi | |
89 | } |
|
89 | } | |
90 | # GPL v2.0 - #https://github.com/sakaki-/bcmrpi3-kernel-bis/blob/master/conform_config.sh |
|
90 | # GPL v2.0 - #https://github.com/sakaki-/bcmrpi3-kernel-bis/blob/master/conform_config.sh | |
91 | set_kernel_config() { |
|
91 | set_kernel_config() { | |
92 | # flag as $1, value to set as $2, config must exist at "./.config" |
|
92 | # flag as $1, value to set as $2, config must exist at "./.config" | |
93 | TGT="CONFIG_${1#CONFIG_}" |
|
93 | TGT="CONFIG_${1#CONFIG_}" | |
94 | REP="${2}" |
|
94 | REP="${2}" | |
95 | if grep -q "^${TGT}[^_]" .config; then |
|
95 | if grep -q "^${TGT}[^_]" .config; then | |
96 | sed -i "s/^\(${TGT}=.*\|# ${TGT} is not set\)/${TGT}=${REP}/" .config |
|
96 | sed -i "s/^\(${TGT}=.*\|# ${TGT} is not set\)/${TGT}=${REP}/" .config | |
97 | else |
|
97 | else | |
98 | echo "${TGT}"="${2}" >> .config |
|
98 | echo "${TGT}"="${2}" >> .config | |
99 | fi |
|
99 | fi | |
100 | } |
|
100 | } | |
101 | # unset kernel config parameter |
|
101 | # unset kernel config parameter | |
102 | unset_kernel_config() { |
|
102 | unset_kernel_config() { | |
103 | # unsets flag with the value of $1, config must exist at "./.config" |
|
103 | # unsets flag with the value of $1, config must exist at "./.config" | |
104 | TGT="CONFIG_${1#CONFIG_}" |
|
104 | TGT="CONFIG_${1#CONFIG_}" | |
105 | sed -i "s/^${TGT}=.*/# ${TGT} is not set/" .config |
|
105 | sed -i "s/^${TGT}=.*/# ${TGT} is not set/" .config | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 | # https://serverfault.com/a/682849 - converts e.g. /24 to 255.255.255.0 |
|
108 | # https://serverfault.com/a/682849 - converts e.g. /24 to 255.255.255.0 | |
109 | cdr2mask () |
|
109 | cdr2mask () | |
110 | { |
|
110 | { | |
111 | # Number of args to shift, 255..255, first non-255 byte, zeroes |
|
111 | # Number of args to shift, 255..255, first non-255 byte, zeroes | |
112 | set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0 |
|
112 | set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0 | |
113 | [ $1 -gt 1 ] && shift $1 || shift |
|
113 | [ $1 -gt 1 ] && shift $1 || shift | |
114 | echo ${1-0}.${2-0}.${3-0}.${4-0} |
|
114 | echo ${1-0}.${2-0}.${3-0}.${4-0} | |
115 | } No newline at end of file |
|
115 | } |
General Comments 0
Vous devez vous connecter pour laisser un commentaire.
Se connecter maintenant