Home > Mobile >  Add device name and size to shell selection script
Add device name and size to shell selection script

Time:06-13

I am writing a shell script to install Arch Linux, currently, I have a very basic disk selection but it does not give enough information, how can I add extra information such as the disk name and size but keep the $DISK variable the same (ie: "/dev/nvme0n1")

select_disk () {
    count=0

    for device in `lsblk -d | tail -n 2 | cut -d" " -f1`; do
    count=$((count 1))
    dev[$count]=$device
    printf '%s: %s\n' "$count" "$device"
    done

    read -rp "Select disk (numbers 1-$count): " selection

    DISK="/dev/${dev[$selection]}"
    echo "$DISK" > /tmp/disk
    echo "Installing on $DISK."
}

Thanks in advance.

CodePudding user response:

You could use the columns format lsblk offers using the -o flag. Also, you can avoid the tail (which I believe is to remove the header) by using the lsblk flag -n which does exactly that. Something like this:

select_disk () {
    count=0
    
    # by default the 'for' loop splits by spaces, change that
    # to split by breaklines by redefining IFS
    IFS=$'\n'

    for device_info in `lsblk -d -n -o NAME,TYPE,SIZE`; do
    count=$((count 1))
    device_name=$(echo $device_info | cut -d" " -f1)
    dev[$count]=$device_name
    printf '%s: %s\n' "$count" "$device_info"
    done

    read -rp "Select disk (numbers 1-$count): " selection

    DISK="/dev/${dev[$selection]}"
    echo "$DISK" > /tmp/disk
    echo "Installing on $DISK."
}

Here is only showed the NAME, TYPE and SIZE, but you can add all the columns you want (separated by a comma). See lsblk -h (the help) for all possible options.

CodePudding user response:

I'm not sure about what you are calling 'disk name' but...

Let's consider, in your 'for;do..done' loop, that $device is your mapped device name (i.e: sda).

You may get the following information:

1)The device label (if exist):

lsblk "/dev/${device}" -dn -o LABEL

2)The device vendor:

lsblk "/dev/${device}" -dn -o VENDOR

3)The device size (in human readable format)

lsblk "/dev/${device}" -dn -o SIZE

and multiple other informations like:

4)device type:

lsblk "/dev/${device}" -dn -o TYPE

5)Hardware block sizes

lsblk "/dev/${device}" -dn -o PHY-SEC,LOG-SEC

6)Linux owners and group assigned to this object

lsblk "/dev/${device}" -dn -o OWNER,GROUP

Or anything in the below list:

NAME KNAME MAJ:MIN FSTYPE MOUNTPOINT LABEL UUID PARTTYPE PARTLABEL PARTUUID PARTFLAGS RA RO RM HOTPLUG MODEL SERIAL SIZE STATE OWNER GROUP MODE ALIGNMENT MIN-IO OPT-IO PHY-SEC LOG-SEC ROTA SCHED RQ-SIZE TYPE DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO WSAME WWN RAND PKNAME HCTL TRAN SUBSYSTEMS REV VENDOR ZONED

hope it helps.

  • Related