aboutsummaryrefslogtreecommitdiff
path: root/linaro-hwpack-install
blob: a6c3d9cc00c547cb411e6d2955646082f2ab93a8 (plain)
1
2
3
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
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/bin/sh
# linaro-hwpack-install - Install a Linaro Hardware Pack.
#   This script is meant to run inside a chroot. It must not depend on anything
#   that's not in there.
# TODO: When upgrading to a newer hwpack, make sure packages and apt sources
# that are no longer needed are removed.

# Copyright (C) 2010, 2011 Linaro
#
# Author: Guilherme Salgado <guilherme.salgado@linaro.org>
#
# This file is part of Linaro Image Tools.
#
# Linaro Image Tools is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Linaro Image Tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Linaro Image Tools; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
# USA.

set -e

if [ -n "${TMPDIR+x}" ]; then
 echo -e "\nWARNING: TMPDIR variable is set. It will be unset because in chroot environment it likely doesn't exist and can lead to error messages.\n"
 unset TMPDIR
fi

LOCKFILE="/var/lock/hwpack"
TEMP_DIR=$(mktemp -d)
HWPACK_DIR="${TEMP_DIR}/unpacked"
INSTALL_LATEST="no"
FORCE_YES="no"
SOURCES_LIST_FILE="${TEMP_DIR}/sources.list"
APT_GET_OPTIONS="Dir::Etc::SourceList=${SOURCES_LIST_FILE}"
SUPPORTED_FORMATS="1.0 2.0 3.0"  # A space-separated list of hwpack formats.
FLASH_KERNEL_SKIP="true" 
export FLASH_KERNEL_SKIP # skip attempting to run flash-kernel-hooks
DISTRIBUTION=`grep '^ID=' /etc/os-release | sed 's/ID=//'`

sudo="sudo"
if [ $(id -u) -eq 0 ]; then
    sudo=""
fi

die() {
  echo -e "$@"
  exit 1
}

usage_msg="Usage: $(basename $0) [--install-latest] [--force-yes] [--extract-kernel-only] --hwpack-version <version> --hwpack-arch <architecture> --hwpack-name <name> HWPACK_TARBALL"
if [ $# -eq 0 ]; then
  die $usage_msg
fi

HWPACK_TARBALL_FOUND="no"
HWPACK_VERSION=""
HWPACK_ARCH=""
HWPACK_NAME=""
EXTRACT_KERNEL_ONLY="no"

while [ $# -gt 0 ]; do
  case "$1" in 
    --install-latest)
      INSTALL_LATEST="yes"
      shift;;
    --force-yes)
      FORCE_YES="yes"
      shift;;
    --hwpack-version)
      HWPACK_VERSION=$2
      shift;
      shift;;
    --hwpack-arch)
      HWPACK_ARCH=$2
      shift;
      shift;;
    --hwpack-name)
      HWPACK_NAME=$2
      shift;
      shift;;
    --extract-kernel-only)
      EXTRACT_KERNEL_ONLY="yes"
      shift;;
    --*)
      die $usage_msg "\nUnrecognized option: \"$1\"";;
    *)
      [ "$HWPACK_TARBALL_FOUND" = "yes" ] && die $usage_msg
      HWPACK_TARBALL="$1"
      HWPACK_TARBALL_FOUND="yes"
      shift;;
  esac
done

[ "$HWPACK_TARBALL_FOUND" = "no" ] && die $usage_msg
[ "$HWPACK_VERSION" = "" ] && die $usage_msg
[ "$HWPACK_ARCH" = "" ] && die $usage_msg
[ "$HWPACK_NAME" = "" ] && die $usage_msg

setup_hwpack() {
  # This creates all the directories we need.
  mkdir -p "$HWPACK_DIR"

  # Unpack the hwpack tarball. We don't download it here because the chroot may
  # not contain any tools that would allow us to do that.
  echo -n "Unpacking hardware pack ..."
  tar zxf "$HWPACK_TARBALL" -C "$HWPACK_DIR"
  echo "Done"

  # Check the format of the hwpack is supported.
  hwpack_format=$(cat ${HWPACK_DIR}/FORMAT)
  supported="false"
  for format in $SUPPORTED_FORMATS; do
    if [ "x$hwpack_format" = "x$format" ]; then
      supported="true"
      break
    fi
  done

  [ $supported = "true" ] || \
    die "Unsupported hwpack format: $hwpack_format. "\
        "Try using a newer version of $(basename $0)."

  # Check the architecture of the hwpack matches that of the host system.
  if [ "x$EXTRACT_KERNEL_ONLY" = "xno" -a "$DISTRIBUTION" = "ubuntu" ]; then
    # TODO: create a generic way to identify the architecture, without depending on dpkg
    [ "$HWPACK_ARCH" = `dpkg --print-architecture` ] || \
      die "Hardware pack architecture ($HWPACK_ARCH) does not match the host's architecture"
  fi
}

setup_apt_sources() {
  # Install the apt sources that contain the packages we need.
  for filename in $(ls "${HWPACK_DIR}"/sources.list.d/); do
    file="${HWPACK_DIR}"/sources.list.d/$filename
    should_install=0
    stripped_file=${TEMP_DIR}/$filename
    grep -v "\(^#\|^\s*$\)" $file > $stripped_file
    while read line; do
      # Only install files that have at least one line not present in the
      # existing sources lists.
      grep -qF "$line" $(find /etc/apt/sources.list.d/ -name '*.list') /etc/apt/sources.list \
        || should_install=1
    done < $stripped_file

    if [ $should_install -eq 1 ]; then
      $sudo cp $file /etc/apt/sources.list.d/hwpack.$filename
    fi
  done

  # Import the OpenPGP keys for the files installed above.
  for filename in $(ls "${HWPACK_DIR}"/sources.list.d.gpg/); do
    file="${HWPACK_DIR}"/sources.list.d.gpg/$filename
    $sudo apt-key add $file
  done

  # Add one extra apt source for the packages included in the hwpack and make
  # sure it's the first on the list of sources so that it gets precedence over
  # the others.
  echo "deb file:${HWPACK_DIR}/pkgs ./" > "$SOURCES_LIST_FILE"
  cat /etc/apt/sources.list >> "$SOURCES_LIST_FILE"

  if [ "$FORCE_YES" = "yes" ]; then
    FORCE_OPTIONS="--yes --force-yes"
  else
    FORCE_OPTIONS=""
  fi

  # Do two updates. The first doesn't try to download package lists:
  # * First update doesn't access net
  #   - not allowed to fail. Image file + hwpack should contain all packages
  #     needed to create image. If this update fails we have problems.
  # * Second update may fail
  #   - If can't download package updates (the only difference between the two
  #     commands), we should still be OK.
  echo "Updating apt package lists ..."
  $sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" update -q --no-download --ignore-missing
  $sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" update -q || true
}

setup_ubuntu_rootfs() {
  # Prevent daemons to start in the chroot
  echo "exit 101" > /usr/sbin/policy-rc.d
  chmod a+x /usr/sbin/policy-rc.d

  mv -f /sbin/start-stop-daemon /sbin/start-stop-daemon.REAL
  cat > /sbin/start-stop-daemon << EOF
#!/bin/sh

echo "Warning: Fake start-stop-daemon called, doing nothing"
EOF
chmod 755 /sbin/start-stop-daemon

if [ -x /sbin/initctl ]; then
  mv -f /sbin/initctl /sbin/initctl.REAL
  cat > /sbin/initctl << EOF
#!/bin/sh

echo "Warning: Fake initctl called, doing nothing"
EOF
    chmod 755 /sbin/initctl
  fi
}

install_deb_packages() {
  echo -n "Installing packages ..."

  # "newer" hwpacks contain a dependency package whose Depends is the
  # same as the packages config setting from the file the hwpack was
  # build from.  But if we just installed that, a newer version of a
  # package than that in the hwpack might have made it to the main
  # archive and apt-get would install that instead.  So we install the
  # specific package versions that make up the hwpack.  /That/ however
  # would leave all the packages from the hwpack marked as manually
  # installed, so if a newer hwpack was installed over the top which no
  # longer depended on one of the packages the older one did, the
  # package would not be eligible for autoremoval.  So we mark the all
  # packages newly installed as part of hwpack installed (apart from the
  # dependency package) as automatically installed with apt-get
  # markauto.
  #
  # For "older" hwpacks that don't have a dependency package, we just
  # manually install the contents of the hwpack.

  dependency_package="hwpack-${HWPACK_NAME}"
  if grep -q "^${dependency_package}=${HWPACK_VERSION}\$" "${HWPACK_DIR}"/manifest; then
    DEP_PACKAGE_PRESENT="yes"
  else
    DEP_PACKAGE_PRESENT="no"
  fi

  packages_without_versions=`sed 's/=.*//' "${HWPACK_DIR}"/manifest`
  packages_with_versions=`cat "${HWPACK_DIR}"/manifest`

  if [ "$INSTALL_LATEST" = "yes" ]; then
    packages="${packages_without_versions}"
  else
    packages="${packages_with_versions}"
  fi

  if [ "$DEP_PACKAGE_PRESENT" = "yes" ]; then
    to_be_installed=
    for package in $packages_without_versions; do
      if [ "${package}" != "${dependency_package}" ]; then
        { dpkg --get-selections $package 2>/dev/null| grep -qw 'install$'; } || to_be_installed="$to_be_installed $package"
      fi
    done
  fi

  $sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" install ${packages}

  if [ "$DEP_PACKAGE_PRESENT" = "yes" ]; then
    if [ -n "${to_be_installed}" ]; then
      $sudo apt-get $FORCE_OPTIONS -o "$APT_GET_OPTIONS" markauto ${to_be_installed}
    fi
  fi
}

extract_kernel_packages() {
  echo "Extracting all kernel packages ..."

  # We assume the hwpack is always available at the rootfs
  ROOTFS_DIR=$(dirname $HWPACK_TARBALL)

  DEB_DIR="${TEMP_DIR}/extracted"
  mkdir -p ${DEB_DIR}

  ls ${HWPACK_DIR}/pkgs/linux-[ih]*.deb | while read pkg; do
    echo "Extracting package `basename $pkg`"
    if [ "$DISTRIBUTION" = "ubuntu" ]; then
      dpkg-deb -x ${pkg} $ROOTFS_DIR
    elif [ "$DISTRIBUTION" = "fedora" ]; then
      ar x ${pkg}
      tar -xf data.tar.* -C ${DEB_DIR}
      rm -f debian-binary control.tar.gz data.tar.*
    fi
  done

  ls ${HWPACK_DIR}/pkgs/arndale-pre-boot_*.deb | while read pkg; do
    echo "Extracting package `basename $pkg`"
    if [ "$DISTRIBUTION" = "ubuntu" ]; then
      dpkg-deb -x ${pkg} $ROOTFS_DIR
    elif [ "$DISTRIBUTION" = "fedora" ]; then
      ar x ${pkg}
      tar -xf data.tar.* -C ${DEB_DIR}
      rm -f debian-binary control.tar.gz data.tar.*
    fi
  done

  if [ "$DISTRIBUTION" = "fedora" ]; then
      cp -a ${DEB_DIR}/boot/* /boot/
      cp -a ${DEB_DIR}/lib/modules/* /usr/lib/modules/
      cp -a ${DEB_DIR}/lib/firmware/* /usr/lib/firmware/
  fi

  if [ -d "${HWPACK_DIR}/u_boot" ]; then
    cp -a ${HWPACK_DIR}/u_boot/usr/lib/* /usr/lib
  fi

  if [ -d "${HWPACK_DIR}/uefi" ]; then
    cp -a ${HWPACK_DIR}/uefi/usr/lib/* /usr/lib
  fi

  # manually generate modules.dep
  ls $ROOTFS_DIR/lib/modules | while read kernel; do
    depmod -b $ROOTFS_DIR ${kernel} || true
    [ "$DISTRIBUTION" = "fedora" ] && dracut /boot/initrd.img-${kernel} ${kernel}
  done;
}

cleanup() {
  # Ensure our temp dir and apt sources are removed.
  echo -n "Cleaning up ..."
  rm -rf $TEMP_DIR
  if [ "x$EXTRACT_KERNEL_ONLY" = "xno" -a "$DISTRIBUTION" = "ubuntu" ]; then
    rm -f /usr/sbin/policy-rc.d
    mv -f /sbin/start-stop-daemon.REAL /sbin/start-stop-daemon
    if [ -x /sbin/initctl.REAL ]; then
      mv -f /sbin/initctl.REAL /sbin/initctl
    fi
    # Do two updates. The first doesn't try to download package lists:
    # * First update doesn't access net
    #   - not allowed to fail. Image file + hwpack should contain all packages
    #     needed to create image. If this update fails we have problems.
    # * Second update may fail
    #   - If can't download package updates (the only difference between the two
    #     commands), we should still be OK.
    $sudo apt-get update -qq --no-download --ignore-missing
    $sudo apt-get update -qq || true
  fi
  echo "Done"
}

## main

# Try to acquire fd #9 (i.e. /var/lock/hwpack)
# Using 9 as the file descriptor because of https://launchpad.net/bugs/249620
exec 9>$LOCKFILE
flock -n 9 || die "Could not acquire lock: $LOCKFILE"

# From now on we'll be making changes to the system, so we need to clean
# things up when the script exits.
trap cleanup EXIT

# Extract and set up the hwpack at the rootfs
setup_hwpack

# In case we only care about the kernel, don't mess up with the system
if [ "x$EXTRACT_KERNEL_ONLY" = "xno" -a "$DISTRIBUTION" = "ubuntu" ]; then
  setup_apt_sources
  setup_ubuntu_rootfs
  install_deb_packages
else
  extract_kernel_packages
fi

echo "Done"