Skip to content

Commit d39fc26

Browse files
oem-factory-reset: Move format confirmation before resetting anything
Move confirmation of formatting flash drive with LUKS percentage selection before any reset actions have been taken, so aborting does not result in a half-reset system. Combine with the more basic "confirm" prompt that existed after selecting the device (but did not include the LUKS size information). Split up prepare_flash_drive into interactive_prepare_flash_drive (both prompts and formats as before), confirm_thumb_drive_format (just confirms the selections), and prepare_thumb_drive (now noninteractive). Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
1 parent a925219 commit d39fc26

File tree

2 files changed

+67
-23
lines changed

2 files changed

+67
-23
lines changed

initrd/bin/oem-factory-reset

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,10 @@ select_thumb_drive_for_key_material() {
434434
warn "If the next operation fails, try with a bigger thumb drive"
435435
fi
436436

437-
thumb_drive_size_message="$(display_size "$disk_size_bytes")"
438-
# confirm with user size of thumb drive to be wiped
439-
whiptail --title "Confirm thumb drive to be wiped" --yesno \
440-
"Are you sure you want to wipe the following thumb drive?\n\n$FILE\n\nSize: $thumb_drive_size_message" 0 0
441-
if [ $? -ne 0 ]; then
437+
select_luks_container_size_percent
438+
thumb_drive_luks_percent="$(cat /tmp/luks_container_size_percent)"
439+
440+
if ! confirm_thumb_drive_format "$FILE" "$thumb_drive_luks_percent"; then
442441
warn "Thumb drive wipe aborted by user!"
443442
continue
444443
fi
@@ -452,8 +451,6 @@ select_thumb_drive_for_key_material() {
452451
die "No USB storage device detected! User decided to not wipe any thumb drive"
453452
fi
454453
done
455-
456-
select_luks_container_size_percent
457454
thumb_drive_luks_percent="$(cat /tmp/luks_container_size_percent)"
458455
}
459456

@@ -468,7 +465,7 @@ wipe_thumb_drive_and_copy_gpg_key_material() {
468465
thumb_drive_luks_percent="$2"
469466

470467
#Wipe thumb drive with a LUKS container of size $(cat /tmp/luks_container_size_percent)
471-
prepare_thumb_drive --device "$thumb_drive" --percentage "$thumb_drive_luks_percent" --pass "${ADMIN_PIN}"
468+
prepare_thumb_drive "$thumb_drive" "$thumb_drive_luks_percent" "${ADMIN_PIN}"
472469
#Export master key and subkeys to thumb drive first partition
473470
export_master_key_subkeys_and_revocation_key_to_private_LUKS_container --mode rw --device "$thumb_drive"1 --mountpoint /media --pass "${ADMIN_PIN}"
474471
#Export public key to thumb drive's public partition

initrd/etc/luks-functions

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ select_luks_container_size_percent() {
5151
fi
5252
}
5353

54-
#Partition a device with two partitions: a first one being a LUKS container containing private ext4 partition and second public exfat partition
54+
# Partition a device interactively with two partitions: a LUKS container
55+
# containing private ext4 partition and second public exFAT partition
5556
# Size provisioning is done by percentage of the device
56-
prepare_thumb_drive()
57+
interactive_prepare_thumb_drive()
5758
{
58-
TRACE "Under /etc/luks-functions:prepare_thumb_drive()"
59+
TRACE "Under /etc/luks-functions:interactive_prepare_thumb_drive()"
5960
#Refactoring: only one parameter needed to be prompted for: the passphrase for LUKS container if not coming from oem-provisioning
6061
#If no passphrase was provided, ask user to select passphrase for LUKS container
6162
# if no device provided as parameter, we will ask user to select device to partition
@@ -84,6 +85,7 @@ prepare_thumb_drive()
8485
;;
8586
*)
8687
echo "usage: prepare_thumb_drive [--device device] [--percentage percentage] [--pass passphrase]"
88+
return 1
8789
;;
8890
esac
8991
done
@@ -171,31 +173,76 @@ prepare_thumb_drive()
171173
PERCENTAGE=$(cat /tmp/luks_container_size_percent)
172174
fi
173175

176+
confirm_thumb_drive_format "$DEVICE" "$PERCENTAGE" ||
177+
die "User cancelled wiping and repartitioning of $DEVICE"
178+
179+
prepare_thumb_drive "$DEVICE" "$PERCENTAGE" "$PASSPHRASE"
180+
}
181+
182+
# Show a prompt to confirm formatting a flash drive with a percentage allocated
183+
# to LUKS. interactive_prepare_thumb_drive() uses this; during OEM reset it is
184+
# used separately before performing any reset actions
185+
#
186+
# parameters:
187+
# $1 - block device of flash drive
188+
# $2 - percent of device allocated to LUKS [1-99]
189+
confirm_thumb_drive_format()
190+
{
191+
TRACE "Under /etc/luks-functions:confirm_thumb_drive_format()"
192+
local DEVICE LUKS_PERCENTAGE DISK_SIZE_BYTES DISK_SIZE_DISPLAY LUKS_PERCENTAGE LUKS_SIZE_MB MSG
193+
194+
DEVICE="$1"
195+
LUKS_PERCENTAGE="$2"
196+
197+
LUKS_SIZE_MB=
174198

175-
#Get disk size in bytes from fdisk
199+
#Get disk size in bytes
176200
DISK_SIZE_BYTES="$(blockdev --getsize64 "$DEVICE")"
201+
DISK_SIZE_DISPLAY="$(display_size "$DISK_SIZE_BYTES")"
177202
#Convert disk size to MB
178203
DISK_SIZE_MB=$((DISK_SIZE_BYTES/1024/1024))
179-
#Get size in bytes from percentage and apply percentage to DISK_SIZE_MB
180-
PERCENTAGE_MB="$((DISK_SIZE_MB*PERCENTAGE/100))"
204+
#Calculate percentage of device in MB
205+
LUKS_SIZE_MB="$((DISK_SIZE_BYTES*LUKS_PERCENTAGE/100/1024/1024))"
181206

182-
#Console and whiptail $BG_COLOR_WARNING prompt (Y/n) validate one last time wiping and repartitioning of $device of total size $DISK_SIZE_MB with $PERCENTAGE_MB assigned to LUKS encrypted private partition
207+
MSG="WARNING: Wiping and repartitioning $DEVICE ($DISK_SIZE_DISPLAY) with $LUKS_SIZE_MB MB\n assigned to private LUKS ext4 partition,\n rest assigned to exFAT public partition.\n\nAre you sure you want to continue?"
183208
if [ -x /bin/whiptail ]; then
184-
whiptail $BG_COLOR_WARNING --title "WARNING: Wiping and repartitioning $DEVICE of $DISK_SIZE_MB MB" --yesno \
185-
"WARNING: Wiping and repartitioning $DEVICE with $PERCENTAGE_MB MB\n assigned to private LUKS contained private ext4 partition,\n rest assigned to extfat public partition.\n\nAre you sure you want to continue?" 0 80 \
186-
|| die "User cancelled wiping and repartitioning of $DEVICE"
209+
whiptail $BG_COLOR_WARNING --title "WARNING: Wiping and repartitioning $DEVICE ($DISK_SIZE_DISPLAY)" --yesno \
210+
"$MSG" 0 80
187211
else
188-
echo -e -n "Warning: Wiping and repartitioning $DEVICE with $PERCENTAGE_MB MB assigned to private LUKS contained private ext4 partition, rest assigned to extfat public partition.\n\nAre you sure you want to continue?"
212+
echo -e -n "$MSG"
189213
read -r -p " [Y/n] " response
190214
#transform response to uppercase with bash parameter expansion
191215
response=${response^^}
192-
#continue if response different then uppercase N
193-
if [[ $response =~ ^(N)$ ]]; then
194-
die "User cancelled wiping and repartitioning of $DEVICE"
216+
#continue if response is Y, y, or empty, abort for anything else
217+
if [ -n "$response" ] && [ "${response^^}" != Y ]; then
218+
return 1
195219
fi
196220
fi
221+
}
222+
223+
# Prepare a flash drive with a private LUKS-encrypted ext4 partition and a
224+
# public exFAT partition. This is not interactive - during OEM reset, any
225+
# selections/confirmations must occur before OEM reset starts resetting the
226+
# system.
227+
#
228+
# $1 - block device of flash drive
229+
# $2 - percentage of flash drive to allocate to LUKS [1-99]
230+
# $3 - passphrase for LUKS container
231+
prepare_thumb_drive()
232+
{
233+
TRACE "Under /etc/luks-functions:prepare_thumb_drive()"
234+
235+
local DEVICE PERCENTAGE PASSPHRASE DISK_SIZE_BYTES PERCENTAGE_MB
236+
DEVICE="$1"
237+
PERCENTAGE="$2"
238+
PASSPHRASE="$3"
239+
240+
#Get disk size in bytes
241+
DISK_SIZE_BYTES="$(blockdev --getsize64 "$DEVICE")"
242+
#Calculate percentage of device in MB
243+
PERCENTAGE_MB="$((DISK_SIZE_BYTES*PERCENTAGE/100/1024/1024))"
197244

198-
echo -e "Preparing $DEVICE with $PERCENTAGE_MB MB for private LUKS container while rest of device will be assigned to extfat public partition...\n"
245+
echo -e "Preparing $DEVICE with $PERCENTAGE_MB MB for private LUKS container while rest of device will be assigned to exFAT public partition...\n"
199246
echo "Please wait..."
200247
DEBUG "Creating empty DOS partition table on device through fdisk to start clean"
201248
echo -e "o\nw\n" | fdisk $DEVICE >/dev/null 2>&1 || die "Error creating partition table"

0 commit comments

Comments
 (0)