Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash menu to select disk for formatting?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
audiodef
Watchman
Watchman


Joined: 06 Jul 2005
Posts: 6639
Location: The soundosphere

PostPosted: Wed Oct 11, 2017 3:15 pm    Post subject: bash menu to select disk for formatting? Reply with quote

How would I write a bash script that lists all physical disks on a system and the user selects one to be formatted in preparation for an install?

Thus far, I'm trying to think of how to use lsblk -dn -o NAME,SIZE,TYPE for this, but it could be done other ways as well.

Current progress:
Code:

IFS=$'\n' read -r -a array <<<`lsblk -dn -o NAME,SIZE,TYPE`
echo "${array[0]}"
for disk in "${array[@]}"
do
     printf "$disk\n"
done


I'm leaving out --include 8 from lsblk for now so I can have multiple lines of output from lsblk for testing. Does lsblk have a /n after each output line? Because the above code is not recognizing /n anywhere.
_________________
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9679
Location: almost Mile High in the USA

PostPosted: Wed Oct 11, 2017 6:30 pm    Post subject: Reply with quote

When using `command` bash will get rid of trailing newlines so read will slurp all lines in one big long line.
_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
audiodef
Watchman
Watchman


Joined: 06 Jul 2005
Posts: 6639
Location: The soundosphere

PostPosted: Wed Oct 11, 2017 6:42 pm    Post subject: Reply with quote

Ah, ok. So I either need to find another way to get `command` into a string or use a different delimiter.
_________________
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9679
Location: almost Mile High in the USA

PostPosted: Wed Oct 11, 2017 6:58 pm    Post subject: Reply with quote

one possible way
Code:
IFS=, read -r -a array <<< $(lsblk -dn -o NAME,SIZE,TYPE|tr '\n' , )
for disk in "${array[@]}"
do
     printf "$disk\n"
done

_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
audiodef
Watchman
Watchman


Joined: 06 Jul 2005
Posts: 6639
Location: The soundosphere

PostPosted: Wed Oct 11, 2017 7:57 pm    Post subject: Reply with quote

I like that more than what I came up with:
Code:

#!/bin/bash

function choosedisk {
        n=0
   for disk in "${array[@]}"
        do
             printf "$n: $disk\n"
                n=$((n+1))
        done
   read choice
}

# Create array of disks to choose from.
lsblk -dn -o NAME,SIZE,TYPE > disks
n=0
while read line; do
        array[$n]=$line
        n=$((n+1))
done < disks

# Ask user to choose disk.
echo "Please choose a disk to install Gentoo Studio to:"
choosedisk
re='^[0-9]+$' #Make sure $choice is a number
until [[ $choice =~ $re ]]; do
        if ! [[ $choice =~ $re ]]; then
                echo "Error: Please enter the NUMBER of your choice."
                choosedisk
        fi
done
# Check for existence of /dev/(choice).
# If it doesn't exist, user entered invalid option.
if [ -f /dev/sda ]; then
        echo "yes"
fi
rm disks #Delete tmp file.


At the end, I will need to parse the array element indicated by $choice so that the first three letters of that element are assigned to a var, and then check for the existence of /dev/$var. Otherwise, back to the selection menu. (User entered bogus choice.)
_________________
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Wed Oct 11, 2017 8:16 pm    Post subject: Reply with quote

General technique to get it a line at a time without a temporary file:
Code:
#!/bin/bash

COUNT=1
lsblk -dn -o NAME,SIZE,TYPE | while read LINE ; do
    echo "Disk $COUNT is $LINE"
    let COUNT=COUNT+1
done
Parse $LINE however you want to collect the pieces. Of course LINE could be ARRAY[$n].

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
audiodef
Watchman
Watchman


Joined: 06 Jul 2005
Posts: 6639
Location: The soundosphere

PostPosted: Wed Oct 11, 2017 8:45 pm    Post subject: Reply with quote

Darn, never thought to use pipe. Thanks!

EDIT:

This is not outputting a list to choose from, can't see why.
Code:

#!/bin/bash

function choosedisk {
        n=0
   for disk in "${array[@]}"
        do
             printf "$n: $disk\n"
                n=$((n+1))
        done
   read choice
}

# Create array of disks to choose from.
n=0
lsblk -dn -o NAME,SIZE,TYPE | while read line; do
        array[$n]=$line
        n=$((n+1))
done
#lsblk -dn -o NAME,SIZE,TYPE > disks
#n=0
#while read line; do
#       array[$n]=$line
#       n=$((n+1))
#done < disks

# Ask user to choose disk.
echo "Please choose a disk to install Gentoo Studio to:"
choosedisk
re='^[0-9]+$' #Make sure $choice is a number
until [[ $choice =~ $re ]]; do
        if ! [[ $choice =~ $re ]]; then
                echo "Error: Please enter the NUMBER of your choice."
                choosedisk
        fi
done
# Check for existence of /dev/(choice). If it doesn't exist, user entered invalid option.
IFS=' ' read -r -a diskparts <<< "${array[$choice]}"
if [ -b /dev/${diskparts[0]} ]; then
        echo "You are installing on /dev/${diskparts[0]}."
        echo "Proceed? Y/N"
        read formatdisk
        if [ "$formatdisk" = "Y" ]; then
                echo "Install will proceed."
        else
               echo "Install will exit."
        fi
fi
rm disks #Delete tmp file.

_________________
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21631

PostPosted: Thu Oct 12, 2017 12:40 am    Post subject: Reply with quote

You have hit one of the limitations of using the pipe technique (which is what I would have recommended too). The latter terms in the pipe expression run in subshells, so their variable assignments are lost when the subshell ends. Consider this simpler example, which has the same problem as your script:
Code:
echo a | while read f; do a="$f"; echo "$BASHPID: a=$a"; done; echo "$BASHPID: a=$a"
18416: a=a
2266: a=
Add braces to allow statements after the loop to be in the same context:
Code:
echo a | { while read f; do a="$f"; echo "$BASHPID: a=$a"; done; echo "$BASHPID: a=$a"; }
18426: a=a
18426: a=a
Back to top
View user's profile Send private message
Mr. T.
Guru
Guru


Joined: 26 Dec 2016
Posts: 477

PostPosted: Thu Oct 12, 2017 6:50 am    Post subject: Reply with quote

I'm also interested in creating an installer for a Gentoo system. I'm not ready yet but I'm looking for examples of installers. You may be interested by an Arch Linux installer: Feliz?

demo (YouTube): Feliz Arch Linux Installation Script (partitionning: 3:21/8:22)

Other installer projects:

N.B: We can use a virtual machine to test these installers.
Back to top
View user's profile Send private message
audiodef
Watchman
Watchman


Joined: 06 Jul 2005
Posts: 6639
Location: The soundosphere

PostPosted: Thu Oct 12, 2017 1:52 pm    Post subject: Reply with quote

helecho,

Mine is in beta and probably could benefit from suggestions, but you're welcome to examine the Gentoo Studio install script: https://gentoostudio.org/src/builds/install.sh
_________________
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20067

PostPosted: Thu Oct 12, 2017 2:15 pm    Post subject: Reply with quote

helecho wrote:
New Gentoo installer project (Quickstart / stager)
Thank you!

I had a local copy once and couldn't find it on my active storage. And I couldn't recall the name.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 7470

PostPosted: Thu Oct 12, 2017 3:56 pm    Post subject: Reply with quote

audiodef, i think you should offer choice to install on a disk without altering the disk (no parted, format...), best would be ask what partitions user wish to use, but the minimum useful would be : tell me what disk and i will use it.
allowing user to overwrite a previous installation without loosing something that is not in gentoo studio stage4 (gentoo users never reinstall, but gentoo studio is not really for gentoo users right? hence the install script)
you are already using labels on the partitions, maybe use a less common labels (gs-boot instead of just boot) to help script finding what partitions to use.

it would also allow user to pickup his own size for the partitions, or just use a different filesystem on them, or just because non gentoo users are reinstallation maniacs.
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3136

PostPosted: Thu Oct 12, 2017 8:43 pm    Post subject: Reply with quote

Quote:
it would also allow user to pickup his own size for the partitions, or just use a different filesystem on them

:lol: been there, done that, failed miserably
Really, audiodef, how comes you didn't think to include support for f2fs in your kernel?

Seriously now, another menu option like "ask me where to dump those files" would be good. It makes the install process more dual-boot friendly.
Asking questions like "what FS is your favourite" is not worth it. You want to make sure it works and is reliable. Mixing and matching more and more alternatives would make it too big, too complex, and too hard to test/maintain/keep going/whatever. Sure, filesystems are mostly drop-in replacements for each other. The key point is "mostly" though.
Back to top
View user's profile Send private message
audiodef
Watchman
Watchman


Joined: 06 Jul 2005
Posts: 6639
Location: The soundosphere

PostPosted: Sat Oct 14, 2017 1:23 pm    Post subject: Reply with quote

This is where things are headed, actually. I'm just taking things a step at a time - hence the open beta status. I do want the user to have as much control as possible over disk partitioning when using the install script. That said, I'm not going to offer a choice of fs. It brings in more potential issues than I can support, especially considering that I'm not a fs expert of any kind.

Gentoo Studio is for anyone who wants a source-based digital audio workstation, Gentoo user or otherwise. That's why the manual install instructions are still there. They're also there to make it easier to comment on the install process.

Now that you've mentioned it, I can already see how I can write a set of "convert" instructions so existing Gentoo installs don't have to be redone. I went ahead and put in a feature request: https://bugs.gentoostudio.org/view.php?id=58

Feel free to add suggestions to it. This is a good idea. Thanks, krinn.

krinn wrote:
audiodef, i think you should offer choice to install on a disk without altering the disk (no parted, format...), best would be ask what partitions user wish to use, but the minimum useful would be : tell me what disk and i will use it.
allowing user to overwrite a previous installation without loosing something that is not in gentoo studio stage4 (gentoo users never reinstall, but gentoo studio is not really for gentoo users right? hence the install script)
you are already using labels on the partitions, maybe use a less common labels (gs-boot instead of just boot) to help script finding what partitions to use.

it would also allow user to pickup his own size for the partitions, or just use a different filesystem on them, or just because non gentoo users are reinstallation maniacs.

_________________
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Back to top
View user's profile Send private message
audiodef
Watchman
Watchman


Joined: 06 Jul 2005
Posts: 6639
Location: The soundosphere

PostPosted: Sat Oct 14, 2017 1:30 pm    Post subject: Reply with quote

I'm happy to help with testing and offer feedback when it's ready. I already have a multi-VM setup for Gentoo Studio development and testing. It would be a simple matter to use one of my test-install VMs to help out here.

pjp wrote:
helecho wrote:
New Gentoo installer project (Quickstart / stager)
Thank you!

I had a local copy once and couldn't find it on my active storage. And I couldn't recall the name.

_________________
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20067

PostPosted: Sun Oct 15, 2017 4:43 am    Post subject: Reply with quote

I've mainly been gathering items (new and old) for possible learning projects. If I recall many years ago when I first noticed it, I was primarily interested in trying to do jumpstart style / automated deployments.

If I do get into that one with any sincerity, I'll try to keep in mind that others might be interested.

As an aside, I also came up with a dynamic menu script I liked fairly well, but I believe it is in offline storage. I'm hoping to get some of that loaded this week. If I find it and it isn't redundant, I'll post it here.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum