Why my bash script isn't partitioning the first two disks?
Hello All,
I'm in a weird situation, I developed a bash script which automatically does the disk partitions based the capacity. I have 62 drivers mix of TB and GB. When I ran the script, it is skipping the first two drivers (/dev/sda and /dev/sdb), rest are getting partitioned. Please see the link for the for the code or file which is attached. Your help is much appreciated. https://pastebin.com/index/hFrAX6dn Thank you All. |
Quote:
Can you list the output of: Code:
for i in $(fdisk -l | grep -i 'Disk /dev/sd*' | awk '{print $2}'| sed 's/://' | sort ); The above snippet works flawlessly on my system (though it shows fewer disks than you have). Can you run the above and show enough of the output to see whether disks "/dev/sd[ab]" are showing up (or not showing up if that's the case)? I've included the case/esac block in a copy of your script and replaced the parted commands with "echo"s and is is running the correct "case" according to the value of "disk_capacity" and it's working correctly, too. No bugs there. It would also be of some help to see what "fdisk -l" is producing for the disks "/dev/sd[ab]". I suspect that those two disks are not being processed because of something that "fdisk" is (or is not) returning. Bottom line: your script seems OK. What "fdisk" is returning is probably the cause of your trouble. Are you seeing the "Check partition" messages. (BTW: You should include the disk name ("${i}") in that message to tie it to a specific device.) |
Do your own basic debugging - echo all the variables as the are created, that way you know what is actually going on rather than guessing.
Seems remarkably complex - my general stance is that anything that pipes sed and/or grep to awk needs serious re-writing. |
Quote:
One reason why this is so complicated is fdisk. To simplify this, you could generate the list of disks with Code:
cd /sys/block/ If you don't like /sys, you can also use lsblk, e.g. Code:
lsblk --list --output name,size |
Quote:
add something like: echo "grep ${i}" I think you will be able to see why there is a bypass. Good luck!! |
I agree with the above comments. In addition,
You need to verify the actual size of the drive i.e a 4 TB drive might output 4000 GB and that needs to be formatted as gpt versus msdos. Quote:
|
Truth be told there is no need for any of the contortions to calculate the size - parted accepts 100% as the end parameter.
Like I said, way too complicated. |
Quote:
Code:
cd /sys/block |
1 Attachment(s)
Hello, Thank you for your reply. I did run the code snippet. I have attached a .txt file. Please take a look
|
1 Attachment(s)
Quote:
Thank you. |
Quote:
Quote:
As syg00 commented, you don't have to treat smaller (GB) and bigger (TB) drives separately. Just use parted's "100%". |
This part in your script creates the problem:
Code:
grep -i 'disk /dev/sd*' | grep ${i} When $i is /dev/sda, it returns many lines, for disks named sda, sdaa, sdab, sdac and so on. Which is not your intention. There is no need for a lot of code. Your script can be simplified radically. |
Berndbausch is right, OP is grepping a wildcard.
Try this but it's not tested, hopefully you will have an idea if it doesn't work. Quote:
Quote:
Good luck~! |
Quote:
for i in $(fdisk -l | grep -Ei 'disk /dev/sd[(a|b)]{1}' | awk '{print $2}'| sed 's/://' | sort); do #disk_size=$(fdisk -l | grep 'Disk /dev/sd*' | grep ${i} | awk '{print $3}') disk_capacity=$(fdisk -l | grep -Ei 'disk /dev/sd[(a|b)]{1}' | grep ${i} | awk '{print $4}' | sed 's/,//') case $disk_capacity in TiB) parted -s ${i} mklabel gpt parted -s ${i} -a min mkpart primary xfs 0 100% ;; GiB) parted -s ${i} mklabel msdos parted -s ${i} -a min mkpart primary xfs 0 100% ;; *) echo -n "Check the Disk partition" ;; esac done This is returning me only 38 drives list and it is not partitioning the firt two drives /dev/sda, /dev/sdb. Thanks! |
fdisk output is hard to parse, leading to an overly complex program. Instead of fdisk, use /sys/block to generate a list of disks. Instead of computing disk sizes, use percentages in the parted commands.
EDIT: I just noticed you do the latter now. Good! But you don't need the capacity anymore and can you remove the case. |
All times are GMT -5. The time now is 08:13 PM. |