fvp dockerfiles and script generation

Signed-off-by: Leonardo Sandoval <leonardo.sandoval@linaro.org>
Change-Id: I9d0da9f58ea232162ae64a4041f5fa77c145d1b3
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7093589
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.tgz
+*/setup-sshd
diff --git a/.gitreview b/.gitreview
new file mode 100644
index 0000000..2e74d81
--- /dev/null
+++ b/.gitreview
@@ -0,0 +1,4 @@
+[gerrit]
+host=review.trustedfirmware.org
+port=29418
+project=ci/fvp-dockerfiles
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2b7c06c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,4 @@
+# fvp-dockerfiles
+
+Host dockerfiles used in the [LAVA lab](https://tf.validation.linaro.org/). For repository usage,
+check the official documentation at https://tf-ci-users-guide.readthedocs.io/en/latest/
diff --git a/create-model-dockerfile.sh b/create-model-dockerfile.sh
new file mode 100755
index 0000000..5c582b9
--- /dev/null
+++ b/create-model-dockerfile.sh
@@ -0,0 +1,59 @@
+#!/bin/bash
+#
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+# Creates a dockerfile based on a fvp model (model).
+
+# The scripts takes two argument: the model tarball's filename (first argument)
+# and target directory to store the created Dockerfile (second argument)
+#
+
+# globals
+OS="${OS:-ubuntu}"
+OS_VER="${OS_VERSION:-bionic}"
+MODEL_DIR="${MODEL_DIR:-/opt/model}"
+
+function usage() {
+    echo "Usage: $0 model-tarball target-dir" 1>&2
+    exit 1
+}
+
+function get_model_model_ver() {
+    local tgz=$1
+    local arr_model=(${tgz//./ })
+    local x=${arr_model[0]##*_}
+    local y=${arr_model[1]}
+    local z=${arr_model[2]}
+    if [ -n "$z" -a "$z" != "tgz" ]; then
+    		MODEL_VER="${x}.${y}.${z}"
+    else
+    		MODEL_VER="${x}.${y}"
+    fi
+    MODEL=$(echo $tgz | sed "s/_${MODEL_VER}.tgz//")
+}
+
+function main() {
+    local tarball=$1
+    local target_dir=$2
+
+    # get MODEL and MODEL_VER
+    get_model_model_ver $tarball
+
+    # check variables are populated
+    MODEL="${MODEL:?}"
+    MODEL_VER="${MODEL_VER:?}"
+
+    # replace template macros with real model values
+    sed -e "s|\${OS}|${OS}|g" \
+        -e "s|\${OS_VER}|${OS_VER}|g" \
+        -e "s|\${MODEL}|${MODEL}|g" \
+        -e "s|\${MODEL_VER}|${MODEL_VER}|g" \
+        -e "s|\${MODEL_DIR}|${MODEL_DIR}|g" < dockerfile-template > $target_dir/Dockerfile
+}
+
+[ $# -ne 2 ] && usage
+
+tarball=$1; target_dir=$2; main ${tarball} ${target_dir}
diff --git a/create-model-tag.sh b/create-model-tag.sh
new file mode 100755
index 0000000..91a31e2
--- /dev/null
+++ b/create-model-tag.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+#
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+# Creates a 'tag' based on a fvp model (model).
+#
+# The script takes a single argument: the model tarball's filename (first argument)
+#
+
+function usage() {
+    echo "Usage: $0 model-tarball" 1>&2
+    exit 1
+}
+
+# Create a tag based a on fvp model
+function create-tag() {
+    local model=$1
+    local tag
+
+    # get model basename
+    tag=$(basename $model)
+
+    # remove any extension (tgz expected)
+    tag=${tag%.*}
+
+    # finally lowercase
+    tag=${tag,,}
+
+    echo $tag
+}
+
+[ $# -ne 1 ] &&  usage
+
+tarball=$1; create-tag ${tarball}
diff --git a/create_fvp_dockerfile.sh b/create_fvp_dockerfile.sh
new file mode 100755
index 0000000..55fbf0f
--- /dev/null
+++ b/create_fvp_dockerfile.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+#
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+OS="${OS:-ubuntu}"
+OS_VER="${OS_VERSION:-bionic}"
+MODEL_DIR="${MODEL_DIR:-/opt/model}"
+
+function model() {
+tgz=$1
+arr_model=(${tgz//./ })
+x=${arr_model[0]##*_}
+y=${arr_model[1]}
+z=${arr_model[2]}
+if [ -n "$z" -a "$z" != "tgz" ]; then
+		MODEL_VER="${x}.${y}.${z}"
+else
+		MODEL_VER="${x}.${y}"
+fi
+MODEL=$(echo $tgz | sed "s/_${MODEL_VER}.tgz//")
+}
+
+target_dir=$1
+tgz=$2
+
+# get MODEL and MODEL_VER
+model $tgz
+
+# replace template with real values
+sed -e "s|\${OS}|${OS}|g" \
+    -e "s|\${OS_VER}|${OS_VER}|g" \
+    -e "s|\${MODEL}|${MODEL}|g" \
+    -e "s|\${MODEL_VER}|${MODEL_VER}|g" \
+    -e "s|\${MODEL_DIR}|${MODEL_DIR}|g" < dockerfile-template > $target_dir/Dockerfile
diff --git a/dockerfile-template b/dockerfile-template
new file mode 100644
index 0000000..97485c3
--- /dev/null
+++ b/dockerfile-template
@@ -0,0 +1,41 @@
+FROM ${OS}:${OS_VER} as FVP
+
+ENV DEBIAN_FRONTEND=noninteractive
+ENV PKG_DEPS="\
+    bc \
+    ca-certificates \
+    curl \
+    libatomic1 \
+    libdbus-1-3 \
+    openssh-server \
+    telnet \
+    xterm \
+"
+
+RUN set -e ;\
+    apt update -q=2 ;\
+    apt full-upgrade -q=2 --yes ;\
+    apt install -q=2 --yes --no-install-recommends ${PKG_DEPS} ;\
+    # Cleanup
+    apt clean ;\
+    rm -rf /var/lib/apt/lists/* /tmp/*
+
+FROM FVP
+
+WORKDIR ${MODEL_DIR}
+
+COPY setup-sshd /usr/sbin/setup-sshd
+
+# Add the FVP model tarball
+# NOTE: some tarballs contain an installer script others don't, so it may be the case
+# that the ADD instruction do install the model under /opt/model
+ADD ${MODEL}_${MODEL_VER}.tgz .
+
+# Install packages and model
+RUN ${MODEL_DIR}/${MODEL}.sh \
+        --i-agree-to-the-contained-eula \
+        --verbose \
+        --destination ${MODEL_DIR}/${MODEL} && rm -f ${MODEL_DIR}/${MODEL}.sh || true
+
+EXPOSE 22
+ENTRYPOINT ["/usr/sbin/setup-sshd"]
diff --git a/setup-sshd b/setup-sshd
new file mode 100755
index 0000000..3cd9657
--- /dev/null
+++ b/setup-sshd
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+passwd -d root
+echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
+echo "PermitEmptyPasswords yes" >> /etc/ssh/sshd_config
+mkdir -p /var/run/sshd
+/usr/sbin/sshd -D &
+
+exec "$@"