Shell Script to access partitions

I'm trying to write a script to copy a file from various partitions on a disk, but can't figure out if there is a simple way of listing how many partitions there are on a disk.

I know I can run gpart show, but then I need to parse the output.

Is geom -t | grep PART the best way?
 
parsing the output of "geom -t" does not obvious. awk & xargs
something in the line
Code:
geom -t | grep DEV | awk '{print $1}' | xargs -n 1 -I {} echo "./myscript {}"

myscript
Code:
mkdir /mnt/$1
mount /dev/$1 /mnt/$1
clone -s /mnt/$1 /DESTDIR/$1
umount /mnt/$1
rmdir /mnt/$1
 
Hi buddy


geom -t | grep PART | awk '{print $NF}'

In awk NF count the number of fields and $[index] print the field you want.
with $NF it's like you write $3 (the last field because geom -t return 3 fields)

let's make a for loop.

sh:
for part in $(geom -t | grep PART | awk '{print $NF}'); do
   
    echo "partition : /dev/$part"
    # add your commands here

done

Perhaps you'd like to work on mountpoints.

df -h | awk '{print $NF}'


will output:

/
/dev
/boot/efi
/tmp
/home
/zroot
/var/mail
/usr/ports
...
 
[…] a simple way of listing how many partitions there are on a disk. […]
Just list the partition device files in /dev:​
Bash:
# In the /dev directory find(1) all files that start with `ada0p` (or `ada0s`)
# but do not contain a period (such as `ada0p4.eli`).
# (s stands for slice, if MBR partitioning scheme is used;
#  p is used for GPT partitioning scheme)
find /dev -name 'ada0[ps]*' '!' -name '*.*' | wc -l
 
Just list the partition device files in /dev:​
Bash:
# In the /dev directory find(1) all files that start with `ada0p` (or `ada0s`)
# but do not contain a period (such as `ada0p4.eli`).
# (s stands for slice, if MBR partitioning scheme is used;
#  p is used for GPT partitioning scheme)
find /dev -name 'ada0[ps]*' '!' -name '*.*' | wc -l
Thanks for that. I wouldn't thought of doing it this way, but it's nice and straightforward.
 
Just list the partition device files in /dev:​
Bash:
# In the /dev directory find(1) all files that start with `ada0p` (or `ada0s`)
# but do not contain a period (such as `ada0p4.eli`).
# (s stands for slice, if MBR partitioning scheme is used;
#  p is used for GPT partitioning scheme)
find /dev -name 'ada0[ps]*' '!' -name '*.*' | wc -l
I failed hopelessly in trying to include that snippet in a for loop which is supposed to create a directory for every partition.I must practice writing for loops!!
 
I failed hopelessly in trying to include that snippet in a for loop which is supposed to create a directory for every partition. […]
Show us your code, a minimal working example (or non‐working example). find(1) has an ‑exec action, but you probably want to translate the items first. A common pattern is a while read loop:​
Bash:
# The -r option to read disables interpretation of backslash escaping, not that
# there should be any backslashes but I like to disable unneeded features.
find /dev -maxdepth 1 -name "${disk}[ps]*" -not -name '*.*' | while read -r partition
do
	# ##*[ps] : remove longest prefix pattern,
	#           here everything up to and including last p or s
	printf '%s\n' "${partition##*[ps]}"
done | xargs mkdir
It still feels strange that you’re working with partitions. Dealing with such low‐level details you’re possibly making your task more difficult than it has to be.​
I get gpart illegal option.
As unitrunker subsequently realized, this parameter is only available in 16 current. Besides this, then you need additional programs transforming the JSON or XML to something usable; there are none in the base system that I know of.​
 
Probably not very helpful, but less noise than the -t option?
Code:
# geom part status -s
da0p1  OK  da0
da0p2  OK  da0
da0p3  OK  da0
da1p1  OK  da1
And with cut ...
Code:
# geom part status -s | cut -w -f1
da0p1
da0p2
da0p3
da1p1
You still need to have a loop etc to use it though ...
 

It still feels strange that you’re working with partitions. Dealing with such low‐level details you’re possibly making your task more difficult than it has to be.
As unitrunker subsequently realized, this parameter is only available in 16 current. Besides this, then you need additional programs transforming the JSON or XML to something usable; there are none in the base system that I know of.​
Just a word of explanation... I have Ventoy disk with over 20 partitions which have various OSes installed.

My system isn't very well documented so I want to pull information from each partition and eventually develop a ventoy_grub.cfg which works for every OS.

I'm trying to mount each partition and find the value of /etc/os-release which will tell me what OS installed on each partition.
 
Show us your code, a minimal working example (or non‐working example). find(1) has an ‑exec action, but you probably want to translate the items first. A common pattern is a while read loop:​


Unfortunately I can't work out how to do this although it should be pretty simple. Here is what is what my pseudo code looks like:-

Code:
for every partition do

fstyp %PART -> $filesystem

mount -t $filesystem %PART /mnt

cp /mnt/etc/os-release /tmp/$PART-os

umount /mnt

done


It should be simple enough but I can't get my head around it.
 
Is it MBR or GPT ?
Is it UFS or ZFS ?
Is it any mix ?

Code:
Note , labels can be diffult.
zpool list -v SSD
NAME               SIZE  ALLOC   FREE  CKPOINT  EXPANDSZ   FRAG    CAP  DEDUP    HEALTH  ALTROOT
SSD                432G   238G   194G        -         -    48%    55%  1.00x    ONLINE  -
  nda0p3           190G   111G  78.0G        -         -    50%  58.7%      -    ONLINE
  indirect-1          -      -      -        -         -      -      -      -    ONLINE
  gpt/MYFREEBSD2   214G   124G  87.6G        -         -    47%  58.7%      -    ONLINE
  indirect-3          -      -      -        -         -      -      -      -    ONLINE
  indirect-4          -      -      -        -         -      -      -      -    ONLINE
special               -      -      -        -         -      -      -      -         -
  nda0p5            32G  2.79G  28.7G        -         -    48%  8.86%      -    ONLINE
logs                  -      -      -        -         -      -      -      -         -
  nda0p6            32G   560K  31.5G        -         -     0%  0.00%      -    ONLINE
cache                 -      -      -        -         -      -      -      -         -
  nda0p7            32G  30.0G  2.02G        -         -     0%  93.7%      -    ONLINE
  nda0p8            32G  30.0G  1.99G        -         -     0%  93.8%      -    ONLIN

You see one is labeled by gpt and i don't know why....
 
Back
Top