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