#!/bin/bash # author sudodus alias nio-wiklund at launchpad # # date editor comment # 2015-04-16 sudodus Modified for mktbl # usage if [ "$(whoami)" != "root" ] || ! ( [ "$1" == "root" ] || [ "$1" == "swap" ] ) then echo "Select partition for the advanced OBI level" echo "-------------------------------------------" echo "Usage: sudo $0 " echo "only alternatives:" echo "sudo $0 root" echo "sudo $0 root mktbl" echo "sudo $0 swap" exit fi # variables LC_ALL=C;LANG=C item=0 seltxt= rootpart= swappart= if [ "$2" == "mktbl" ] then partitions="/usr/share/partitions" ## changed else partitions=partitions fi partitionS="Partition Start End Size Type FSys Label" partd1=$(mktemp) partd2=$(mktemp) version=$( grep -i 'obi version' change.log|tail -n1|tr -s ' ' ' '| \ cut -d ' ' -f 5-6) # live drive and mounted partitions ltest0=$(grep -m 1 " / " /etc/mtab|cut -d ' ' -f 1) if [ "$ltest0" == "/cow" ] then ltest1=$(grep " /isodevice" /etc/mtab|cut -c 5-8) livedrive=$ltest1 elif [ "$ltest0" == "rootfs" ] then ltest1=$(grep "iso9660" /etc/mtab|cut -c 5-8) livedrive=$ltest1 elif [ "${ltest0/\/dev\/[^f]d}" != "$ltest0" ] then livedrive=$(grep " / " /etc/mtab|cut -c 5-8) else livedrive="_not_found" fi livedrive="/dev$livedrive" #echo ltest0=$ltest0 #echo ltest1=$ltest1 #echo livedrive=$livedrive booted=$(df|grep ^/dev/|grep ' /$'|cut -d ' ' -f 1) # mark *LIVE # Create a first list from parted parted -ls|grep -v extended|grep -v ^Sector|grep -v ^Partition \ |sed -e 's/ / /' -e 's/boot//' -e 's/hidden//' -e 's/msftdata//' \ -e 's/linux-swap(v1)/swap/' \ -e 's/ *, *//' \ -e 's/primary /prim /' \ -e 's/primary$/prim n.a./' \ -e 's/logical /logic /' \ -e 's/logical$/logic n.a./' \ -e 's/^Model: /Model /' \ -e 's/.*Error:/Error /' \ -e 's/^Disk /Disk /' \ -e s/Number.*/"$partitionS"/ \ > "$partd1" # Create a second (tweaked) list from the first list # if [ "${tj:1:7}" == "Warning" ] || [ "${tj:1:5}" == "Error" ] # then # tj=$(sudo fdisk -lu "$j" 2>/dev/null|grep "$j":|sed s/\,\ .*//) # fi while read line do # echo "$line" # sleep 0.2 if [ "$line" != "" ] then if [ "${line:0:4}" == "Disk" ] then device="${line:11:8}" if [ "$livedrive" == "$device" ] then tmpstr="${line:18:1} Booted from device${line:8} ***LIVE***" else tmpstr="${line:18:1} Device${line:8}" fi line=$tmpstr fi if [ "${line/Error}" != "$line" ] then # echo "ErrorTO 1 2" # echo "12345678901234567890" device="${line:11:8}" # echo "$device" if [ "$device" == "/dev/zra" ] then line="zram zram device found" else tmpstr=$(sudo fdisk -lu "$device" 2>/dev/null|grep "$device": \ |sed -e s/\,\ .*// -e 's/Disk/Device /') tmpstr="${line:18:1} ${tmpstr} - Booted? busy or wiped or bad partition table" line="$tmpstr" fi fi if [ "${line/Warning}" != "$line" ] then echo "${line:0:7} Warning test output" tmpstr="No (wiped?) or bad partition table: Disk ${line/Warning:}" line="$tmpstr" # line=$(sudo fdisk -lu "$j" 2>/dev/null|grep "$j":|sed s/\,\ .*//) fi if [ "${line:0:1}" '>' "0" ] && ( [ "${line:0:1}" '<' "9" ] || [ "${line:0:1}" == "9" ] ) then fsys=$(blkid $device$line:0:2}|grep ' TYPE'|sed -e 's/.*TYPE=//'|cut -d\" -f2) if [ "$fsys" != "" ] then line="${line/n.a./$fsys}" fi label=$(blkid $device$line:0:2}|grep LABEL|sed -e 's/.*LABEL=//'|cut -d\" -f2) # Lines for partitions fixed for > 9 partitions, lines written if [ "${line:1:1}" == " " ] then line="$device${line:0:1} ${line:2} $label" else line="$device${line:0:2}${line:3} $label" fi fi echo "$line" >> "$partd2" fi done < "$partd1" echo "$partitionS" >> "$partd2" #less "$partd2" # Check if there are default partitions partitions=partitions rootpart= swappart= if test -f "$partitions" then read rootpart swappart < "$partitions" if [ $? -ne 0 ] then read rootpart dummy < "$partitions" fi fi #echo "rootpart=$rootpart" #echo "swappart=$swappart" #read -p "checking rootpart and swappart" # Select root or swap partition if [ "$1" == "root" ] || [ "$rootpart" == "" ] then targ="Root" if [ "$rootpart" != "" ] then choice="$rootpart" else choice=1 fi elif [ "$1" == "swap" ] then targ="Swap" if [ "$swappart" != "" ] then choice="$swappart" else choice=1 fi fi # Create the list variables aind and bvar for the dialog screen aind=0 bind=0 while read line do # adev[$aind]="${line:0:9}"# Lines for partitions fixed for > 9 partitions adev[$aind]="${line:0:10}" atxt[$aind]="${line:11}" # echo ${adev[$aind]} # echo ${atxt[$aind]} df|grep "${line:0:10}">/dev/null if [ $? -eq 0 ] then mounted=true umount "${line:0:10}" 2>/dev/null sync df|grep "${line:0:10}">/dev/null if [ $? -eq 0 ] then mounted=true else mounted=false fi else mounted=false fi if [ "${line:0:9}" == "Partition" ] || [ "${line:0:5}" == "Model" ] || \ [ "${line:02:6}" == "Device" ] || [ "${line:02:6}" == "Booted" ] || \ [ "${line:0:4}" == "zram" ] then atxt[$aind]="\Z4\Zb"${atxt[$aind]}"\Zn" fi if [ "${line/9660}" != "$line" ] then adev[$aind]="ISOSYS:${line:7:2}" atxt[$aind]="\Z2"${line:11}"\Zn" fi if [ "${adev[$aind]}" == "$booted" ] then adev[$aind]="MOUNTD:${line:7:2}" atxt[$aind]="\Z2"${line:11}" \Z2\Z5*SYSTEM\Zn" elif $mounted then adev[$aind]="MOUNTD:${line:7:2}" atxt[$aind]="\Z1"${line:11}" \Zn\Z5*MOUNTED\Zn" elif [ "${adev[$aind]}" == "${rootpart}" ] \ || [ "${adev[$aind]}" == "${rootpart} " ] then atxt[$aind]=${line:11}" \Z1\Zb*ROOT\Zn" elif [ "${adev[$aind]}" == "${swappart}" ] \ || [ "${adev[$aind]}" == "${swappart} " ] then atxt[$aind]=${line:11}" \Z1\Zb*SWAP\Zn" fi aind=$(($aind + 1)) done < "$partd2" if [ "$targ" == "Swap" ] then adev[$aind]="n" atxt[$aind]="select to have no swap partition" aind=$(($aind + 1)) fi adev[$aind]="w" atxt[$aind]="wipe previous selection of partitions" aind=$(($aind + 1)) adev[$aind]="q" atxt[$aind]="quit selecting partition" aind=$(($aind + 1)) for (( xi=0; xi < $aind ; xi++ )) do if [ "${adev[$xi]}" == "q" ] then bvar[$bind]="q" else # bvar[$bind]=$(($xi+1)) bvar[$bind]=${adev[$xi]} fi bind=$(($bind + 1)) bvar[$bind]="${atxt[$xi]}" bind=$(($bind + 1)) done mentxt="Available partitions black" if [ "$2" != "mktbl" ] then mentxt="$mentxt \Z4\Zb(set basic OBI level to select a device)\Zn" fi #echo "$aind" #read -p "stop to read aind" # select device - the Advanced button # ans=$(zenity --list --width=640 --height=560 \ # --title "One Button Installer $version - Select partition - the $targ button" \ # --text "Select the $targ partition with the arrow keys, scroll if necessary #$mentxt" \ # --window-icon="/usr/share/icons/zmktbl.png" \ # --ok-label "$targ partition" --cancel-label "Quit" \ # --column="Device" --column="Description" \ # "${bvar[@]}" ) # #echo "after zenity: ans=$ans" ans=$(dialog \ --no-shadow --backtitle "One Button Installer $version - Select partition - the $targ button" \ --title "Select the $targ partition with the arrow keys, scroll if necessary" \ --default-item "$choice" --colors --ok-label "$targ partition" --cancel-label "Quit" \ --menu "$mentxt" \ 0 80 $aind "${bvar[@]}" 3>&1 1>&2 2>&3 3>&- ) #echo "after dialog: ans=$ans" rm "$partd1" "$partd2" # identify selection if [ "${ans:0:4}" == "/dev" ] then if [ "$targ" == "Root" ] then rootpart="$ans" elif [ "$targ" == "Swap" ] then swappart="$ans" fi # select to have no swap partition elif [ "$ans" == "n" ] then echo "$rootpart" > "$partitions" exit # wipe selection and exit elif [ "$ans" == "w" ] then > "$partitions" exit # quit without writing else exit fi # write selection if [ "$rootpart" != "$swappart" ] then echo "$rootpart" "$swappart" > "$partitions" else echo "$rootpart" > "$partitions" fi #echo select-part2 cat "$partitions" #pwd #ls -l "$partitions"