-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace2sing
executable file
·472 lines (418 loc) · 11.5 KB
/
trace2sing
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/bin/sh
#
# Copyright (c) 2017 Cedric Clerget - HPC Center of Franche-Comte University
#
# http://github.com/mesocentrefc/trace2sing
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
LC_ALL="C"
export LC_ALL
current="$(pwd)"
dump="$(mktemp -d)"
fifo="$(mktemp -u)"
perl_script="$dump/t2s.pl"
pid="0"
if [ -z "$TRACE2SING_OUTPUT" ]; then
output="singrun.img"
else
output="$TRACE2SING_OUTPUT"
fi
pfatal () {
echo "$1" >&2
if [ -e "/proc/$pid/exe" ]; then
kill -9 "$pid"
fi
if [ -e "$fifo" ]; then
rm -f "$fifo"
fi
if [ -e "$dump" ]; then
rm -rf "$dump"
fi
if [ -e "$perl_script" ]; then
rm -f "$perl_script"
fi
exit 1
}
if [ $# -lt 1 ]; then
pfatal "usage: $0 command [arguments]"
fi
if [ -z "$(which perl)" ]; then
pfatal "[ERROR]: perl is missing, you must install it first"
fi
if [ -z "$(which strace)" ]; then
pfatal "[ERROR]: strace is missing, you must install it first"
fi
if [ -z "$(which sed)" ]; then
pfatal "[ERROR]: sed is missing, you must install it first"
fi
# -y option greatly simplify processing of file descriptors
strace -y -o /dev/null /bin/true 2> /dev/null
if [ $? != 0 ]; then
version="$(strace -V)"
pfatal "[ERROR]: strace version >= 4.7 is required, current: $version"
fi
# syscalls to trace
syscalls="open,execve,chdir,fchdir,openat,oldstat,stat,stat64"
strace -e trace=execveat -o /dev/null /bin/true 2> /dev/null
if [ $? = 0 ]; then
syscalls="$syscalls,execveat"
fi
trap 'pfatal "Execution was interrupted"' INT
split_path () {
path="${1%*/}"
split=""
if [ "$path" != "${path#/}" ]; then
while [ "$path" != "" ]; do
split="$path $split"
path="${path%/*}"
done
fi
echo "$split"
}
relmkdir () {
rdir="$(readlink -f $1)"
dir="$1"
if [ "$rdir" != "$dir" ]; then
for path in "$(split_path $dir)"; do
if [ ! -e "$dump$path" ]; then
if [ -L "$path" ]; then
relmkdir "$(readlink -f $path)"
cp -a "$path" "$dump$path" 2> /dev/null
else
mkdir "$dump$path" 2> /dev/null
fi
fi
done
else
if [ "$dir" != "${dir#/}" ]; then
d="$dump$dir"
else
d="$dump/$dir"
fi
mkdir -p "$d" 2> /dev/null
fi
}
copy () {
relmkdir "$(dirname $1)"
if [ "$1" != "${1#/}" ]; then
filename="$dump$1"
else
filename="$dump/$1"
fi
cp -a "$1" "$filename" 2> /dev/null
# take shebang script into account
# as it's executed by kernel ... no trace
line="$(sed -n '1{s/^#![[:blank:]]*//p;q}' $1 2> /dev/null)"
if [ ! -z "$line" ]; then
duplicate "${line% *}"
fi
}
duplink () {
copy "$(readlink -f $1)"
copy "$1"
target="$(readlink $1)"
if [ "$target" != "${target#/}" ]; then
linkval="$target"
else
linkval="$(dirname $1)/$target"
fi
copy "$linkval"
if [ -L "$linkval" ]; then
duplicate "$linkval"
fi
}
duplicate () {
if [ -e "$1" ]; then
if [ -d "$1" ]; then
relmkdir "$1"
elif [ -f "$1" ]; then
if [ -L "$1" ]; then
duplink "$1"
else
file="$(readlink -f $1)"
if [ "$1" != "$file" ]; then
copy "$file"
fi
copy "$1"
fi
fi
fi
}
mkfifo -m 0600 "$fifo"
# Parse and remove duplicate files
cat > "$perl_script" << 'PERL'
use strict;
use warnings;
my $fifo = $ARGV[0];
my $current = $ARGV[1];
my $dumpdir = $ARGV[2];
my $fifo_fh;
open($fifo_fh, "<", $fifo) or die "fifo \"$fifo\" doesn't exist.";
open(my $envfile, '>', "$dumpdir/environment");
foreach (sort keys %ENV) {
if (!($_ =~ /^(DISPLAY|SHELL|SSH)/)) {
print $envfile "export $_=\"$ENV{$_}\"\n";
}
}
print $envfile "export SHELL=\"/bin/sh\"\n";
close $envfile;
my %pids;
my %files;
while (<$fifo_fh>) {
if ($_ =~ /([0-9]+)(.*)?\b(open|openat|execve|execveat|chdir|fchdir|oldstat|stat|stat64)\b\((.*)/) {
my $pid = $1;
my $syscall = $3;
if (!$pids{$pid}) {
$pids{$pid} = $current;
}
if ($4 =~ /(.*)(\)\s*=\s*([^-][0-9]+)|<unfinished)/) {
my $content = $1;
my $filename = "";
if ($syscall eq "execve" || $syscall eq "execveat") {
if ($content =~ /"PWD=([^"]*)"/) {
$pids{$pid} = $1;
} elsif ($content =~ /"OLDPWD=([^"]*)"/) {
$pids{$pid} = $1;
}
if ($content =~ /^"([^"]*)"/) {
$filename = $1;
} elsif ($content =~ /^([0-9]+<(.*)>|AT_FDCWD)(.*)"(.*)"/) {
if ($1 eq "AT_FDCWD") {
$filename = $pids{$pid} . "/" . $4;
} else {
$filename = $2 . "/" . $4;
}
}
$current = $pids{$pid};
} elsif ($syscall eq "chdir") {
if ($content =~ /^"(.*)"/) {
my $first = substr $1, 0, 1;
if ($first eq "/") {
$pids{$pid} = $1;
} else {
$pids{$pid} = $pids{$pid} . "/" . $1;
}
$current = $pids{$pid};
}
} elsif ($syscall eq "fchdir") {
if ($content =~ /^[0-9]+<(.*)>/) {
my $first = substr $1, 0, 1;
if ($first eq "/") {
$pids{$pid} = $1;
} else {
$pids{$pid} = $pids{$pid} . "/" . $1;
}
$current = $pids{$pid};
}
} else {
if ($syscall eq "openat") {
if ($content =~ /^([0-9]+<(.*)>|AT_FDCWD)(.*)"(.*)"/) {
if ($1 eq "AT_FDCWD") {
$filename = $pids{$pid} . "/" . $4;
} else {
$filename = $2 . "/" . $4;
}
}
} elsif ($content =~ /^"([^"]*)"/) {
$filename = $1;
}
}
if ($filename ne "") {
if (!($filename =~ /^(\/dev|\/proc|\/run|\/sys)/)) {
if (!$files{$filename}) {
$files{$filename} = 1;
print $pids{$pid} . "#" . $filename . "\n";
}
}
}
}
}
}
close $fifo_fh;
exit(0);
PERL
perl "$perl_script" "$fifo" "$current" "$dump" | while read line; do
current="${line%#*}"
filepath="${line#*#}"
if [ "$filepath" != "${filepath#/}" ]; then
duplicate "$filepath"
else
duplicate "$current/$filepath"
fi
done &
pid="$!"
cmdline=""
for arg in "$@"; do
if [ "$arg" != "${arg% *}" ]; then
if [ "$arg" != "${arg%\'}" ]; then
carg="\"$arg\""
else
carg="'$arg'"
fi
else
carg="$arg"
fi
if [ "$cmdline" != "" ]; then
cmdline="$cmdline $carg"
else
cmdline="$carg"
fi
done;
echo "Tracing execution of '$cmdline'"
strace -qq -v -f -y \
-e trace=$syscalls \
-s 256 \
-e abbrev=none \
-o "$fifo" \
/bin/sh -c "$cmdline"
status=$?
if [ $status = 0 ]; then
echo "Generating self-extracting $output archive ... please wait"
fi
wait "$pid"
rm -f "$fifo"
rm -f "$perl_script"
if [ $status != 0 ]; then
pfatal "[ERROR]: Failed to execute $cmdline"
fi
install -d -m 0755 "$dump/dev"
install -d -m 0755 "$dump/bin"
install -d -m 0755 "$dump/boot"
install -d -m 0755 "$dump/home"
install -d -m 0755 "$dump/etc"
install -d -m 0750 "$dump/root"
install -d -m 0755 "$dump/proc"
install -d -m 0755 "$dump/sys"
install -d -m 0755 "$dump/run"
install -d -m 1777 "$dump/tmp"
install -d -m 1777 "$dump/var/tmp"
install -d -m 0755 "$dump/realhome"
if [ -z "$(which strings)" ]; then
loader="$(grep -m 1 -o -a -E "\/[0-9\/\.a-zA-Z_-]?+" /bin/sh)"
else
loader="$(strings /bin/sh|head -n 1)"
fi
duplicate "$loader"
duplicate "$HOME"
duplicate "$current"
cat > "$dump/.run" << EOF
#!/bin/sh
. /environment
cd $current
$cmdline
EOF
chmod 0755 "$dump/.run"
cat > "$dump/.exec" << EOF
#!/bin/sh
. /environment
exec "\$@"
EOF
chmod 0755 "$dump/.exec"
cat > "$dump/.shell" << EOF
#!/bin/sh
. /environment
exec \$SHELL "\$@"
EOF
chmod 0755 "$dump/.shell"
echo -n "" > "$dump/etc/resolv.conf"
echo -n "" > "$dump/etc/hosts"
echo -n "" > "$dump/etc/passwd"
echo -n "" > "$dump/etc/group"
echo -n "" > "$dump/etc/localtime"
cat > "$current/$output" <<"EOF"
#!/bin/sh
#
# Generated by trace2sing: http://github.com/mesocentrefc/trace2sing
#
extract_archive () {
sed '0,/^#EOF#$/d' $0
}
launch () {
current=$(pwd)
cmd=$1
directory=$(mktemp -d)
root="$directory/singrun_rootfs"
mkdir $root
extract_archive | tar -xzf - -C $root
env|sed -n 's/^SENV_\(.*\)=\(.*\)$/export \1="\2"/p' >> $root/environment
if [ "$cmd" = "exec" ]; then
shift 2
args="$@"
singularity $cmd -w $root $args
else
cd $directory
singularity $cmd -w -H $HOME:/realhome $root
fi
code=$?
cd $current
rm -rf $directory
exit $code
}
usage () {
echo "$0 usage:"
echo " -x or [--extract] : to extract file tree in singrun_rootfs directory"
echo " -s or [--shell] : to launch a shell into container"
echo " -e or [--exec] args : to execute a command into container"
echo " -l or [--list] : to list container files tree"
echo " -d or [--display-env] : display exported environment variables"
exit 1
}
if [ "$#" -gt 0 ]; then
case $1 in
-x|--extract)
echo "Extracting file tree into singrun_rootfs directory"
if [ -d "singrun_rootfs" ]; then
echo "There is already a directory singrun_rootfs"
exit 1
else
mkdir singrun_rootfs
extract_archive | tar -xzf - -C singrun_rootfs
exit $?
fi
;;
-l|--list)
echo "List file tree"
extract_archive | tar -ztvf -
exit $?
;;
-d|--display-env)
echo "Extracting environment variables"
dir=$(mktemp -d -p .)
extract_archive | tar -zxvf - -C $dir ./environment > /dev/null
cat $dir/environment
rm -rf $dir
exit $?
;;
-s|--shell)
echo "Run shell into container"
launch "shell"
;;
-e|--exec)
echo "Execute command into container"
launch "exec" "$@"
;;
*|-h|--help)
usage
;;
esac
fi
launch "run"
exit 0
#EOF#
EOF
cd "$dump"
tar -czf - . >> "$current/$output"
cd "$current"
chmod 0755 "$current/$output"
rm -rf "$dump"