Skip to content

Commit

Permalink
v8.22
Browse files Browse the repository at this point in the history
- DietPi-Imager | Handle image size correctly in 512 sectors when adding setup FAT partition, convert to bytes afterwards. Obtain requried free disk space correctly if no 7z archive is generated.
  • Loading branch information
MichaIng committed Sep 11, 2023
1 parent b553331 commit 4fb188a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions .build/images/dietpi-imager
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,15 @@
fi

# Derive target image size from last partition end + 1 sector
IMAGE_SIZE=$(( ( $(sfdisk -qlo End "$FP_SOURCE" | tail -1) + 1 ) * 512 )) # 512 byte sectors => bytes
IMAGE_SIZE=$(( $(sfdisk -qlo End "$FP_SOURCE" | tail -1) + 1 )) # 512 byte sectors

# Add trailing FAT partition to simplify first run setup if requested
# - WARNING: this assumes that the partitions in the table are in order (which we do in other places as well)
if (( $ADD_DOS_PART ))
then
G_DIETPI-NOTIFY 2 'Adding a 1 MiB FAT partition to simplify first run setup'
# Increase source image size if required
[[ $SOURCE_TYPE == 'Image' ]] && (( $(stat -c '%s' "$FP_SOURCE_IMG") < $IMAGE_SIZE + 1048576 )) && G_EXEC truncate -s "$(( $IMAGE_SIZE + 1048576 ))" "$FP_SOURCE_IMG" && G_EXEC losetup -c "$FP_SOURCE"
[[ $SOURCE_TYPE == 'Image' ]] && (( $(stat -c '%s' "$FP_SOURCE_IMG") < $IMAGE_SIZE * 512 + 1048576 )) && G_EXEC truncate -s "$(( $IMAGE_SIZE * 512 + 1048576 ))" "$FP_SOURCE_IMG" && G_EXEC losetup -c "$FP_SOURCE"
# Add new DOS partition
local type='EBD0A0A2-B9E5-4433-87C0-68B6B72699C7' # https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
[[ $PART_TABLE_TYPE == 'dos' ]] && type='c'
Expand Down Expand Up @@ -549,14 +549,15 @@ they have UNIX style LF line endings:
_EOF_
G_EXEC umount "$root_mountpoint" "$fat_mountpoint"
G_EXEC rmdir "$root_mountpoint" "$fat_mountpoint"
((IMAGE_SIZE+=1048576))
((IMAGE_SIZE+=2048))
fi

# Exit now if source shall be shrunk only
(( $SHRINK_ONLY )) && exit 0

# Add 33 (34 overall) sectors for GPT backup partition table
[[ $PART_TABLE_TYPE == 'gpt' ]] && IMAGE_SIZE=$(( $IMAGE_SIZE + 33*512 ))
[[ $PART_TABLE_TYPE == 'gpt' ]] && ((IMAGE_SIZE+=33))
IMAGE_SIZE=$(( $IMAGE_SIZE * 512 )) # 512 byte sectors => bytes

# Image file source and dd target
if [[ $FP_SOURCE_IMG && $CLONING_TOOL == 'dd' ]]
Expand All @@ -571,13 +572,15 @@ _EOF_
[[ $(readlink -f "$PWD/$OUTPUT_IMG_NAME.$OUTPUT_IMG_EXT") != "$(readlink -f "$FP_SOURCE_IMG")" ]] && G_EXEC mv "$FP_SOURCE_IMG" "$OUTPUT_IMG_NAME.$OUTPUT_IMG_EXT"

# Check for sufficient free disk space to store the 7z archive with 100 MiB buffer
G_CHECK_FREESPACE . $(( $IMAGE_SIZE * 15/100 / 1024**2 + 100 )) || exit 1
(( $SKIP_ARCHIVE )) || G_CHECK_FREESPACE . $(( $IMAGE_SIZE * 15/100 / 1024**2 + 100 )) || exit 1

# Drive source and dd target
elif [[ $CLONING_TOOL == 'dd' ]]
then
# Check for sufficient free disk space to store the image and the 7z archive with 100 MiB buffer
G_CHECK_FREESPACE . $(( $IMAGE_SIZE * 115/100 / 1024**2 + 100 )) || exit 1
# Check for sufficient free disk space to store the image and in case the 7z archive with 100 MiB buffer
local free_space_percent=100
(( $SKIP_ARCHIVE )) || free_space_percent=115 # 15% image size for 7z archive
G_CHECK_FREESPACE . $(( $IMAGE_SIZE * $free_space_percent/100 / 1024**2 + 100 )) || exit 1

G_DIETPI-NOTIFY 2 "Creating final image with actually used size: $(( $IMAGE_SIZE / 1024**2 + 1 )) MiB"
G_EXEC_OUTPUT=1 G_EXEC dd if="$FP_SOURCE" of="$OUTPUT_IMG_NAME.$OUTPUT_IMG_EXT" bs=1M status=progress count=$(( $IMAGE_SIZE / 1024**2 + 1 ))
Expand Down Expand Up @@ -736,7 +739,6 @@ _EOF_
(( $(free -m | mawk '/Mem:/{print $2}') < 1750 && $(nproc) > 2 )) && limit_threads=('-mmt2')
[[ -f $OUTPUT_IMG_NAME.7z ]] && G_EXEC rm "$OUTPUT_IMG_NAME.7z"
[[ ( -t 0 || -t 1 ) && $TERM != 'dumb' ]] && G_EXEC_OUTPUT=1
# Add "-bsp1 -bso1 -bse2" to print output to regular STDOUT and STDERR, else the pipe to "tee" makes it omit at least the progress output.
G_EXEC_DESC='Creating final 7zip archive' G_EXEC "$c7zz" a -mx=9 "${limit_threads[@]}" "$OUTPUT_IMG_NAME.7z" "$OUTPUT_IMG_NAME.$OUTPUT_IMG_EXT" hash.txt README.md

G_EXEC_NOHALT=1 G_EXEC rm hash.txt README.md
Expand Down
2 changes: 1 addition & 1 deletion dietpi/dietpi-software
Original file line number Diff line number Diff line change
Expand Up @@ -11710,7 +11710,7 @@ _EOF_
esac

local fallback_url="https://github.com/restic/restic/releases/download/v0.15.2/restic_0.15.2_linux_$arch.bz2"
Download_Install "$(curl -sSfL 'https://api.github.com/repos/restic/restic/releases/latest' | mawk -F\" "/^ * \"browser_download_url\": \".*\/restic_[^\"\/]*_linux_$arch\.bz2\"$/{print \$4}")" /usr/local/bin/restic
Download_Install "$(curl -sSfL 'https://api.github.com/repos/restic/restic/releases/latest' | mawk -F\" "/^ *\"browser_download_url\": \".*\/restic_[^\"\/]*_linux_$arch\.bz2\"$/{print \$4}")" /usr/local/bin/restic
G_EXEC chmod +x /usr/local/bin/restic
fi

Expand Down

0 comments on commit 4fb188a

Please sign in to comment.