Blame view

software/nayuos/scripts/cros_full_build.in 12.6 KB
iv committed
1
#! {{ bash_path }}
iv committed
2

Tomáš Peterka committed
3

iv committed
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
function increase_revision_number() {
  # this increase the revision number an ebuild
  # (except for revision number 9999 which is a value for "live ebuild", for dev revisions)
  # see https://devmanual.gentoo.org/general-concepts/ebuild-revisions/

  if [ -z "$1" ] ; then
    echo "ERROR: No parameter given to function: increase_revision_number."
    echo "Usage: increase_revision_number /path/to/dir/containing/ebuild"
    exit 1
  elif [ -d "$1" ]; then
    DIR="$1"
    CURRENT_FILE=""
    CURRENT_REVISION=0
    # get the biggest revision number
    # XXX: may break if there are many version of an ebuild
    # (eg: bash-4.3_p42-r4.ebuild and bash-3.7-r8.ebuild)
    for FILENAME in $(find "${DIR}" -maxdepth 1 -regextype sed -regex .*-r[0-9]*\.ebuild); do
      echo $FILENAME
      REVISION="$(echo ${FILENAME} | rev | cut -d- -f1 | rev | tr -d [:alpha:] | tr -d [:punct:])"
      echo "${REVISION}"
      if [ "$CURRENT_REVISION" -lt "$REVISION" ] ; then
          CURRENT_REVISION=${REVISION}
          CURRENT_FILE=${FILENAME}
      fi
    done
    if [ "$CURRENT_REVISION" -ne "9999" ]; then
      # increase REVISION
      NEW_REVISION=$((CURRENT_REVISION+1))
      NEW_FILE="$(echo ${CURRENT_FILE} | sed -r 's/-r'${CURRENT_REVISION}'/-r'${NEW_REVISION}'/')"
      echo "changing revision number: ${CURRENT_FILE} -> ${NEW_FILE}"
      mv "${CURRENT_FILE}" "${NEW_FILE}"
    fi
  fi
}

Tomáš Peterka committed
39

Tomáš Peterka committed
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
function insert_packages_into_ebuild() {
  # Insert packages (in form of "category/package") into RDEPEND part of ebuild.
  # Optionally increasing version of given ebuild.
  EBUILD=$1
  changed=0
  shift
  while [ ! -z "$1" ]
  do
    package=$1
    if [[ $( grep "${package}" "${EBUILD}" ) ]] ; then
      echo "no need to change ${EBUILD} file to add ${package}..." >> "${BUILD_LOG}"
    else
      printf "\n\nRDEPEND=\"\${RDEPEND}\n          ${package}\"\n">> ${EBUILD}
      changed=1
    fi
    shift
  done;

  if [ $changed -eq 1 ]
  then
    # if we inserted at least one package then increase the version to be sure
    # it gets selected in case of more overlays with the same package
    increase_revision_number $(dirname $EBUILD)
  fi
}

Tomáš Peterka committed
66 67 68 69 70 71 72 73

function latest_ebuild() {
  # find the latest ebuild file (previously we hardcoded the versions)
  # @argument ebuild directory to find the file in
  find $1 -type f -name '*ebuild' | sort -r | head -1
}


iv committed
74
######################## Download of sources using the "repo" command ########################
iv committed
75 76 77
DL_LOG="{{ instance_log_dir }}/cros_sources_dl.log"
BRANCH="{{ branch }}"
CHROMIUM_OVERLAY="{{ cros_location }}/{{ branch }}/src/third_party/chromiumos-overlay"
iv committed
78
PORTAGE_STABLE="{{ cros_location }}/{{ branch }}/src/third_party/portage-stable"
iv committed
79

iv committed
80
{{ export_path_cmd }}
iv committed
81
echo "getting Chromium OS sources..." >> $DL_LOG
iv committed
82 83 84 85
echo "{{ cros_location }}/{{ branch }}"
install -d "{{ cros_location }}/{{ branch }}"
cd "{{ cros_location }}/{{ branch }}"

iv committed
86 87 88 89 90 91 92
# git setup
if ! git config user.name || ! git config user.email ; then
  git config --global user.name "Your Name"
  git config --global user.email "you@example.com" 
fi

repo init -u https://chromium.googlesource.com/chromiumos/manifest.git -b {{ branch }} >> $DL_LOG || { echo "Problem while initiating Chromium OS repo (repo init). PATH is: '${PATH}'. Exiting." ; exit 1 ; }
iv committed
93

iv committed
94
# in case changes were already made, stash changes to be able to pull
iv committed
95 96
cd ${CHROMIUM_OVERLAY}
git stash
iv committed
97 98
cd ${PORTAGE_STABLE}
git stash
iv committed
99

iv committed
100 101
repo sync >> $DL_LOG || { echo "Problem while downloading Chromium OS sources (repo sync). Exiting." ; exit 1 ; }

iv committed
102

iv committed
103
############################## Prepare chroot environment ###################################
iv committed
104
BUILD_LOG={{ instance_log_dir }}/cros_build.log
iv committed
105
TEST_LOG={{ instance_log_dir }}/tests_nayuos_image.log
iv committed
106
cd {{ cros_location }}/{{ branch }}
iv committed
107 108

# create chroot environment (exit on failure)
iv committed
109
cros_sdk --download || { echo "Problem while entering chroot or downloading chroot environment. PATH is: '${PATH}' Exiting." ; exit 1 ; }
iv committed
110

iv committed
111 112 113
# compile Python with sqlite support (-> change USE flag) for dev-lang/python
# it is needed by re6st
# just need to change -sqlite by sqlite on the right line
iv committed
114
# same for IPv6 support
iv committed
115
PACKAGE_USE_FILE="${CHROMIUM_OVERLAY}/profiles/targets/chromeos/package.use"
iv committed
116
line_number=$(sed -n '/dev-lang\/python/=' ${PACKAGE_USE_FILE})
iv committed
117
if [ $line_number ] ; then
iv committed
118
    echo "$line_number"
iv committed
119 120 121 122
    sed -i "${line_number}s/-sqlite/sqlite/" ${PACKAGE_USE_FILE}
    if [[ ! $(sed -n ${line_number}p ${PACKAGE_USE_FILE} | grep ipv6) ]]; then
        sed -i "${line_number}s/$/ ipv6/" ${PACKAGE_USE_FILE}
    fi
iv committed
123 124
fi

iv committed
125
# add some ebuilds, specific to NayuOS
iv committed
126 127
for category in $( ls {{ ebuilds_dir }} ); do
  echo ${category}
iv committed
128
  cp -R "{{ ebuilds_dir }}/${category}/"* "${CHROMIUM_OVERLAY}/${category}/"
iv committed
129 130
done

iv committed
131 132
install -m 770 "{{ scripts_dir }}/test_nayuos_image" "{{ cros_location }}/{{ branch }}/src/scripts/test_nayuos_image"

iv committed
133 134
### packages management ###

Tomáš Peterka committed
135 136
# make virtual ebuild responsible for installing all packages dependent on NayuOS own packages
insert_packages_into_ebuild \
Tomáš Peterka committed
137
  $(latest_ebuild ${CHROMIUM_OVERLAY}/virtual/target-chromium-os-dev) \
Tomáš Peterka committed
138
  {{ nayu_dev_packages }}
Tomáš Peterka committed
139 140 141

# insert policies into /etc/chromium/policies/recommended with NayuOS customizations
insert_packages_into_ebuild \
Tomáš Peterka committed
142
  $(latest_ebuild ${CHROMIUM_OVERLAY}/chromeos-base/chromeos-dev-root) \
Tomáš Peterka committed
143
  {{ nayu_dev_rootfs_packages }}
iv committed
144

iv committed
145
# do not install the Upstart init script that starts ssh daemon at boot time
Tomáš Peterka committed
146 147
find ${CHROMIUM_OVERLAY}/chromeos-base/chromeos-sshd-init* -name openssh-server.conf -delete

Tomáš Peterka committed
148
# strip away the src_install part because it only sets up booting scripts
Tomáš Peterka committed
149 150
# and increase version in case of other overlays with the same packages
sed -i -n '/src_install/q;p' $(latest_ebuild ${CHROMIUM_OVERLAY}/chromeos-base/openssh-server-init)
iv committed
151
increase_revision_number ${CHROMIUM_OVERLAY}/chromeos-base/openssh-server-init/
Tomáš Peterka committed
152
sed -i -n '/src_install/q;p' $(latest_ebuild ${CHROMIUM_OVERLAY}/chromeos-base/chromeos-sshd-init)
iv committed
153 154
increase_revision_number ${CHROMIUM_OVERLAY}/chromeos-base/chromeos-sshd-init/

Tomáš Peterka committed
155

iv committed
156
# bashrc modifications
iv committed
157
BASH_EBUILD_DIR="${PORTAGE_STABLE}/app-shells/bash"
iv committed
158
if ! grep "BEGIN NayuOS configuration" "${BASH_EBUILD_DIR}/files/dot-bashrc" > /dev/null ; then
Tristan Cavelier committed
159 160 161 162
  cat >> "${BASH_EBUILD_DIR}/files/dot-bashrc" <<EOF

# ----- BEGIN NayuOS configuration -----

Tristan Cavelier committed
163 164 165
# use vim as default editor if nano does not exist
which nano &> /dev/null || export EDITOR=vim

Tristan Cavelier committed
166 167 168 169 170 171
# git quickfix for finding right git executables
export GIT_EXEC_PATH=/usr/local/libexec/git-core

# configure .gitconfig once
if [ ! -e ~/.gitconfig ] ; then
  which less &> /dev/null && git config --global core.pager less
iv committed
172 173
fi

iv committed
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
# go to ~/Download
[ "\$(pwd)" = / ] && cd ~/Downloads

alias ls='ls -CF --color=auto'
alias l='ls -lF'
alias lh='ls -hAl'
alias la='ls -la'
alias ll='ls -l'

tree() {
  python -Sc 'import sys, os
aa = sys.argv[1:] or ["."]
for a in aa:
  o = len(a.split(os.sep))
  for r, dd, ff in os.walk(a):
    dp = len(r.split(os.sep)) - o + 1
    print "%s- %s" % ((dp - 1) * "  ", os.path.basename(r))
    for f in ff:
      print "%s- %s" % (dp * "  ", f)' "\$@"
}

showzip() {
  [ \$# = 1 ] || { echo "Usage: showzip ZIPFILE" >&2 ; return 1 ; }
  python -Sc 'import sys, zipfile
zfile = zipfile.ZipFile(sys.argv[1])
zfile.printdir()
zfile.close()' "\$@"
}

zip() {
  [ \$# -gt 1 ] || { echo "Usage: zip ZIPFILE FILE..." >&2 ; return 1 ; }
  python -Sc 'import sys, zipfile, os
if os.path.lexists(sys.argv[1]):
  sys.stderr.write("zip: file %r already exists\n" % sys.argv[1])
  sys.exit(1)
zfile = zipfile.ZipFile(sys.argv[1], "w", zipfile.ZIP_DEFLATED)
def arc(arg):
  if os.path.islink(arg):
    sys.stderr.write("zip: symlink %r ignored\n" % arg)
  elif os.path.isdir(arg):
    for r, _, ff in os.walk(arg):
      zfile.write(r)
      for f in ff: arc(os.path.join(r, f))
  else:
    zfile.write(arg)
for arg in sys.argv[2:]: arc(arg)
zfile.close()' "\$@"
}

unzip() {
  [ \$# != 0 ] || { echo "Usage: unzip ZIPFILE [TARGET]" >&2 ; return 1 ; }
  python -Sc 'import sys, zipfile
zfile = zipfile.ZipFile(sys.argv[1])
zfile.extractall(sys.argv[2] if len(sys.argv) > 2 else ".")
zfile.close()' "\$@"
}

encmount() {
  if [ -z \$1 ] ; then
      echo "Please, give a device as parameter, ex: /dev/sda"
      return 1
  fi
  if  [ ! -d /media/removable/key ] ; then
      echo "New dir at /media/removable/key"
      sudo mkdir /media/removable/key
  fi
  echo "Mounting \$1 on /media/removable/key"
  sudo cryptsetup luksOpen \$1 key
  sudo mount -o noexec /dev/mapper/key /media/removable/key
}

__safewrite() {
  local cmd=safewrite
  local usage="Usage: \$cmd DST < stdin

Where DST is the destination file

Copy stdin to DST. Data is flushed regularly to avoid some eventual crash during cache sync.

Example:
  gunzip myimage.img.gz -c | sudosafewrite /dev/sda"

  local KiB=1024
  local MiB=\$((1024 * \$KiB))
  local bs=4096
  local count=\$((50 * \$MiB / \$bs))

  local infostep=50
  local infounit=MiB

  local safewrite_seek=0
  local safewrite_out=
  local safewrite_length=\$infostep

  [ \$# = 2 ] || { echo "\$usage" >&2 ; return 2 ; }
  [ \$1 != - ] || { echo "DST should not be '-'" >&2 ; return 2 ; }

  while [ \$? = 0 ] ; do
    echo "+ copying until \$safewrite_length \$infounit"
    safewrite_out=\$(\$1 bs=\$bs count=\$count seek=\$safewrite_seek of="\$2" 2>&1) || { echo "\$safewrite_out" ; return 1 ; }
    safewrite_length=\$((\$safewrite_length + \$infostep))
    safewrite_seek=\$((\$safewrite_seek + \$count))
    echo "\$safewrite_out"
    ! echo "\$safewrite_out" | grep '(0 B) copied' > /dev/null
  done
  return 0
}
safewrite() { __safewrite dd "\$@" ; }
sudodd() { sudo dd "\$@" ; }
sudosafewrite() { __safewrite sudodd "\$@" ; }

Tristan Cavelier committed
285 286
# ----- END NayuOS configuration -----
EOF
iv committed
287
fi
iv committed
288

Tristan Cavelier committed
289
increase_revision_number "${BASH_EBUILD_DIR}"
iv committed
290

iv committed
291 292
######################################## Build ##############################################
BOARDS="{{ boards_list }}"
iv committed
293
KEEP_CACHE="{{ keep_cache }}"
iv committed
294
for board in ${BOARDS} ; do
iv committed
295
  echo ${board}
iv committed
296
  if [ ${board} == daisy ] ; then
iv committed
297
    # XXX: broken by sucessive wrapping
iv committed
298
    echo "daisy board: accepting license for Mali drivers..."
iv committed
299 300
    cros_sdk -- "sudo cp /etc/make.conf.user /etc/make.conf.user.save"
    cros_sdk -- "echo 'ACCEPT_LICENSE=\"*\"' | sudo tee --append /etc/make.conf.user"
iv committed
301
  fi
iv committed
302

iv committed
303
  # preparing packages (for chroot and image)
iv committed
304 305
  date >> "${BUILD_LOG}"
  echo "building packages for a ${board}-flavoured Chromium OS..." >> "${BUILD_LOG}"
Tomáš Peterka committed
306
  cros_sdk --nouse-image -- ./build_packages --board=${board} >> "${BUILD_LOG}"
iv committed
307

iv committed
308 309 310 311 312 313 314 315
  # change boot pictures
  cros_sdk -- cros_workon --board=${board} start chromiumos-assets
  cros_sdk -- cros_workon_make --board=${board} chromiumos-assets
  cros_sdk -- cros_workon_make --board=${board} chromiumos-assets --test
  cros_sdk -- cros_workon_make --board=${board} chromiumos-assets --install
  cp {{ logo_dir }}/* {{ cros_location }}/{{ branch }}/src/platform/chromiumos-assets/images_100_percent/
  cp {{ logo_dir }}/* {{ cros_location }}/{{ branch }}/src/platform/chromiumos-assets/images_200_percent/

Romain Courteaud committed
316
  NAYU_IMAGE_LOCATION=/tmp/${board}.nayuos.img
iv committed
317 318

  # rebuild packages with boot pictures
Tomáš Peterka committed
319
  cros_sdk --nouse-image-- ./build_packages --board=${board} >> "${BUILD_LOG}"
iv committed
320

iv committed
321
  # NayuOS images
iv committed
322
  date >> "${BUILD_LOG}"
iv committed
323
  echo "building image" >> "${BUILD_LOG}"
Tomáš Peterka committed
324
  cros_sdk --nouse-image -- ./build_image --board=${board} dev >> "${BUILD_LOG}" \
iv committed
325
    && cros_sdk -- rm -f $NAYU_IMAGE_LOCATION && cros_sdk -- touch $NAYU_IMAGE_LOCATION \
iv committed
326
    && cros_sdk -- cros flash --board=${board} file://$NAYU_IMAGE_LOCATION >> "${BUILD_LOG}" \
iv committed
327
    && cros_sdk -- ./test_nayuos_image ${board} > "${TEST_LOG}" \
iv committed
328
    || { echo "An error occured while building ${board} NayuOS image. Exiting." ; exit 1 ;}
iv committed
329

iv committed
330 331
  # save a lot of space on the server but delete cache and build files
  # (it means that the next build will be as long and use as much resources as this one)
iv committed
332
  if [ ${KEEP_CACHE,,} == "no" ] ; then
iv committed
333 334
    cros_sdk -- sudo rm -R "/var/cache/chromeos-chrome/chrome-src/src/out_${board}"
    cros_sdk -- sudo rm -R "/build/${board}"
iv committed
335 336
  fi

iv committed
337
  if [ ${board} == daisy ]; then
iv committed
338
    # XXX: broken by sucessive wrapping
iv committed
339
    echo "daisy board: removing accepted license for the next builds..."
iv committed
340
    cros_sdk -- "sudo mv /etc/make.conf.user.save /etc/make.conf.user"
iv committed
341 342
  fi
done
iv committed
343 344

####################################### Post build ##########################################
iv committed
345
# keep only the substring between - as current release number
iv committed
346
RELEASE=$(echo ${BRANCH} | cut -d- -f2)
iv committed
347
DIR_IMAGE_LOCATION={{ cros_location }}/images/${RELEASE}/$(date +'%F')
iv committed
348
install ${DIR_IMAGE_LOCATION} -d
Romain Courteaud committed
349
mv {{ cros_location }}/{{ branch }}/chroot/tmp/*.img ${DIR_IMAGE_LOCATION}
iv committed
350 351

cd ${DIR_IMAGE_LOCATION}
iv committed
352
for hashfunction in md5sum sha1sum sha256sum sha512sum; do
iv committed
353
  echo ${hashfunction} >> hashes.txt
iv committed
354
  ${hashfunction} *.img >> hashes.txt
iv committed
355
  printf "\n\n" >> hashes.txt
iv committed
356 357 358
done

for file in $(ls *.img); do
Lorenzo Martinico committed
359
  gzip -9 -f ${file}
iv committed
360 361
done

iv committed
362
exit 0