-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path2-publish-image.sh
executable file
·600 lines (383 loc) · 15.9 KB
/
2-publish-image.sh
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#!/bin/bash
image_commit=""
blueprint_name=""
repo_server_ip=$(ip a show dev $(ip route | grep default | awk '{print $5}') | grep "inet " | awk '{print $2}' | awk -F / '{print $1}')
repo_server_port="8090"
kickstart_file=""
http_boot_mode=false
http_boot_port=""
iso_kickstart_mode=false
iso_standard=""
baserelease=$(cat /etc/redhat-release | awk '{print $6}' | awk -F . '{print $1}')
basearch=$(arch)
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "This Script creates a container with the ostree repo from an edge-commit image"
echo ""
echo "Syntax: $0 [-i <image ID>|-h <IP>|-p <port>|-k <KS file>]|-x <port>|-e <standard ISO file>]]"
echo ""
echo "options:"
echo "i Image ID to be published (required)."
echo "h Repo server IP (default=$repo_server_ip)."
echo "p Repo server port (default=$repo_server_port)."
echo "k Kickstart file. If not defined kickstart.ks is autogenerated. File should be at the same directory level than $0"
echo "x Create UEFI HTTP Boot server on this port. If enabled (default=disabled) it creates an ISO and publish it on this server. You need to include option -e "
echo "e Path to RHEL boot ISO. It will Embedd the kickstart in a new ISO so you don't need to modify kernel args during boot(default=disabled)"
echo ""
echo "Example: $0 -i 125c1433-2371-4ae9-bda3-91efdbb35b92"
echo "Example: $0 -i 125c1433-2371-4ae9-bda3-91efdbb35b92 -h 192.168.122.129 -p 8090 -k kickstart.v1.ks"
echo "Example: $0 -i 125c1433-2371-4ae9-bda3-91efdbb35b92 -h 192.168.122.129 -p 8090 -k kickstart.v1.ks -x 8091 -e images/rhel-8.5-${basearch}-boot.iso"
echo ""
}
############################################################
############################################################
# Main program #
############################################################
############################################################
############################################################
# Process the input options. Add options as needed. #
############################################################
# Get the options
while getopts ":i:h:p:k:x:e:" option; do
case $option in
i)
image_commit=$OPTARG;;
h)
repo_server_ip=$OPTARG;;
p)
repo_server_port=$OPTARG;;
k)
kickstart_file=$OPTARG;;
x)
http_boot_mode=true
http_boot_port=$OPTARG;;
e)
iso_kickstart_mode=true
iso_standard=$OPTARG;;
\?) # Invalid option
echo "Error: Invalid option"
echo ""
Help
exit -1;;
esac
done
if [ -z "$image_commit" ]
then
echo ""
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "You must define the Commit ID with option -c"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo ""
echo ""
Help
exit -1
fi
if [ $iso_kickstart_mode = false ] && [ $http_boot_mode = true ]
then
echo ""
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "If you want to use UEFI HTTP boot you need to create a custom iso too!"
echo "Please, introduce the path to default RHEL ISO with -e"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo ""
echo ""
Help
exit -1
fi
if [ -z "$kickstart_file" ]
then
cat <<EOFIN > kickstart.ks
lang en_US.UTF-8
keyboard us
timezone Etc/UTC --isUtc
text
zerombr
clearpart --all --initlabel
autopart
reboot
rootpw --iscrypted !\$6\$0XQkWnCyj3XZqA6F\$MOHw/Sn3hec23mKjtxqGa.qnflkLb6FNR8s72yCsk.xVDoI35EOpgetjII/DkJVYrHVwF.ET3TLvVl7WYbMef0
user --name=admin --group=wheel
network --bootproto=dhcp --device=link --activate --onboot=on
ostreesetup --nogpg --osname=rhel --remote=edge --url=http://${repo_server_ip}:${repo_server_port}/repo/ --ref=rhel/${baserelease}/${basearch}/edge
%post
cat << EOF > /etc/greenboot/check/required.d/check-dns.sh
#!/bin/bash
DNS_SERVER=$(grep nameserver /etc/resolv.conf | cut -f2 -d" ")
COUNT=0
# check DNS server is available
ping -c1 $DNS_SERVER
while [ $? != '0' ] && [ $COUNT -lt 10 ]; do
((COUNT++))
echo "Checking for DNS: Attempt $COUNT ."
sleep 10
ping -c 1 $DNS_SERVER
done
EOF
%end
EOFIN
kickstart_file="kickstart.ks"
fi
blueprint_name=$(composer-cli compose status | grep $image_commit | awk '{print $8}')
cat <<EOF > nginx.conf
events {
}
http {
server{
listen 8080;
root /usr/share/nginx/html;
location / {
autoindex on;
}
}
}
pid /run/nginx.pid;
daemon off;
EOF
cat <<EOF > Dockerfile
FROM registry.access.redhat.com/ubi8/ubi
RUN yum -y install nginx && yum clean all
ARG kickstart
ARG commit
ADD \$kickstart /usr/share/nginx/html/
ADD \$commit /usr/share/nginx/html/
ADD nginx.conf /etc/
EXPOSE 8080
CMD ["/usr/sbin/nginx", "-c", "/etc/nginx.conf"]
EOF
############################################################
# Download the image.
############################################################
echo ""
echo "Downloading image $image_commit..."
mkdir -p images
cd images
composer-cli compose image $image_commit
cd ..
echo $image_commit > .lastimagecommit
############################################################
# Publish the image.
############################################################
# Stop previous container
running_container_id=$(podman ps | grep 0.0.0.0:$repo_server_port | awk '{print $1}')
if [ -z "$running_container_id" ]
then
echo ""
echo "No running container"
echo ""
else
echo ""
echo "Stopping previous container... ($running_container_id)"
echo ""
podman stop $running_container_id 2>/dev/null
fi
previous_container_name=$(podman ps -a | grep $image_commit | awk '{print $2}')
previous_container_id=$(podman ps -a | grep $image_commit | awk '{print $1}')
if [ "$previous_container_name" == "localhost/${blueprint_name}-repo:$image_commit" ]
then
echo ""
echo "Deleting previous container... ($previous_container_name) "
echo ""
podman rm $previous_container_id 2>/dev/null
fi
# Start repo container
echo ""
echo "Building and running the container serving the image..."
podman build -t ${blueprint_name}-repo:latest --build-arg kickstart=${kickstart_file} --build-arg commit=images/${image_commit}-commit.tar .
podman tag ${blueprint_name}-repo:latest ${blueprint_name}-repo:$image_commit
podman run --name ${blueprint_name}-repo-$image_commit -d -p $repo_server_port:8080 ${blueprint_name}-repo:$image_commit
# Wait for container to be running
until [ "$(sudo podman inspect -f '{{.State.Running}}' ${blueprint_name}-repo-$image_commit)" == "true" ]; do
sleep 1;
done;
if [ $iso_kickstart_mode = true ]
then
############################################################
# Embedd kickstart in the ISO
############################################################
# steps from -> https://access.redhat.com/solutions/60959
echo ""
echo ""
echo "Creating ISO image with kickstart embedded..."
echo ""
mkdir -p mnt/rhel-iso/
mount -o loop $iso_standard mnt/rhel-iso/
shopt -s dotglob
mkdir -p tmp/rhel-iso
cp -avRf mnt/rhel-iso/* tmp/rhel-iso
# Kickstart could be imported into the image and be used instead downloading from the HTTP server... but modifiying the kickstart in the HTTP server is easier...
#cp ${kickstart_file} tmp/rhel-iso/ks.cfg
iso_label=$(blkid $iso_standard | awk -F 'LABEL="' '{print $2}' | cut -d '"' -f 1)
#iso_label=$(grep RHEL isolinux/isolinux.cfg | head -n 1 | awk '{print $3}' | cut -d "=" -f 3)
cd tmp/rhel-iso
if [ $(arch) = aarch64 ]
then
sed -i 's/timeout=60/timeout=1/g' EFI/BOOT/grub.cfg
#sed -i "s/quiet/inst.ks=hd:LABEL=${iso_label}:\/ks.cfg/g" EFI/BOOT/grub.cfg
sed -i "s/inst.stage2/inst.ks=http:\/\/${repo_server_ip}:${repo_server_port}\/${kickstart_file} inst.stage2/g" EFI/BOOT/grub.cfg
sed -i "s/RHEL-.-.-0-BaseOS-${basearch}/${iso_label}/g" EFI/BOOT/grub.cfg
rm -rf ../../images/${image_commit}-custom-kernelarg.iso
xorriso -as mkisofs -V ${iso_label} -r -o ../../images/${image_commit}-custom-kernelarg.iso -J -joliet-long -cache-inodes -efi-boot-part --efi-boot-image -e images/efiboot.img -no-emul-boot .
implantisomd5 ../../images/${image_commit}-custom-kernelarg.iso
else
#sed -i "s/quiet/inst.ks=hd:LABEL=${iso_label}:\/ks.cfg/g" isolinux/isolinux.cfg
sed -i "s/quiet/inst.ks=http:\/\/${repo_server_ip}:${repo_server_port}\/${kickstart_file}/g" isolinux/isolinux.cfg
sed -i 's/timeout 600/timeout 1/g' isolinux/isolinux.cfg
sed -i "s/RHEL-.-.-0-BaseOS-${basearch}/${iso_label}/g" isolinux/isolinux.cfg
sed -i 's/timeout 60/timeout 1/g' isolinux/grub.conf
sed -i 's/timeout=60/timeout=1/g' EFI/BOOT/BOOT.conf
sed -i 's/timeout=60/timeout=1/g' EFI/BOOT/grub.cfg
#sed -i "s/quiet/inst.ks=hd:LABEL=${iso_label}:\/ks.cfg/g" EFI/BOOT/grub.cfg
sed -i "s/quiet/inst.ks=http:\/\/${repo_server_ip}:${repo_server_port}\/${kickstart_file}/g" EFI/BOOT/grub.cfg
sed -i "s/RHEL-.-.-0-BaseOS-${basearch}/${iso_label}/g" EFI/BOOT/grub.cfg
rm -rf ../../images/${image_commit}-custom-kernelarg.iso
## LEGACY boot
#mkisofs -o ../../images/${image_commit}-custom-kernelarg.iso -b isolinux/isolinux.bin -c isolinux/boot.cat --joliet-long --no-emul-boot --boot-load-size 4 --boot-info-table -J -R -V "${iso_label}" .
#isohybrid ../../images/${image_commit}-custom-kernelarg.iso
# UEFI boot
mkisofs -o ../../images/${image_commit}-custom-kernelarg.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e images/efiboot.img -no-emul-boot -graft-points -J -R -l -V "${iso_label}" .
isohybrid --uefi ../../images/${image_commit}-custom-kernelarg.iso
implantisomd5 ../../images/${image_commit}-custom-kernelarg.iso
fi
cd ../../
umount mnt/rhel-iso
rm -rf mnt
rm -rf tmp
fi
if [ $http_boot_mode = true ]
then
############################################################
# UEFI HTTP Boot server
############################################################
# info about the setup on libvirt: https://www.redhat.com/sysadmin/uefi-http-boot-libvirt
# libvirt network example:
# <network xmlns:dnsmasq="http://libvirt.org/schemas/network/dnsmasq/1.0">
# <name>default</name>
# <uuid>3328ebe7-2202-4e3b-9ca3-9ddf357db576</uuid>
# <forward mode="nat">
# <nat>
# <port start="1024" end="65535"/>
# </nat>
# </forward>
# <bridge name="virbr0" stp="on" delay="0"/>
# <mac address="52:54:00:16:0e:63"/>
# <ip address="192.168.122.1" netmask="255.255.255.0">
# <tftp root="/var/lib/tftpboot"/>
# <dhcp>
# <range start="192.168.122.2" end="192.168.122.254"/>
# <bootp file="pxelinux.0"/>
# </dhcp>
# </ip>
# <dnsmasq:options>
# <dnsmasq:option value="dhcp-vendorclass=set:efi-http,HTTPClient:Arch:00016"/>
# <dnsmasq:option value="dhcp-option-force=tag:efi-http,60,HTTPClient"/>
# <dnsmasq:option value="dhcp-boot=tag:efi-http,"http://192.168.122.128:8091/EFI/BOOT/BOOTX64.EFI""/>
# </dnsmasq:options>
# </network>
# Create VM:
# sudo virt-install --name=edge-node-uefi-boot --ram=2048 --vcpus=1 --os-type=linux --os-variant=rhel8.5 --graphics=vnc --pxe --disk size=20,bus=sata --check path_in_use=off --network=network=default,model=virtio --boot=uefi
# dhcpd example
# class "pxeclients" {
# match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
# next-server 192.168.111.1;
# filename "/bootx64.efi";
# }
# class "httpclients" {
# match if substring (option vendor-class-identifier, 0, 10) = "HTTPClient";
# option vendor-class-identifier "HTTPClient";
# filename "http://192.168.122.128:8091/EFI/BOOT/BOOTX64.EFI";
# }
echo ""
echo ""
echo "Creating UEFI HTTP boot server..."
echo ""
# offline fully automated ISO could be used too
#./3-create-offline-deployment.sh -h $repo_server_ip -p $repo_server_port > /dev/null
#iso_file="$(cat .lastimagecommit)-simplified-installer.iso"
iso_file="$(cat .lastimagecommit)-custom-kernelarg.iso"
mkdir -p mnt/rhel-install/
mount -o loop,ro -t iso9660 images/$iso_file mnt/rhel-install/
mkdir -p tmp/boot-server/var/www/html/
cp -R mnt/rhel-install/* tmp/boot-server/var/www/html/
chmod -R +r tmp/boot-server/var/www/html/*
# Changes on simplified ISO
#sed -i 's/linux \/images\/pxeboot\/vmlinuz/linuxefi \/pxeboot\/vmlinuz/g' tmp/boot-server/var/www/html/EFI/BOOT/grub.cfg
#sed -i 's/initrd \/images\/pxeboot\/initrd.img/initrdefi \/pxeboot\/initrd.img/g' tmp/boot-server/var/www/html/EFI/BOOT/grub.cfg
#sed -i "s/coreos.inst.image_file=\/run\/media\/iso\/disk.img.xz/coreos.inst.image_url=http:\/\/${repo_server_ip}:$http_boot_port\/disk.img.xz/g" tmp/boot-server/var/www/html/EFI/BOOT/grub.cfg
sed -i "s/inst.stage2=.* /inst.stage2=http:\/\/${repo_server_ip}:${http_boot_port} /g" tmp/boot-server/var/www/html/EFI/BOOT/grub.cfg
cat <<EOF > Dockerfile-http-boot
FROM registry.access.redhat.com/ubi8/ubi
RUN yum -y install nginx && yum clean all
ARG content
COPY \$content/ /usr/share/nginx/html/
ADD nginx.conf /etc/
EXPOSE 8080
CMD ["/usr/sbin/nginx", "-c", "/etc/nginx.conf"]
EOF
running_container_id=$(podman ps | grep 0.0.0.0:$repo_server_port | awk '{print $1}')
if [ -z "$running_container_id" ]
then
echo ""
echo "No running container"
echo ""
else
echo ""
echo "Stopping previous container... ($running_container_id)"
echo ""
podman stop $running_container_id 2>/dev/null
fi
previous_container_name=$(podman ps -a | grep $image_commit | awk '{print $2}')
previous_container_id=$(podman ps -a | grep $image_commit | awk '{print $1}')
if [ "$previous_container_name" == "localhost/${blueprint_name}-repo:$image_commit" ]
then
echo ""
echo "Deleting previous container... ($previous_container_name) "
echo ""
podman rm $previous_container_id 2>/dev/null
fi
podman build -f Dockerfile-http-boot -t http-boot:latest --build-arg content="tmp/boot-server/var/www/html" .
podman run --name http-boot-$iso_file -d -p $http_boot_port:8080 http-boot:latest
umount mnt/rhel-install/
rm -rf mnt
rm -rf tmp
fi
echo ""
echo ""
echo ""
echo ""
if [ $iso_kickstart_mode = false ]
then
echo "************************************************************************"
echo "Now you can create an offline installation media or install using the "
echo "standard RHEL ISO including this kernel argument:"
echo ""
echo "<kernel args> inst.ks=http://$repo_server_ip:$repo_server_port/$(basename ${kickstart_file})"
echo "************************************************************************"
echo ""
echo ""
else
echo "************************************************************************"
echo "You can install using the custom ISO ${image_commit}-custom-kernelarg.iso"
echo "************************************************************************"
echo ""
echo ""
if [ $http_boot_mode = true ]
then
echo "******************************************************************************"
echo "You have activated UEFI HTTP boot, be sure that you have your DHCP configured!"
echo "DHCP-boot: http://$repo_server_ip:$http_boot_port/EFI/BOOT/BOOTX64.EFI"
echo ""
echo "Remember to use UEFI boot (instead legacy BIOS) and NIC as first boot device"
echo ""
echo "Remember that it takes time until boot reaches UEFI HTTP boot (first tries PXE)"
echo ""
echo "Your edge system must have at least 2GB of memory"
echo "******************************************************************************"
echo ""
echo ""
fi
fi
echo ""