#!/bin/bash # # mkdiscimage # # Create a disc image # # Usage mkdiscimage [] # size is in MB # # Version 1.0 # # Copyright 2007 Simtec Electronics # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, copy, # modify, merge, publish, distribute, sublicense, and/or sell copies # of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # DEFAULTSIZE=512 SWAPSIZE=128 DEBIAN_MIRROR="http://mirror/debian" DEBIAN_SUITE=etch DEBIAN_ARCH=arm KERNEL_SRC=http://www.simtec.co.uk/products/SWLINUX/files KERNEL_IMG=s3c24xx-linux-2.6.23-simtec1.tar.bz2 ###################### end of parameters ###################### bootstrap_fs() { MOUNT=$1 ROOT_UUID=$2 if [ "x${MOUNT}" = "x" ]; then return 1 fi if [ "x${ROOT_UUID}" = "x" ]; then return 2 fi OLDDIR=$(pwd) cd ${MOUNT} debootstrap --arch ${DEBIAN_ARCH} --foreign ${DEBIAN_SUITE} ${MOUNT} ${DEBIAN_MIRROR} wget ${KERNEL_SRC}/${KERNEL_IMG} tar -jxf ${KERNEL_IMG} rm ${KERNEL_IMG} echo "${DEBIAN_ARCH}" > ${MOUNT}/etc/hostname mknod ${MOUNT}/dev/console c 5 1 mknod ${MOUNT}/dev/ttySAC0 c 204 64 mknod ${MOUNT}/dev/ttySAC1 c 204 65 mknod ${MOUNT}/dev/ttySAC2 c 204 66 echo "# " > ${MOUNT}/etc/fstab echo "proc /proc proc defaults 0 0" >> ${MOUNT}/etc/fstab echo "UUID=${ROOT_UUID} / ext3 defaults,errors=remount-ro 0" >> ${MOUNT}/etc/fstab cd ${OLDDIR} } bootstrap_e2fs () { DMDEVICE=$1 if [ "x${DMDEVICE}" = "x" ]; then return 1 fi # the devicemapper setup worked mke2fs -O dir_index,has_journal,sparse_super ${DMDEVICE} UUID=$(tune2fs -l ${DMDEVICE} | sed -n 's/^Filesystem UUID:[ \t]*\(.*\)/\1/p') mount ${DMDEVICE} /mnt if [ $? -eq 0 ];then bootstrap_fs /mnt $UUID fi umount ${DMDEVICE} } ###################### main code ###################### FORM=virtdisc # command line parameters D=$(dirname $0) # required tools TOOLS="${D}/lspartab dd losetup parted dmsetup" DISCIMAGE=$1 if [ "x${DISCIMAGE}" = "x" ]; then echo "usage:" echo "mkvirtdisc []" exit 1 fi # ensure superuser privileges are available if [ "x${UID}" = "x" -o "${UID}" -ne 0 ]; then which sudo >/dev/null if [ $? -ne 0 ]; then echo "Script requires superuser privileges!" exit 1 else sudo $0 $* exit $? fi fi DISCSIZE=$2 if [ "x${DISCSIZE}" = "x" ]; then echo "Using default size of ${DEFAULTSIZE}" DISCSIZE=${DEFAULTSIZE} fi # ensure the required commands are present which ${TOOLS} >/dev/null if [ $? -ne 0 ]; then echo "Required command is missing" echo "${TOOLS}" exit 1 fi # calculated values ROOTSIZE=$(expr ${DISCSIZE} - ${SWAPSIZE}) READTAB="${D}/lspartab" # check we can setup a loopback device LOOP=$(losetup -f) if [ "x${LOOP}" = "x" ]; then echo "Cannot find a loop" exit 1 fi # create image file dd if=/dev/zero of=${DISCIMAGE} bs=1M count=${DISCSIZE} # partition it parted --script ${DISCIMAGE} mklabel msdos mkpart primary ext2 0 ${ROOTSIZE} mkpartfs primary linux-swap ${ROOTSIZE} ${DISCSIZE} # create device nodes if ! losetup "${LOOP}" "${DISCIMAGE}"; then losetup -d "${LOOP}" echo "Unable to losetup!" exit 1 fi LOOPNODE=$(stat -c '%t:%T' "${LOOP}") "${READTAB}" "${DISCIMAGE}" | while read partnum start length type; do echo "Partition $FORM$partnum starts at $start and is $length sectors" echo "0 $length linear $LOOPNODE $start" | dmsetup create ${FORM}${partnum} if [ -b /dev/mapper/${FORM}${partnum} ]; then #device mapper setup was succesful case "${type}" in ( "131" ) bootstrap_e2fs /dev/mapper/${FORM}${partnum} ;; esac dmsetup remove ${FORM}${partnum} fi done if ! losetup -d "${LOOP}"; then echo "losetup -d failed?" exit 1 fi exit 0