usbinstall/src/usbinstall.sh

283 lines
8.2 KiB
Bash
Raw Normal View History

2011-11-09 22:51:07 +01:00
#!/bin/bash
2012-01-07 17:55:29 +01:00
#usbinstall.sh
# Arg 1: usb device for installation
# Arg 2: boot file
# Arg 3: iso file
2011-11-09 22:51:07 +01:00
2012-01-07 17:55:29 +01:00
#~ # openmamba-usbinstall è uno script per la generazione automatica di una chiavetta di boot per openmamba snapshot.
#~ # Se i file necessari sono presenti sul disco fisso verranno utilizzati quelli altrimenti
#~ # scaricherà direttamente la versione attuale dal sito di openmamba.
#~ # se il file iso è presente nella home verrà usato direttamente altrimenti si aprirà un dialogo dove sarà possibile cercarlo.
#~ # (c) 2009 ercole 'ercolinux' carpanetto - ercole69@gmail.com
#~ # (c) 2009-2010 Silvan Calarco - silvan.calarco@mambasoft.it
# License: GPL v.3
2011-11-09 22:51:07 +01:00
2012-01-07 17:55:29 +01:00
#%Changelog:
# 17/10/11 (michiamophil)
# * Script modificato organizzando il codice in funzioni, chiamate dal modulo installazione.py
# 06/01/12 (michiamophil)
# * "sudo u/mount -> udisks"
# * export $1, $2, $3 -> OPENMAMBA_*
# 08/01/12 (michiamophil)
# * INITIAL-CHECK
2011-11-09 22:51:07 +01:00
2012-01-07 17:55:29 +01:00
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
#-------------------------------------(DBUS)-----------------------------------------
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
function dbus_properties_get_string() {
REPLY=`dbus-send --system --print-reply --dest=$1 $2 \
org.freedesktop.DBus.Properties.Get string:$3 string:"$4"`
[ $? -eq 0 ] || return 1
echo $REPLY | sed "s|.*string.*\"\(.*\)\"|\1|"
return 0
2011-11-09 22:51:07 +01:00
}
2012-01-07 17:55:29 +01:00
function dbus_properties_get_stringlist() {
# FIXME: only returns first element
REPLY=`dbus-send --system --print-reply --dest=$1 $2 \
org.freedesktop.DBus.Properties.Get string:$3 string:"$4"`
[ $? -eq 0 ] || return 1
echo $REPLY | sed "s|.*array.*\[ string \"\(.*\)\" ]|\1|"
return 0
2011-11-09 22:51:07 +01:00
}
2012-01-07 17:55:29 +01:00
function dbus_properties_get_uint64() {
REPLY=`dbus-send --system --print-reply --dest=$1 $2 \
org.freedesktop.DBus.Properties.Get string:$3 string:$4`
[ $? -eq 0 ] || return 1
echo $REPLY | sed "s|.*uint64 \(.*\)|\1|"
return 0
}
2011-11-13 18:04:35 +01:00
function dbus_properties_get_bool() {
REPLY=`dbus-send --system --print-reply --dest=$1 $2 org.freedesktop.DBus.Properties.Get string:$3 string:"$4"`
[ $? -eq 0 ] || return 1
echo $REPLY | sed "s|.*boolean \(.*\)|\1|"
return 0
}
2012-01-07 20:52:28 +01:00
# Set progressbar (only if $DBUS_USBINSTALL = 1)
2012-01-07 17:55:29 +01:00
# Arg 1: value%
set_pbar()
2011-11-09 22:51:07 +01:00
{
if [ "$DBUS_USBINSTALL" == "1" ]; then
2012-01-07 20:52:28 +01:00
dbus-send --print-reply --session --dest=org.openmamba.usbinstall /org/openmamba/usbinstall org.openmamba.usbinstall.Set_progressBar int32:$1
fi
2011-11-09 22:51:07 +01:00
}
# Set status label (only if $DBUS_USBINSTALL = 1)
2012-01-07 17:55:29 +01:00
# Arg 1: string
set_status()
2011-11-09 22:51:07 +01:00
{
if [ "$DBUS_USBINSTALL" == "1" ]; then
2012-01-07 20:52:28 +01:00
dbus-send --print-reply --session --dest=org.openmamba.usbinstall /org/openmamba/usbinstall org.openmamba.usbinstall.Set_description "string:$1"
fi
2011-11-09 22:51:07 +01:00
}
2012-01-07 17:55:29 +01:00
####################################(DBUS)###########################################
2011-11-09 22:51:07 +01:00
function errorAndExit()
{
echo "$1"
# Se l'argomento 2 è settato stampa anche l'help
if [ -n "$2" ]; then
usage
fi
exit 1
}
function usage()
{
echo "Usbistall: write Openmamba into your usb key!"
2012-02-18 19:07:03 +01:00
echo -e "\tUsage: sudo usbinstall Arg1 Arg2 Arg3 [-y]"
echo -e "\tArg 1: Usb device path (es. /dev/sdc)"
echo -e "\tArg 2: Cpio.gz file (es. ~/openmamba-bootusb-it-milestone2-2.0pre8.i586.cpio.gz)"
echo -e "\tArg 3: Iso file (es. ~/openmamba-livecd-it-milestone2-2.0pre8.i586.iso)"
2012-02-18 19:07:03 +01:00
echo -e "\t-y: Installa senza chiedere alcuna conferma [Attenzione!]"
}
2011-11-09 22:51:07 +01:00
2012-01-07 17:55:29 +01:00
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
#--------------------------------(INITIAL-CHECK)-------------------------------------
2012-01-07 17:55:29 +01:00
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
# Help
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
exit 0
fi
# Check if usbinstall have root permission
if [ "`id -u`" != 0 ]; then
errorAndExit "Error: usbinstall must be run as root" 1
fi
# Check if Arg 1 is valid
if [ -b "$1" ]; then # If is a block device
ISKEY=`dbus_properties_get_bool org.freedesktop.UDisks /org/freedesktop/UDisks/devices/${1/*\/} \
org.freedesktop.UDisks.Device DeviceIsRemovable`
if [ "$ISKEY" = "true" ]; then # If is a usb
export SELECT_USB=$1
else # If not, user can choose
echo "Warning: you haven't choose a usb key ($1). Are you sure to continue? [yes/no]"
read KEY
case "$KEY" in
yes)
export SELECT_USB=$1
;;
*)
errorAndExit "Installation aborted"
;;
esac
fi
else
errorAndExit "Error(Arg 1): \"$1\" isn't a block device" 1
fi
# Check if Arg 2 is valid
if [ -f "$2" ]; then # Check if is a file
if [ -r "$2" ]; then # Chek is is readable
export SELECT_BOOT=$2
else
errorAndExit "Error(Arg 2): \"$2\" is not readable" 1
fi
else
errorAndExit "Error(Arg 2): \"$2\" isn't a regular file" 1
fi
# Check if Arg 2 is a gz file
if [ "${2##*.}" != "gz" ]; then
errorAndExit "Error(Arg 2): you must chose a cpio.gz file" 1
fi
# Check if Arg 3 is valid
if [ -f "$3" ]; then # Check if is a file
if [ -r "$3" ]; then # Chek is is readable
export SELECT_ISO=$3
else
errorAndExit "Error(Arg 3): \"$3\" is not readable" 1
fi
else
errorAndExit "Error(Arg 3): \"$3\" isn't a regular file" 1
fi
# Check if Arg 3 is an iso file
if [ "${3##*.}" != "iso" ]; then
errorAndExit "Error(Arg 3): you must chose a iso file" 1
fi
2012-02-18 19:07:03 +01:00
# Confirm
if [ "$4" = "-y" ]; then
export CONFIRM=0
else
export CONFIRM=1
fi
#################################(INITIAL-CHECK)#####################################
2011-11-09 22:51:07 +01:00
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
#--------------------------------(INSTALLATION)--------------------------------------
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
2012-02-18 17:56:33 +01:00
export LOGFILE=/var/log/usbinstall.log
2012-01-07 17:55:29 +01:00
set_status "Smonto la chiavetta"
DEVICEISMOUNTED=`dbus_properties_get_bool org.freedesktop.UDisks /org/freedesktop/UDisks/devices/${SELECT_USB/*\/}1 \
org.freedesktop.UDisks.Device DeviceIsMounted`
if [ "$DEVICEISMOUNTED" = "true" ]; then
LANG=C udisks --unmount ${SELECT_USB}1 | grep failed && {
2012-02-09 19:12:11 +01:00
errorAndExit "Error: fail to umount the usb key (${SELECT_USB}1)"
}
fi
2012-01-07 17:55:29 +01:00
set_pbar 2
2012-02-18 19:07:03 +01:00
# Prima di formattare :P
if [ "$CONFIRM" = "1" ]; then
echo "Warning: You are going to format this device ($1). Are you sure to continue? [yes/no]"
read KEY
case "$KEY" in
yes)
;;
*)
errorAndExit "Installation aborted"
;;
esac
fi
2012-01-07 17:55:29 +01:00
2012-02-18 19:07:03 +01:00
set_status "Partizionamento della chiavetta in corso..."
echo "Partitioning $SELECT_USB..."
2012-02-18 17:56:33 +01:00
sudo fdisk $SELECT_USB < /usr/share/openmamba/usbinstall/fdisk_template &>$LOGFILE
2012-02-09 19:12:11 +01:00
if [ "$?" != "0" ]; then
2012-02-18 17:56:33 +01:00
errorAndExit "Error: \"fdisk\" exit with error (see $LOGFILE)"
2012-02-09 19:12:11 +01:00
fi
2012-01-07 17:55:29 +01:00
set_pbar 5
2012-02-09 19:12:11 +01:00
2012-02-18 19:07:03 +01:00
set_status "Formattazione della chiavetta in corso..."
echo "Formatting..."
2012-02-18 17:56:33 +01:00
sudo mkfs.ext3 ${SELECT_USB}1 -L openmamba_live &>>$LOGFILE
if [ "$?" != "0" ]; then
errorAndExit "Error: \"mkfs.ext3\" exit with error (see $LOGFILE)"
fi
2012-02-18 19:07:03 +01:00
LANG=C udisks --mount ${SELECT_USB}1 &>>$LOGFILE
2012-02-18 17:56:33 +01:00
if [ "$?" != "0" ]; then
errorAndExit "Error: \"udisks --mount\" exit with error (see $LOGFILE)"
fi
2012-01-07 17:55:29 +01:00
set_pbar 30
# Riceve la mountdir della chiavetta da dbus
MOUNTDIR=`dbus_properties_get_stringlist org.freedesktop.UDisks /org/freedesktop/UDisks/devices/${SELECT_USB/*\/}1 \
2012-01-07 17:55:29 +01:00
org.freedesktop.UDisks.Device DeviceMountPaths`
2012-02-18 19:07:03 +01:00
if [ ! -d "$MOUNTDIR" ]; then
errorAndExit "Error: unable to find usb mount path"
fi
2012-01-07 17:55:29 +01:00
set_status "Estrazione del file di boot in corso..."
2012-01-07 18:14:13 +01:00
pushd $MOUNTDIR >/dev/null
2012-01-07 17:55:29 +01:00
sudo chmod 777 ./
gunzip -c $SELECT_BOOT | cpio -i
set_pbar 38
set_status "Scrittura del bootloader in corso..."
sudo extlinux --install boot
sudo install-mbr -e 1 $SELECT_USB
set_pbar 45
set_status "Copia del file iso di openmamba in corso..."
# Copy code
ORIG_SIZE=`stat -c %s $SELECT_ISO`
cp $SELECT_ISO ./ &
DEST_SIZE=0
DEST_FILE=./"`basename $SELECT_ISO`"
2012-01-07 17:55:29 +01:00
while [ $ORIG_SIZE -gt $DEST_SIZE ]; do
if [ -e $DEST_FILE ]; then
DEST_SIZE=$(stat -c %s $DEST_FILE)
2012-01-07 17:55:29 +01:00
percentuale=$((45 + ( 50 * $DEST_SIZE ) / $ORIG_SIZE ))
set_pbar $percentuale
fi
sleep 2
2012-01-07 17:55:29 +01:00
done
ln -fs `basename $SELECT_ISO` ./openmamba-live.iso
2012-01-07 18:14:13 +01:00
popd >/dev/null
2012-01-07 17:55:29 +01:00
set_pbar 98
set_status "Smonto la chiavetta"
LANG=C udisks --unmount ${SELECT_USB}1 | grep failed && {
2012-01-07 17:55:29 +01:00
echo "Error: fail to umount the usb key (${SELECT_USB}1)"
exit 2
}
set_pbar 100
#################################(INSTALLATION)######################################