usbinstall/src/usbinstall.sh

415 lines
13 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-2013 Silvan Calarco - silvan.calarco@mambasoft.it
2012-01-07 17:55:29 +01:00
# 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-02-21 00:17:51 +01:00
TEXTDOMAINDIR=/usr/share/locale/
TEXTDOMAIN=usbinstall
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-02-20 20:06:52 +01:00
dbus-send --system --print-reply --dest=org.openmamba.usbinstall /org/openmamba/usbinstall org.openmamba.usbinstall.Set_progressBar int32:$1 &>/dev/null
if [ "$?" != "0" ]; then
[ "$CPPID" ] && kill -9 $CPPID
exit 1
fi
2012-01-07 20:52:28 +01:00
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-02-20 20:06:52 +01:00
dbus-send --system --print-reply --dest=org.openmamba.usbinstall /org/openmamba/usbinstall org.openmamba.usbinstall.Set_description "string:$1" &>/dev/null
if [ "$?" != "0" ]; then
[ "$CPPID" ] && kill -9 $CPPID
exit 1
fi
fi
}
# Terminate the frontend (only if $DBUS_USBINSTALL = 1)
exit_frontend()
{
if [ "$DBUS_USBINSTALL" == "1" ]; then
dbus-send --system --print-reply --dest=org.openmamba.usbinstall /org/openmamba/usbinstall org.openmamba.usbinstall.Exit "string:$1" &>/dev/null
if [ "$?" != "0" ]; then
[ "$CPPID" ] && kill -9 $CPPID
exit 1
fi
2012-01-07 20:52:28 +01:00
fi
2011-11-09 22:51:07 +01:00
}
2012-02-20 20:06:52 +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
2012-02-20 20:06:52 +01:00
exit_frontend "$1" # Termina il frontend con codice di errore 1
exit 1
}
function usage()
{
2012-02-21 00:17:51 +01:00
echo $"usbinstall: install openmamba into your usb key!"
echo -e "\t"$"Usage: sudo usbinstall Arg1 Arg2 Arg3 [-y]"
echo -e "\t"$"Arg 1: Usb device path (e.g. /dev/sdc)"
echo -e "\t"$"Arg 2: Cpio.gz file (e.g. ~/openmamba-bootusb-it-milestone2-2.0pre8.i586.cpio.gz)"
echo -e "\t"$"Arg 3: ISO file (e.g. ~/openmamba-livecd-it-milestone2-2.0pre8.i586.iso)"
2012-02-21 00:17:51 +01:00
echo -e "\t"$"-y: install without asking confirmation [Be careful!]"
}
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
#=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
2012-02-20 20:06:52 +01:00
# Confirm
if [ "$4" = "-y" ]; then
CONFIRM=0
elif [ "$4" = "-d" ]; then
CONFIRM=0
DBUS_USBINSTALL=1 # First set this
else
CONFIRM=1
fi
# Help
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
exit 0
fi
# Check if usbinstall have root permission
if [ "`id -u`" != 0 ]; then
2012-02-21 00:17:51 +01:00
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
2012-02-21 00:17:51 +01:00
echo $"Warning: you haven't choosen a removable device ($1). Are you sure to continue? [yes/no]"
read KEY
case "$KEY" in
2012-02-21 00:17:51 +01:00
$yes)
export SELECT_USB=$1
;;
*)
2012-02-21 00:17:51 +01:00
errorAndExit $"Installation aborted"
;;
esac
fi
else
2012-02-21 00:17:51 +01:00
errorAndExit $"Error(Arg 1): \"$1\" isn't a block device" 1
fi
# Check if Arg 2 is valid (it can be null)
if ["$2" ]; then
if [ -r "$2" ]; then # Chek is is readable
# 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
export SELECT_BOOT=$2
else
errorAndExit $"Error(Arg 2): \"$2\" is not readable" 1
fi
fi
# Check if Arg 3 is valid
if [ -r "$3" ]; then # Chek is is readable
# Check if Arg 3 is an iso file
if [ "${3##*.}" != "iso" ]; then
errorAndExit "Error(Arg 3): you must chose a ISO (.iso) file" 1
fi
SELECT_ISO=`readlink -f $3`
else
errorAndExit "Error(Arg 3): \"$3\" is not readable" 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-02-20 20:06:52 +01:00
trap "[ \"$CPPID\" ] && kill -9 $CPPID" HUP INT QUIT ABRT KILL TERM
2012-02-21 00:17:51 +01:00
set_status $"Unmount usb device"
echo $"Unmount usb device"
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-21 00:17:51 +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
2012-02-21 00:17:51 +01:00
echo $"Warning: You are going to format this device ($1). Are you sure to continue? [yes/no]"
2012-02-18 19:07:03 +01:00
read KEY
case "$KEY" in
$"yes"|"Y"|"y")
2012-02-18 19:07:03 +01:00
;;
*)
2012-02-21 00:17:51 +01:00
errorAndExit $"Installation aborted"
2012-02-18 19:07:03 +01:00
;;
esac
fi
2012-01-07 17:55:29 +01:00
2012-02-21 00:17:51 +01:00
set_status $"Partitioning $SELECT_USB..."
echo $"Partitioning $SELECT_USB..."
# Start from a new partition table
sudo parted -s $SELECT_USB mktable msdos &>>$LOGFILE
if [ "$?" != "0" ]; then
errorAndExit $"Error:"" \"parted\" "$"exit with error (see $LOGFILE)"
fi
# EFI partition
sudo parted -s -a optimal -- $SELECT_USB mkpart primary fat32 0 20M &>>$LOGFILE
2012-02-09 19:12:11 +01:00
if [ "$?" != "0" ]; then
errorAndExit $"Error:"" \"parted\" "$"exit with error (see $LOGFILE)"
2012-02-09 19:12:11 +01:00
fi
# openmamba partition
sudo parted -s -a optimal -- $SELECT_USB mkpart primary ext4 20M -1 &>>$LOGFILE
if [ "$?" != "0" ]; then
errorAndExit $"Error:"" \"parted\" "$"exit with error (see $LOGFILE)"
fi
# boot flag on openmamba partition
sudo parted -s $SELECT_USB set 2 boot on &>>$LOGFILE
if [ "$?" != "0" ]; then
errorAndExit $"Error:"" \"parted\" "$"exit with error (see $LOGFILE)"
fi
2012-01-07 17:55:29 +01:00
set_pbar 5
2012-02-09 19:12:11 +01:00
2012-02-21 00:17:51 +01:00
set_status $"Formatting $SELECT_USB..."
echo $"Formatting $SELECT_USB..."
sudo mkfs.vfat ${SELECT_USB}1 -n EFI &>>$LOGFILE
if [ "$?" != "0" ]; then
errorAndExit $"Error:"" \"mkfs.vfat\" "$"exit with error (see $LOGFILE)"
fi
sudo mkfs.ext4 ${SELECT_USB}2 -L openmamba_live &>>$LOGFILE
2012-02-18 17:56:33 +01:00
if [ "$?" != "0" ]; then
errorAndExit $"Error:"" \"mkfs.ext4\" "$"exit with error (see $LOGFILE)"
2012-02-18 17:56:33 +01:00
fi
2012-02-20 20:06:52 +01:00
sleep 1
LANG=C udisks --mount ${SELECT_USB}1 &>>$LOGFILE
if [ "$?" != "0" ]; then
errorAndExit $"Error:"" \"udisks --mount\" "$"exit with error (see $LOGFILE)"
fi
LANG=C udisks --mount ${SELECT_USB}2 &>>$LOGFILE
2012-02-18 17:56:33 +01:00
if [ "$?" != "0" ]; then
2012-02-21 00:17:51 +01:00
errorAndExit $"Error:"" \"udisks --mount\" "$"exit with error (see $LOGFILE)"
2012-02-18 17:56:33 +01:00
fi
2012-01-07 17:55:29 +01:00
set_pbar 30
# Riceve la mountdir della chiavetta da dbus
MOUNTDIR_EFI=`dbus_properties_get_stringlist org.freedesktop.UDisks /org/freedesktop/UDisks/devices/${SELECT_USB/*\/}1 \
org.freedesktop.UDisks.Device DeviceMountPaths`
if [ ! -d "$MOUNTDIR_EFI" ]; then
errorAndExit $"Error: unable to found usb mount path"
fi
# create EFI grub 32 and 64 bit images
mkdir -p $MOUNTDIR_EFI/EFI/BOOT/
grub-mkimage -o $MOUNTDIR_EFI/EFI/BOOT/bootx64.efi -O x86_64-efi -p /efi/boot \
part_gpt part_msdos ntfs ntfscomp hfsplus fat ext2 normal chain boot linux echo \
help gfxterm gettext png efi_gop efi_uga search search_label search_fs_uuid || {
errorAndExit $"Error: unable to create GRUB x86_64-efi image"
}
# 32bit EFI optional because untested and curently missing in x86_64 arch
[ -d /usr/lib/grub/i386-efi ] && \
grub-mkimage -o $MOUNTDIR_EFI/EFI/BOOT/bootia32.efi -O i386-efi -p /efi/boot \
part_gpt part_msdos ntfs ntfscomp hfsplus fat ext2 normal chain boot linux echo \
help gfxterm gettext png efi_gop efi_uga search search_label search_fs_uuid || {
errorAndExit $"Error: unable to create GRUB i386-efi image"
}
2012-01-07 17:55:29 +01:00
# Riceve la mountdir della chiavetta da dbus
MOUNTDIR=`dbus_properties_get_stringlist org.freedesktop.UDisks /org/freedesktop/UDisks/devices/${SELECT_USB/*\/}2 \
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
2012-02-21 00:17:51 +01:00
errorAndExit $"Error: unable to found usb mount path"
2012-02-18 19:07:03 +01:00
fi
2012-01-07 18:14:13 +01:00
pushd $MOUNTDIR >/dev/null
2012-01-07 17:55:29 +01:00
2012-02-21 00:17:51 +01:00
set_status $"Copying ISO..."
echo $"Copying ISO..."
2012-01-07 17:55:29 +01:00
# Copy code
ORIG_SIZE=`stat -c %s $SELECT_ISO`
cp $SELECT_ISO ./ &
2012-02-20 20:06:52 +01:00
export CPPID=$!
2012-01-07 17:55:29 +01:00
DEST_SIZE=0
DEST_FILE=./"`basename $SELECT_ISO`"
2012-02-20 22:27:16 +01:00
ps $CPPID >/dev/null
while [ $? -eq 0 ]; do
if [ -e $DEST_FILE ]; then
DEST_SIZE=$(stat -c %s $DEST_FILE)
percentuale=$((30 + ( 50 * $DEST_SIZE ) / $ORIG_SIZE ))
2012-01-07 17:55:29 +01:00
set_pbar $percentuale
fi
sleep 2
2012-02-20 22:27:16 +01:00
ps $CPPID >/dev/null
2012-01-07 17:55:29 +01:00
done
ln -fs `basename $SELECT_ISO` ./openmamba-live.iso
sudo chmod 777 ./
if [ "$SELECT_BOOT" ]; then
set_status $"Extracting bootusb archive..."
echo $"Extracting bootusb archive..."
gunzip -c $SELECT_BOOT | cpio -i
if [ ! -d "./boot" ]; then
errorAndExit $"Error: Invalid bootusb archive"
fi
else
set_status $"Extracting bootloader data from ISO image..."
echo $"Extracting bootloader data from ISO image..."
ISOMOUNTDIR=`mktemp -d --suffix=.usbinstall`
mount -o loop $SELECT_ISO $ISOMOUNTDIR || {
errorAndExit $"Error: unable to mount ISO image"
}
mkdir boot
cp $ISOMOUNTDIR/boot/{initrd*,vmlinuz*} boot/
cp $ISOMOUNTDIR/boot/isolinux/* boot/
mv boot/isolinux.cfg boot/extlinux.conf
sed -i "s|root=[^[:space:]]*|root=live:/openmamba_live/openmamba-live.iso|" boot/extlinux.conf
cp /usr/share/syslinux/{libcom32,libutil,vesamenu}.c32 boot/ || {
errorAndExit $"Error: can't copy some files to /boot directory"
}
rm -f boot/isolinux*
umount $ISOMOUNTDIR
rmdir $ISOMOUNTDIR
# basic extlinux.conf -> grub.cfg conversion
echo "search --no-floppy --label --set=root openmamba_live" > $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
while read line; do
case $line in
"MENU LABEL "*) [ "$CURR_BRACKET_OPEN" = "1" ] && echo "}" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
echo "menuentry \"${line/MENU LABEL }\" {" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
CURR_BRACKET_OPEN=1
;;
"kernel "*) CURR_KERNEL="${line/kernel }"
echo "echo \"Loading ${CURR_KERNEL}\"" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
;;
"append "*) set -- ${line/append initrd=}
CURR_INITRD="$1"
CURR_CMDLINE="${line/append initrd=$CURR_INITRD}"
echo "linux ${CURR_KERNEL} ${CURR_CMDLINE}" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
echo "echo \"Loading ${CURR_INITRD}\"" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
echo "initrd ${CURR_INITRD}" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
echo "}" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
CURR_BRACKET_OPEN=0
;;
esac
done < boot/extlinux.conf
[ "$CURR_BRACKET_OPEN" = "1" ] && echo "}" >> $MOUNTDIR_EFI/EFI/BOOT/grub.cfg
fi
set_pbar 85
set_status $"Writing bootloader..."
echo $"Writing bootloader..."
sudo extlinux --install boot
set_pbar 90
2012-01-07 18:14:13 +01:00
popd >/dev/null
2012-02-20 22:27:16 +01:00
sync
set_pbar 95
2012-01-07 17:55:29 +01:00
2012-02-21 00:17:51 +01:00
set_status $"Unmount usb device"
echo $"Unmount usb device"
LANG=C udisks --unmount ${SELECT_USB}1 | grep failed && {
errorAndExit $"Error: fail to umount the usb key (${SELECT_USB}1)"
}
LANG=C udisks --unmount ${SELECT_USB}2 | grep failed && {
errorAndExit $"Error: fail to umount the usb key (${SELECT_USB}2)"
2012-01-07 17:55:29 +01:00
}
# WARNING: calling install-mbr after unmounting to prevent problems of FAT since EFI partition introduction
sudo install-mbr $SELECT_USB
2012-01-07 17:55:29 +01:00
set_pbar 100
2012-02-20 20:06:52 +01:00
exit_frontend ""
2012-02-21 00:17:51 +01:00
echo $"Done!"
#################################(INSTALLATION)######################################