aboutsummaryrefslogtreecommitdiff
path: root/linaro-hwpack-install
blob: ab0b3083fa3a6f26cf94b41d1fbe2c4042108e56 (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
#!/bin/bash
# linaro-hwpack-install - Install a Linaro Hardware Pack.
#   This script is meant to run inside a chroot containing nothing other than
#   ubuntu-minimal, so 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 2010 Linaro
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as published
# by the Free Software Foundation.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

LOCKFILE="/var/lock/hwpack"
TEMP_DIR=$(mktemp -d)
HWPACK_DIR="${TEMP_DIR}/unpacked"
INSTALL_LATEST="no"
SOURCES_LIST_FILE="${TEMP_DIR}/sources.list"
APT_GET_OPTIONS="Dir::Etc::SourceList=${SOURCES_LIST_FILE}"
SUPPORTED_FORMATS="1.0"  # A space-separated list of hwpack formats.

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

usage_msg="Usage: $(basename $0) [--install-latest] HWPACK_TARBALL"
if [ $# -eq 0 ]; then
  die $usage_msg
elif [ $# -eq 1 ]; then
  HWPACK_TARBALL=$1
elif [ $# -eq 2 ]; then
  if [ $1 != "--install-latest" ]; then
    die "Unknown argument: $1 \n$usage_msg"
  fi
  INSTALL_LATEST="yes"
  HWPACK_TARBALL=$2
else
  die $usage_msg
fi

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

cleanup() {
  # Ensure our temp dir and apt sources are removed.
  echo -n "Cleaning up ..."
  rm -rf $TEMP_DIR
  sudo apt-get update -qq
  echo "Done"
}

# 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

# 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 [ $hwpack_format == $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.
HWPACK_ARCH=`grep ARCHITECTURE "${HWPACK_DIR}/metadata" | cut -d "=" -f2`
[ "$HWPACK_ARCH" == `dpkg --print-architecture` ] || \
  die "Hardware pack architecture ($HWPACK_ARCH) does not match the host's architecture"

# 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" /etc/apt/sources.list.d/* /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"

sudo apt-get -o "$APT_GET_OPTIONS" update -qq

echo -n "Installing packages ..."
if [ "$INSTALL_LATEST" == "no" ]; then
  packages=`sed 's/=.*//' "${HWPACK_DIR}"/manifest`
else
  packages=`cat "${HWPACK_DIR}"/manifest`
fi
sudo apt-get -o "$APT_GET_OPTIONS" install $packages
echo "Done"