-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The 'flash.sh' script makes image flashing easy and hassle-free, both for compressed and uncompressed images. Signed-off-by: Ivan Nikolaenko <[email protected]> Co-authored-by: Valentin Kharin <[email protected]>
- Loading branch information
1 parent
388a8b1
commit 217e7a6
Showing
6 changed files
with
115 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
{ | ||
coreutils, | ||
util-linux, | ||
writeShellApplication, | ||
zstd, | ||
}: | ||
writeShellApplication { | ||
name = "flash-script"; | ||
runtimeInputs = [ | ||
coreutils | ||
util-linux | ||
zstd | ||
]; | ||
text = builtins.readFile ./flash.sh; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env bash | ||
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Function to print usage and exit | ||
print_usage() { | ||
echo "Usage: $0 -d <disk> -i <imagefile>" | ||
exit 1 | ||
} | ||
|
||
if [ "$EUID" -ne 0 ]; then | ||
echo "Please run as root" | ||
exit | ||
fi | ||
|
||
# Check the number of parameters | ||
if [ "$#" -ne 4 ]; then | ||
print_usage | ||
fi | ||
|
||
# Parse the parameters | ||
while getopts "d:i:" opt; do | ||
case $opt in | ||
d) DEVICE="$OPTARG" ;; | ||
i) FILENAME="$OPTARG" ;; | ||
*) print_usage ;; | ||
esac | ||
done | ||
|
||
# Check if disk and imagefile exist | ||
if [ ! -e "$DEVICE" ]; then | ||
echo "No such block device: ${DEVICE}" | ||
exit 1 | ||
fi | ||
|
||
if [ ! -e "$FILENAME" ]; then | ||
echo "No such file: ${FILENAME}" | ||
exit 1 | ||
fi | ||
|
||
# Function to wipe any ZFS leftovers exising on the disk | ||
wipe_filesystem () { | ||
echo "Wiping filesystem..." | ||
# Set sector size to 512 bytes | ||
SECTOR=512 | ||
# 10 MiB in 512-byte sectors | ||
MIB_TO_SECTORS=20480 | ||
# Disk size in 512-byte sectors | ||
SECTORS=$(blockdev --getsz "$DEVICE") | ||
# Unmount possible mounted filesystems | ||
sync; umount -q "$DEVICE"* || true; | ||
# Wipe first 10MiB of disk | ||
dd if=/dev/zero of="$DEVICE" bs="$SECTOR" count="$MIB_TO_SECTORS" conv=fsync status=none | ||
# Wipe last 10MiB of disk | ||
dd if=/dev/zero of="$DEVICE" bs="$SECTOR" count="$MIB_TO_SECTORS" seek="$((SECTORS - MIB_TO_SECTORS))" conv=fsync status=none | ||
echo "Flashing..." | ||
} | ||
|
||
echo "Found ${FILENAME}..." | ||
|
||
# Check the extension of the image file and run appropriate command | ||
if [[ "$FILENAME" == *.zst ]]; then | ||
wipe_filesystem | ||
zstdcat "$FILENAME" | dd of="$DEVICE" bs=32M status=progress conv=fsync oflag=direct iflag=fullblock | ||
elif [[ "$FILENAME" == *.iso || "$FILENAME" == *.img ]]; then | ||
wipe_filesystem | ||
dd if="$FILENAME" of="$DEVICE" bs=32M status=progress conv=fsync oflag=direct iflag=fullblock | ||
else | ||
echo "Unsupported file format" | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters