Browse code

scripts/build_spec.sh: new script

It allows to build single spec file inidividually using Photon OS
docker image. It does not depend on package builder.
It is known as SDK.

examples/build_spec/simple-module contain example spec file
demonstrating how to build kernel module (sources are in
module_example.tar.xz) for Photon OS.
It is known as NDK.

Change-Id: I8e2c25af9e5f09b7e075a41d6cd39a008da6259d
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/6303
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Sharath George

Alexey Makhalov authored on 2018/12/06 06:04:58
Showing 4 changed files
1 1
new file mode 100644
2 2
Binary files /dev/null and b/tools/examples/build_spec/simple-module/module_example.tar.xz differ
3 3
new file mode 100755
... ...
@@ -0,0 +1,4 @@
0
+#! /bin/bash
1
+
2
+WDIR=$(dirname $(readlink -m $0))
3
+$WDIR/../../../scripts/build_spec.sh $WDIR/simple-module.spec
0 4
new file mode 100644
... ...
@@ -0,0 +1,38 @@
0
+Summary:        Simple Linux module
1
+Name:           simple-module
2
+Version:        4.18.9
3
+Release:        5%{?dist}
4
+License:    	GPLv2
5
+Group:        	System Environment/Kernel
6
+Vendor:         VMware, Inc.
7
+Distribution: 	Photon
8
+Source0:        module_example.tar.xz
9
+BuildRequires:  linux-devel = 4.18.9
10
+BuildRequires:  kmod
11
+Requires:       linux = 4.18.9
12
+
13
+%description
14
+Example of building linux module for Photon OS
15
+
16
+%prep
17
+%setup -q -n module_example
18
+
19
+%build
20
+make -C `echo /usr/src/linux-headers-4.18.9*` M=`pwd` VERBOSE=1 modules %{?_smp_mflags}
21
+
22
+%install
23
+make -C `echo /usr/src/linux-headers-4.18.9*` M=`pwd` INSTALL_MOD_PATH=%{buildroot} modules_install
24
+# fix permissins to generate non empty debuginfo
25
+find %{buildroot}/lib/modules -name '*.ko' -print0 | xargs -0 chmod u+x
26
+
27
+%post
28
+/sbin/depmod -a
29
+
30
+%files
31
+%defattr(-,root,root)
32
+/lib/modules/*
33
+
34
+%changelog
35
+*   Tue Dec 04 2018 Alexey Makhalov <amakhalov@vmware.com> 4.18.9-5
36
+-   Initial build. First version
37
+
0 38
new file mode 100755
... ...
@@ -0,0 +1,146 @@
0
+#! /bin/bash
1
+
2
+# Target to Photon OS version
3
+VERSION=3
4
+
5
+# Keep running container instance alive?
6
+KEEP_SANDBOX_AFTER_FAILURE=1
7
+
8
+# Draw spinner while waiting
9
+DRAW_SPINNER=1
10
+
11
+test "$#" -ne 1 && echo "Usage: $0 spec-file-to-build.spec" && exit 1
12
+
13
+CONTAINER=build_spec
14
+SPECPATH=$(readlink -m $1)
15
+SPECFILE=$(basename $SPECPATH)
16
+SPECDIR=$(dirname $SPECPATH)
17
+mkdir -p $SPECDIR/stage/LOGS
18
+LOGFILE=$SPECDIR/stage/LOGS/$(basename $SPECFILE .spec).log
19
+
20
+# use &3 for user output
21
+exec 3>&1
22
+# redirect &1 and &2 to the log file
23
+exec &>$LOGFILE
24
+
25
+function wait_for_result() {
26
+  local pid=$!
27
+  if [ "$DRAW_SPINNER" -eq 1 ]; then
28
+    local spin='-\|/'
29
+    local i=0
30
+    echo -n " " >&3
31
+    while [ -d /proc/$pid ]; do
32
+      sleep .25
33
+      echo -ne "\b${spin:i++%4:1}" >&3
34
+    done
35
+    echo -ne "\b" >&3
36
+  fi
37
+  wait $pid
38
+  if [ $? -eq 0 ]; then
39
+    echo -e "\033[0;32mOK\033[0m" >&3
40
+  else
41
+    echo -e "\033[0;31mFAIL\033[0m" >&3
42
+    fail
43
+  fi
44
+}
45
+
46
+function run() {
47
+  echo -ne "\t$1 " >&3
48
+  shift
49
+  echo "run: $@"
50
+  $@ &
51
+  wait_for_result
52
+}
53
+
54
+function in_sandbox() {
55
+  docker exec $CONTAINER "$@"
56
+}
57
+
58
+function create_sandbox() {
59
+  docker ps -f "name=$CONTAINER" && docker rm -f $CONTAINER
60
+  docker inspect --format='{{.Created}}' photon_build_spec:$VERSION.0
61
+  local status=$?
62
+  local cdate=$(date --date=`docker inspect --format='{{.Created}}' photon_build_spec:$VERSION.0` '+%s')
63
+  # image exists?
64
+  if [ $status -eq 0 ]; then
65
+    local vdate=$((`date '+%s'` - 1209600))
66
+    # image is less then 2 weeks
67
+    if [ $cdate -gt $vdate ]; then
68
+      # use this image
69
+      run "Use local build template image" docker run -d --name $CONTAINER --network="host" photon_build_spec:$VERSION.0 tail -f /dev/null
70
+      return 0
71
+    else
72
+      # remove old image
73
+      docker image rm photon_build_spec:$VERSION.0
74
+    fi
75
+  fi
76
+
77
+
78
+  run "Pull photon image" docker run -d --name $CONTAINER --network="host" photon:$VERSION.0 tail -f /dev/null
79
+
80
+  # replace toybox with coreutils and install default build tools
81
+  run "Replace toybox with coreutils" in_sandbox tdnf remove -y toybox
82
+  run "Install default build tools" in_sandbox tdnf install -y rpm-build build-essential tar sed findutils file gzip patch
83
+
84
+  run "Create build template image for future use" docker commit `docker ps -q -f "name=$CONTAINER"` photon_build_spec:$VERSION.0
85
+}
86
+
87
+function prepare_buildenv() {
88
+  run "Create source folder" in_sandbox mkdir -p /usr/src/photon/SOURCES
89
+  run "Copy sources from $SPECDIR" docker cp $SPECDIR/. $CONTAINER:/usr/src/photon/SOURCES
90
+  local br=`sed -n 's/BuildRequires://p' $SPECPATH | sed 's/ \(<\|\)= /=/g;s/>\(=\|\) [^ ]*//g'`
91
+  if [ "$br" != "" ]; then
92
+    run "Install build requirements" in_sandbox tdnf install -y $br 
93
+  fi
94
+}
95
+
96
+function build() {
97
+  echo -ne "\tRun rpmbuild " >&3
98
+  docker exec $CONTAINER rpmbuild -bb --define "dist .ph$VERSION" /usr/src/photon/SOURCES/$SPECFILE &
99
+  wait_for_result
100
+}
101
+
102
+function get_rpms() {
103
+  run "Copy RPMS" docker cp $CONTAINER:/usr/src/photon/RPMS $SPECDIR/stage
104
+  run "Copy SRPMS" docker cp $CONTAINER:/usr/src/photon/SRPMS $SPECDIR/stage
105
+}
106
+
107
+function destroy_sandbox() {
108
+  run "Stop container" docker kill $CONTAINER
109
+  run "Remove container" docker rm $CONTAINER
110
+}
111
+
112
+function clean_up() {
113
+  echo "Post clean up" >&3
114
+  docker ps -f "name=$CONTAINER" &>/dev/null && destroy_sandbox &>/dev/null
115
+}
116
+
117
+function fail() {
118
+  test "$KEEP_SANDBOX_AFTER_FAILURE" -ne 1 && clean_up || \
119
+    echo "Sandbox is preserved for analisys. Use 'docker exec -it $CONTAINER /bin/bash'" >&3
120
+  echo "Build failed. See $LOGFILE for full output" >&3
121
+  echo -e "\033[1;33m" >&3
122
+  tail $LOGFILE >&3
123
+  echo -e "\033[0m" >&3
124
+  exit 1
125
+}
126
+
127
+trap clean_up SIGINT SIGTERM
128
+
129
+echo "1. Create sandbox" >&3
130
+create_sandbox
131
+
132
+echo "2. Prepare build environment" >&3
133
+prepare_buildenv
134
+
135
+echo "3. Build" >&3
136
+build
137
+
138
+echo "4. Get binaries" >&3
139
+get_rpms
140
+
141
+echo "5. Destroy sandbox" >&3
142
+destroy_sandbox
143
+
144
+echo "Build completed. RPMS are in '$SPECDIR/stage' folder" >&3
145
+