forked from TritonDataCenter/mi-freebsd-10
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_freebsd_iso
executable file
·123 lines (98 loc) · 3.77 KB
/
build_freebsd_iso
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
#!/usr/bin/env zsh
if [[ -n "$TRACE" ]]; then
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
fi
set -euo pipefail
IFS=$'\n\t'
RELEASE=${RELEASE:-12.2}
MIRROR="ftp.uk.freebsd.org"
MIRROR_PATH="pub/FreeBSD/releases/ISO-IMAGES"
# ISO="FreeBSD-${RELEASE}-RELEASE-amd64-bootonly.iso"
ISO="FreeBSD-${RELEASE}-RELEASE-amd64-disc1.iso"
ISO_DIR="data/freebsd-iso"
ISO_MOUNT="/mnt/freebsd-iso"
CUSTOM_ISO_LAYOUT="data/custom-freebsd-iso"
SHA256SUM="CHECKSUM.SHA256-FreeBSD-${RELEASE}-RELEASE-amd64"
CUSTOM_ISO_FILENAME="freebsd-custom.iso"
INSTALLER_CFG=installerconfig.${RELEASE}
# Clean up ISO file if download is interupted
trap "rm -rf $PWD/${ISO_DIR}/${ISO}" SIGHUP SIGINT SIGKILL SIGTERM
# Delete Previous custom layout
trap "rm -rf $CUSTOM_ISO_LAYOUT" SIGHUP SIGINT SIGKILL SIGTERM
function fetch_iso() {
if [[ ! -d $ISO_DIR ]]; then
mkdir -p $ISO_DIR
fi
echo "==> Fetching ${SHA256SUM}"
curl -sS -o ${ISO_DIR}/${SHA256SUM} ftp://${MIRROR}/${MIRROR_PATH}/${RELEASE}/${SHA256SUM}
echo "==> Checking for local copy of $ISO..."
if [[ -e $ISO_DIR/$ISO ]]; then
echo "==> Found local copy of $ISO"
else
echo "==> Local copy not found."
echo "==> Fetching ISO from $MIRROR..."
curl -#S -o ${ISO_DIR}/${ISO} ftp://${MIRROR}/${MIRROR_PATH}/${RELEASE}/${ISO}
echo "==> Done!"
echo "==> ${ISO} saved to $ISO_DIR/"
echo "==> Verifying $ISO with ${SHA256SUM}"
if [[ "$(sha256 $ISO_DIR/$ISO | cut -f2 -d= | tr -d '[[:space:]]')" == "$(grep "($ISO)" $ISO_DIR/${SHA256SUM} | cut -f2 -d= | tr -d '[[:space:]]')" ]]; then
echo "==> Checksums match."
else
echo "==> Checksums don't match!"
exit 1
fi
fi
}
function create_layout() {
echo "==> Creating custom ISO Layout"
if [[ -d $CUSTOM_ISO_LAYOUT ]]; then
echo "==> Previous layout $CUSTOM_ISO_LAYOUT exists...deleting"
rm -rf $CUSTOM_ISO_LAYOUT
fi
echo "==> Creating $CUSTOM_ISO_LAYOUT"
mkdir -p $CUSTOM_ISO_LAYOUT
if [[ ! -d $ISO_MOUNT ]]; then
echo "==> Creating $ISO_MOUNT ..."
mkdir $ISO_MOUNT
fi
if [[ $(df | grep $ISO_MOUNT) ]]; then
echo "==> Unmounting previous $ISO_MOUNT..."
umount $ISO_MOUNT
fi
echo "==> Mounting $ISO to $ISO_MOUNT"
mount -t cd9660 /dev/`mdconfig -f $ISO_DIR/$ISO` $ISO_MOUNT
echo "==> Copying layout from $ISO_MOUNT to $CUSTOM_ISO_LAYOUT"
rsync -aq $ISO_MOUNT/ $CUSTOM_ISO_LAYOUT
echo "==> Copying $INSTALLER_CFG to $CUSTOM_ISO_LAYOUT"
cp $INSTALLER_CFG $CUSTOM_ISO_LAYOUT/etc/installerconfig
echo "==> Enable serial and internal consoles"
echo "-Dhv" > $CUSTOM_ISO_LAYOUT/boot.config
echo "cuau0 \"/usr/libexec/getty std.38400\" xterm on secure" >> $CUSTOM_ISO_LAYOUT/etc/ttys
echo "==> Setting autoboot delay to 5 seconds"
echo "autoboot_delay=\"5\"" >> $CUSTOM_ISO_LAYOUT/boot/loader.conf
echo "==> Get network up automatically in the blank VM"
echo "ifconfig_em0=\"DHCP\"" >> $CUSTOM_ISO_LAYOUT/etc/rc.conf
echo "==> Remove resolv.conf symlink"
rm $CUSTOM_ISO_LAYOUT/etc/resolv.conf
echo "==> Set resolvers in the blank VM"
echo "nameserver 9.9.9.9" >> $CUSTOM_ISO_LAYOUT/etc/resolv.conf
echo "nameserver 1.1.1.1" >> $CUSTOM_ISO_LAYOUT/etc/resolv.conf
echo "==> Unmounting $ISO_MOUNT"
umount $ISO_MOUNT
}
function create_newiso() {
echo "==> Getting Volume ID for ${ISO}"
CUSTOM_ISO_TITLE=$(isoinfo -d -i ${ISO_DIR}/${ISO} | grep "Volume id" | awk '{print $3}')
echo "==> Volume ID is $CUSTOM_ISO_TITLE"
echo "==> Preparing NEW ISO"
mkisofs -J -R -no-emul-boot \
-V "$CUSTOM_ISO_TITLE" \
-p "Probably" -b boot/cdboot \
-o $CUSTOM_ISO_FILENAME $CUSTOM_ISO_LAYOUT
echo "==> Custom ISO now ready: $CUSTOM_ISO_FILENAME"
}
echo "==> ISO Build Starting!"
fetch_iso
create_layout
create_newiso