summaryrefslogblamecommitdiffstats
path: root/inc/functions.usb.sh
blob: f5b01f6197e824f54220ac3f84dd95b3eba76a74 (plain) (tree)

















































































































                                                                                                            
                                                                                                        

















































                                                                                             
                                                                                          




























                                                                                                   

                            







                                                       
SHORT_OPTS=":vfdhs"
LONG_OPTS="version,force,sudo,debug,help"

KERNEL="kernel-preboot-latest"
INITRAMFS="initramfs-default"

LOG_DIR="/tmp/pbmtk"

run_module_checks () {
	
	if [ "x$SUDO" != "x1" -a $(whoami) != "root" ]; then
	  	perror "You should be root or use '--sudo' switch."
	  	exit 1;
	fi
	
	if [ ! -z $1 ]; then
	    perror "Too many parameters. \n"
	    print_usage
	    exit 1
	fi
	
	if [ -z $OUT_DEV ]; then
	    perror "No output device specified.\n"
	    print_usage
	    exit 1
	fi
	
	if [ ! -e "/dev/$OUT_DEV" ]; then
	    perror "There is no /dev/${OUT_DEV}."
	    exit 1
	fi
	
#	if [ -z $(which uuencode) ]; then
#	    perror "'uuencode' is missing (if you are on a debian/ubuntu system: apt-get install sharutils)"
#	    exit 1
#	fi
}

init_params () {
	FORCE=0
	DEBUG=0
	SUDO=0
}

print_usage() {
  echo "Usage: $(basename $SELF) usb [OPTIONS] DEVICE"
  echo -e "  -d  --debug   \t give more debug output"
  echo -e "  -f  --force   \t don't ask questions"
  echo -e "  -s  --sudo    \t use sudo instead of requesting uid=0"
  echo -e "  -h  --help    \t print help"
  echo -e "  -v  --version \t print version information"
  echo "DEVICE is the device used by the usb stick (e.g. \"sdX\" for /dev/sdX)."
}

read_params() {
	getopt_start $@
	
	eval set -- "$GETOPT_TEMP"
	
	while true ; do
	        case "$1" in
	                -v|--version)
	                     echo  "OpenSLX PreBoot USB stick installer ($VERSION - $VDATE)."
	                     exit 0
	                   ;;
	                -h|--help)
	                     print_usage
	                     exit 0
	                   ;;
	                -f|--force) pinfo "Disable user-interaction."; FORCE=1; shift ;;
	                -d|--debug) pinfo "Enabled debugmode."; DEBUG=1; unset_quiet; shift ;;
	                -s|--sudo)  pinfo "Enabled usage of sudo."; SUDO=1; shift ;;
	                --) shift ; break ;;
	                *) perror "Internal error!" ; exit 1 ;;
	        esac
	done
	
	OUT_DEV=$1
	shift
	
	[ "x$SUDO" = "x1" ] && SUDOCMD="sudo"
}

ask_user () {
	unset_quiet
	if [ "x$FORCE" != "x1" ]; then
	  pinfo "Device \"/dev/${OUT_DEV}\" will be repartitioned and formatted."
	  pechon "Do you really want to proceed? Then type in 'yes' or 'no' (default: 'no'): "
	  read cont
	  [ "x${cont}" != "xyes" ] && exit 1
	fi
    set_quiet
}

umount_dev () {
	pinfo "Make sure stick is unmounted."
	for i in 1 2 3 4 5 6 7 8 9; do
	   $SUDOCMD umount /dev/${OUT_DEV}$i &> /dev/null
	done	
}

create_partitions () {
	pinfo "Create partitions on stick."
    # get the total size of the device
	DISKSIZE=$($SUDOCMD sfdisk -s /dev/${OUT_DEV} 2>/dev/null || echo 0)
	DISKSIZE=$((${DISKSIZE}/1024))
	
	# get the size of boot stuff and calculate the left over free space on device
	KSIZE=$(ls -l ${ROOT_DIR}/build/${KERNEL}|awk '{print $5}')
	ISIZE=$(ls -l ${ROOT_DIR}/build/${INITRAMFS}|awk '{print $5}')
	IMAGESIZE=$(((${KSIZE}+${ISIZE}+6000000)/1048576))
	FREE=$((${DISKSIZE}-${IMAGESIZE}))
	
	if [ $IMAGESIZE -lt 16 ]; then
		perror "Calculated image size too small, something went wrong probably ($IMAGESIZE MB)."
	fi
	
	# create bootable vfat at the end of the device if big enough, otherwise use
	# the entire device
	if [ ${FREE} -ge 50 ] ; then
	  MAIN_DEV="/dev/${OUT_DEV}2"
	  HOME_DEV="/dev/${OUT_DEV}1" 
	  $SUDOCMD sfdisk /dev/${OUT_DEV} -uM << EOF
,${FREE},L,
,,6,*
EOF
	else
	  MAIN_DEV="/dev/${OUT_DEV}1"
	  HOME_DEV="" 
	  $SUDOCMD sfdisk /dev/${OUT_DEV}  << EOF
,,6,*
EOF
	fi
	
	sync
}	
	
create_fs () {
	pinfo "Create bootable MBR and filesystems."
	# copy mbr to stick
	$SUDOCMD dd bs=440 count=1 conv=notrunc if=${SYSLINUX}/mbr/mbr.bin of=/dev/${OUT_DEV}
	
	# create filesystems on newly createt partitions
	$SUDOCMD mkfs.vfat -F 16 -n  pbstick ${MAIN_DEV}
	[ ! -z ${HOME_DEV} ] && $SUDOCMD mkfs.ext2 -L pbstick-home ${HOME_DEV}
}

mount_stick () {
	pinfo "Mounting stick."
	$SUDOCMD mkdir -p /media/openslx-stick
    $SUDOCMD mount ${MAIN_DEV} /media/openslx-stick
    
	if [ ! -z ${HOME_DEV} ]; then
	  $SUDOCMD mkdir -p /media/openslx-stick-usrhome
	  $SUDOCMD mount ${HOME_DEV} /media/openslx-stick-usrhome
	fi
	
	$SUDOCMD mkdir -p /media/openslx-stick/boot	
}

copy_files () {
	pinfo "Copy files to stick."
	$SUDOCMD cp -v ${ROOT_DIR}/build/${KERNEL} /media/openslx-stick/boot/kernel
	
	$SUDOCMD cp -v ${ROOT_DIR}/build/${INITRAMFS} /media/openslx-stick/boot/initramfs
	$SUDOCMD cp -v ${ROOT_DIR}/build/initramfs-serial /media/openslx-stick/boot/serial
	
	$SUDOCMD cp -v ${SYSLINUX}/com32/menu/menu.c32 /media/openslx-stick/boot/
	$SUDOCMD cp -v ${SYSLINUX}/com32/menu/vesamenu.c32 /media/openslx-stick/boot/
	
	$SUDOCMD cp -v ${ROOT_DIR}/config/extlinux/* /media/openslx-stick/boot
	$SUDOCMD mv /media/openslx-stick/boot/extlinux.conf  /media/openslx-stick/boot/syslinux.cfg
}

umount_stick () {
	pinfo "Unmount stick (it can take some time to sync)."
	$SUDOCMD umount /media/openslx-stick
 	[ ! -z ${HOME_DEV} ] && $SUDOCMD umount /media/openslx-stick-usrhome

	$SUDOCMD rm -rf /media/openslx-stick*
}

run_syslinux () {
	pinfo "Complete syslinux installation."
	echo "${SYSLINUX}/linux/syslinux --install -d /boot -f ${MAIN_DEV}"
	$SUDOCMD ${SYSLINUX}/linux/syslinux --install -d /boot -f ${MAIN_DEV}
}

run () {
	set_quiet
	
	ask_user
	umount_dev
	create_partitions
	create_fs
	mount_stick
	create_serial_module
	copy_files
	umount_stick
	run_syslinux
	
	unset_quiet
	
	pinfo "FINISHED: You can unplug the stick now!"
}