Commit 58bdaf4488f50bf68ce6e22387e8eb37148a31ac

Authored by Tim Gover
1 parent 78e1830e

make-boot-image: Use losetup instead of udisksctl

This has fewer dependencies and avoids problems where the temporary
disk image is automounted.
Readme.md
... ... @@ -112,7 +112,7 @@ e.g. `root=/dev/mmcblk0p2` for the normal partition on CM4 EMMC.
112 112 #### Create the boot image
113 113 The `-p` product argument (pi4,pi400,cm4) tells the script to discard files which are not required by that product. This makes the image smaller and reduces the time taken to calculate the hash of the image file thereby reducing the boot time.
114 114 ```bash
115   -../tools/make-boot-image -d secure-boot-files -o boot.img -p pi4
  115 +sudo ../tools/make-boot-image -d secure-boot-files -o boot.img -p pi4
116 116 ```
117 117  
118 118 #### Sign the boot image
... ...
tools/make-boot-image
... ... @@ -8,6 +8,7 @@ IMAGE_SIZE=0
8 8 MEGABYTE=$((1024 * 1024))
9 9 BOOT_MOUNT=""
10 10 LOOP=""
  11 +NAME=$(basename $0)
11 12  
12 13 # Define these environment variables to override mkfs options
13 14 SECTOR_SIZE=${SECTOR_SIZE:-512}
... ... @@ -20,7 +21,6 @@ FAT_OVERHEAD=${FAT_OVERHEAD:-64}
20 21 cleanup() {
21 22 unmount_image
22 23  
23   - [ -z "${TMP_DIR}" ] && return
24 24 if [ -d "${TMP_DIR}" ]; then
25 25 rm -rf "${TMP_DIR}"
26 26 fi
... ... @@ -56,26 +56,23 @@ createfs() {
56 56 mountfs() {
57 57 image="$1"
58 58  
59   - LOOP=$(udisksctl loop-setup -f "${image}" \
60   - | grep loop \
61   - | sed 's/.*\/dev\/loop\([0-9]*\).*/\/dev\/loop\1/')
62   - [ -e "${LOOP}" ] || die "Failed to create loop device"
  59 + LOOP=$(losetup -f)
  60 + losetup ${LOOP} "${image}"
  61 + [ -e "${LOOP}" ] || die "Failed to create loop device ${LOOP}"
63 62  
64   - BOOT_MOUNT=$(udisksctl mount --options rw -b "${LOOP}" | sed 's/.*Mounted \/dev\/.* at \(.*\)\.$/\1/')
  63 + BOOT_MOUNT=$(mktemp -d)
  64 + mount "${LOOP}" "${BOOT_MOUNT}"
65 65 [ -d "${BOOT_MOUNT}" ] || die "Failed to mount bootfs @ ${BOOT_MOUNT}"
66 66  
67 67 echo "Mounted ${LOOP} @ ${BOOT_MOUNT}"
68 68 }
69 69  
70 70 unmount_image() {
71   - if [ -d "${BOOT_MOUNT}" ]; then
72   - udisksctl unmount -b "${LOOP}" > /dev/null || true
73   - BOOT_MOUNT=""
74   - fi
75   - if [ -e "${LOOP}" ];then
76   - udisksctl loop-delete -b "${LOOP}"
77   - LOOP=""
  71 + image="$1"
  72 + if [ -f "${LOOP}" ]; then
  73 + umount "${LOOP}" > /dev/null || true
78 74 fi
  75 + losetup -d "${LOOP}"
79 76 }
80 77  
81 78  
... ... @@ -85,7 +82,7 @@ createstaging() {
85 82 board="$3"
86 83  
87 84 mkdir -p "${staging}" || die "Failed to create ${staging}"
88   - cp -a "${source_dir}/"* "${staging}"
  85 + cp -r "${source_dir}/"* "${staging}"
89 86  
90 87 # Remove files for previous hardware version
91 88 if [ "${board}" = "pi4" ] || [ "${board}" = "pi400" ] || [ "${board}" = "cm4" ]; then
... ... @@ -133,15 +130,12 @@ checkDependencies() {
133 130 if [ ! -f /sbin/mkfs.fat ]; then
134 131 die "mkfs.fat is requried. Run this script on Linux"
135 132 fi
136   -
137   - if ! command -v udisksctl; then
138   - die "udisksctl ot found. Try installing the udisks2 package."
139   - fi
140 133 }
141 134  
142 135 usage() {
143 136 cat <<EOF
144   -make-boot-image -d SOURCE_DIR -o OUTPUT
  137 +sudo ${NAME} -d SOURCE_DIR -o OUTPUT
  138 +
145 139 Options:
146 140 -a Select 32 or 64 bit kernel
147 141 -b Optionally prune the files to those required for the given board type.
... ... @@ -150,10 +144,10 @@ Options:
150 144  
151 145 Examples:
152 146 # Include all files in bootfs/
153   -make-boot-image -d bootfs/ -o boot.img
  147 +sudo ${NAME} -d bootfs/ -o boot.img
154 148  
155 149 # Include only the files from bootfs/ required by Pi 4B
156   -make-boot-image -b pi4 -d bootfs/ -o boot.img
  150 +sudo ${NAME} -b pi4 -d bootfs/ -o boot.img
157 151  
158 152 Environment variables:
159 153 The following environment variables may be specified to optionally override mkfs.vfat
... ... @@ -194,6 +188,7 @@ checkDependencies
194 188  
195 189 [ -d "${SOURCE_DIR}" ] || usage
196 190 [ -n "${OUTPUT}" ] || usage
  191 +[ "$(id -u)" = "0" ] || die "\"${NAME}\" must be run as root. Try \"sudo ${NAME}\""
197 192  
198 193 trap cleanup EXIT
199 194 TMP_DIR="$(mktemp -d)"
... ... @@ -208,10 +203,10 @@ createfs ${IMAGE_SIZE} &quot;${TMP_IMAGE}&quot;
208 203  
209 204 echo "Copying files"
210 205 mountfs "${TMP_IMAGE}"
211   -cp -a "${staging}"/* "${BOOT_MOUNT}"
  206 +cp -rv "${staging}"/* "${BOOT_MOUNT}"
212 207  
213 208 echo "Sync"
214   -unmount_image
  209 +unmount_image "${IMAGE}"
215 210 cp -f "${TMP_IMAGE}" "${OUTPUT}"
216 211  
217 212 echo "Created image"
... ...