Skip to content

Commit e3a43ef

Browse files
committed
Merge remote-tracking branch 'pigen/master' into MSW
2 parents 8713ed1 + b403540 commit e3a43ef

File tree

59 files changed

+413
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+413
-176
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
output/
22
work/
33
deploy/
4+
apt-cacher-ng/
5+
.git/objects/*

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ work/*
33
config
44
postrun.sh
55
SKIP
6+
SKIP_IMAGES
67
.pc
78
*-pc
9+
apt-cacher-ng/

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
FROM debian:jessie
1+
FROM debian:stretch
2+
3+
ENV DEBIAN_FRONTEND noninteractive
24

35
RUN apt-get -y update && \
46
apt-get -y install \
57
git vim parted \
68
quilt realpath qemu-user-static debootstrap zerofree pxz zip dosfstools \
7-
bsdtar libcap2-bin rsync grep \
9+
bsdtar libcap2-bin rsync grep udev xz-utils curl xxd \
810
&& rm -rf /var/lib/apt/lists/*
911

1012
COPY . /pi-gen/

License.md renamed to LICENSE

File renamed without changes.

README.md

Lines changed: 149 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
# pi-gen
2+
23
_Tool used to create the raspberrypi.org Raspbian images_
34

4-
### TODO
5-
1. Documentation
65

76
## Dependencies
87

9-
`quilt parted realpath qemu-user-static debootstrap zerofree pxz zip dosfstools bsdtar libcap2-bin grep rsync`
8+
pi-gen runs on Debian based operating systems. Currently it is only supported on
9+
either Debian Stretch or Ubuntu Xenial and is known to have issues building on
10+
earlier releases of these systems.
11+
12+
To install the required dependencies for pi-gen you should run:
13+
14+
```bash
15+
apt-get install quilt parted realpath qemu-user-static debootstrap zerofree pxz zip \
16+
dosfstools bsdtar libcap2-bin grep rsync xz-utils
17+
```
18+
19+
The file `depends` contains a list of tools needed. The format of this
20+
package is `<tool>[:<debian-package>]`.
21+
1022

1123
## Config
1224

@@ -29,29 +41,117 @@ The following environment variables are supported:
2941
will not be included in the image, making it safe to use an `apt-cacher` or
3042
similar package for development.
3143

44+
If you have Docker installed, you can set up a local apt caching proxy to
45+
like speed up subsequent builds like this:
46+
47+
docker-compose up -d
48+
echo 'APT_PROXY=http://172.17.0.1:3142' >> config
49+
50+
* `BASE_DIR` (Default: location of `build.sh`)
51+
52+
**CAUTION**: Currently, changing this value will probably break build.sh
53+
54+
Top-level directory for `pi-gen`. Contains stage directories, build
55+
scripts, and by default both work and deployment directories.
56+
57+
* `WORK_DIR` (Default: `"$BASE_DIR/work"`)
58+
59+
Directory in which `pi-gen` builds the target system. This value can be
60+
changed if you have a suitably large, fast storage location for stages to
61+
be built and cached. Note, `WORK_DIR` stores a complete copy of the target
62+
system for each build stage, amounting to tens of gigabytes in the case of
63+
Raspbian.
64+
65+
**CAUTION**: If your working directory is on an NTFS partition you probably won't be able to build. Make sure this is a proper Linux filesystem.
66+
67+
* `DEPLOY_DIR` (Default: `"$BASE_DIR/deploy"`)
68+
69+
Output directory for target system images and NOOBS bundles.
70+
71+
* `USE_QEMU` (Default: `"0"`)
72+
73+
This enable the Qemu mode and set filesystem and image suffix if set to 1.
74+
75+
3276
A simple example for building Raspbian:
3377

3478
```bash
3579
IMG_NAME='Raspbian'
3680
```
3781

82+
83+
## How the build process works
84+
85+
The following process is followed to build images:
86+
87+
* Loop through all of the stage directories in alphanumeric order
88+
89+
* Move on to the next directory if this stage directory contains a file called
90+
"SKIP"
91+
92+
* Run the script ```prerun.sh``` which is generally just used to copy the build
93+
directory between stages.
94+
95+
* In each stage directory loop through each subdirectory and then run each of the
96+
install scripts it contains, again in alphanumeric order. These need to be named
97+
with a two digit padded number at the beginning.
98+
There are a number of different files and directories which can be used to
99+
control different parts of the build process:
100+
101+
- **00-run.sh** - A unix shell script. Needs to be made executable for it to run
102+
103+
- **00-run-chroot.sh** - A unix shell script which will be run in the chroot
104+
of the image build directory. Needs to be made executable for it to run.
105+
106+
- **00-debconf** - Contents of this file are passed to debconf-set-selections
107+
to configure things like locale, etc.
108+
109+
- **00-packages** - A list of packages to install. Can have more than one, space
110+
separated, per line.
111+
112+
- **00-packages-nr** - As 00-packages, except these will be installed using
113+
the ```--no-install-recommends -y``` parameters to apt-get
114+
115+
- **00-patches** - A directory containing patch files to be applied
116+
117+
* If the stage directory contains files called "EXPORT_NOOBS" or "EXPORT_IMAGE" then
118+
add this stage to a list of images to generate
119+
120+
* Generate the images for any stages that have specified them
121+
122+
It is recommended to examine build.sh for finer details.
123+
124+
38125
## Docker Build
39126

40127
```bash
41128
vi config # Edit your config file. See above.
42129
./build-docker.sh
43130
```
131+
44132
If everything goes well, your finished image will be in the `deploy/` folder.
45-
You can then remove the build container with `docker rm pigen_work`
133+
You can then remove the build container with `docker rm -v pigen_work`
46134

47135
If something breaks along the line, you can edit the corresponding scripts, and
48136
continue:
49137

50-
```
138+
```bash
51139
CONTINUE=1 ./build-docker.sh
52140
```
53141

54-
There is a possibility that even when running from a docker container, the installation of `qemu-user-static` will silently fail when building the image because `binfmt-support` _must be enabled on the underlying kernel_. An easy fix is to ensure `binfmt-support` is installed on the host machine before starting the `./build-docker.sh` script (or using your own docker build solution).
142+
After successful build, the build container is by default removed. This may be undesired when making incremental changes to a customized build. To prevent the build script from remove the container add
143+
144+
```bash
145+
PRESERVE_CONTAINER=1 ./build-docker.sh
146+
```
147+
148+
There is a possibility that even when running from a docker container, the
149+
installation of `qemu-user-static` will silently fail when building the image
150+
because `binfmt-support` _must be enabled on the underlying kernel_. An easy
151+
fix is to ensure `binfmt-support` is installed on the host machine before
152+
starting the `./build-docker.sh` script (or using your own docker build
153+
solution).
154+
55155

56156
## Stage Anatomy
57157

@@ -86,32 +186,62 @@ maintenance and allows for more easy customization.
86186
standard console hardware permission groups.
87187

88188
There are a few tools that may not make a whole lot of sense here for
89-
development purposes on a minimal system such as basic python and lua
189+
development purposes on a minimal system such as basic Python and Lua
90190
packages as well as the `build-essential` package. They are lumped right
91191
in with more essential packages presently, though they need not be with
92192
pi-gen. These are understandable for Raspbian's target audience, but if
93-
you were looking for something between truly minimal and Raspbian-lite,
193+
you were looking for something between truly minimal and Raspbian-Lite,
94194
here's where you start trimming.
95195

96196
- **Stage 3** - desktop system. Here's where you get the full desktop system
97197
with X11 and LXDE, web browsers, git for development, Raspbian custom UI
98198
enhancements, etc. This is a base desktop system, with some development
99199
tools installed.
100200

101-
- **Stage 4** - complete Raspbian system. More development tools, an email
102-
client, learning tools like Scratch, specialized packages like sonic-pi and
103-
wolfram-engine, system documentation, office productivity, etc. This is
104-
the stage that installs all of the things that make Raspbian friendly to
105-
new users.
201+
- **Stage 4** - Raspbian system meant to fit on a 4GB card. More development
202+
tools, an email client, learning tools like Scratch, specialized packages
203+
like sonic-pi, system documentation, office productivity, etc. This is the
204+
stage that installs all of the things that make Raspbian friendly to new
205+
users.
206+
207+
- **Stage 5** - The official Raspbian Desktop image. Right now only adds
208+
Mathematica.
106209

107210
### Stage specification
108-
If you wish to build up to a specified stage (such as building up to stage 2 for a lite system), place an empty file named `SKIP` in each of the `./stage` directories you wish not to include.
109211

110-
Then remove the `EXPORT*` files from `./stage4` (if building up to stage 2) or from `./stage2` (if building a minimal system).
212+
If you wish to build up to a specified stage (such as building up to stage 2
213+
for a lite system), place an empty file named `SKIP` in each of the `./stage`
214+
directories you wish not to include.
111215

112-
```
216+
Then remove the `EXPORT*` files from `./stage4` (if building up to stage 2) or
217+
from `./stage2` (if building a minimal system).
218+
219+
```bash
113220
# Example for building a lite system
114-
$ touch ./stage3/SKIP ./stage4/SKIP
115-
$ rm stage4/EXPORT*
221+
echo "IMG_NAME='Raspbian'" > config
222+
touch ./stage3/SKIP ./stage4/SKIP ./stage5/SKIP
223+
rm stage4/EXPORT* stage5/EXPORT*
224+
sudo ./build.sh # or ./build-docker.sh
116225
```
117-
If you wish to build further configurations upon (for example) the lite system, you can also delete the contents of `./stage3` and `./stage4` and replace with your own contents in the same format.
226+
227+
If you wish to build further configurations upon (for example) the lite
228+
system, you can also delete the contents of `./stage3` and `./stage4` and
229+
replace with your own contents in the same format.
230+
231+
232+
## Skipping stages to speed up development
233+
234+
If you're working on a specific stage the recommended development process is as
235+
follows:
236+
237+
* Add a file called SKIP_IMAGES into the directories containing EXPORT_* files
238+
(currently stage2, stage4 and stage5)
239+
* Add SKIP files to the stages you don't want to build. For example, if you're
240+
basing your image on the lite image you would add these to stages 3, 4 and 5.
241+
* Run build.sh to build all stages
242+
* Add SKIP files to the earlier successfully built stages
243+
* Modify the last stage
244+
* Rebuild just the last stage using ```sudo CLEAN=1 ./build.sh```
245+
* Once you're happy with the image you can remove the SKIP_IMAGES files and
246+
export your image to test
247+

build-docker.sh

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash -e
2+
23
DOCKER="docker"
34
set +e
45
$DOCKER ps >/dev/null 2>&1
@@ -12,26 +13,28 @@ if ! $DOCKER ps >/dev/null; then
1213
fi
1314
set -e
1415

15-
config_mount=
16+
config_file=()
1617
if [ -f config ]; then
17-
config_mount="-v $(pwd)/config:/pi-gen/config:ro"
18+
config_file=("--env-file" "$(pwd)/config")
1819
source config
1920
fi
2021

2122
CONTAINER_NAME=${CONTAINER_NAME:-pigen_work}
2223
CONTINUE=${CONTINUE:-0}
24+
PRESERVE_CONTAINER=${PRESERVE_CONTAINER:-0}
2325

2426
if [ "$*" != "" ] || [ -z "${IMG_NAME}" ]; then
2527
if [ -z "${IMG_NAME}" ]; then
26-
echo "IMG_NAME not set in 'build'" 1>&2
28+
echo "IMG_NAME not set in 'config'" 1>&2
2729
echo 1>&2
2830
fi
2931
cat >&2 <<EOF
3032
Usage:
3133
build-docker.sh [options]
3234
Optional environment arguments: ( =<default> )
3335
CONTAINER_NAME=pigen_work set a name for the build container
34-
CONTINUE=0 continue from a previously started container
36+
CONTINUE=1 continue from a previously started container
37+
PRESERVE_CONTAINER=1 keep build container even on successful build
3538
EOF
3639
exit 1
3740
fi
@@ -45,29 +48,39 @@ fi
4548
if [ "$CONTAINER_EXISTS" != "" ] && [ "$CONTINUE" != "1" ]; then
4649
echo "Container $CONTAINER_NAME already exists and you did not specify CONTINUE=1. Aborting."
4750
echo "You can delete the existing container like this:"
48-
echo " docker rm $CONTAINER_NAME"
51+
echo " $DOCKER rm -v $CONTAINER_NAME"
4952
exit 1
5053
fi
5154

5255
$DOCKER build -t pi-gen .
5356
if [ "$CONTAINER_EXISTS" != "" ]; then
54-
trap "echo 'got CTRL+C... please wait 5s';docker stop -t 5 ${CONTAINER_NAME}_cont" SIGINT SIGTERM
57+
trap "echo 'got CTRL+C... please wait 5s'; $DOCKER stop -t 5 ${CONTAINER_NAME}_cont" SIGINT SIGTERM
5558
time $DOCKER run --rm --privileged \
5659
--volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
60+
-e IMG_NAME=${IMG_NAME}\
5761
pi-gen \
5862
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
5963
cd /pi-gen; ./build.sh;
6064
rsync -av work/*/build.log deploy/" &
61-
wait
65+
wait "$!"
6266
else
63-
trap "echo 'got CTRL+C... please wait 5s'; docker stop -t 5 ${CONTAINER_NAME}" SIGINT SIGTERM
64-
$DOCKER run --name "${CONTAINER_NAME}" --privileged \
65-
-v $(pwd)/deploy:/pi-gen/deploy \
66-
${config_mount} \
67+
trap "echo 'got CTRL+C... please wait 5s'; $DOCKER stop -t 5 ${CONTAINER_NAME}" SIGINT SIGTERM
68+
time $DOCKER run --name "${CONTAINER_NAME}" --privileged \
69+
-e IMG_NAME=${IMG_NAME}\
70+
"${config_file[@]}" \
6771
pi-gen \
6872
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
6973
cd /pi-gen; ./build.sh &&
7074
rsync -av work/*/build.log deploy/" &
71-
wait
75+
wait "$!"
76+
fi
77+
echo "copying results from deploy/"
78+
$DOCKER cp "${CONTAINER_NAME}":/pi-gen/deploy .
79+
ls -lah deploy
80+
81+
# cleanup
82+
if [ "$PRESERVE_CONTAINER" != "1" ]; then
83+
$DOCKER rm -v $CONTAINER_NAME
7284
fi
85+
7386
echo "Done! Your image(s) should be in deploy/"

build.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ run_stage(){
8585
unmount ${WORK_DIR}/${STAGE}
8686
STAGE_WORK_DIR=${WORK_DIR}/${STAGE}
8787
ROOTFS_DIR=${STAGE_WORK_DIR}/rootfs
88-
if [ -f ${STAGE_DIR}/EXPORT_IMAGE ]; then
89-
EXPORT_DIRS="${EXPORT_DIRS} ${STAGE_DIR}"
88+
if [ ! -f SKIP_IMAGES ]; then
89+
if [ -f ${STAGE_DIR}/EXPORT_IMAGE ]; then
90+
EXPORT_DIRS="${EXPORT_DIRS} ${STAGE_DIR}"
91+
fi
9092
fi
9193
if [ ! -f SKIP ]; then
9294
if [ "${CLEAN}" = "1" ]; then
@@ -119,6 +121,7 @@ if [ "$(id -u)" != "0" ]; then
119121
exit 1
120122
fi
121123

124+
122125
if [ -f config ]; then
123126
source config
124127
fi
@@ -128,7 +131,8 @@ if [ -z "${IMG_NAME}" ]; then
128131
exit 1
129132
fi
130133

131-
export IMG_DATE=${IMG_DATE:-"$(date -u +%Y-%m-%d)"}
134+
export USE_QEMU=${USE_QEMU:-0}
135+
export IMG_DATE=${IMG_DATE:-"$(date +%Y-%m-%d)"}
132136

133137
export BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
134138
export SCRIPT_DIR="${BASE_DIR}/scripts"
@@ -177,10 +181,12 @@ for EXPORT_DIR in ${EXPORT_DIRS}; do
177181
source "${EXPORT_DIR}/EXPORT_IMAGE"
178182
EXPORT_ROOTFS_DIR=${WORK_DIR}/$(basename ${EXPORT_DIR})/rootfs
179183
run_stage
180-
if [ -e ${EXPORT_DIR}/EXPORT_NOOBS ]; then
181-
source ${EXPORT_DIR}/EXPORT_NOOBS
182-
STAGE_DIR=${BASE_DIR}/export-noobs
183-
run_stage
184+
if [ "${USE_QEMU}" != "1" ]; then
185+
if [ -e ${EXPORT_DIR}/EXPORT_NOOBS ]; then
186+
source ${EXPORT_DIR}/EXPORT_NOOBS
187+
STAGE_DIR=${BASE_DIR}/export-noobs
188+
run_stage
189+
fi
184190
fi
185191
done
186192

depends

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ mkdosfs:dosfstools
99
capsh:libcap2-bin
1010
bsdtar
1111
grep
12-
rsync
12+
rsync
13+
xz:xz-utils
14+
curl
15+
xxd

0 commit comments

Comments
 (0)