-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivialfs_gen
executable file
·134 lines (131 loc) · 2.75 KB
/
trivialfs_gen
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
132
133
134
#! /bin/sh
HDR_KEY=TrivialFS
HDR_VAL=80a29844-f5e3-11e3-b1c1-b827eb896db5
# Generates a new trivial file system recursively from the contents of the
# current directory. Symbolic links will automatically be transformed into
# hard links within the generated file system. The generated file system will
# either be written to the file or block device given as the only argument, or
# to standard output if invoked without arguments.
#
# trivialfs_gen version 2016.118
#
# Copyright (c) 2014-2016 Guenther Brunthaler. All rights reserved.
#
# This script is free software.
# Distribution is permitted under the terms of the GPLv3.
cleanup() {
local rc=$?
for t in "$T" "$T2" "$T3"
do
test -n "$t" && rm -- "$t"
done
test $rc = 0 || echo "${0##*/} failed!" >& 2
}
set -e
T=; T2=; T3=
trap cleanup 0
FSUUID=
FSLABEL=
VERBOSE=
OUT=
DUMP=
while getopts L:l:dvtru: OPT
do
case $OPT in
v) VERBOSE=y;;
u) FSUUID=$OPTARG;;
L | l) FSLABEL=$OPTARG;;
r | t) FSUUID=`uuidgen -$OPT`;;
d) DUMP=y;;
*) fail
esac
done
shift `expr $OPTIND - 1`
test -n "$1" && { OUT=$1; shift; }
test $# = 0
if test -n "$DUMP"
then
# Perform a hex dump with decimal offsets for stdin or file argument.
set -- hexdump -e '"%10_ad" 16/1 " %02x"' -e '" |" 16 "%_p" "|\n"'
test -n "$OUT" && set -- "$@" "$OUT"
exec "$@"
fi
test -z "$FSUUID" && FSUUID=`uuidgen -r`
tempfile() {
mktemp -- "${TMPDIR:-/tmp}/${0##*/}".XXXXXXXXXX
}
T=`tempfile`; T2=`tempfile`; T3=`tempfile`
find -type f -o -type l | cut -d / -f2- | LC_ALL=C sort \
| while IFS= read -r fn
do
stat -Lc "%i |%n" -- "$fn"
done | sort -sn -k2 | {
cat <<- .
$HDR_KEY=$HDR_VAL
COMPATIBLE_VERSION=3
UUID=$FSUUID
LABEL=$FSLABEL
ACTUAL_VERSION=3
CREATED=`date -u +%s`
.
start=0
li=
while read -r i fn
do
if test x"$li" != x"$i"
then
fn=${fn#?}
test -f "$fn"
off=$start
size=`stat -c %s -- "$fn"`
test $size = 0 && off=0
echo "@$off+$size=$fn"
cat "$fn" >> "$T"
start=`expr $start + $size`
li=$i
else
echo "$fn"
fi
done
echo EOF
} > "$T2"
lsize=0
its=0
while
size=`stat -c %s -- "$T2"`
test $lsize != $size
do
test $lsize -lt $size
# Add increment of $size relative to $lsize to all offsets in $T2.
inc=`expr $size - $lsize`
cat "$T2" > "$T3"
while read -r LINE
do
case $LINE in
"@"*)
rest="+${LINE#*+}"
case $rest in
"+0="*) ;;
*)
start=${LINE%"$rest"}
start=${start#?}
start=`expr $start + $inc`
LINE="@"$start$rest
esac
esac
echo "$LINE"
done < "$T3" > "$T2"
lsize=$size
its=`expr $its + 1`
done
cat "$T" >> "$T2"
if test -n "$OUT"
then
cat "$T2" > "$OUT"
else
cat "$T2"
tty -s <& 1 && echo "================" >& 2
fi
echo "Trivial FS creation successful." >& 2
test -n "$VERBOSE" && echo \
"$its iterations for offset correction were required." >& 2