3 # Copyright (C) 2008 Roman Kurakin rik@freebsd.org. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # Merges FreeBSD's several CD installation medias to the single DVD disk.
32 echo "Usage: $0 <dvd_img_name> <cd_img_name <cd_img_name ..>>"
35 # Copy data from the cd
49 echo "Error: function ${0} takes exactly four parameters."
52 if [ "${_os}" != "FreeBSD" -a "${_os}" != "Linux" ]
54 echo "Error: do not know how to handle ${_os} os."
59 echo "Error: file ${_img} does not exists or not a regula file."
64 echo "Error: you do not have the read permissions."
69 echo "Error: ${_mnt} is not a directory or does not exists."
73 echo "Error: ${_dst} is not a directory or does not exists."
77 echo "Error: you do not have write permissions granted for ${_dst} directory."
79 if [ "${_os}" != "Linux" ]
81 md=`mdconfig -a -t vnode -f ${_img}` || exit 1
82 mount_cd9660 /dev/${md} ${_mnt} || exit 1
84 mount -o loop ${_img} ${_mnt} || exit 1
86 if [ ! -f ${_mnt}/cdrom.inf ]
88 echo "Error: Failed to find cdrom.inf for ${_img}."
91 cdvol=`grep "^CD_VOLUME.*" ${_mnt}/cdrom.inf | sed "s/CD_VOLUME[[:space:]]*=[[:space:]]*//"`
92 if test -z "${cdvol}" -o ! "${cdvol}" -eq "${cdvol}" 2> /dev/null
94 echo "Error: failed to get volume id for ${_img}."
97 cdver=`grep "^CD_VERSION.*" ${_mnt}/cdrom.inf | sed "s/CD_VERSION[[:space:]]*=[[:space:]]*//"`
100 echo "Error: failed to get version id for ${_img}."
108 if [ "${VERID}" != "${cdver}" ]
110 echo "Error: cd version ids mismatch while processing ${_img}."
113 # _exclude="--exclude ./cdrom.inf --exclude ./packages/INDEX"
114 _exclude="! -regex ./cdrom.inf ! -regex ./packages/INDEX"
116 echo "Merging ${_img}:"
118 (cd "${_mnt}" && find . ${_exclude} | cpio -p -d -m -V --quiet "${_dst}") || exit 1
119 # (cd "${_mnt}" && tar ${_exclude} -cvf - .) | (cd "${_dst}" && tar xvf -) || exit 1
120 if [ "${_os}" != "Linux" ]
122 umount /dev/${md} || exit 1
123 mdconfig -d -u "${md}" || exit 1
125 umount ${_mnt} || exit 1
130 # Clear mounted image
131 # $1 mounted directory
137 echo "Error: function ${0} takes exactly two parameters."
142 test -z "${2}" || exit "${2}"
145 umount "${1}" 2>/dev/null
146 test -z "${2}" || exit "${2}"
150 # Clear CD image allocation
158 echo "Error: function ${0} takes exactly three parameters."
161 if [ "${1}" != "FreeBSD" -o -z "${2}" ]
163 test -z "${3}" || exit "${3}"
166 mdconfig -d -u "${2}" 2>/dev/null
167 test -z "${3}" || exit "${3}"
175 echo "Error, this script should take more than two parameters."
180 if [ -z "${1}" ]; then
185 # Check if already exists
186 if [ -e "${1}" ]; then
187 if [ ! -f "${1}" ]; then
188 echo "Destination DVD image file already exists and is not a regular file."
191 while echo "The ${1} file exists. Overwrite? (y/n)"
201 echo "Please, run program again with a new value."
215 if test -z "${i}"; then
218 if [ ! -f "${i}" -o ! -r "${i}" ]
220 echo "Error: The ${i} is not readable, do not exists or not a regular file."
223 count=`expr ${count} \+ 1`
226 # Check if we have at the least two CD images
227 if [ "${count}" -lt 2 ]
229 echo "Error: less than two CD images specified."
232 ## Some useful variables
234 tmpdirin="${pwd}/tmp-$$-in"
235 tmpdirout="${pwd}/tmp-$$-out"
239 # set the trap options
240 trap 'echo ""; echo "Cleaning up"; clearmount "${tmpdirin}" ""; clearmd "${system}" "${md}" ""; rm -rf "${tmpdirin}" "${tmpdirout}";' 0 1 2 3 15
241 mkdir "${tmpdirin}" || (echo "Error: failed to create tempory ${tmpdirin}"; exit 1)
242 mkdir "${tmpdirout}" || (echo "Error: failed to create tempory ${tmpdirout}"; exit 1)
247 if test -z "${i}"; then
250 copycd "${system}" "${i}" "${tmpdirin}" "${tmpdirout}"
251 mv "${tmpdirout}"/packages/INDEX "${tmpdirout}"/packages/INDEX~ || exit 1
252 cat "${tmpdirout}"/packages/INDEX~ | sed "s/^\(.*\)|${cdvol}$/\1|1/" > "${tmpdirout}"/packages/INDEX || exit 1
253 rm "${tmpdirout}"/packages/INDEX~ || exit 1
256 mv "${tmpdirout}"/cdrom.inf "${tmpdirout}"/cdrom.inf~ || exit 1
257 cat "${tmpdirout}"/cdrom.inf~ | sed "s/^\(CD_VOLUME[[:space:]]\{0,\}=[[:space:]]\{0,\}\)[[:digit:]]\{1,\}/\11/" > "${tmpdirout}"/cdrom.inf || exit 1
258 rm "${tmpdirout}"/cdrom.inf~ || exit 1
260 mkisofs -b boot/cdboot -no-emul-boot -r -J \
261 -V "FreeBSD_Install" \
262 -publisher "The FreeBSD Project. https://www.freebsd.org/" \
263 -o ${DVDIMAGE} "${tmpdirout}" \