Added a preliminary README. (warning: untested)
[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  -b 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 ARCH="$1"
57 if [ "$1" = "x86" ] || [ "$1" = "amd64" ] || [ "$1" = "sparc" ]
58 then
59         ARCH="$1"
60 else
61 fi
62 case ${ARCH} in
63         x86)    BASEARCH="x86";;
64         amd64)  BASEARCH="x86";;
65         sparc)  BASEARCH="sparc";;
66         *)      usage; exit 1;;
67 esac
68 VERSION=$(cat ${REPOSRC}/overlay-squashfs-${BASEARCH}/root/version)
69 VOLNAME="sysrcd-${VERSION}" 
70
71 ## ERROR HANDLING
72 #####################################
73
74 die()
75 {
76         if [ -n "$1" ]
77         then
78                 echo "${PROG}: error: $1"
79         else
80                 echo "${PROG}: aborting."
81         fi
82         exit 1
83 }
84
85 # ========= copy files from the temp iso image ================================
86 CURFILE="${ISODIR}/systemrescuecd-${ARCH}-current.iso"
87
88 mkdir -p ${DESTDIR}
89
90 [ -f "${CURFILE}"  ] || die "Cannot find \"${CURFILE}\""
91
92 umount ${MOUNTDIR} 2>/dev/null || die "Cannot unmount \"${MOUNTDIR}\""
93 mount -o loop,ro ${CURFILE} ${MOUNTDIR} || die "Cannot mount \"${CURFILE}\""
94
95 [ -f ${MOUNTDIR}/image.squashfs ] || die "Cannot find a valid file in the ISO"
96
97 [ -d ${WORKDIR} ] && rm -rf ${WORKDIR}
98 mkdir -p ${WORKDIR}
99 cp ${MOUNTDIR}/isolinux/rescuecd* ${REPOBIN}/kernels-${ARCH}/
100 cp ${MOUNTDIR}/image.squashfs ${WORKDIR}/sysrcd.dat
101 ( cd ${WORKDIR} ; md5sum sysrcd.dat > sysrcd.md5 ; chmod 644 sysrcd.* ) 
102 umount ${MOUNTDIR}
103
104 # ========= copy files from overlays ===========================================
105 rsync -ax ${REPOBIN}/overlay-iso-${BASEARCH}/ "${WORKDIR}/"
106 rsync -ax ${REPOSRC}/overlay-iso-${BASEARCH}/isolinux/ "${WORKDIR}/isolinux/"
107 rsync -ax ${REPOBIN}/kernels-${ARCH}/ ${WORKDIR}/isolinux/ --exclude='*.igz'
108 cp ${REPOSRC}/overlay-squashfs-${BASEARCH}/root/version ${WORKDIR}
109
110 # ========= integrate the version number in f1boot.msg =========================
111 sed -i -e "s/VERSION/${VERSION}${EXTRAVER}/" ${WORKDIR}/isolinux/f1boot.msg
112
113 # ========= merge (rescuecd.igz+rescue64.igz+altker32.igz) --> rescuecd.igz ====
114 curdir="${WORKDIR}/isolinux"
115 newramfs="${curdir}/initram-root"
116 newinitrfs="${curdir}/initram.igz"
117 modulesdir="${newramfs}/lib/modules"
118
119 # prepare root of new initramfs
120 [ -d ${newramfs} ] && rm -rf ${newramfs}
121 mkdir -p ${newramfs}
122 cp -a ${REPOBIN}/overlay-initramfs/* ${newramfs}/
123 mkdir -p ${modulesdir}
124
125 # extract the old ramdisks
126 for ker in rescuecd rescue64 altker32 altker64
127 do
128         oldimg="${REPOBIN}/kernels-${ARCH}/${ker}.igz"
129         newdir="${curdir}/${ker}-tmp"
130         echo "extracting ${oldimg}..."
131         mkdir -p "${newdir}"
132         ( cd "${newdir}" && cat ${oldimg} | gzip -d | cpio -id 2>/dev/null )
133 done
134
135 # copy {rescue64,altker32,altker64}/lib/modules to the new initramfs
136 for ker in rescuecd rescue64 altker32 altker64
137 do
138         cp -a ${curdir}/${ker}-tmp/lib/modules/* ${modulesdir}/
139 done
140
141 # copy custom busybox binary to the new initramfs
142 cp ${curdir}/rescuecd-tmp/bin/busybox ${newramfs}/bin/
143 ( cd ${newramfs}/bin/ ; ln busybox sh )
144
145 # update the init boot script in the initramfs
146 cp ${REPOSRC}/mainfiles/linuxrc* ${newramfs}/
147 cp ${REPOSRC}/mainfiles/linuxrc ${newramfs}/init
148
149 # strip and compress kernel modules which are in the sysrcd.dat to save space
150 find ${modulesdir} -name "*.ko" -exec strip --strip-unneeded '{}' \;
151 find ${modulesdir} -name "*.ko" -exec gzip '{}' \;
152
153 # build new initramfs
154 echo 'building the new initramfs...'
155 ( cd ${newramfs} && find . | cpio -H newc -o | gzip -9 > ${newinitrfs} )
156
157 # remove old igz-images and tmp-dirs
158 [ -d ${newramfs} ] && rm -rf ${newramfs} 
159 for ker in rescuecd rescue64 altker32 altker64
160 do
161         #[ -f "${curdir}/${ker}.igz" ] && rm -f "${curdir}/${ker}.igz"
162         [ -d "${curdir}/${ker}-tmp" ] && rm -rf "${curdir}/${ker}-tmp"
163 done
164
165 # ========= copy the new files to the pxe environment =========================
166 if [ -d /tftpboot ]
167 then
168         cp ${WORKDIR}/sysrcd.dat /tftpboot/
169         cp ${WORKDIR}/sysrcd.md5 /tftpboot/
170         cp ${WORKDIR}/isolinux/* /tftpboot/
171 fi
172
173 # ========= prepare the ISO image =============================================
174 ISOFILE="${DESTDIR}/systemrescuecd-${ARCH}-${VERSION}-${MYDATE}.iso"
175
176 if [ "${ARCH}" = "x86" ] || [ "${ARCH}" = "amd64" ]
177 then
178         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}
179 fi
180
181 if [ "${ARCH}" = "sparc" ]
182 then
183         mkisofs -G /boot/isofs.b -J -V ${VOLNAME} -B ... -r -o ${ISOFILE} ${WORKDIR}
184 fi
185
186 # ========= prepare the backup ==================================================
187 tar cfjp "${DESTDIR}/systemrescuecd-${ARCH}-${VERSION}-${MYDATE}.tar.bz2" ${REPOSRC} ${REPOBIN} --exclude='.git'