home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
host-198-236-40-254.wlwv.k12.or.us
/
host-198-236-40-254.wlwv.k12.or.us.tar
/
host-198-236-40-254.wlwv.k12.or.us
/
tsv
/
BareMetalInstall.iso
/
tools
/
liblinuxlive
< prev
next >
Wrap
Text File
|
2013-12-03
|
17KB
|
583 lines
#!/bin/ash
# Functions library :: for Linux Live scripts 5.x.y
# Author: Tomas M. <http://www.linux-live.org>
#
#determine which mksquashfs is being called
export BUILD_BIN_DIR=$HOST_BUILD_BIN_DIR
# ===========================================================
# variables
# ===========================================================
# linux live flag to fstab, if fstab line doesn't contain it,
# never remove it from fstab automatically (user added it)
FSTABLLFLAG="# AutoUpdate"
# We have to set these variables very carefully
UNION=union
MEMORY=memory
MOUNTDIR=mnt
CHANGES=$MEMORY/changes
COPY2RAM=$MEMORY/copy2ram
IMAGES=$MEMORY/images
INITRAMDISK=$MOUNTDIR/live
ISOMOUNT=$MOUNTDIR/iso
LIVECDSGN=livecd.sgn
# ===========================================================
# user interface functions
# ===========================================================
# echogreen will echo $@ in green color
# $1 = text
#
echogreen()
{
echo -ne "
""$@""
"
}
# echolog
# $1 = text to show and to write to /var/log/messages
#
echolog()
{
if [ "$1" != "" ]; then
echogreen "* "
if [ "$LIVEDBG_LOGFILE" = "" ]; then
echo "LIVECD:" "$@" >>/var/log/livedbg
else
echo "LIVECD:" "$@" >>$LIVEDBG_LOGFILE; fi
echo "$@"
fi
}
# debug
# commands executed when debug boot parameter is present
#
debug()
{
echo
echo "====="
echo ": Debugging started. Here is the root shell for you."
echo ": Type your desired command or hit Ctrl+D to continue booting."
echo
ash < /dev/console
echo
}
# header
# $1 = text to show
#
header()
{
echo "
""$@""
"
}
fatal()
{
echolog
header "Fatal error occured - $1"
echolog "Something went wrong and we can't continue. This should never happen."
echolog "Please reboot your computer with Ctrl+Alt+Delete ..."
echolog
umount -a -r
mount -t proc proc /proc
mount -t sysfs sysfs /sys
ash < /dev/console
}
allow_only_root()
{
# test if the script is started by root user. If not, exit
if [ "0$UID" -ne 0 ]; then
echo "Only root can run `basename $0`"; exit 1
fi
}
# ===========================================================
# text processing functions
# ===========================================================
# egrep_o is a replacement for "egrep -o". It prints only the last
# matching text
# $1 = regular expression
#
egrep_o()
{
SED="`echo \"$1\" | sed -r 's/\\//\\\\\\//g'`"
cat | egrep "$1" | sed -r "s/.*$SED.*/\\1/" 2>/dev/null
}
# look into cmdline and echo $1 back if $1 is set
# $1 = value name, case sensitive, for example livecd_subdir
# $2 = file to use instead /proc/cmdline, optional
#
cmdline_parameter()
{
CMDLINE=/proc/cmdline
if [ "$2" != "" ]; then CMDLINE="$2"; fi
cat "$CMDLINE" | egrep_o "((^|[[:space:]]+)$1(\$|=|[[:space:]]+))" | egrep_o "($1)"
}
# look into cmdline and echo value of $1 option
# $1 = value name, case sensitive, for example livecd_subdir
# $2 = file to use instead /proc/cmdline, optional
#
cmdline_value()
{
CMDLINE=/proc/cmdline
if [ "$2" != "" ]; then CMDLINE="$2"; fi
cat "$CMDLINE" | egrep_o "((^|[[:space:]]+)$1=([^[:space:]]+))" | egrep_o "(=.*)" | cut -b 2- | tail -n 1
}
# ===========================================================
# system functions
# ===========================================================
# modprobe module $1, including all dependencies, suppress all messages
# (own function because modprobe in busybox doesn't work with gzipped modules)
# $1 = module name, eg. ehci-hcd
# $2 = optional argument
#
modprobe_module()
{
if [ "$1" = "" ]; then return 1; fi
PRINTK=`cat /proc/sys/kernel/printk`
echo "7" >/proc/sys/kernel/printk
KERNEL="`uname -r`"; LSMOD=/tmp/_lsmod
MODULEDEPS="`cat /lib/modules/$KERNEL/modules.dep | egrep \"$1\\.ko(\\.gz)?:\"`"
for MODULE in `echo $MODULEDEPS | cut -d ":" -f 2-` `echo $MODULEDEPS | cut -d ":" -f 1`; do
TMPMOD="/tmp/j/`basename $MODULE .gz`";
# if the module is not loaded already
if [ "`cat $LSMOD 2>/dev/null | egrep \"^$TMPMOD\\\$\"`" = "" ]; then
mkdir -p /tmp/j
mount -t tmpfs tmpfs /tmp/j
gunzip -c $MODULE 2>/dev/null >$TMPMOD
if [ 0$? -ne 0 ]; then cp $MODULE $TMPMOD; fi # can't gunzip? copy
insmod $TMPMOD $2 >/dev/null 2>/dev/null; err=$?
if [ 0$err -eq 0 ]; then echo $TMPMOD >>$LSMOD; fi # module log
rm $TMPMOD
umount /tmp/j
rmdir /tmp/j
fi
done
echo "$PRINTK" >/proc/sys/kernel/printk
if [ 0$err -ne 0 ]; then echolog "error inserting module $1 ($err) for your kernel `uname -r`"; fi
return $err
}
# Mount device $1 to $2
# $1 = /dev device to mount, eg. /dev/hda1, or loop file, or directory
# $2 = mountpoint, eg. /mnt/hda1
# $3 = optional mount options, for example "ro", or "remount,rw"
# $4 = optional filesystem type
#
mount_device()
{
# make sure we have enough arguments
if [ "$2" = "" ]; then return 1; fi
mkdir -p "$2"
if [ -f "$1" ]; then LOOP="-o loop"; else LOOP=""; fi
if [ -d "$1" ]; then LOOP="-o rbind"; fi
if [ "$OPTIONS" ]; then OPTIONS="-o $3"; else OPTIONS=""; fi
if [ "$4" ]; then FILESYSTEM="-t $4"; else FILESYSTEM=""; fi
PRINTK=`cat /proc/sys/kernel/printk`
echo "0" >/proc/sys/kernel/printk
mount $FILESYSTEM "$1" "$2" $OPTIONS $LOOP >/dev/null 2>/dev/null
err=$?
if [ 0$err -ne 0 ]; then rmdir $2 2>/dev/null; fi
echo "$PRINTK" >/proc/sys/kernel/printk
return $err
}
# Force unmount of all parameters
# $1..$n = directories to be unmounted
#
fumount()
{
while [ "$1" ]; do
umount "$1" >/dev/null 2>&1
if [ 0$? -ne 0 ]; then
umount -l "$1" >/dev/null 2>&1
fi
shift
done
}
# ===========================================================
# live module functions
# ===========================================================
# Create module
# call mksquashfs with apropriate arguments
# $1 = directory which will be compressed to squashfs module
# $2 = output .mo file
# $3 = optional -keep-as-directory argument
#
create_module()
{
if [ -x $BUILD_BIN_DIR/mksquashfs ] ; then
$BUILD_BIN_DIR/mksquashfs "$1" "$2" $3 >/dev/null
else
mksquashfs "$1" "$2" $3 >/dev/null
fi
if [ 0$? -ne 0 ]; then return 1; fi
chmod oga-x "$2" # remove execute attrib
}
# Mountpoint exits with 0 if $1 is mountpoint, else exits with 1
# $1 = directory
#
mountpoint()
{
cat /proc/mounts | cut -d " " -f 2 | egrep "^$1\$" >/dev/null 2>&1
}
# Mount .mo module to destination directory
# $1 = path to .mo livecd compressed module
# $2 = destination folder
#
mount_module()
{
mount -t squashfs -o loop,ro "$1" "$2"
err=$?
if [ 0$err -eq 0 ]; then
echo "$1 $2" >>/tmp/_mounts
fi
return $err
}
# Insert a directory tree $2 to an union specified by $1
# Top-level read-write branch is specified by it's index 0
# $1 = union absolute path (starting with /)
# $2 = path to data directory
#
union_insert_dir()
{
mount -n -t unionfs -o remount,add=:"$2"=ro none "$1"
}
# List all modules in all directories (base, modules, optional)
# and filter out unneeded optional modules (not specified by load= kernel parameter)
# separator for load and noload arguments is "," or ";"
# $1 = root directory of mounted DATAdir
#
list_modules()
{
LOAD="`cmdline_value load | sed -r 's/\*/.\*/g' | sed -r 's/,|;/|/g'`"
NOLOAD="`cmdline_value noload | sed -r 's/\*/.\*/g' | sed -r 's/,|;/|/g'`"
find $1/base $1/modules $1/optional -name "*.mo" 2>/dev/null | sort | while read LINE; do
MODNAME="`echo $LINE | cut -b ${#1}- | cut -b 2-`"
if [ "`echo $LINE | grep /optional/`" ]; then
if [ ! "$LOAD" -o ! "`echo $MODNAME | egrep -i \"$LOAD\"`" ]; then continue; fi
fi
if [ "$NOLOAD" -a "`echo $MODNAME | egrep -i \"$NOLOAD\"`" ]; then continue; fi
echo $LINE
done
}
# Insert one single .mo module to the union
# $1 = union absolute path
# $2 = module.mo full path
# $3 = destination folder, where images will be mounted to
# $4 = preffix length strip (number of characters)
#
union_insert_module()
{
TARGET="$3/`basename $2`"
if mountpoint $TARGET >/dev/null 2>&1; then return; fi # skip mounted modules
mkdir -p $TARGET
mount_module $2 $TARGET
if [ 0$? -ne 0 ]; then echo "Cannot read module data. corrupted download?" >&2; return 1; fi
union_insert_dir $1 $TARGET
if [ 0$? -ne 0 ]; then echo "can't insert module to union" >&2; return 2; fi
echo "$2" | cut -b $4- | cut -b 2-
echolog "$2" >/dev/null
return 0
}
# Insert all .mo modules, in $2 directory and subdirectories, to the union
# $1 = union absolute path (starting with /)
# $2 = LiveCD data dir (with directories /base, /modules, etc.)
# $3 = destination folder, where images will be mounted to
#
union_insert_modules()
{
list_modules $2 | while read MODULE; do
echolog -n " -> "
union_insert_module $1 $MODULE $3 ${#2}
done
}
# Copy LiveCD modules to RAM directory
# will copy only /boot, and module files from $1
# $1 = data directory
# $2 = target directory in RAM
#
copy_to_ram()
{
cp -a "$1/rootcopy" "$2" 2>/dev/null # could be empty
list_modules "$1" | while read MODULE; do
TARGET="$2/`basename \`dirname $MODULE\``"
mkdir -p "$TARGET"
cp "$MODULE" "$TARGET"
if [ $? -ne 0 ]; then fatal "Not enough memory. Using ramsize=$RAMSIZE"; fi
done
}
# ===========================================================
# discovery functions
# ===========================================================
# List all CD-ROMs
# by using /proc entries
#
list_cdrom_devices()
{
if [ "`cmdline_parameter nocd`" != "" ]; then return 1; fi
for CDDEVICE in `cat /proc/sys/dev/cdrom/info 2>/dev/null | head -n 3 | tail -n 1 | cut -d ":" -f 2`; do
echo "/dev/$CDDEVICE"
done
}
# List all devices with filesystems
# Return empty result when nohd parameter was given.
#
list_partition_devices()
{
if [ "`cmdline_parameter nohd`" != "" ]; then return 1; fi
cat /proc/partitions | grep -v loop |grep -v major | grep -v "^\$" | sed -r "s/^[0-9 ]+/\\/dev\\//"
}
# List all disk devices
#
list_disk_devices()
{
list_partition_devices | egrep -v "[0-9]"
}
# List all partitions marked as Linux Swap
#
list_swap_devices()
{
if [ "`cmdline_parameter nohd`" != "" -o "`cmdline_parameter noswap`" != "" ]; then return 1; fi
blkid -t TYPE="swap" | cut -d : -f 1
}
# List all block devices
#
list_block_devices()
{
list_cdrom_devices
list_partition_devices
}
# Find file-path on given device
# Mounts the device in $1 and returns path if found,
# else unmounts and exits
# $1 = mountpoint, eg. /mnt
# $2 = device
# $3 = path/filename
#
find_filepath()
{
DIR="$1/`basename $2`"
mount_device $2 $DIR
if [ 0$? -ne 0 ]; then rmdir $DIR 2>/dev/null; return 1; fi
FOUND="`ls -A1d $DIR/$3 2>/dev/null | head -n 1`"
if [ "$FOUND" = "" ]; then umount $DIR 2>/dev/null; rmdir $DIR 2>/dev/null; return 1
else echo "$FOUND" | tr -s '/'; return 0; fi
}
# Find file in computer by mounting disks or other storage devices in $1
# and searching for $2 in the mounted directory
# $1 = mountpoint, eg. /mnt
# $2 = filename or device-path or devicepath/filename
#
find_file()
{
# if parameter is just a device, echo it and exit
if [ -b "$2" -o -c "$2" -o "$2" = "" ]; then echo "$2"; return; fi
# if path doesn't start with /dev/, find the path exactly on all devices
# split DEV/PATH parts
DEVPART="`echo \"$2\" | egrep_o \"(^/dev/[^/]+)\"`"
if [ "$DEVPART" = "" ]; then
# no device is specified, search all devices for filename $1
PATHPART="$2";
list_block_devices | while read DEVICE; do
find_filepath $1 $DEVICE $PATHPART
if [ 0$? -eq 0 ]; then return 0; fi
done
else
# try to find PATHPART only on the given device
PATHPART="`echo \"$2\" | egrep_o \"^/dev/[^/]+/(.*)\"`"
find_filepath $1 $DEVPART $PATHPART
fi
}
# ===========================================================
# hardware preparation functions
# ===========================================================
# Create block devices to /dev described by /sys entries
#
create_block_devices()
{
echolog "creating /dev entries for block devices"
ls -A1d /sys/block/*/dev /sys/block/*/*/dev 2>/dev/null | grep -v loop | while read BLOCK; do
DEVICE="/dev/`basename \`dirname $BLOCK\``"
if [ ! -b $DEVICE ]; then
MINORMAJOR="`head -n 1 $BLOCK | tr ':' ' '`"
mknod $DEVICE b $MINORMAJOR
fi
done
}
# modprobe kernel modules needed for the LiveCD
#
modprobe_essential_modules()
{
echolog "starting loop device support"
modprobe_module loop max_loop=255
echolog "starting cdrom filesystem support"
modprobe_module isofs
echolog "starting squashfs support"
modprobe_module squashfs
echolog "starting unionfs support"
modprobe_module unionfs
modprobe_module ide-cd
echolog "starting vfat support"
modprobe_module nls_cp437
modprobe_module nls_iso8859-1
modprobe_module nls_iso8859-2
modprobe_module vfat
echolog "starting ntfs support"
modprobe_module ntfs
echolog "starting USB Core"
modprobe_module usbcore
modprobe_module ehci-hcd
modprobe_module ohci-hcd
modprobe_module uhci-hcd
modprobe_module usbhid
create_block_devices
}
# modprobe kernel modules needed for USB masstorage devices
#
modprobe_usb_modules()
{
SLEEP="`cmdline_value probeusb | sed -r 's/[^0-9]*([0-9]+).*/\1/'`"
if [ "$SLEEP" = "" ]; then SLEEP=5; fi
echolog "starting USB support"
modprobe_module usb-storage
sleep $SLEEP
create_block_devices
}
# enable/disable CD autoejecting when unmounted
# $1 = 1|0 ... enable|disable
#
cd_autoeject()
{
echo $1 >/proc/sys/dev/cdrom/autoeject
}
# Disable DMA if nodma boot parameter is present
#
setup_dma()
{
if [ ! "`cmdline_parameter nodma`" = "" ]; then
for DEVICE in `list_cdrom_devices` `list_disk_devices`; do
echolog "setting DMA support off for $DEVICE"
hdparm -d 0 $DEVICE
done
fi
}
# update given line in fstab, add new values only if the device is not found
# $1 = fstab file to parse
# $2 = device name
# $3 = mountpoint
# $4 = filesystem
# $5 = mount options
#
fstab_update_line()
{
if [ "`cat \"$1\" | sed -r \"s/#.*//\" | egrep \"^[[:space:]]*[^[:space:]]+[[:space:]]+$2[[:space:]]+\"`" = "" ]; then
echo "$2" "$3" "$4" "$5" 0 0 "$FSTABLLFLAG" >>$1
fi
}
# create correct fstab file in $1/etc/fstab and create apropriate
# mount directories in $1/mnt. Check for iocharset boot option,
# if present, add iocharset to all DOS/Win filesystems (vfat,ntfs)
# $1 = root directory (union)
#
fstab_update()
{
IOCHARSET="`cmdline_value iocharset`"
AUTO="`cmdline_parameter noauto`"
if [ "$AUTO" = "" ]; then AUTO="auto"; fi
FSTAB="$1/etc/fstab"
mkdir -p $1/etc $1/mnt
cat $FSTAB 2>/dev/null | grep -v "$FSTABLLFLAG" >$FSTAB~
# fstab_update_line $FSTAB~ tmpfs / tmpfs defaults
fstab_update_line $FSTAB~ proc /proc proc defaults
fstab_update_line $FSTAB~ sysfs /sys sysfs defaults
fstab_update_line $FSTAB~ devpts /dev/pts devpts defaults
fstab_update_line $FSTAB~ relayfs /mnt/relay relayfs defaults
fstab_update_line $FSTAB~ tmpfs /dev/shm tmpfs defaults
list_cdrom_devices | while read DEVICE; do
MOUNTDIR="/mnt/`basename $DEVICE`_cdrom"
mkdir -p $1/$MOUNTDIR
fstab_update_line $FSTAB~ $DEVICE $MOUNTDIR iso9660 noauto,users,exec
done
list_partition_devices | while read DEVICE; do
unset REMOVABLE; unset OPT; DEV="`basename $DEVICE`"; DEV0="`echo $DEV | cut -b 1-3`"
if [ "0`cat /sys/block/$DEV0/removable 2>/dev/null`" -ne 0 ]; then
REMOVABLE="_removable"
fi
MOUNTDIR="/mnt/$DEV$REMOVABLE"
FS="`blkid -s TYPE $DEVICE | cut -d = -f 2 | tr -d ' \"'`"
# add special options for NTFS
if [ "$FS" = "ntfs" ]; then
OPT=",ro"
if [ "$IOCHARSET" != "" ]; then OPT="$OPT,nls=$IOCHARSET"; fi
fi
# add special options for VFAT
if [ "$FS" = "vfat" -a "$IOCHARSET" != "" ]; then OPT=",iocharset=$IOCHARSET"; fi
# if the partition has filesystem, add it to fstab
if [ "$FS" != "" ]; then
if [ "$FS" = "swap" ]; then
fstab_update_line $FSTAB~ $DEVICE swap swap defaults
else
fstab_update_line $FSTAB~ $DEVICE $MOUNTDIR $FS $AUTO,users,suid,dev,exec$OPT
mkdir -p "$1/$MOUNTDIR"
fi
fi
done
fstab_update_line $FSTAB~ /dev/fd0 /mnt/floppy vfat,msdos noauto,users,suid,dev,exec
mkdir -p $1/mnt/floppy
mv -f $FSTAB~ $FSTAB
}