forked from fsfw-dresden/usb-live-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-distribution-image
executable file
·131 lines (102 loc) · 4.84 KB
/
create-distribution-image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
. "$(dirname $(realpath "${0}"))/functions.sh"
. "$(dirname $(realpath "${0}"))/functions.bash"
cd_repo_root
# let failures in pipe fail the whole pipe
set -o pipefail
check_help_wanted() {
# give some rudimentary help if requested
case "${1}" in
"-h")
;&
"--help")
print_info "usage is ${0} ISO_FILE [INSTALL_VARIANT [SIZE_MB_PARTITION_FAT32]]"
exit
;;
esac
}
release_loop_device() {
# Skip in case loop device is released already (should not happen)
losetup "${IMAGE_LOOP_DEVICE}" > /dev/null || return
print_info "Releasing ${IMAGE_LOOP_DEVICE}"
grep "${IMAGE_LOOP_DEVICE}" /proc/mounts && print_warn "Mounted partitions still around, will be released after this exit trap has completed"
losetup --verbose --detach "${IMAGE_LOOP_DEVICE}"
}
setup_loop_device() {
# connect file to loop device
losetup --partscan --verbose --show --nooverlap --find "${1}"
}
get_matching_install_variant() {
for INSTALL_VARIANT in variants.install/*/
do
INSTALL_VARIANT=$(basename "${INSTALL_VARIANT}")
[[ "${LIVE_IMAGE}" =~ .*${INSTALL_VARIANT}.* ]] \
&& echo "${INSTALL_VARIANT}" \
&& return
done
# wasn't able to find a match
print_warn "Could not find a matching install variant from image name, defaulting to 'Schulstick'"
echo "Schulstick"
}
check_parameters() {
# Make sure that first parameter is live-image ISO file
LIVE_IMAGE="${1##*/}"
[ -n "${LIVE_IMAGE}" ] && [ -s "artifacts/${LIVE_IMAGE}" ] || die "ISO not given or empty file"
# The "install variant" defines the set of install procedures to be executed
[ -n "${2}" ] && {
[ -d "variants.install/${2}" ] && INSTALL_VARIANT="${2}" || {
print_warn "Invalid install variant '${2}' given, trying to detect"; false
}
} || INSTALL_VARIANT=$(get_matching_install_variant)
# Set exchange / EFI partition size to 1.5G if not given as 2nd parameter
[ -n "${3}" ] && SIZE_MB_PARTITION_FAT32=${3} || \
SIZE_MB_PARTITION_FAT32=1500
}
calculate_partition_sizes() {
# Calculate ISO / "live-system" partition size
SIZE_MB_SPACE_BUFFER=150 # Same buffer as in stick-install
SIZE_MB_LIVE_IMAGE=$(( $(stat --dereference --format=%s "artifacts/${LIVE_IMAGE}") / (1024 * 1024) ))
SIZE_MB_PARTITION_LIVE_IMAGE=$(( SIZE_MB_LIVE_IMAGE + SIZE_MB_SPACE_BUFFER ))
print_info "Using ${SIZE_MB_LIVE_IMAGE}MiB ISO artifacts/${LIVE_IMAGE} as live image"
SIZE_MB_OFFSET=16
SIZE_MB_PARTITION_PERSISTENCE=256
SIZE_MB_ROUNDING_MARGIN=16
SIZE_MB_IMAGE=$(( SIZE_MB_OFFSET + SIZE_MB_PARTITION_FAT32 + SIZE_MB_PARTITION_LIVE_IMAGE \
+ SIZE_MB_PARTITION_PERSISTENCE + 2 * SIZE_MB_ROUNDING_MARGIN ))
}
create_image_loop() {
# Create image file and make it loop
TARGET_IMAGE_FILE="${LIVE_IMAGE%.iso}.img"
TARGET_BMAP="${LIVE_IMAGE%.iso}.bmap"
truncate --size="${SIZE_MB_IMAGE}M" "build/${TARGET_IMAGE_FILE}"
IMAGE_LOOP_DEVICE=$(setup_loop_device "build/${TARGET_IMAGE_FILE}")
print_info "Looped ${SIZE_MB_IMAGE}MiB image build/${TARGET_IMAGE_FILE} as ${IMAGE_LOOP_DEVICE}"
# free loop device again on premature script termination
trap "release_loop_device" EXIT SIGHUP SIGQUIT SIGTERM
}
create_image_main(){
STARTTIME=$(date +%s)
is_root_user_or_die
check_help_wanted "${@}"
check_parameters "${@}"
calculate_partition_sizes
create_image_loop
print_info "Invoking stick-install ${IMAGE_LOOP_DEVICE} artifacts/${LIVE_IMAGE} ${INSTALL_VARIANT} ${SIZE_MB_PARTITION_FAT32}"
scripts/stick-install "${IMAGE_LOOP_DEVICE}" "artifacts/${LIVE_IMAGE}" "${INSTALL_VARIANT}" "${SIZE_MB_PARTITION_FAT32}"
print_info "Returned from stick-install script"
# Unloop the image and remove the trap to do so on abortion
release_loop_device && trap - EXIT SIGHUP SIGQUIT SIGTERM
# GZ compression is widely and good enough, supported ISO itself is compressed already
print_info "Multi-core gzipping the disk image to create release product"
pigz --independent --blocksize 1024 --stdout < "build/${TARGET_IMAGE_FILE}" | pipemeter > "build/${TARGET_IMAGE_FILE}.gz"
print_info "generating checksum"
( cd build && sha256sum "${TARGET_IMAGE_FILE}.gz" > "${TARGET_IMAGE_FILE}.gz.sha256sum" )
print_info "generating block map for efficient stick writing"
bmaptool create --output "build/${TARGET_BMAP}" "build/${TARGET_IMAGE_FILE}"
print_info "Removing uncompressed image"
rm -v "build/${TARGET_IMAGE_FILE}"
print_info "Moving finished files to artifacts/ folder"
mv -vi "build/${TARGET_IMAGE_FILE}.gz"{,.sha256sum} "build/${TARGET_BMAP}" artifacts/
print_success "Finished distribution image at artifacts/${TARGET_IMAGE_FILE}.gz in $(format_timespan $(($(date +%s) - STARTTIME))), enjoy."
}
create_image_main "${@}"