View previous topic :: View next topic |
Author |
Message |
audiodef Watchman
Joined: 06 Jul 2005 Posts: 6639 Location: The soundosphere
|
Posted: Wed Oct 11, 2017 3:15 pm Post subject: bash menu to select disk for formatting? |
|
|
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 |
|
|
eccerr0r Watchman
Joined: 01 Jul 2004 Posts: 9679 Location: almost Mile High in the USA
|
Posted: Wed Oct 11, 2017 6:30 pm Post subject: |
|
|
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 |
|
|
audiodef Watchman
Joined: 06 Jul 2005 Posts: 6639 Location: The soundosphere
|
|
Back to top |
|
|
eccerr0r Watchman
Joined: 01 Jul 2004 Posts: 9679 Location: almost Mile High in the USA
|
Posted: Wed Oct 11, 2017 6:58 pm Post subject: |
|
|
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 |
|
|
audiodef Watchman
Joined: 06 Jul 2005 Posts: 6639 Location: The soundosphere
|
Posted: Wed Oct 11, 2017 7:57 pm Post subject: |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Wed Oct 11, 2017 8:16 pm Post subject: |
|
|
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 |
|
|
audiodef Watchman
Joined: 06 Jul 2005 Posts: 6639 Location: The soundosphere
|
Posted: Wed Oct 11, 2017 8:45 pm Post subject: |
|
|
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 |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21631
|
Posted: Thu Oct 12, 2017 12:40 am Post subject: |
|
|
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 |
|
|
Mr. T. Guru
Joined: 26 Dec 2016 Posts: 477
|
Posted: Thu Oct 12, 2017 6:50 am Post subject: |
|
|
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 |
|
|
audiodef Watchman
Joined: 06 Jul 2005 Posts: 6639 Location: The soundosphere
|
|
Back to top |
|
|
pjp Administrator
Joined: 16 Apr 2002 Posts: 20067
|
Posted: Thu Oct 12, 2017 2:15 pm Post subject: |
|
|
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 |
|
|
krinn Watchman
Joined: 02 May 2003 Posts: 7470
|
Posted: Thu Oct 12, 2017 3:56 pm Post subject: |
|
|
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 |
|
|
szatox Advocate
Joined: 27 Aug 2013 Posts: 3136
|
Posted: Thu Oct 12, 2017 8:43 pm Post subject: |
|
|
Quote: | it would also allow user to pickup his own size for the partitions, or just use a different filesystem on them |
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 |
|
|
audiodef Watchman
Joined: 06 Jul 2005 Posts: 6639 Location: The soundosphere
|
Posted: Sat Oct 14, 2017 1:23 pm Post subject: |
|
|
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 |
|
|
audiodef Watchman
Joined: 06 Jul 2005 Posts: 6639 Location: The soundosphere
|
|
Back to top |
|
|
pjp Administrator
Joined: 16 Apr 2002 Posts: 20067
|
Posted: Sun Oct 15, 2017 4:43 am Post subject: |
|
|
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 |
|
|
|