Skip to content

Commit 238b4b9

Browse files
authored
Merge pull request #3 from grycap/links
properly handling links and including the usecases in the documentation
2 parents 25baa0f + f9f73cb commit 238b4b9

File tree

9 files changed

+318
-102
lines changed

9 files changed

+318
-102
lines changed

README.md

Lines changed: 75 additions & 87 deletions
Large diffs are not rendered by default.

minicon

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,22 @@ function copy() {
216216
# copies one file (or folder) to the same destination in the root filesystem
217217
# - it does not overwrite contents
218218
local SRC="$1"
219-
local FILE="$2"
219+
220+
if [ "$SRC" == "." -o "$SRC" == ".." ]; then
221+
p_warning "cowardly refusing to copy folder $SRC"
222+
return
223+
fi
224+
220225
local DST
221226
if [ "$VERBOSE" == "true" ]; then
222227
p_info " processing $SRC... "
223228
fi
224229
if [ ! -e "$SRC" ]; then
225230
p_error 1 "failed to read $SRC"
226231
fi
227-
if [ "$FILE" != "" ]; then
228-
mkdir -p "$(dirname "$ROOTFS/$FILE")"
229-
fi
230232
local SRCDIR="$(dirname "$SRC")"
231233
DST="$ROOTFS/$SRCDIR"
232234
mkdir -p "$DST"
233-
if [ "$FILE" != "" ]; then
234-
echo "$SRCDIR" >> "$ROOTFS/$FILE"
235-
fi
236235
p_debug " copying $SRC to $DST"
237236
if [ -d "$SRC" ]; then
238237
cp -n -r "$SRC" "$DST"
@@ -241,6 +240,19 @@ function copy() {
241240
fi
242241
}
243242

243+
# Credits fot this function go to https://stackoverflow.com/a/18898782
244+
# Return relative path from canonical absolute dir path $1 to canonical
245+
# absolute dir path $2 ($1 and/or $2 may end with one or no "/").
246+
# Does only need POSIX shell builtins (no external command)
247+
function relPath () {
248+
local common path up
249+
common=${1%/} path=${2%/}/
250+
while test "${path#"$common"/}" = "$path"; do
251+
common=${common%/*} up=../$up
252+
done
253+
path=$up${path#"$common"/}; path=${path%/}; printf %s "${path:-.}"
254+
}
255+
244256
function is_plugin_active() {
245257
# Checks whether a plugin is activated or not
246258
if [[ "$PLUGINS_ACTIVATED" =~ (^|,)$1(|:[^,]+)(,|$) ]]; then
@@ -271,7 +283,41 @@ function plugin_parameter() {
271283
return 1
272284
}
273285

274-
function PLUGIN_00_folder() {
286+
function PLUGIN_00_link() {
287+
# If the path is a link to other path, we will create the link and analyze the real path
288+
local L_PATH="$1"
289+
290+
if [ -h "$L_PATH" ]; then
291+
local L_DST="$ROOTFS/$(dirname "$L_PATH")"
292+
local R_PATH="$(readlink -f "$L_PATH")"
293+
local R_DST="$ROOTFS/$(dirname "$R_PATH")"
294+
mkdir -p "$L_DST"
295+
296+
if [ ! -e "$L_DST/$(basename $L_PATH)" ]; then
297+
local REL_PATH="$(relPath "$L_DST" "$R_DST")"
298+
p_debug "$L_PATH is a link to $REL_PATH/$(basename $R_PATH)"
299+
ln -s $REL_PATH/$(basename $R_PATH) $L_DST/$(basename $L_PATH)
300+
fi
301+
302+
add_command "$R_PATH"
303+
return 1
304+
fi
305+
}
306+
307+
function PLUGIN_01_which() {
308+
# This plugin tries to guess whether the command to analize is in the path or not.
309+
# If the command can be obtained calling which, we'll analyze the actual command and not the short name.
310+
local S_PATH="$1"
311+
local W_PATH="$(which $S_PATH)"
312+
313+
if [ "$W_PATH" != "" -a "$W_PATH" != "$S_PATH" ]; then
314+
p_debug "$1 is $W_PATH"
315+
add_command "$W_PATH"
316+
return 1
317+
fi
318+
}
319+
320+
function PLUGIN_02_folder() {
275321
# If it is a folder, just copy it to its location in the new FS
276322
local S_PATH="$1"
277323

@@ -289,6 +335,7 @@ function PLUGIN_09_ldd() {
289335
local S_PATH="$1"
290336
local LIBS= LIB=
291337
local COMMAND="$(which -- $S_PATH)"
338+
local LIB_DIR=
292339
if [ "$COMMAND" == "" ]; then
293340
COMMAND="$S_PATH"
294341
fi
@@ -304,7 +351,12 @@ function PLUGIN_09_ldd() {
304351
if [ $? -eq 0 ]; then
305352
LIBS="$(ldd "$COMMAND" | grep -v 'linux-vdso' | grep -v 'statically' | sed 's/^[ \t]*//g' | sed 's/^.* => //g' | sed 's/(.*)//' | sed '/^[ ]*$/d')"
306353
for LIB in $LIBS; do
307-
copy "$LIB" "$LDCONFIGGILE"
354+
# Here we build the ld config file to add the new paths where the libraries are located
355+
if [ "$LDCONFIGFILE" != "" ]; then
356+
LIB_DIR="$(dirname "$LIB")"
357+
mkdir -p "$ROOTFS/$(dirname $LDCONFIGFILE)"
358+
echo "$LIB_DIR" >> "$ROOTFS/$LDCONFIGFILE"
359+
fi
308360
add_command "$LIB"
309361
done
310362
fi
@@ -465,7 +517,8 @@ function PLUGIN_11_scripts() {
465517
# If it is, adds the interpreter to the list of commands to add to the container
466518
p_debug "trying to guess if $1 is a interpreted script"
467519

468-
local S_PATH="$(which -- $1)"
520+
local S_PATH="$(which $1)"
521+
local ADD_PATHS=
469522

470523
if [ "$S_PATH" == "" ]; then
471524
p_warning "$1 cannot be executed (if it should, please check the path)"
@@ -477,16 +530,26 @@ function PLUGIN_11_scripts() {
477530
FILE_RES="${FILE_RES,,}"
478531
local SHELL_EXEC=
479532
case "$FILE_RES" in
480-
python) SHELL_EXEC=$(which python);;
533+
python) SHELL_EXEC=$(which python)
534+
ADD_PATHS="$(python -c 'import sys;print "\n".join(sys.path)' | grep -v '/home')";;
481535
"bourne-again shell") SHELL_EXEC=$(which bash);;
482-
"a /usr/bin/perl") SHELL_EXEC=$(which perl);;
536+
"a /usr/bin/perl") SHELL_EXEC=$(which perl)
537+
ADD_PATHS="$(perl -e "print qq(@INC)" | tr ' ' '\n' | grep -v '/home')";;
483538
esac
484539

485540
# If we found a known interpreter, we'll add to the list of dependencies
486541
if [ "$SHELL_EXEC" != "" ]; then
487542
p_debug "found that $S_PATH needs $SHELL_EXEC"
488543
add_command "$SHELL_EXEC"
489544
fi
545+
# The include folders
546+
if [ "$ADD_PATHS" != "" ]; then
547+
p_debug "found that $S_PATH needs $ADD_PATHS"
548+
local P
549+
while read P; do
550+
add_command "$P"
551+
done <<< "$ADD_PATHS"
552+
fi
490553
return 0
491554
}
492555

@@ -504,7 +567,7 @@ function plugin_list() {
504567
}
505568

506569
# Now we are activating the basic plugins
507-
PLUGINS_ACTIVATED=folder,ldd
570+
PLUGINS_ACTIVATED=link,which,folder,ldd
508571
COMMANDS_TO_ADD=()
509572
FORCEFOLDER=false
510573
ROOTFS=
@@ -533,7 +596,7 @@ while [ $n -lt ${#ARR[@]} ]; do
533596
--quiet|-q) QUIET=true;;
534597
--tarfile|-t) n=$(($n+1))
535598
TARFILE="${ARR[$n]}";;
536-
--ldconfig|-l) LDCONFIGGILE=/etc/ld.so.conf;;
599+
--ldconfig|-l) LDCONFIGFILE=/etc/ld.so.conf;;
537600
--verbose|-v) VERBOSE=true;;
538601
--debug) DEBUG=true;;
539602
--force|-f) FORCEFOLDER=true;;
@@ -606,8 +669,11 @@ while [ $i_current -lt ${#COMMANDS_TO_ADD[@]} ]; do
606669
i_current=$(($i_current+1))
607670
done
608671

609-
if [ "$LDCONFIGGILE" != "" -a -e "$ROOTFS/$LDCONFIGGILE" ]; then
672+
if [ "$LDCONFIGFILE" != "" -a -e "$ROOTFS/$LDCONFIGFILE" ]; then
610673
p_debug "creating ldconfig"
674+
TMPFILE=$(tempfile)
675+
awk '!a[$0]++' "$ROOTFS/$LDCONFIGFILE" > "$TMPFILE"
676+
mv "$TMPFILE" "$ROOTFS/$LDCONFIGFILE"
611677
ldconfig -r "$ROOTFS"
612678
fi
613679

usecases/uc1/uc1.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
#
3+
# minicon - Minimization of filesystems for containers
4+
# https://github.com/grycap/minicon
5+
#
6+
# Copyright (C) GRyCAP - I3M - UPV
7+
# Developed by Carlos A. [email protected]
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
#
21+
22+
UC=1
23+
UCFOLDER=usecases/uc${UC}
24+
CURDIR=$(dirname $0)
25+
MINICONDIR=$(readlink -f $CURDIR/../..)
26+
27+
set -x
28+
docker images ubuntu:latest
29+
docker run --rm -it -v $MINICONDIR:/tmp/minicon ubuntu:latest /tmp/minicon/minicon -t /tmp/minicon/$UCFOLDER/uc${UC}.tar bash ls mkdir less cat find
30+
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar minicon:uc${UC}
31+
docker images minicon:uc${UC}

usecases/uc2/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM ubuntu
2+
RUN apt-get update && apt-get install -y ssh iproute2 iputils-ping wget

usecases/uc2/uc2.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
#
3+
# minicon - Minimization of filesystems for containers
4+
# https://github.com/grycap/minicon
5+
#
6+
# Copyright (C) GRyCAP - I3M - UPV
7+
# Developed by Carlos A. [email protected]
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
#
21+
22+
UC=2
23+
UCFOLDER=usecases/uc${UC}
24+
REFIMAGE=minicon:uc${UC}fat
25+
MINICONIMAGE=minicon:uc${UC}
26+
27+
CURDIR=$(dirname $0)
28+
MINICONDIR=$(readlink -f $CURDIR/../..)
29+
30+
docker build $UCFOLDER -t $REFIMAGE
31+
32+
cat > $UCFOLDER/execfile-cmd <<\EOF
33+
ssh localhost
34+
/usr/bin/ssh localhost
35+
/bin/ping -c 1 www.google.es
36+
EOF
37+
38+
set -x
39+
docker images $REFIMAGE
40+
docker run --privileged --rm -it -v $MINICONDIR:/tmp/minicon $REFIMAGE bash -c "apt-get install -y strace && /tmp/minicon/minicon -t /tmp/minicon/$UCFOLDER/uc${UC}.tar -l --plugin=strace:execfile=/tmp/minicon/$UCFOLDER/execfile-cmd bash ssh ip id cat ls mkdir ping wget"
41+
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar $MINICONIMAGE
42+
docker images $MINICONIMAGE

usecases/uc3/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM node:latest
2+
3+
# Create app directory
4+
RUN mkdir -p /usr/src/app
5+
WORKDIR /usr/src/app
6+
7+
# Install app dependencies
8+
COPY miniapp/package.json /usr/src/app/
9+
RUN npm install
10+
11+
# Bundle app source
12+
COPY miniapp /usr/src/app
13+
14+
EXPOSE 3000

usecases/uc3/uc3.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
#
3+
# minicon - Minimization of filesystems for containers
4+
# https://github.com/grycap/minicon
5+
#
6+
# Copyright (C) GRyCAP - I3M - UPV
7+
# Developed by Carlos A. [email protected]
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
#
21+
22+
UC=3
23+
UCFOLDER=usecases/uc${UC}
24+
REFIMAGE=minicon:uc${UC}fat
25+
MINICONIMAGE=minicon:uc${UC}
26+
27+
CURDIR=$(dirname $0)
28+
MINICONDIR=$(readlink -f $CURDIR/../..)
29+
30+
set -x
31+
express -f ./usecases/uc3/miniapp
32+
docker build $UCFOLDER -t $REFIMAGE
33+
docker images $REFIMAGE
34+
docker run --rm -it -v $MINICONDIR:/tmp/minicon $REFIMAGE /tmp/minicon/minicon -l -t /tmp/minicon/$UCFOLDER/uc${UC}.tar node /usr/src/app
35+
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar $MINICONIMAGE
36+
docker images $MINICONIMAGE

usecases/uc4/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM ubuntu
2+
RUN apt-get update && apt-get install -y ffmpeg

usecases/uc4/uc4.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
#
3+
# minicon - Minimization of filesystems for containers
4+
# https://github.com/grycap/minicon
5+
#
6+
# Copyright (C) GRyCAP - I3M - UPV
7+
# Developed by Carlos A. [email protected]
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
#
21+
22+
UC=4
23+
UCFOLDER=usecases/uc${UC}
24+
REFIMAGE=minicon:uc${UC}fat
25+
MINICONIMAGE=minicon:uc${UC}
26+
27+
CURDIR=$(dirname $0)
28+
MINICONDIR=$(readlink -f $CURDIR/../..)
29+
30+
set -x
31+
docker build $UCFOLDER -t $REFIMAGE
32+
docker images $REFIMAGE
33+
docker run --rm -it -v $MINICONDIR:/tmp/minicon $REFIMAGE /tmp/minicon/minicon -l -t /tmp/minicon/$UCFOLDER/uc${UC}.tar ffmpeg
34+
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar $MINICONIMAGE
35+
docker images $MINICONIMAGE

0 commit comments

Comments
 (0)