summaryrefslogtreecommitdiffstats
path: root/tools/tools/cd2dvd/cd2dvd.sh
blob: c591080a02b59a7bd3cf2017b0907d2f9ff6ab39 (plain)
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
#!/bin/sh
#
# Copyright (C) 2008 Roman Kurakin rik@freebsd.org. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#       
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Merges FreeBSD's several CD installation medias to the single DVD disk.
#
# $FreeBSD$

## Helper functions
usage () {
	echo "Usage: $0 <dvd_img_name> <cd_img_name <cd_img_name ..>>"
}

# Copy data from the cd
# $1 os name
# $2 disk image name
# $3 mount dir
# $4 destination dir
copycd () {
	# Set some variables
	md=""
	_os="${1}"
	_img="${2}"
	_mnt="${3}"
	_dst="${4}"
	if [ $# -ne "4" ]
	then
		echo "Error: function ${0} takes exactly four parameters."
		exit 1
	fi
	if [ "${_os}" != "FreeBSD" -a "${_os}" != "Linux" ]
	then
		echo "Error: do not know how to handle ${_os} os."
		exit 1
	fi
	if [ ! -f "${_img}" ]
	then
		echo "Error: file ${_img} does not exists or not a regula file."
		exit 1
	fi
	if [ ! -r "${_img}" ]
	then
		echo "Error: you do not have the read permissions."
		exit 1
	fi
	if [ ! -d "${_mnt}" ]
	then
		echo "Error: ${_mnt} is not a directory or does not exists."
	fi
	if [ ! -d "${_dst}" ]
	then
		echo "Error: ${_dst} is not a directory or does not exists."
	fi
	if [ ! -w "${_dst}" ]
	then
		echo "Error: you do not have write permissions granted for ${_dst} directory."
	fi
	if [ "${_os}" != "Linux" ]
	then
		md=`mdconfig -a -t vnode -f ${_img}` || exit 1
		mount_cd9660 /dev/${md} ${_mnt} || exit 1
	else
		mount -o loop ${_img} ${_mnt} || exit 1
	fi
	if [ ! -f ${_mnt}/cdrom.inf ]
	then
		echo "Error: Failed to find cdrom.inf for ${_img}."
		exit 1
	fi
	cdvol=`grep "^CD_VOLUME.*" ${_mnt}/cdrom.inf | sed "s/CD_VOLUME[[:space:]]*=[[:space:]]*//"`
	if test -z "${cdvol}" -o ! "${cdvol}" -eq "${cdvol}" 2> /dev/null
	then
		echo "Error: failed to get volume id for ${_img}."
		exit 1
	fi
	cdver=`grep "^CD_VERSION.*" ${_mnt}/cdrom.inf | sed "s/CD_VERSION[[:space:]]*=[[:space:]]*//"`
	if test -z "${cdver}"
	then
		echo "Error: failed to get version id for ${_img}."
		exit 1
	fi
	if [ -z "${VERID}" ]
	then
		VERID="${cdver}"
		_exclude=""
	else
		if [ "${VERID}" != "${cdver}" ]
		then
			echo "Error: cd version ids mismatch while processing ${_img}."
			exit 1
		fi
#		_exclude="--exclude ./cdrom.inf --exclude ./packages/INDEX"
		_exclude="! -regex ./cdrom.inf ! -regex ./packages/INDEX"
	fi
	echo "Merging ${_img}:"
# --quite -u -V
	(cd "${_mnt}" && find . ${_exclude} | cpio -p -d -m -V --quiet "${_dst}") || exit 1
#	(cd "${_mnt}" && tar ${_exclude} -cvf - .) | (cd "${_dst}" && tar xvf -) || exit 1
	if [ "${_os}" != "Linux" ]
	then	
		umount /dev/${md} || exit 1
		mdconfig -d -u "${md}" || exit 1
	else
		umount ${_mnt} || exit 1
	fi
#	exit 0
}

# Clear mounted image
# $1 mounted directory
# $2 error code
clearmount ()
{
	if [ $# -ne "2" ]
	then
		echo "Error: function ${0} takes exactly two parameters."
		exit 1
	fi
	if [ -z "${1}" ]
	then
		test -z "${2}" || exit "${2}"
	else
		# Ignore errors
		umount "${1}" 2>/dev/null
		test -z "${2}" || exit "${2}"
	fi
}

# Clear CD image allocation
# $1 os name
# $2 md
# $3 error code
clearmd ()
{
	if [ $# -ne "3" ]
	then
		echo "Error: function ${0} takes exactly three parameters."
		exit 1
	fi
	if [ "${1}" != "FreeBSD" -o -z "${2}" ]
	then
		test -z "${3}" || exit "${3}"
	else
		# Ignore errors
		mdconfig -d -u "${2}" 2>/dev/null
		test -z "${3}" || exit "${3}"
	fi
}

## Check params
if [ $# -lt 3 ]
then
	usage
	echo "Error, this script should take more than two parameters."
	exit 1
fi

# Check if zero
if [ -z "${1}" ]; then
	usage
	exit 1
fi

# Check if already exists
if [ -e "${1}" ]; then
	if [ ! -f "${1}" ]; then
		echo "Destination DVD image file already exists and is not a regular file."
		exit 1
	fi
	while echo "The ${1} file exists. Overwrite? (y/n)"
	do
		read line
		case "${line}" in
		y|Y)
			rm -rf "${1}"
			touch "${1}"
			break
			;;
		n|N)
			echo "Please, run program again with a new value."
			exit 1
			;;
		esac
	done
fi
DVDIMAGE="${1}"

shift

count=0
for i in "$@"
do
	# Skip empty params.
	if test -z "${i}"; then
		continue
	fi
	if [ ! -f "${i}" -o ! -r "${i}" ]
	then
		echo "Error: The ${i} is not readable, do not exists or not a regular file."
		exit 1
	fi
	count=`expr ${count} \+ 1`
done

# Check if we have at the least two CD images
if [ "${count}" -lt 2 ]
then
	echo "Error: less than two CD images specified."
fi

## Some usefull variables
pwd=`pwd`
tmpdirin="${pwd}/tmp-$$-in"
tmpdirout="${pwd}/tmp-$$-out"
system=`uname -s`
md=""

# set the trap options
trap 'echo ""; echo "Cleaning up"; clearmount "${tmpdirin}" ""; clearmd "${system}" "${md}" ""; rm -rf "${tmpdirin}" "${tmpdirout}";' 0 1 2 3 15
mkdir "${tmpdirin}" || (echo "Error: failed to create tempory ${tmpdirin}"; exit 1)
mkdir "${tmpdirout}" || (echo "Error: failed to create tempory ${tmpdirout}"; exit 1)

for i in "$@"
do
	# Skip empty params.
	if test -z "${i}"; then
		continue
	fi
	copycd "${system}" "${i}" "${tmpdirin}" "${tmpdirout}"
	mv "${tmpdirout}"/packages/INDEX "${tmpdirout}"/packages/INDEX~ || exit 1
	cat "${tmpdirout}"/packages/INDEX~ | sed "s/^\(.*\)|${cdvol}$/\1|1/" > "${tmpdirout}"/packages/INDEX || exit 1
	rm "${tmpdirout}"/packages/INDEX~ || exit 1
done

mv "${tmpdirout}"/cdrom.inf "${tmpdirout}"/cdrom.inf~ || exit 1
cat "${tmpdirout}"/cdrom.inf~ | sed "s/^\(CD_VOLUME[[:space:]]\{0,\}=[[:space:]]\{0,\}\)[[:digit:]]\{1,\}/\11/" > "${tmpdirout}"/cdrom.inf || exit 1
rm "${tmpdirout}"/cdrom.inf~ || exit 1

mkisofs -b boot/cdboot -no-emul-boot -r -J \
	-V "FreeBSD_Install" \
	-publisher "The FreeBSD Project.  http://www.freebsd.org/" \
	-o ${DVDIMAGE} "${tmpdirout}" \
	|| exit 1 

exit 0

OpenPOWER on IntegriCloud