-
Notifications
You must be signed in to change notification settings - Fork 17
/
build_freebsd_iso
executable file
·155 lines (122 loc) · 4.84 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
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
#!/usr/bin/env bash
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="10.3"
MIRROR="ftp.freebsd.org"
MIRROR_PATH="pub/FreeBSD/releases/ISO-IMAGES"
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-10-custom.iso"
ME_FREEBSD_DIST_NAME="joyent.txz"
INSTALLER_CFG="installerconfig"
# 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 -sS -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 guest tools to $CUSTOM_ISO_LAYOUT:"
if [[ ! -d me-freebsd ]]; then
echo "===> Getting me-freebsd..."
git clone https://github.com/joyent/me-freebsd.git
else
echo "===> Updating me-freebsd before copying to $CUSTOM_ISO_LAYOUT..."
( cd me-freebsd
git pull
)
fi
integrate_me_freebsd_dstribution
echo "==> Copying $INSTALLER_CFG to $CUSTOM_ISO_LAYOUT"
cp $INSTALLER_CFG $CUSTOM_ISO_LAYOUT/etc/
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_vtnet0=\"DHCP\"" >> $CUSTOM_ISO_LAYOUT/etc/rc.conf
echo "ifconfig_vtnet1=\"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 8.8.8.8" >> $CUSTOM_ISO_LAYOUT/etc/resolv.conf
echo "nameserver 8.8.4.4" >> $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 "Joyent" -b boot/cdboot \
-o $CUSTOM_ISO_FILENAME $CUSTOM_ISO_LAYOUT
echo "==> Custom ISO now ready: $CUSTOM_ISO_FILENAME"
}
function integrate_me_freebsd_dstribution() {
echo "==> Integrating me-freebsd distribution"
[[ -f $ME_FREEBSD_DIST_NAME ]] && rm $ME_FREEBSD_DIST_NAME
( cd me-freebsd
tar -cvJpf ../${ME_FREEBSD_DIST_NAME} ./etc ./lib ./usr
)
DISTFILE_SHASUM_STDOUT=$(shasum -a 256 $ME_FREEBSD_DIST_NAME)
DISTFILE_SHASUM_PLAIN=${DISTFILE_SHASUM_STDOUT% *}
DISTFILE_FILECOUNT=$(tar -tf $ME_FREEBSD_DIST_NAME | wc -l | tr -d ' ')
echo -e "${ME_FREEBSD_DIST_NAME}\t${DISTFILE_SHASUM_PLAIN}\t${DISTFILE_FILECOUNT}\t${ME_FREEBSD_DIST_NAME%.*}\t\"Joyent smartmachine tools\"\ton" >> ${CUSTOM_ISO_LAYOUT}/usr/freebsd-dist/MANIFEST
cp $ME_FREEBSD_DIST_NAME ${CUSTOM_ISO_LAYOUT}/usr/freebsd-dist/${ME_FREEBSD_DIST_NAME}
}
echo "==> ISO Build Starting!"
fetch_iso
create_layout
create_newiso