nand-sata-install formatting cleanup

This commit is contained in:
zador-blood-stained 2016-10-22 14:03:24 +03:00
parent 731afd99c2
commit b5b23b4d63

View File

@ -19,56 +19,54 @@ CWD="/usr/lib/nand-sata-install"
EX_LIST="${CWD}/exclude.txt"
nanddevice="/dev/nand"
# read in board info
[[ -f /etc/armbian-release ]] && source /etc/armbian-release || read ID </run/machine.id
[[ -f /etc/armbian-release ]] && source /etc/armbian-release
backtitle="Armbian for $BOARD_NAME install script, http://www.armbian.com | Author: Igor Pecovnik"
title="NAND, eMMC, SATA and USB Armbian installer v""$VERSION"
# exception
if cat /proc/cpuinfo | grep -q 'sun4i'; then DEVICE_TYPE="a10"; else DEVICE_TYPE="a20"; fi
BOOTLOADER="${CWD}/${DEVICE_TYPE}/bootloader"
# find targets: NAND, EMMC, SATA
nandcheck=$(ls -l /dev/ | grep -w 'nand' | awk '{print $NF}'); [[ -n $nandcheck ]] && nandcheck="/dev/$nandcheck"
emmccheck=$(ls -l /dev/ | grep -w 'mmcblk[1-9]' | awk '{print $NF}'); [[ -n $emmccheck ]] && emmccheck="/dev/$emmccheck"
nandcheck=$(ls -l /dev/ | grep -w 'nand' | awk '{print $NF}');
[[ -n $nandcheck ]] && nandcheck="/dev/$nandcheck"
emmccheck=$(ls -l /dev/ | grep -w 'mmcblk[1-9]' | awk '{print $NF}');
[[ -n $emmccheck ]] && emmccheck="/dev/$emmccheck"
satacheck=$(cat /proc/partitions | grep 'sd' | awk '{print $NF}')
# Create boot and root file system $1 = boot, $2 = root (Example: create_armbian "/dev/nand1" "/dev/sda3")
create_armbian() {
create_armbian()
{
# create mount points, mount and clean
sync && mkdir -p /mnt/bootfs /mnt/rootfs
[ -n "$2" ] && mount $2 /mnt/rootfs
[ -n "$1" ] && mount $1 /mnt/bootfs
[[ -n $2 ]] && mount $2 /mnt/rootfs
[[ -n $1 ]] && mount $1 /mnt/bootfs
rm -rf /mnt/bootfs/* /mnt/rootfs/*
# sata root part
# UUID=xxx...
satauuid=$(blkid -o export $2 | grep -w UUID)
# SD card boot part
# UUID=xxx...
sduuid=$(blkid -o export /dev/mmcblk0p1 | grep -w UUID)
# calculate usage and see if it fits on destination
USAGE=$(df -BM | grep ^/dev | head -1 | awk '{print $3}' | tr -cd '[0-9]. \n')
DEST=$(df -BM | grep ^/dev | grep /mnt/rootfs | awk '{print $4}' | tr -cd '[0-9]. \n')
if [ $USAGE -gt $DEST ]; then
if [[ $USAGE -gt $DEST ]]; then
dialog --title "$title" --backtitle "$backtitle" --colors --infobox\
"\n\Z1Partition too small.\Zn Needed: $USAGE Mb Avaliable: $DEST Mb" 5 60
umountdevice "$1"; umountdevice "$2"
exit 1
fi
if [[ "$1" == *nand* ]]; then
if [[ $1 == *nand* ]]; then
# creating nand boot. Copy precompiled uboot
rsync -aqc $BOOTLOADER/* /mnt/bootfs
fi
# count files is needed for progress bar
dialog --title "$title" --backtitle "$backtitle" --infobox "\n Counting files ... few seconds." 5 40
TODO=$(rsync -ahvrltDn --delete --stats --exclude-from=$EX_LIST / /mnt/rootfs | grep "Number of files:"|awk '{print $4}' | tr -d '.,')
@ -85,7 +83,7 @@ create_armbian() {
sed -e 's,'"$root_partition"','"$2"',g' -i /mnt/rootfs/etc/fstab
# creating fstab, kernel and boot script for NAND partition
if [[ "$1" == *nand* ]]; then
if [[ $1 == *nand* ]]; then
REMOVESDTXT="and remove SD to boot from NAND"
sed -i '/boot/d' /mnt/rootfs/etc/fstab
echo "$1 /boot vfat defaults 0 0" >> /mnt/rootfs/etc/fstab
@ -93,16 +91,18 @@ create_armbian() {
mkimage -A arm -O linux -T kernel -C none -a "0x40008000" -e "0x40008000" -n "Linux kernel" -d \
/boot/zImage /mnt/bootfs/uImage >/dev/null 2>&1
cp /boot/script.bin /mnt/bootfs/
cat > /mnt/bootfs/uEnv.txt <<EOF
console=ttyS0,115200
root=$2 rootwait
extraargs="console=tty1 hdmi.audio=EDID:0 disp.screen0_output_mode=EDID:0 consoleblank=0 loglevel=1"
EOF
cat <<-EOF > /mnt/bootfs/uEnv.txt
console=ttyS0,115200
root=$2 rootwait
extraargs="console=tty1 hdmi.audio=EDID:0 disp.screen0_output_mode=EDID:0 consoleblank=0 loglevel=1"
EOF
sync
[[ $DEVICE_TYPE = "a20" ]] && echo "machid=10bb" >> /mnt/bootfs/uEnv.txt
[[ $DEVICE_TYPE = a20 ]] && echo "machid=10bb" >> /mnt/bootfs/uEnv.txt
# ugly hack becouse we don't have sources for A10 nand uboot
if [[ "${ID}" == "Cubieboard" || "${BOARD_NAME}" == "Cubieboard" || "${ID}" == "Lime A10" || "${BOARD_NAME}" == "Lime A10" ]]; then
if [[ $ID == Cubieboard || $BOARD_NAME == Cubieboard || $ID == "Lime A10" || $BOARD_NAME == "Lime A10" ]]; then
cp /mnt/bootfs/uEnv.txt /mnt/rootfs/boot/uEnv.txt
cp /mnt/bootfs/script.bin /mnt/rootfs/boot/script.bin
cp /mnt/bootfs/uImage /mnt/rootfs/boot/uImage
@ -112,27 +112,27 @@ EOF
tune2fs -O ^has_journal /dev/nand2 >/dev/null 2>&1
e2fsck -f /dev/nand2 >/dev/null 2>&1
elif [[ "$2" == "$emmccheck""p1" || "$1" == "$emmccheck""p1" ]]; then
elif [[ $2 == ${emmccheck}p1 || $1 == ${emmccheck}p1 ]]; then
# fix that we can have one exlude file
cp -R /boot/ /mnt/bootfs
# eMMC install
sed -e 's,root='"$root_partition"',root='"$emmcuuid"',g' -i /mnt/bootfs/boot/boot.cmd
# eMMC install
sed -e 's,root='"$root_partition"',root='"$emmcuuid"',g' -i /mnt/bootfs/boot/boot.cmd
mkimage -C none -A arm -T script -d /mnt/bootfs/boot/boot.cmd /mnt/bootfs/boot/boot.scr >/dev/null 2>&1 || (echo "Error"; exit 0)
# fstab adj
sed -e 's,'"$2"','"$emmcuuid"',g' -i /mnt/rootfs/etc/fstab
sed -e 's,'"$2"','"$emmcuuid"',g' -i /mnt/rootfs/etc/fstab
if [[ $(type -t write_uboot_platform) != function ]]; then
echo "Error: no u-boot package found, exiting"
exit -1
fi
write_uboot_platform $DIR $emmccheck
elif [[ -f /boot/boot.cmd ]]; then
sed -e 's,root='"$root_partition"',root='"$satauuid"',g' -i /boot/boot.cmd
sed -e 's,root='"$root_partition"',root='"$satauuid"',g' -i /boot/boot.cmd
mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr >/dev/null 2>&1 || (echo "Error"; exit 0)
mkdir -p /mnt/rootfs/media/mmc/boot
if ! grep -q "/boot" /mnt/rootfs/etc/fstab; then # in two partition setup
@ -147,13 +147,13 @@ EOF
umountdevice "/dev/sda"
} # create_armbian
# Accept device as parameter: for example /dev/sda unmounts all their mounts
umountdevice() {
if [ -n "$1" ]; then
device=$1;
umountdevice()
{
if [[ -n $1 ]]; then
device=$1;
for n in ${device}*; do
if [ "${device}" != "$n" ]; then
if [[ $device != "$n" ]]; then
if mount|grep -q ${n}; then
umount -l $n >/dev/null 2>&1
fi
@ -162,9 +162,9 @@ umountdevice() {
fi
} # umountdevice
# Recognize root filesystem
recognize_root() {
recognize_root()
{
# replace with PARTUUID approach parsing /proc/cmdline when ready
local device="/dev/"$(lsblk -idn -o NAME | grep mmcblk0)
local partitions=$(($(fdisk -l $device | grep $device | wc -l)-1))
@ -172,19 +172,19 @@ recognize_root() {
local root_device=$(mountpoint -d /)
for file in /dev/* ; do
local current_device=$(printf "%d:%d" $(stat --printf="0x%t 0x%T" $file))
if [ $current_device = $root_device ]; then
if [[ $current_device = $root_device ]]; then
root_partition=$file
break;
fi
done
} # recognize_root
# Formatting NAND - no parameters. Fixed solution.
formatnand() {
formatnand()
{
[[ ! -e /dev/nand ]] && echo "NAND error" && exit 0
dialog --title "$title" --backtitle "$backtitle" --infobox "\nFormating ... up to one minute." 5 40
if [[ "$DEVICE_TYPE" = "a20" ]]; then
if [[ $DEVICE_TYPE = a20 ]]; then
(echo y;) | sunxi-nand-part -f a20 /dev/nand 65536 'bootloader 65536' 'linux 0' >/dev/null 2>&1
else
(echo y;) | sunxi-nand-part -f a10 /dev/nand 65536 'bootloader 65536' 'linux 0' >/dev/null 2>&1
@ -193,9 +193,9 @@ formatnand() {
mkfs.ext4 /dev/nand2 >/dev/null 2>&1
} # formatnand
# Formatting eMMC [device] example /dev/mmcblk1
formatemmc() {
formatemmc()
{
# deletes all partitions
dialog --title "$title" --backtitle "$backtitle" --infobox "\n Formating eMMC ... one moment." 5 40
dd bs=1 seek=446 count=64 if=/dev/zero of=$1 >/dev/null 2>&1
@ -203,8 +203,8 @@ formatemmc() {
# to other media 'of the same size' (one sector less and cloning will fail)
QUOTED_DEVICE=$(echo "${1}" | sed 's:/:\\\/:g')
CAPACITY=$(parted ${1} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {printf (\"%0d\", \$2 / ( 1024 / \$4 ))}")
if [ ${CAPACITY} -lt 4000000 ]; then
if [[ $CAPACITY -lt 4000000 ]]; then
# Leave 2 percent unpartitioned when eMMC size is less than 4GB (unlikely)
LASTSECTOR=$(( 32 * $(parted ${1} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {printf (\"%0d\", ( \$2 * 98 / 3200))}") -1 ))
else
@ -220,20 +220,20 @@ formatemmc() {
emmcuuid=$(blkid -o export $1"p1" | grep -w UUID)
} # formatemmc
# formatting SATA/USB [device] example /dev/sda3
formatsata() {
formatsata()
{
dialog --title "$title" --backtitle "$backtitle" --infobox "\nFormating ... up to one minute." 5 40
mkfs.ext4 $1 >/dev/null 2>&1
tune2fs $1 -o journal_data_writeback >/dev/null 2>&1
} # formatsata
# choose target SATA/USB partition.
checksatatarget() {
checksatatarget()
{
IFS=" "
SataTargets=$(awk '/sd/ {print "/dev/"$4}' </proc/partitions | grep -E '[0-9]{1,4}' | nl | xargs echo -n)
if [[ "$SataTargets" == "" ]]; then
if [[ -z $SataTargets ]]; then
dialog --title "$title" --backtitle "$backtitle" --colors --infobox \
"\n\Z1There are no avaliable partitions. Please create them.\Zn" 5 60
exit 1
@ -242,57 +242,58 @@ checksatatarget() {
SataOptions=($SataTargets)
SataCmd=(dialog --title "Select destination:" --backtitle "$backtitle" --menu "\n$infos" 10 60 16)
SataChoices=$("${SataCmd[@]}" "${SataOptions[@]}" 2>&1 >/dev/tty)
if [ $? -ne 0 ]; then exit 1; fi
[[ $? -ne 0 ]] && exit 1
SDA_ROOT_PART=${SataOptions[(2*$SataChoices)-1]}
} # checksatatarget
# show warning [TEXT]
ShowWarning() {
ShowWarning()
{
dialog --title "$title" --backtitle "$backtitle" --cr-wrap --colors --yesno " \Z1$(toilet -f mono12 WARNING)\Zn\n$1" 17 74
if [ $? -ne 0 ]; then exit 1; fi
[[ $? -ne 0 ]] && exit 1
} # ShowWarning
main() {
main()
{
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# This tool must run under root
if [[ ${EUID} -ne 0 ]]; then
if [[ $EUID -ne 0 ]]; then
echo "This tool must run as root. Exiting ..."
exit 1
fi
# Check if we run it from SD card
if [[ "$(sed -n 's/^DEVNAME=//p' /sys/dev/block/$(mountpoint -d /)/uevent)" != mmcblk* ]]; then
if [[ $(sed -n 's/^DEVNAME=//p' /sys/dev/block/$(mountpoint -d /)/uevent) != mmcblk* ]]; then
dialog --title "$title" --backtitle "$backtitle" --colors --infobox "\n\Z1This tool must run from SD-card!\Zn" 5 42
exit 1
fi
#recognize_root
root_partition=$(cat /proc/cmdline | sed -e 's/^.*root=//' -e 's/ .*$//')
IFS="'"
options=()
if [[ -n "$emmccheck" ]]; then
if [[ -n $emmccheck ]]; then
ichip="eMMC";
dest_boot=$emmccheck"p1"
dest_root=$emmccheck"p1"
else
else
ichip="NAND"
dest_boot="/dev/nand1"
dest_root="/dev/nand2"
fi
[[ -n "$nandcheck" || -n "$emmccheck" ]] && options=(${options[@]} 1 'Boot from '$ichip' - system on '$ichip)
[[ ( -n "$nandcheck" || -n "$emmccheck" ) && -n "$satacheck" ]] && options=(${options[@]} 2 'Boot from '$ichip' - system on SATA or USB')
[[ -n "$satacheck" ]] && options=(${options[@]} 3 'Boot from SD - system on SATA or USB')
[[ -n $nandcheck || -n $emmccheck ]] && options=(${options[@]} 1 'Boot from '$ichip' - system on '$ichip)
[[ ( -n $nandcheck || -n $emmccheck ) && -n $satacheck ]] && options=(${options[@]} 2 'Boot from '$ichip' - system on SATA or USB')
[[ -n $satacheck ]] && options=(${options[@]} 3 'Boot from SD - system on SATA or USB')
[[ ${#options[@]} -eq 0 || "$root_partition" == "$emmcuuid" || "$root_partition" == "/dev/nand2" ]] && dialog --title "$title" --backtitle "$backtitle" --colors --infobox "\n\Z1There are no targets. Please check your drives.\Zn" 5 60 && exit 1
cmd=(dialog --title "Choose an option:" --backtitle "$backtitle" --menu "\nCurrent root: $root_partition \n \n" 14 60 7)
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
if [ $? -ne 0 ]; then exit 1; fi
[[ $? -ne 0 ]] && exit 1
for choice in $choices
do
@ -301,28 +302,28 @@ main() {
title="$ichip install"
command="Power off"
ShowWarning "This script will erase your $ichip. Continue?"
if [[ -n "$emmccheck" ]]; then
if [[ -n $emmccheck ]]; then
umountdevice "$emmccheck"
formatemmc "$emmccheck"
else
else
umountdevice "/dev/nand"
formatnand
fi
formatnand
fi
create_armbian "$dest_boot" "$dest_root"
;;
2)
title="$ichip boot / SATA root install"
command="Power off"
checksatatarget
checksatatarget
ShowWarning "This script will erase your $ichip and $SDA_ROOT_PART. Continue?"
if [[ -n "$emmccheck" ]]; then
if [[ -n $emmccheck ]]; then
umountdevice "$emmccheck"
formatemmc "$emmccheck"
else
umountdevice "/dev/nand"
formatnand
formatnand
fi
umountdevice "${SDA_ROOT_PART//[0-9]*/}"
umountdevice "${SDA_ROOT_PART//[0-9]*/}"
formatsata "$SDA_ROOT_PART"
create_armbian "$dest_boot" "$SDA_ROOT_PART"
;;
@ -338,7 +339,7 @@ main() {
done
dialog --title "$title" --backtitle "$backtitle" --yes-label "$command" --no-label "Exit" --yesno "\nAll done. $command $REMOVESDTXT" 7 60
if [ $? -eq 0 ]; then "$(echo ${command,,} | sed 's/ //')"; fi
[[ $? -eq 0 ]] && "$(echo ${command,,} | sed 's/ //')"
} # main
main "$@"