-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·68 lines (59 loc) · 1.58 KB
/
build.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
#!/usr/bin/env bash
set -eu
# Default values
image_file="image.ext2"
dockerfile="Dockerfile"
size="1500M"
# Help message function
print_help() {
echo "Usage: $0 [options] [<image_file>]"
echo ""
echo "Options:"
echo " --size, -s Specify the size (default: 1500M)"
echo " --dockerfile, -f Specify the dockerfile (default: ./Dockerfile)"
echo " --output, -o Specify the output image file (default: ./image.ext2)"
echo " --help, -h Display this help message"
}
# Parse options
while getopts ":s:f:o:h-:" opt; do
case $opt in
s) size="$OPTARG" ;;
f) dockerfile="$OPTARG" ;;
o) image_file="$OPTARG" ;;
h) print_help; exit 0 ;;
-)
case $OPTARG in
size) size="${!OPTIND}"; OPTIND=$((OPTIND + 1)) ;;
dockerfile) dockerfile="${!OPTIND}"; OPTIND=$((OPTIND + 1)) ;;
output) image_file="${!OPTIND}"; OPTIND=$((OPTIND + 1)) ;;
help) print_help; exit 0 ;;
*)
echo "Invalid option: --$OPTARG" >&2
exit 1
;;
esac
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
echo "Image file: $image_file"
echo "Docker file: $dockerfile"
echo "Size: $size"
# Privileged section running in a namespace
buildah unshare bash << EOF
set -eu
img=\$(buildah bud --dns=none --quiet --layers -f ${dockerfile})
cnt=\$(buildah from \$img)
mnt=\$(buildah mount \$cnt)
cleanup() {
buildah umount \$cnt > /dev/null
buildah rm \$cnt > /dev/null
}
trap cleanup EXIT
rm -f ${image_file}
mkfs.ext2 -b 4096 -d \$mnt ${image_file} ${size} > /dev/null
EOF