forked from rancher/elemental
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathros-image-build
executable file
·241 lines (215 loc) · 7.02 KB
/
ros-image-build
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
#!/bin/bash
# Note: ros-image-build requires the input image to be pushed
# due to buildx usage.
# Export this here so users dont need to
export DOCKER_BUILDKIT=1
set -e
build()
{
dockerfile | docker build -f - --build-arg IMAGE="${IMAGE}" . "${@}"
}
dockerfile()
{
cat << "EOF"
ARG IMAGE=rancher/os2:dev
FROM ${IMAGE} AS os
FROM quay.io/costoolkit/elemental:v0.0.14-5bdba36 AS elemental
# Keep using leap 15.3. libslirp looks broken and incompatible with packer
FROM opensuse/leap:15.3 AS tools
COPY --from=elemental /usr/bin/elemental /usr/bin
RUN sed -i -s 's/^# rpm.install.excludedocs/rpm.install.excludedocs/' /etc/zypp/zypp.conf
RUN zypper ref
ENV LUET_NOLOCK=true
# Copy luet from the official images
RUN zypper in -y squashfs xorriso curl unzip git qemu-arm qemu-x86 qemu-tools tar e2fsprogs dosfstools pigz go1.16 qemu-uefi-aarch64 mtools rsync
RUN cd /usr/sbin && \
rm packer && \
SUFFIX=amd64 && \
if [ "$(uname -m)" = "aarch64" ]; then SUFFIX=arm64; fi && \
curl https://releases.hashicorp.com/packer/1.7.4/packer_1.7.4_linux_${SUFFIX}.zip > tmp && \
unzip tmp && \
rm tmp
RUN cd /usr/src && \
git clone https://github.com/rancher-sandbox/cOS-toolkit && \
cd cOS-toolkit && git checkout ${COS_VERSION} -b build
RUN mkdir -p /iso/iso-overlay/boot/grub2
RUN echo -e \
'search --file --set=root /boot/kernel.xz\n'\
'set default=0\n'\
'set timeout=10\n'\
'set timeout_style=menu\n'\
'set linux=linux\n'\
'set initrd=initrd\n'\
'if [ "${grub_cpu}" = "x86_64" -o "${grub_cpu}" = "i386" -o "${grub_cpu}" = "arm64" ];then\n'\
' if [ "${grub_platform}" = "efi" ]; then\n'\
' if [ "${grub_cpu}" != "arm64" ]; then\n'\
' set linux=linuxefi\n'\
' set initrd=initrdefi\n'\
' fi\n'\
' fi\n'\
'fi\n'\
'if [ "${grub_platform}" = "efi" ]; then\n'\
' echo "Please press 't' to show the boot menu on this console"\n'\
'fi\n'\
'set font=($root)/boot/${grub_cpu}/loader/grub2/fonts/unicode.pf2\n'\
'if [ -f ${font} ];then\n'\
' loadfont ${font}\n'\
'fi\n'\
'menuentry "RancherOS Install" --class os --unrestricted {\n'\
' echo Loading kernel...\n'\
' $linux ($root)/boot/kernel.xz cdroot root=live:CDLABEL=COS_LIVE rd.live.dir=/ rd.live.squashimg=rootfs.squashfs console=tty1 console=ttyS0 rd.cos.disable rancheros.install.automatic=true rancheros.install.config_url=/run/initramfs/live/config\n'\
' echo Loading initrd...\n'\
' $initrd ($root)/boot/rootfs.xz\n'\
'}\n'\
'\n'\
'if [ "${grub_platform}" = "efi" ]; then\n'\
' hiddenentry "Text mode" --hotkey "t" {\n'\
' set textmode=true\n'\
' terminal_output console\n'\
' }\n'\
'fi\n' > /iso/iso-overlay/boot/grub2/grub.cfg
RUN echo -e '#cloud-config\n'\
'rancheros:\n'\
' install:\n'\
' automatic: false\n' > /iso/iso-overlay/config
ARG CONFIG
RUN if [ -n "$CONFIG" ]; then echo "$CONFIG" > /iso/iso-overlay/config; fi
WORKDIR /usr/src/cOS-toolkit/packer
FROM tools AS iso-build
COPY --from=os / /iso/overlay
RUN cd /iso && \
elemental --debug build-iso -n output --overlay-iso /iso/iso-overlay /iso/overlay
FROM iso-build AS qcow-build
ARG ACCEL=tcg
RUN SUFFIX= && \
FIRMWARE= && \
if [ "$(uname -m)" = "aarch64" ]; then SUFFIX=-arm64; FIRMWARE=/usr/share/qemu/qemu-uefi-aarch64.bin; fi && \
echo '#!/bin/bash' > /usr/bin/image && \
echo 'set -e -x' >> /usr/bin/image && \
echo PACKER_LOG=1 packer build \
-var "aws_temporary_security_group_source_cidr=$(curl -sf https://api.ipify.org)/32" \
-var "root_password=ros" \
-var "firmware=${FIRMWARE}" \
-var "memory=1024" \
-var "iso=/iso/output.iso" \
-var "accelerator=${ACCEL}" \
-only qemu.cos${SUFFIX} . >> /usr/bin/image && \
chmod +x /usr/bin/image
RUN echo 'mkdir /output &&' >> /usr/bin/image && \
echo 'mv *.box /output/output.box' >> /usr/bin/image && \
echo 'pigz -dc *.tar.gz | tar xvf -' >> /usr/bin/image && \
echo 'cat cOS | pigz -c > /output/output.qcow.gz'>> /usr/bin/image
ENTRYPOINT /usr/bin/image
FROM qcow-build AS qcow-build2
RUN bash -x /usr/bin/image
FROM scratch AS qcow
COPY --from=qcow-build2 /output/ /
FROM scratch AS iso
COPY --from=iso-build /iso/output.iso /
FROM tools AS ami
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
ARG AWS_DEFAULT_REGION
ARG IMAGE=rancher/os2:dev
ARG NAME=RancherOS-Image-dev
ARG VERSION=1
ARG GIT_COMMIT=HEAD
RUN packer build \
-var "cos_version=${VERSION}" \
-var "git_sha=${GIT_COMMIT}" \
-var 'aws_instance_type=t3.medium' \
-var 'aws_source_ami_filter_owners=["053594193760"]' \
-var "aws_cos_deploy_args=cos-deploy --no-verify --no-cosign --docker-image ${IMAGE}" \
-var "name=${NAME}" \
-only amazon-ebs.cos .
FROM scratch AS default
COPY --from=iso / /
COPY --from=qcow / /
EOF
}
iso()
{
if [ -n "$CONFIG" ]; then
CONFIG_DATA="$(<$CONFIG)"
fi
build --target iso -o build/ --build-arg CONFIG="${CONFIG_DATA}"
}
qcow()
{
ID=qcow-${RANDOM}
if docker run -i --device /dev/kvm busybox /bin/true; then
build --target qcow-build --build-arg ACCEL=kvm -t $ID
docker run --net=host -i --device /dev/kvm --name $ID $ID
else
build --target qcow-build --build-arg ACCEL=tcg -t $ID
docker run --net=host -i --name $ID $ID
fi || {
docker rm -fv $ID
docker rmi $ID
exit 1
}
mkdir -p build/
docker export $ID | tar xvf - -C build/ output/ --strip-components=1
docker rm -fv $ID
docker rmi $ID
}
ami()
{
if [ -z "${AWS_ACCESS_KEY_ID}" ] || [ -z "${AWS_SECRET_ACCESS_KEY}" ] || [ -z "${AWS_DEFAULT_REGION}" ]; then
echo ERROR: The following environment variables must be set: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION
exit 1
fi
build --target ami \
--build-arg AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} \
--build-arg AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} \
--build-arg AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} \
--build-arg NAME="${NAME}" \
--build-arg GIT_COMMIT="${GIT_COMMIT}" \
--build-arg VERSION="${VERSION}"
}
usage()
{
echo "Usage:"
echo " $0 IMAGE OUTPUT [ISO_CLOUD_CONFIG]"
echo
echo " IMAGE: a Docker image"
echo " OUTPUT: Comma seperated value of output image formats. Valid: aws,iso,qcow"
echo " ISO_CLOUD_CONFIG: An option file that will be used as the default cloud-init in an ISO"
}
IMAGE=$1
OUTPUT=$2
CONFIG=$3
VERSION=${IMAGE##*:}
GIT_COMMIT=${GIT_COMMIT:-HEAD}
NAME=${IMAGE%%:${VERSION}}
NAME=${NAME//[^a-zA-Z0-9-@.\/_]/-}
COS_VERSION=${COS_VERSION:-e95d49a66605d6622776391e95e175a80528e872}
if [ "$1" == dockerfile ]; then
dockerfile
exit 0
fi
if [ -z "${OUTPUT}" ] || [ -z "${IMAGE}" ] || echo "$@" | grep -q -- -h; then
usage
exit 1
fi
{
IFS=,
for i in ${OUTPUT}; do
case $i in
ami)
ami
;;
qcow)
qcow
;;
iso)
iso
;;
*)
echo Unknown format $i
echo
usage
exit 1
esac
done
}