1#!/usr/bin/env bash 2set -e 3 4# 5# This script creates a bootable image and should at some point in the future 6# be replaced by makefs. 7# 8 9: ${ARCH=i386} 10: ${OBJ=../obj.${ARCH}} 11: ${CROSS_TOOLS=${OBJ}/"tooldir.`uname -s`-`uname -r`-`uname -m`"/bin} 12: ${CROSS_PREFIX=${CROSS_TOOLS}/i586-elf32-minix-} 13: ${JOBS=1} 14: ${DESTDIR=${OBJ}/destdir.$ARCH} 15: ${RELEASETOOLSDIR=./releasetools/} 16: ${FSTAB=${DESTDIR}/etc/fstab} 17: ${BUILDVARS=} 18: ${BUILDSH=build.sh} 19: ${CREATE_IMAGE_ONLY=0} 20: ${RC=minix_x86.rc} 21 22# 23# Directory where to store temporary file system images 24# 25: ${IMG_DIR=${OBJ}/img} 26: ${CDFILES=${IMG_DIR}/cd} 27 28# All sized are written in 512 byte blocks 29# 30# we create a disk image of about 2 gig's 31# for alignment reasons, prefer sizes which are multiples of 4096 bytes 32# 33: ${ROOT_SIZE=$(( 64*(2**20) / 512))} 34: ${HOME_SIZE=$(( 128*(2**20) / 512))} 35: ${USR_SIZE=$(( 1792*(2**20) / 512))} 36 37# 38# Do some math to determine the start addresses of the partitions. 39# Don't leave holes so the 'partition' invocation later is easy. 40# 41 42 43# Where the kernel & boot modules will be 44MODDIR=${DESTDIR}/boot/minix/.temp 45 46while getopts "i" c 47do 48 case "$c" in 49 i) : ${IMG=minix_x86.iso} 50 ISOMODE=1 51 ;; 52 esac 53done 54 55: ${IMG=minix_x86.img} 56 57if [ "x${ISOMODE}" = "x1" ] 58then 59 # In iso mode, make all FSes fit (i.e. as small as possible), but 60 # leave some space on / 61 ROOTSIZEARG="-x 5" 62else 63 # In hd image mode, FSes have fixed sizes 64 ROOTSIZEARG="-b $((${ROOT_SIZE} / 8))" 65 USRSIZEARG="-b $((${USR_SIZE} / 8))" 66 HOMESIZEARG="-b $((${HOME_SIZE} / 8))" 67fi 68 69if [ ! -f ${BUILDSH} ] 70then 71 echo "Please invoke me from the root source dir, where ${BUILDSH} is." 72 exit 1 73fi 74 75export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:${PATH} 76 77# 78# Are we going to build the minix sources? 79# 80 81if [ ${CREATE_IMAGE_ONLY} -eq 1 ] 82then 83 if [ ! -d ${DESTDIR} ] 84 then 85 echo "Minix source code does'nt appear to have been built." 86 echo "Please try with \$CREATE_IMAGE_ONLY set to 0." 87 exit 1 88 fi 89fi 90 91# 92# Artifacts from this script are stored in the IMG_DIR 93# 94rm -rf ${IMG_DIR} ${IMG} 95mkdir -p ${IMG_DIR} ${CDFILES} 96 97if [ ${CREATE_IMAGE_ONLY} -eq 0 ] 98then 99 echo "Going to build Minix source code..." 100 # 101 # Remove the generated files to allow us call build.sh without '-V SLOPPY_FLIST=yes'. 102 # 103 rm -f ${FSTAB} 104 105 # 106 # Now start the build. 107 # 108 sh ${BUILDSH} -j ${JOBS} -m ${ARCH} -O ${OBJ} -D ${DESTDIR} ${BUILDVARS} -U -u distribution 109 110fi 111 112# 113# create a fstab entry in /etc this is normally done during the 114# setup phase on x86 115# 116cat >${FSTAB} <<END_FSTAB 117/dev/c0d0p2 /usr mfs rw 0 2 118/dev/c0d0p3 /home mfs rw 0 2 119none /sys devman rw,rslabel=devman 0 0 120END_FSTAB 121 122rm -f ${DESTDIR}/SETS.* 123 124${CROSS_TOOLS}/nbpwd_mkdb -V 0 -p -d ${DESTDIR} ${DESTDIR}/etc/master.passwd 125 126# 127# make the different file system. this part is *also* hacky. We first convert 128# the METALOG.sanitised using mtree into a input METALOG containing uids and 129# gids. 130# After that we do some magic processing to add device nodes (also missing from METALOG) 131# and convert the METALOG into a proto file that can be used by mkfs.mfs 132# 133echo "Creating the file systems" 134 135# 136# read METALOG and use mtree to convert the user and group names into uid and gids 137# FIX put "input somewhere clean" 138# 139cat ${DESTDIR}/METALOG.sanitised | ${CROSS_TOOLS}/nbmtree -N ${DESTDIR}/etc -C -K device > ${IMG_DIR}/input 140 141# add rc (if any) 142if [ -f ${RC} ]; then 143 cp ${RC} ${DESTDIR}/usr/etc/rc.local 144 echo "./usr/etc/rc.local type=file uid=0 gid=0 mode=0644" >> ${IMG_DIR}/input 145fi 146 147# add fstab 148echo "./etc/fstab type=file uid=0 gid=0 mode=0755 size=747 time=1365060731.000000000" >> ${IMG_DIR}/input 149 150# fill root.img (skipping /usr entries while keeping the /usr directory) 151cat ${IMG_DIR}/input | grep -v "^./usr/" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR} -o ${IMG_DIR}/root.proto 152 153# 154# Create proto files for /usr and /home using toproto. 155# 156cat ${IMG_DIR}/input | grep "^\./usr/\|^. " | sed "s,\./usr,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR}/usr -o ${IMG_DIR}/usr.proto 157cat ${IMG_DIR}/input | grep "^\./home/\|^. " | sed "s,\./home,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR}/home -o ${IMG_DIR}/home.proto 158 159if [ "x${ISOMODE}" = "x1" ] 160then 161 cp ${DESTDIR}/usr/mdec/boot_monitor ${CDFILES}/boot 162 cp ${MODDIR}/* ${CDFILES}/ 163 . ${RELEASETOOLSDIR}/release.functions 164 cd_root_changes # uses $CDFILES and writes $CDFILES/boot.cfg 165 # start the image off with the iso image; reduce root size to reserve 166 ${CROSS_TOOLS}/nbwriteisofs -s0x0 -l MINIX -B ${DESTDIR}/usr/mdec/bootxx_cd9660 -n ${CDFILES} ${IMG} 167 ISO_SIZE=$((`${CROSS_TOOLS}/nbstat -f %z ${IMG}` / 512)) 168else 169 # just make an empty iso partition 170 ISO_SIZE=8 171fi 172 173# 174# Generate /root, /usr and /home partition images. 175# 176echo "Writing Minix filesystem images" 177ROOT_START=${ISO_SIZE} 178echo " - ROOT" 179_ROOT_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -d ${ROOTSIZEARG} -I $((${ROOT_START}*512)) ${IMG} ${IMG_DIR}/root.proto`/512)) 180USR_START=$((${ROOT_START} + ${_ROOT_SIZE})) 181echo " - USR" 182_USR_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -d ${USRSIZEARG} -I $((${USR_START}*512)) ${IMG} ${IMG_DIR}/usr.proto`/512)) 183HOME_START=$((${USR_START} + ${_USR_SIZE})) 184echo " - HOME" 185_HOME_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -d ${HOMESIZEARG} -I $((${HOME_START}*512)) ${IMG} ${IMG_DIR}/home.proto`/512)) 186 187# 188# Write the partition table using the natively compiled 189# minix partition utility 190# 191${CROSS_TOOLS}/nbpartition -m ${IMG} 0 81:${ISO_SIZE} \ 192 81:${_ROOT_SIZE} 81:${_USR_SIZE} 81:${_HOME_SIZE} 193 194mods="`( cd ${MODDIR}; echo mod* | tr ' ' ',' )`" 195if [ "x${ISOMODE}" = "x1" ] 196then 197 echo "CD image at `pwd`/${IMG}" 198else 199 echo "To boot this image on kvm:" 200 echo "cd ${MODDIR} && qemu-system-i386 -display none -serial stdio -kernel kernel -append \"console=tty00 rootdevname=c0d0p1\" -initrd \"${mods}\" -hda `pwd`/${IMG} --enable-kvm" 201fi 202