30 lines
752 B
Bash
Executable File
30 lines
752 B
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
DEVICE="$1"
|
|
FAKE_DEV="/tmp/dev"
|
|
|
|
mkdir -p "$FAKE_DEV"
|
|
PARENT_NAME=$(basename "$DEVICE")
|
|
|
|
echo "Refreshing partition table for $DEVICE..."
|
|
partx -u "$DEVICE" 2>/dev/null || partx -a "$DEVICE" || true
|
|
|
|
# Remove old fake nodes for this loop device first
|
|
find "$FAKE_DEV" -maxdepth 1 -type b -name "${PARENT_NAME}*" -exec rm -f {} \;
|
|
|
|
lsblk -rn -o NAME,MAJ:MIN "$DEVICE" | while read -r NAME MAJMIN; do
|
|
# Skip the parent loop device itself
|
|
if [[ "$NAME" == "$PARENT_NAME" ]]; then
|
|
continue
|
|
fi
|
|
|
|
PART_PATH="$FAKE_DEV/$NAME"
|
|
MAJOR="${MAJMIN%%:*}"
|
|
MINOR="${MAJMIN##*:}"
|
|
|
|
echo "Creating node: $PART_PATH (b $MAJOR $MINOR)"
|
|
rm -f "$PART_PATH"
|
|
mknod "$PART_PATH" b "$MAJOR" "$MINOR"
|
|
done
|