Added "recreate-iso.sh -e EXTRAVER" option
[systemrescuecd.git] / buildscripts / recreate-iso.sh
1 #!/bin/sh
2
3 ## HELP AND BASIC ARGUMENT PROCESSING
4 #####################################
5
6 PROG="${0}"
7 EXTRAVER=""
8 ISODIR=/worksrc/isofiles
9 MOUNTDIR=/mnt/cdrom
10 WORKDIR=/worksrc/catalyst/isotemp
11 DESTDIR=/home/sysresccdiso
12 REPOSRC=/worksrc/sysresccd-src
13 REPOBIN=/worksrc/sysresccd-bin
14 MYDATE=$(date +%Y%m%d-%Hh%M)
15
16 usage()
17 {
18         cat <<EOF
19 Usage: ${PROG} [options] <arch>
20
21 Rebuild the distribution iso by applying the SystemRescueCD overlays
22 and recent kernels to a previous vesion.
23
24 arch: x86 | amd64 | sparc
25
26 Options:
27  -h|--help         Display this screen and exit
28  -i ISODIR         Directory containing the iso file to rebuild ($ISODIR)
29                      <isodir>/systemrescuecd-<arch>-current.iso
30  -w WORKDIR        Set the work directory for the custom CD (${WORKDIR})
31  -r REPOSRC        Location of git systemrescuecd repo ($REPOSRC)
32  -r REPOBIN        ? ($REPOBIN)
33  -d DESTDIR        Set the directory for saving the rebuild iso file ($DESTDIR)
34  -m MOUNTDIR       Set the directory for mounting the current iso ($MOUNTDIR)
35  -e EXTRAVER       Extra version information for f1boot.msg ($EXTRAVER)
36
37 Distributed under the GNU Public License version 2 - http://www.sysresccd.org
38 EOF
39 }
40
41 while getopts ":i:w:r:b:d:m:e:" Option
42 do
43         case $Option in
44                 i ) ISODIR="$OPTARG";;
45                 w ) WORKDIR="$OPTARG";;
46                 r ) REPOSRC="$OPTARG";;
47                 b ) REPOBIN="$OPTARG";;
48                 d ) DESTDIR="$OPTARG";;
49                 m ) MOUNTDIR="$OPTARG";;
50                 e ) EXTRAVER="$OPTARG";;
51                 * ) usage; exit 1;;  # Default, handles -h
52         esac
53 done
54 shift $(($OPTIND - 1))
55
56 if [ "$1" = "x86" ] || [ "$1" = "amd64" ] || [ "$1" = "sparc" ]
57 then
58         ARCH="$1"
59 else
60         usage  # handles --help
61         exit 1
62 fi
63 VERSION=$(cat ${REPOSRC}/overlay-squashfs-${ARCH}/root/version)
64 VOLNAME="sysrcd-${VERSION}" 
65
66 ## ERROR HANDLING
67 #####################################
68
69 die()
70 {
71         if [ -n "$1" ]
72         then
73                 echo "${PROG}: error: $1"
74         else
75                 echo "${PROG}: aborting."
76         fi
77         exit 1
78 }
79
80 # ========= copy files from the temp iso image ================================
81 CURFILE="${ISODIR}/systemrescuecd-${ARCH}-current.iso"
82
83 mkdir -p ${DESTDIR}
84
85 [ -f "${CURFILE}"  ] || die "Cannot find \"${CURFILE}\""
86
87 umount ${MOUNTDIR} 2>/dev/null || die "Cannot unmount \"${MOUNTDIR}\""
88 mount -o loop,ro ${CURFILE} ${MOUNTDIR} || die "Cannot mount \"${CURFILE}\""
89
90 [ -f ${MOUNTDIR}/image.squashfs ] || die "Cannot find a valid file in the ISO"
91
92 [ -d ${WORKDIR} ] && rm -rf ${WORKDIR}
93 mkdir -p ${WORKDIR}
94 cp ${MOUNTDIR}/isolinux/rescuecd* ${REPOBIN}/kernels-${ARCH}/
95 cp ${MOUNTDIR}/image.squashfs ${WORKDIR}/sysrcd.dat
96 ( cd ${WORKDIR} ; md5sum sysrcd.dat > sysrcd.md5 ; chmod 644 sysrcd.* ) 
97 umount ${MOUNTDIR}
98
99 # ========= copy files from overlays ===========================================
100 rsync -ax ${REPOBIN}/overlay-iso-${ARCH}/ "${WORKDIR}/"
101 rsync -ax ${REPOSRC}/overlay-iso-${ARCH}/isolinux/ "${WORKDIR}/isolinux/"
102 rsync -ax ${REPOBIN}/kernels-${ARCH}/ ${WORKDIR}/isolinux/ --exclude='*.igz'
103 cp ${REPOSRC}/overlay-squashfs-${ARCH}/root/version ${WORKDIR}
104
105 # ========= integrate the version number in f1boot.msg =========================
106 sed -i -e "s/VERSION/${VERSION}${EXTRAVER}/" ${WORKDIR}/isolinux/f1boot.msg
107
108 # ========= merge (rescuecd.igz+rescue64.igz+altker32.igz) --> rescuecd.igz ====
109 curdir="${WORKDIR}/isolinux"
110 newramfs="${curdir}/initram-root"
111 newinitrfs="${curdir}/initram.igz"
112 modulesdir="${newramfs}/lib/modules"
113
114 # prepare root of new initramfs
115 [ -d ${newramfs} ] && rm -rf ${newramfs}
116 mkdir -p ${newramfs}
117 cp -a ${REPOBIN}/overlay-initramfs/* ${newramfs}/
118 mkdir -p ${modulesdir}
119
120 # extract the old ramdisks
121 for ker in rescuecd rescue64 altker32 altker64
122 do
123         oldimg="${REPOBIN}/kernels-${ARCH}/${ker}.igz"
124         newdir="${curdir}/${ker}-tmp"
125         echo "extracting ${oldimg}..."
126         mkdir -p "${newdir}"
127         ( cd "${newdir}" && cat ${oldimg} | gzip -d | cpio -id 2>/dev/null )
128 done
129
130 # copy {rescue64,altker32,altker64}/lib/modules to the new initramfs
131 for ker in rescuecd rescue64 altker32 altker64
132 do
133         cp -a ${curdir}/${ker}-tmp/lib/modules/* ${modulesdir}/
134 done
135
136 # copy custom busybox binary to the new initramfs
137 cp ${curdir}/rescuecd-tmp/bin/busybox ${newramfs}/bin/
138 ( cd ${newramfs}/bin/ ; ln busybox sh )
139
140 # update the init boot script in the initramfs
141 cp ${REPOSRC}/mainfiles/linuxrc* ${newramfs}/
142 cp ${REPOSRC}/mainfiles/linuxrc ${newramfs}/init
143
144 # strip and compress kernel modules which are in the sysrcd.dat to save space
145 find ${modulesdir} -name "*.ko" -exec strip --strip-unneeded '{}' \;
146 find ${modulesdir} -name "*.ko" -exec gzip '{}' \;
147
148 # build new initramfs
149 echo 'building the new initramfs...'
150 ( cd ${newramfs} && find . | cpio -H newc -o | gzip -9 > ${newinitrfs} )
151
152 # remove old igz-images and tmp-dirs
153 [ -d ${newramfs} ] && rm -rf ${newramfs} 
154 for ker in rescuecd rescue64 altker32 altker64
155 do
156         #[ -f "${curdir}/${ker}.igz" ] && rm -f "${curdir}/${ker}.igz"
157         [ -d "${curdir}/${ker}-tmp" ] && rm -rf "${curdir}/${ker}-tmp"
158 done
159
160 # ========= copy the new files to the pxe environment =========================
161 if [ -d /tftpboot ]
162 then
163         cp ${WORKDIR}/sysrcd.dat /tftpboot/
164         cp ${WORKDIR}/sysrcd.md5 /tftpboot/
165         cp ${WORKDIR}/isolinux/* /tftpboot/
166 fi
167
168 # ========= prepare the ISO image =============================================
169 ISOFILE="${DESTDIR}/systemrescuecd-${ARCH}-${VERSION}-${MYDATE}.iso"
170
171 if [ "${ARCH}" = "x86" ] || [ "${ARCH}" = "amd64" ]
172 then
173         mkisofs -J -l -V ${VOLNAME} -input-charset utf-8 -o ${ISOFILE} -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ${WORKDIR}
174 fi
175
176 if [ "${ARCH}" = "sparc" ]
177 then
178         mkisofs -G /boot/isofs.b -J -V ${VOLNAME} -B ... -r -o ${ISOFILE} ${WORKDIR}
179 fi
180
181 # ========= prepare the backup ==================================================
182 tar cfjp "${DESTDIR}/systemrescuecd-${ARCH}-${VERSION}-${MYDATE}.tar.bz2" ${REPOSRC} ${REPOBIN} --exclude='.git'