Skip to content

Commit

Permalink
ns-install: halt on completion (#331)
Browse files Browse the repository at this point in the history
* halt instead of reboot after installation
* use lsblk to list available disks
* skip 0 sized disk (empty sd card readers)
* copy only the size of source img
  • Loading branch information
filippocarletti authored Feb 13, 2024
1 parent 3a7209d commit 1b1a9ff
Showing 1 changed file with 40 additions and 75 deletions.
115 changes: 40 additions & 75 deletions files/usr/sbin/ns-install
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
#!/bin/bash
#
# Copyright (C) 2022 Nethesis S.r.l.
# Copyright (C) 2022-2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-2.0-only
#

set -e

function list_disks {
find /dev/ -name 'sd[a-z]' -o -name 'nvme[0-9]' -o -name 'hd[a-z]' -o -name 'vd[a-z]' -o -name 'xvd[a-z]' -o -name 'mmcblk[0-9]' | sort | uniq
}

function color_green {
echo -ne '\e[32m'$1'\e[0m'
}
function color_white {
echo -ne '\e[97m'$1'\e[0m'
}
function wrong_command {
echo -e '\e[31m'"Wrong option."'\e[0m';
}

function install() {
# Find boot device and umount it
part=$(mount | grep boot | uniq | awk '{print $1}')
device=${part::-1}
umount /boot
umount /boot
umount /overlay
umount /rom
sleep 1
echo "Writing image ... "
dd if=$device of=$1 bs=1M iflag=fullblock conv=fsync 2>/dev/null
}
white=$'\033[0;97m'
green=$'\033[0;32m'

function print_help {
echo "Usage: $0 [-t <device>] -f"
Expand All @@ -41,10 +17,10 @@ function print_help {
}

function check {
disk=$1
if [ $(grep "${disk##*/}" /proc/partitions | wc -l) -gt 1 ]; then
echo -e "Multiple partitions found on $(color_white $disk), all content on device will be deleted!"
read -rp "$(color_white 'Do you want to proceed? (y/N) ')" force
disk=$(basename $1)
if [ $(grep -c $disk /proc/partitions) -gt 1 ]; then
echo -e "Multiple partitions found on $1, all content on device will be deleted!"
read -rp "Do you want to proceed? (y/N) " force
case $force in
[yY] ) return 0;;
* ) echo "Aborted"; return 1;;
Expand All @@ -53,17 +29,25 @@ function check {
return 0
}

function bytes_to_h() {
echo $1 | awk 'function human(x) {
x=x*512
s=" B KiB MiB GiB TiB EiB PiB YiB ZiB"
while (x>=1024 && length(s)>1)
{x/=1024; s=substr(s,5)}
s=substr(s,1,4)
xf=(s==" B ")?"%3d":"%8.2f"
return sprintf( xf"%s\n", x, s)
}
{gsub(/^[0-9]+/, human($1)); print}'
function install() {
# Find boot device and umount it
part=$(mount | grep boot | uniq | awk '{print $1}')
device=${part::-1}
if [ $device == $1 ]; then echo "Error: can't install on source disk" >&2 ; exit 1; fi
umount /boot
umount /boot
umount /overlay
umount /rom

sleep 1
count=$(parted -sm $device print 2>/dev/null | grep "^2:" | cut -d: -f3 | sed "s/MB//")
echo "Writing image... "
dd if=$device of=$1 bs=1M count=$count
sync
echo 3 > /proc/sys/vm/drop_caches
sleep 1
echo "Installation complete. Halting... "
halt
}


Expand All @@ -79,51 +63,32 @@ done

# select existing disks
if [ -z "$target" ]; then
echo "Select target disk:"
echo "Select target disk (or press Enter to exit):"
counter=1
for disk in $(list_disks); do
model=$(cat /sys/class/block/${disk:5}/device/model 2>/dev/null || echo '')
vendor=$(cat /sys/class/block/${disk:5}/device/vendor 2>/dev/null || echo '')
if [ ! -z "$model" ] && [ -z "$vendor" ]; then
model="($vendor: $model)"
fi
size=$(bytes_to_h $(cat /sys/class/block//${disk:5}/device/block//${disk:5}/size))
if grep -q $disk /proc/mounts; then
used='used'
else
used='free'
fi
echo -n "$(color_green "${counter})" ) $(color_white ${disk:5}) $size $used $model"
counter=$((counter+1))
echo
done;
read -rp "$(color_white 'Your choice: ')" choice
if [ ${choice:-1} -gt 0 ] && [ ${choice} -lt ${counter} ]; then
target=$(list_disks | awk 'NR=='$choice'{ print; }')
disks="$(lsblk -nlo NAME,SIZE,TYPE,MODEL | awk '$3 ~ /^disk/ && $1 !~ /ram/ && $2 !~ /^0B$/')"
while read -r disk size type model; do
used=$(grep -q $disk /proc/mounts && echo "used" || echo "free")
echo -e "${green}${counter}) ${white}${disk} $size $used $model"
counter=$((counter+1))
done <<< "$disks"

read -rp "Your choice: " choice
if [ ${choice:-0} -gt 0 ] && [ ${choice} -lt ${counter} ]; then
target=$(echo "$disks" | awk 'NR=='$choice'{ print $1; }')
target="/dev/${target}"
else
echo "Aborted: invalid choice"
exit 1
fi
fi

if [ -b $target ]; then
if [ -b "${target}" ]; then
if [ "$force" -eq 1 ]; then
install $target
else
check $target && install $target
if [ $? -eq 0 ]; then
echo -e "Installation completed"
cp /sbin/reboot /tmp
sync
sleep 1
reset
read -rp "$(color_white 'Remove install medium and press enter to reboot')" reboot;
/tmp/reboot
fi
fi
else
echo -e "Target device not found: '$target'"
error=1
exit 1
fi

exit ${error:-0}

0 comments on commit 1b1a9ff

Please sign in to comment.