build 7.02 KB
Newer Older
1
#!/bin/bash
amnesia's avatar
amnesia committed
2

intrigeri's avatar
intrigeri committed
3
set -x
amnesia's avatar
amnesia committed
4

amnesia's avatar
amnesia committed
5
6
umask 022

7
8
9
10
11
12
13
### functions

fatal () {
   echo "$*" >&2
   exit 1
}

14
15
16
17
18
19
20
21
syslinux_utils_upstream_version () {
   dpkg-query -W -f='${Version}\n' syslinux-utils | \
       # drop epoch
       sed -e 's,.*:,,' | \
       # drop +dfsg and everything that follows
       sed -e 's,\+dfsg.*,,'
}

22
23
24
25
26
27
28
print_iso_size () {
   local isofile="$1"
   [ -f "$isofile" ] || return 23
   size=$(stat --printf='%s' "$isofile")
   echo "The ISO is ${size} bytes large."
}

29
30
### Main

31
# we require building from git
32
33
git rev-parse --is-inside-work-tree &> /dev/null \
   || fatal "${PWD} is not a Git tree."
34

35
36
37
. config/amnesia
if [ -e config/amnesia.local ] ; then
   . config/amnesia.local
amnesia's avatar
amnesia committed
38
39
40
41
42
fi

# a clean starting point
rm -rf cache/stages_rootfs

43
# get LB_BINARY_IMAGES
44
45
. config/binary

46
# get LB_ARCHITECTURE and LB_DISTRIBUTION
47
. config/bootstrap
48

49
50
# save variables that are needed by chroot_local-hooks
echo "LB_DISTRIBUTION=${LB_DISTRIBUTION}" >> config/chroot_local-includes/usr/share/amnesia/build/variables
51
52
53
54
55
echo "POTFILES_DOT_IN='$(
         /bin/grep -E --no-filename '[^ #]*\.in$' po/POTFILES.in \
       | sed -e 's,^config/chroot_local-includes,,' | tr "\n" ' '
   )'" \
   >> config/chroot_local-includes/usr/share/amnesia/build/variables
56

57
58
# fix permissions on some source files that will be copied as is to the chroot.
# they may be wrong, e.g. if the Git repository was cloned with a strict umask.
59
chmod -R go+rX config/binary_local-includes/
60
chmod -R go+rX config/chroot_local-includes/etc
61
chmod    0440  config/chroot_local-includes/etc/sudoers.d/*
62
chmod    go+rX config/chroot_local-includes/home
63
64
65
chmod    go+rX config/chroot_local-includes/lib
chmod    go+rX config/chroot_local-includes/lib/live
chmod -R go+rx config/chroot_local-includes/lib/live/config
66
chmod    go+rX config/chroot_local-includes/lib/live/mount
67
chmod -R go+rX config/chroot_local-includes/lib/systemd
68
chmod    go+rX config/chroot_local-includes/live
69
70
71
chmod -R go+rX config/chroot_local-includes/usr
chmod -R go+rx config/chroot_local-includes/usr/local/bin
chmod -R go+rx config/chroot_local-includes/usr/local/sbin
72
chmod -R go+rX config/chroot_local-includes/usr/share/doc/tails
73
chmod -R go+rX config/chroot_local-includes/var
74
75
chmod -R go+rX config/chroot_apt
chmod -R go+rX config/chroot_sources
76

77
# build the image
amnesia's avatar
amnesia committed
78

79
: ${MKSQUASHFS_OPTIONS:='-comp xz -Xbcj x86 -b 1024K -Xdict-size 1024K'}
80
MKSQUASHFS_OPTIONS="${MKSQUASHFS_OPTIONS} -wildcards -ef chroot/usr/share/amnesia/build/mksquashfs-excludes"
81
export MKSQUASHFS_OPTIONS
82

83
# get git branch or tag so we can set the basename appropriately, i.e.:
anonym's avatar
anonym committed
84
85
# * if we build from a tag: tails-$ARCH-$TAG.iso
# * otherwise:              tails-$ARCH-$BRANCH-$VERSION-$TIME-$COMMIT.iso
86
if GIT_REF="$(git symbolic-ref HEAD)"; then
87
    GIT_BRANCH="${GIT_REF#refs/heads/}"
88
    CLEAN_GIT_BRANCH=$(echo "$GIT_BRANCH" | sed 's,/,_,g')
anonym's avatar
anonym committed
89
90
    GIT_SHORT_ID="$(git rev-parse --short HEAD)"
    BUILD_BASENAME="tails-${LB_ARCHITECTURE}-${CLEAN_GIT_BRANCH}-${AMNESIA_VERSION}-${AMNESIA_NOW}-${GIT_SHORT_ID}"
91
92
93
else
    GIT_CURRENT_COMMIT="$(git rev-parse HEAD)"
    if GIT_TAG="$(git describe --tags --exact-match ${GIT_CURRENT_COMMIT})"; then
94
        CLEAN_GIT_TAG=$(echo "$GIT_TAG" | tr '/-' '_~')
95
	BUILD_BASENAME="tails-${LB_ARCHITECTURE}-${CLEAN_GIT_TAG}"
96
97
98
    else
	# this shouldn't reasonably happen (e.g. only if you checkout a
        # tag, remove the tag and then build)
99
	fatal "Neither a Git branch nor a tag, exiting."
100
101
102
    fi
fi

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
GIT_BASE_BRANCH=$(head -n1 config/base_branch) \
    || fatal "GIT_BASE_BRANCH could not be guessed."

# Merge base branch into the branch being built, iff. we're building
# in Jenkins, and not building from a tag, and not building the base
# branch itself
if [ -n "$JENKINS_URL" ] && [ -z "$GIT_TAG" ] \
       && [ "$GIT_BRANCH" != "$GIT_BASE_BRANCH" ] ; then
    GIT_BASE_BRANCH_COMMIT=$(git rev-parse "origin/${GIT_BASE_BRANCH}") \
	|| fatal "Base branch's top commit could not be guessed."

    echo "Merging base branch origin/${GIT_BASE_BRANCH}"
    echo "(at commit ${GIT_BASE_BRANCH_COMMIT})..."
    git merge --no-edit "origin/${GIT_BASE_BRANCH}" \
	|| fatal "Failed to merge base branch."
118
119
120
121
122
123
124

    # Adjust BUILD_BASENAME to embed the base branch name and its top commit
    CLEAN_GIT_BASE_BRANCH=$(echo "$GIT_BASE_BRANCH" | sed 's,/,_,g')
    GIT_BASE_BRANCH_SHORT_ID=$(git rev-parse --short "origin/${GIT_BASE_BRANCH}") \
	|| fatal "Base branch's top commit short ID could not be guessed."
    BUILD_BASENAME="${BUILD_BASENAME}+${CLEAN_GIT_BASE_BRANCH}"
    BUILD_BASENAME="${BUILD_BASENAME}@${GIT_BASE_BRANCH_SHORT_ID}"
125
126
fi

127
# build the doc wiki
elouann's avatar
elouann committed
128
./build-website
129
130
131
132

# refresh translations of our programs
./refresh-translations || fatal "refresh-translations failed ($?)."

133
case "$LB_BINARY_IMAGES" in
134
135
136
   iso)
      BUILD_FILENAME_EXT=iso
      BUILD_FILENAME=binary
137
138
139
140
141
142
143
144
145
146
      which isohybrid >/dev/null || fatal 'Cannot find isohybrid in $PATH'
      installed_syslinux_utils_upstream_version="$(syslinux_utils_upstream_version)"
      if dpkg --compare-versions \
	   "$installed_syslinux_utils_upstream_version" \
	   'lt' \
	   "$REQUIRED_SYSLINUX_UTILS_UPSTREAM_VERSION" ; then
	  fatal \
	      "syslinux-utils '${installed_syslinux_utils_upstream_version}' is installed, " \
	      "while we need at least '${REQUIRED_SYSLINUX_UTILS_UPSTREAM_VERSION}'."
      fi
147
148
      ;;
   iso-hybrid)
149
      BUILD_FILENAME_EXT=iso
150
      BUILD_FILENAME=binary-hybrid
151
152
153
154
155
156
157
158
159
160
      ;;
   tar)
      BUILD_FILENAME_EXT=tar.gz
      BUILD_FILENAME=binary-tar
      ;;
   usb-hdd)
      BUILD_FILENAME_EXT=img
      BUILD_FILENAME=binary
      ;;
   *)
161
      fatal "Image type ${LB_BINARY_IMAGES} is not supported."
162
163
164
165
166
167
      ;;
esac
BUILD_DEST_FILENAME="${BUILD_BASENAME}.${BUILD_FILENAME_EXT}"
BUILD_MANIFEST="${BUILD_DEST_FILENAME}.list"
BUILD_PACKAGES="${BUILD_DEST_FILENAME}.packages"
BUILD_LOG="${BUILD_DEST_FILENAME}.buildlog"
168
169
BUILD_START_FILENAME="${BUILD_DEST_FILENAME}.start.timestamp"
BUILD_END_FILENAME="${BUILD_DEST_FILENAME}.end.timestamp"
amnesia's avatar
amnesia committed
170

171
172
173
174
175
176
# Clone all output, from this point on, to the log file
exec >  >(tee -a "$BUILD_LOG")
trap "kill -9 $! 2>/dev/null" EXIT HUP INT QUIT TERM
exec 2> >(tee -a "$BUILD_LOG" >&2)
trap "kill -9 $! 2>/dev/null" EXIT HUP INT QUIT TERM

177
echo "Building $LB_BINARY_IMAGES image ${BUILD_BASENAME}..."
178
set -o pipefail
179
[ -z "$JENKINS_URL" ] || date --utc '+%s' > "$BUILD_START_FILENAME"
180
time eatmydata lb build noauto ${@}
181
182
183
RET=$?
if [ -e "${BUILD_FILENAME}.${BUILD_FILENAME_EXT}" ]; then
   if [ "$RET" -eq 0 ]; then
184
      [ -z "$JENKINS_URL" ] || date --utc '+%s' > "$BUILD_END_FILENAME"
185
      echo "Image was successfully created"
186
      if [ "$LB_BINARY_IMAGES" = iso ]; then
187
188
	  ISO_FILE="${BUILD_FILENAME}.${BUILD_FILENAME_EXT}"
	  print_iso_size "$ISO_FILE"
189
	  echo "Hybriding it..."
190
191
192
193
	  isohybrid $AMNESIA_ISOHYBRID_OPTS "$ISO_FILE"
	  print_iso_size "$ISO_FILE"
	  truncate -s %2048 "$ISO_FILE"
	  print_iso_size "$ISO_FILE"
194
      fi
195
196
197
   else
      echo "Warning: image created, but lb build exited with code $RET"
   fi
198
199
   echo "Renaming generated files..."
   mv -i "${BUILD_FILENAME}.${BUILD_FILENAME_EXT}" "${BUILD_DEST_FILENAME}"
200
   mv -i binary.packages "${BUILD_PACKAGES}"
201
else
202
   fatal "lb build failed ($?)."
203
fi