]> git.wh0rd.org Git - home.git/blob - .bin/cros-board
gpg.conf: switch to sks servers
[home.git] / .bin / cros-board
1 #!/bin/bash
2 . /etc/init.d/functions.sh || exit 1
3 die() { eerror "$*"; exit 1; }
4 set -e
5
6 BASE="/mnt/host/source"
7 d="${BASE}/src/scripts"
8 BUILD_DIR="${BASE}/src/build"
9 cd "${BASE}/src/scripts/"
10
11 : "${BOARD:="$(cat .default_board 2>/dev/null)"}"
12 if [[ -z ${BOARD} ]] ; then
13         die "missing BOARD"
14 fi
15 IMAGES="${BASE}/src/build/images/${BOARD}"
16 BUILD="/build/${BOARD}"
17
18 usage() {
19         exec cat <<EOF
20 Usage: cros-board [options]
21
22 Build for the board listed in \${BOARD} (current: ${BOARD}).
23 Normally only builds a dev image.
24
25 Options:
26   -t          Build test packages & image
27   --autotest  Build & install autotest packages.
28   --factory   Build factory packages & image.
29   --nousepkg  Force build from source.
30   --vm        Create VM image.
31
32   --clean     ?
33
34   -Y          Write image to ${ARG_DEV} w/out prompting.
35   -y <dev>    Write image to <dev> w/out prompting.
36
37   -p          Pretend -- show commands that would be run.
38   -v          Verbose -- send command output to stdout.
39 EOF
40 }
41
42 find_first_removable() {
43         local d
44         cd /sys/block
45         for d in sd? ; do
46                 if [[ $(cat "${d}/removable" 2>/dev/null) == "1" ]]; then
47                         echo "/dev/${d}"
48                         return 0
49                 fi
50         done
51         return 0
52 }
53
54 ARG_CLEAN=false
55 ARG_DEV="$(find_first_removable)"
56 ARG_DEV_USER=false
57 ARG_PRETEND=false
58 ARG_NOPKG=false
59 ARG_AUTOTEST=false
60 ARG_FACTORY=false
61 ARG_VERBOSE=false
62 ARG_TEST=false
63 ARG_VM=false
64 while [[ $# -ne 0 ]] ; do
65         case $1 in
66         --clean) ARG_CLEAN=true;;
67         -p) ARG_PRETEND=true;;
68         -Y) ARG_DEV_USER=true;;
69         -y) shift; ARG_DEV=$1; ARG_DEV_USER=true;;
70         -v) ARG_VERBOSE=true;;
71         -t) ARG_TEST=true;;
72         --nousepkg) ARG_NOPKG=true;;
73         --autotest) ARG_AUTOTEST=true;;
74         --factory) ARG_FACTORY=true;;
75         --vm) ARG_VM=true;;
76         -h) usage;;
77         *) echo "unknown opt: $*"; exit 1;;
78         esac
79         shift
80 done
81
82 piperet() { set -- ${PIPESTATUS[@]/0}; [[ $# -eq 0 ]]; }
83 run() {
84         set +e
85         if ! ${ARG_PRETEND} ; then
86                 ebegin "$*"
87                 local log="${IMAGES}"/log.${1##*/}
88                 if ${ARG_VERBOSE} ; then
89                         "$@" |& tee "${log}"
90                 else
91                         "$@" >& "${log}"
92                 fi
93                 piperet
94                 eend $? || exit $?
95         else
96                 echo "$*"
97         fi
98         set -e
99 }
100
101 tfe() { ($1 && echo $2 || echo $3); }
102
103 mkdir -p "${IMAGES}"
104 rm -f "${IMAGES}"/log.*
105
106 einfo "Log root: ${IMAGES}/log."
107
108 run ./update_chroot --toolchain_boards=${BOARD}
109
110 if ${ARG_CLEAN} || [[ ! -d ${BUILD} ]] ; then
111         run sudo rm -rf "${BUILD}"
112         run setup_board --board=${BOARD} --skip-chroot-upgrade
113         if [[ $? -ne 0 ]] ; then
114                 run sudo rm -rf "${BUILD}"
115                 exit 1
116         fi
117 fi
118
119 run ./build_packages \
120         --board=${BOARD} \
121         --nowithdebug \
122         --nowithrevdeps \
123         --skip-chroot-upgrade \
124         --autosetgov \
125         $(tfe ${ARG_AUTOTEST} --{,no}withautotest) \
126         $(tfe ${ARG_FACTORY} --{,no}withfactory) \
127         $(tfe ${ARG_NOPKG} --{no,}usepkg) \
128         $(tfe ${ARG_TEST} --{,no}withtest)
129 BUILD_IMG="./build_image --board=${BOARD} --noenable_rootfs_verification $(tfe ${ARG_TEST} test base)"
130 run ${BUILD_IMG}
131
132 IMG_TO_USB="cros flash --yes usb://${ARG_DEV} xbuddy://local/${BOARD}/latest/dev"
133 if ${ARG_DEV_USER} ; then
134         run ${IMG_TO_USB}
135         sync &
136 fi
137
138 IMG_TO_VM=( ${d}/image_to_vm.sh --board=${BOARD} --from=${BUILD_DIR}/images/${BOARD}/latest --test_image )
139 if ${ARG_VM} ; then
140         run "${IMG_TO_VM[@]}"
141 fi
142
143 cd "${IMAGES}"
144 rm -rf $(ls -td1R `find -mindepth 1 -maxdepth 1 -type d` | sed -e 1d -e 2d)
145
146 BUILD_IMG="${d}/${BUILD_IMG#./}"
147 cat <<EOF
148
149 ### LIVE
150 ${IMG_TO_USB}
151
152 ### KVM
153 ${IMG_TO_VM[*]}
154 ~/chromiumos/chromite/bin/cros_vm --start --no-display --image-path=\${HOME}/chromiumos/src/build/images/${BOARD}/latest/chromiumos_qemu_image.bin
155 #--kvm_cpu "max,vmx=on"
156
157 https://dev.chromium.org/chromium-os/testing/running-smoke-suite-on-a-vm-image
158
159 /usr/local/autotest/bin/autologin.py
160
161 export CROS_USER_ID_HASH=\$(ls /home/user/)
162 sudo -u chronos CROS_USER_ID_HASH="${CROS_USER_ID_HASH}" crosh
163   vmc start termina  # might run twice
164
165 ### RECOVERY
166 ${d}/mod_image_for_recovery.sh --board=${BOARD}
167 ${IMG_TO_USB%/dev}/recovery
168
169 ### FACTORY
170 ${BUILD_IMG} --factory_install
171 ${IMG_TO_USB} -i factory_install_shim.bin
172
173 EOF