summaryrefslogtreecommitdiffstats
path: root/server/includes/packing.inc
blob: 2891aeef6c30ea7472cc264b04bdcd416b435a76 (plain) (blame)
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

#
# generate initramfs of directory
# usage:
#	generate_initramfs <source_dir> <files> <destination_dir/filename>
# example:
#	generate_initramfs "./server/boot/stage32_sqfs" "./mnt/openslx.sqfs" "./server/boot/initramfs2"
#	generate_initramfs "./server/build/stage31" "." "./server/boot/initramfs"
generate_initramfs() {
	[ $# -lt 3 ] && perror "Sanity check failed: generate_initramfs needs at least three params, but $# were given."
	cd "$1" || perror "Cannot cd to '$1'"
	rm -f -- "$3"

	find $2 | cpio --format="newc" --create | xz --check=crc32 --lzma2=dict=1MiB > "$3"
	local PS=(${PIPESTATUS[*]})
	[ "x${PS[0]}" != "x0" ] && perror "'find $2' in '$(pwd)' failed."
	[ "x${PS[1]}" != "x0" ] && perror "cpio create failed."
	[ "x${PS[2]}" != "x0" ] && perror "gzip to '$3' failed."
	cd - > /dev/null
	pinfo "Created initramfs of $1 at $3"
	pinfo "Size: $(du -bsh "$3" | awk 'END {print $1}')"
}

# <dir> <list_file>
md5list() {
	(
		cd "$1" || perror "Cannot cd to $1"
		while IFS='' read -r line || [[ -n "$line" ]]; do
			md5sum "$line"
		done < "$2"
	)
}

# generates squashfs of directory
# usage:
#	generate_squashfs <source_dir> <destination_dir/filename> [ignore_root]
generate_squashfs() {
	[ $# -lt 2 ] && perror "Sanity check failed: generate_squashfs needs at least two params, but $# were given."
	[ -d "$1" ] || perror "$1 is not a directory."
	local IGNORE=$(mktemp)
	truncate -s 0 "$IGNORE"
	if [ -n "$3" ]; then
		[ -d "$3" ] || perror "Ignore dir does not exist!"
		local list_this=$(mktemp)
		local list_other=$(mktemp)
		local list_common=$(mktemp)
		local hash_this=$(mktemp)
		local hash_other=$(mktemp)
		( cd "$3" && find -type f | sort > "$list_other" )
		( cd "$1" && find -type f | sort > "$list_this" )
		comm -12 "$list_this" "$list_other" > "$list_common"
		echo "Common to both:"
		cat "$list_common"
		md5list "$3" "$list_common" | sort > "$hash_other"
		md5list "$1" "$list_common" | sort > "$hash_this"
		echo "Hash other:"
		cat "$hash_other"
		echo "Hash this:"
		cat "$hash_this"
		comm -12 "$hash_this" "$hash_other" > "$list_common"
		echo "Identical:"
		cat "$list_common"
		awk '{gsub(/^\.?\//, "", $2); print $2}' "$list_common" > "$IGNORE"
		rm -f -- "$list_this" "$list_other" "$hash_this" "$hash_other" "$list_common"
	fi
	# Detect mksquashfs xz support
	local PARAMS
	if mksquashfs 2>&1 | grep -A 8 '^-comp' | grep -q '\sxz'; then
		pinfo "Using xz compression"
		PARAMS="-comp xz"
	else
		pinfo "Using default compression"
		PARAMS=""
	fi
	rm -f -- "$2" # -no-exports ?
	mksquashfs "$1" "$2" -ef "$IGNORE" $PARAMS -b 1M -no-recovery -always-use-fragments >&6 \
		|| perror "mksquashfs failed ($?)."
	pinfo "Created squashfs of $1 at $2"
	pinfo "Size: $(du -bsh "$2" | awk 'END {print $1}')"
	rm -f -- "$IGNORE"
}