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,7 +112,7 @@ e.g. `root=/dev/mmcblk0p2` for the normal partition on CM4 EMMC.
112 #### Create the boot image 112 #### Create the boot image
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. 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 ```bash 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 #### Sign the boot image 118 #### Sign the boot image
tools/make-boot-image
@@ -8,6 +8,7 @@ IMAGE_SIZE=0 @@ -8,6 +8,7 @@ IMAGE_SIZE=0
8 MEGABYTE=$((1024 * 1024)) 8 MEGABYTE=$((1024 * 1024))
9 BOOT_MOUNT="" 9 BOOT_MOUNT=""
10 LOOP="" 10 LOOP=""
  11 +NAME=$(basename $0)
11 12
12 # Define these environment variables to override mkfs options 13 # Define these environment variables to override mkfs options
13 SECTOR_SIZE=${SECTOR_SIZE:-512} 14 SECTOR_SIZE=${SECTOR_SIZE:-512}
@@ -20,7 +21,6 @@ FAT_OVERHEAD=${FAT_OVERHEAD:-64} @@ -20,7 +21,6 @@ FAT_OVERHEAD=${FAT_OVERHEAD:-64}
20 cleanup() { 21 cleanup() {
21 unmount_image 22 unmount_image
22 23
23 - [ -z "${TMP_DIR}" ] && return  
24 if [ -d "${TMP_DIR}" ]; then 24 if [ -d "${TMP_DIR}" ]; then
25 rm -rf "${TMP_DIR}" 25 rm -rf "${TMP_DIR}"
26 fi 26 fi
@@ -56,26 +56,23 @@ createfs() { @@ -56,26 +56,23 @@ createfs() {
56 mountfs() { 56 mountfs() {
57 image="$1" 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 [ -d "${BOOT_MOUNT}" ] || die "Failed to mount bootfs @ ${BOOT_MOUNT}" 65 [ -d "${BOOT_MOUNT}" ] || die "Failed to mount bootfs @ ${BOOT_MOUNT}"
66 66
67 echo "Mounted ${LOOP} @ ${BOOT_MOUNT}" 67 echo "Mounted ${LOOP} @ ${BOOT_MOUNT}"
68 } 68 }
69 69
70 unmount_image() { 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 fi 74 fi
  75 + losetup -d "${LOOP}"
79 } 76 }
80 77
81 78
@@ -85,7 +82,7 @@ createstaging() { @@ -85,7 +82,7 @@ createstaging() {
85 board="$3" 82 board="$3"
86 83
87 mkdir -p "${staging}" || die "Failed to create ${staging}" 84 mkdir -p "${staging}" || die "Failed to create ${staging}"
88 - cp -a "${source_dir}/"* "${staging}" 85 + cp -r "${source_dir}/"* "${staging}"
89 86
90 # Remove files for previous hardware version 87 # Remove files for previous hardware version
91 if [ "${board}" = "pi4" ] || [ "${board}" = "pi400" ] || [ "${board}" = "cm4" ]; then 88 if [ "${board}" = "pi4" ] || [ "${board}" = "pi400" ] || [ "${board}" = "cm4" ]; then
@@ -133,15 +130,12 @@ checkDependencies() { @@ -133,15 +130,12 @@ checkDependencies() {
133 if [ ! -f /sbin/mkfs.fat ]; then 130 if [ ! -f /sbin/mkfs.fat ]; then
134 die "mkfs.fat is requried. Run this script on Linux" 131 die "mkfs.fat is requried. Run this script on Linux"
135 fi 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 usage() { 135 usage() {
143 cat <<EOF 136 cat <<EOF
144 -make-boot-image -d SOURCE_DIR -o OUTPUT 137 +sudo ${NAME} -d SOURCE_DIR -o OUTPUT
  138 +
145 Options: 139 Options:
146 -a Select 32 or 64 bit kernel 140 -a Select 32 or 64 bit kernel
147 -b Optionally prune the files to those required for the given board type. 141 -b Optionally prune the files to those required for the given board type.
@@ -150,10 +144,10 @@ Options: @@ -150,10 +144,10 @@ Options:
150 144
151 Examples: 145 Examples:
152 # Include all files in bootfs/ 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 # Include only the files from bootfs/ required by Pi 4B 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 Environment variables: 152 Environment variables:
159 The following environment variables may be specified to optionally override mkfs.vfat 153 The following environment variables may be specified to optionally override mkfs.vfat
@@ -194,6 +188,7 @@ checkDependencies @@ -194,6 +188,7 @@ checkDependencies
194 188
195 [ -d "${SOURCE_DIR}" ] || usage 189 [ -d "${SOURCE_DIR}" ] || usage
196 [ -n "${OUTPUT}" ] || usage 190 [ -n "${OUTPUT}" ] || usage
  191 +[ "$(id -u)" = "0" ] || die "\"${NAME}\" must be run as root. Try \"sudo ${NAME}\""
197 192
198 trap cleanup EXIT 193 trap cleanup EXIT
199 TMP_DIR="$(mktemp -d)" 194 TMP_DIR="$(mktemp -d)"
@@ -208,10 +203,10 @@ createfs ${IMAGE_SIZE} &quot;${TMP_IMAGE}&quot; @@ -208,10 +203,10 @@ createfs ${IMAGE_SIZE} &quot;${TMP_IMAGE}&quot;
208 203
209 echo "Copying files" 204 echo "Copying files"
210 mountfs "${TMP_IMAGE}" 205 mountfs "${TMP_IMAGE}"
211 -cp -a "${staging}"/* "${BOOT_MOUNT}" 206 +cp -rv "${staging}"/* "${BOOT_MOUNT}"
212 207
213 echo "Sync" 208 echo "Sync"
214 -unmount_image 209 +unmount_image "${IMAGE}"
215 cp -f "${TMP_IMAGE}" "${OUTPUT}" 210 cp -f "${TMP_IMAGE}" "${OUTPUT}"
216 211
217 echo "Created image" 212 echo "Created image"