1

I'm running Freebsd 13R and as shell I'm using csh. I would like to create a script,that looks the presence of the word "r1w1e2" or "NM13N4CZ" or "3e4d31334e34435a" when I give the command "geom disk list" and it gives "da3" as output. I need this script because the mapping of my USB disks change often. But I need to identify with certainty what's the disk where I have stored all the *.img (virtual machines) files that I have created. For example,at the moment,when I give the command :

geom disk list :

Geom name: da3
Providers:
1. Name: da3
   Mediasize: 2000398933504 (1.8T)
   Sectorsize: 512
   Stripesize: 4096
   Stripeoffset: 0
   Mode: r1w1e2
   descr: Seagate M3 Portable
   lunname: NM13N4CZ
   lunid: 3e4d31334e34435a
   ident: NM13N4CZ
   rotationrate: unknown
   fwsectors: 63
   fwheads: 255

I see that the correct disk is da3. So,I can start the VM I want with bhyve like this :

bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 2,virtio-blk,/mnt/$disk$p2/bhyve/os/Linux/photon.img \
-s 3:0,passthru,1/0/0 \
-s 9,virtio-net,tap0 \
-s 10,virtio-9p,sharename=/ \
-s 29,fbuf,tcp=0.0.0.0:5900,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
-l com1,stdio \
vm0

like $disk$ is da3 and p2 never change. very thanks.

Solution :

solution :

set vmdisk=`geom disk list | awk '/^Geom name: /{d=$NF} /^ *lunname: (r1w1e2|NM13N4CZ|3e4d31334e34435a)/ && d{print d}'`

echo $vmdisk
da2

-s 2,virtio-blk,/mnt/$vmdisk'p2'/bhyve/os/Linux/photon.img \
3
  • 4
    Just because you're using csh doesn't mean your shell scripts also have to be written in csh. In fact, it's a terrible shell for writing scripts. Does it really have to be a csh script? Feb 20, 2022 at 21:21
  • 3
    Please, please do not write scripts in csh. See unix.stackexchange.com/a/548046/74393 Feb 20, 2022 at 21:21
  • Consider giving the partition a GPT label, and then you won't need to care what the device number is, you can reference the partition entirely by the GPT label you chose. See answer below.
    – Jim L.
    Feb 25, 2022 at 19:25

2 Answers 2

2

If i understand your question, you would like to get the Geom name if and only lunname match one of the following : r1w1e2, NM13N4CZ or 3e4d31334e34435a. This sed should work (I can't test with bsd sed):

your_cmd | sed -n 's/^Geom name: \(.*\)$/\1/;ta;/^ *lunname: \(r1w1e2\|NM13N4CZ\|3e4d31334e34435a\)$/{g;p};d;:a h'

i'm pretty sure this awk will work :

your_cmd | awk '/^Geom name: /{d=$NF} /^ *lunname: (r1w1e2|NM13N4CZ|3e4d31334e34435a)$/ && d{print d}'
3
  • it works. do u know how can I save the value inside a variable that I can use with the bhyve command ? the value should be copied here : -s 2,virtio-blk,/mnt/$diskp2/bhyve/os/Linux/photon.img \ ; where you see $disk
    – Marietto
    Feb 20, 2022 at 19:19
  • i'm not familiar with csh, but in a POSIX shell you can save the result of a command with the syntax : $(cmd). So if csh let you do that you can save in that way: disk=$(awk or sed cmd). Feb 20, 2022 at 20:03
  • 1
    Nice observation @EdMorton , thanks! Feb 24, 2022 at 17:37
1

I guess this makes two responders who aren't clear on what your purpose really is, but one of the main design goals of the GEOM layer is to avoid having to search through raw device nodes trying to find where your device went. Your current method eschews all the device-agnostic advantages that using the GEOM layer affords.

Let's assume that your 2T USB drive has a GPT partition table. If you apply a unique (and hopefully, descriptive) GPT label to partition 2 of that drive, you will be able to mount or reference the partition by that label, rather than being forced to figure out the correct (and ever-changing) /dev/daNpM device node.

First, figure out one last time, where your USB drive is. Let's assume it's /dev/da3. Here we'll label partition 2 on da3 as my-vm-partition:

# gpart modify -l my-vm-partition -i 2 /dev/da3

Now you can:

# mount /dev/gpt/my-vm-partition /mnt

without having to know anything about the underlying /dev/daN device.

Or, if it's truly that Bhyve command line that's your end target, something like:

# mkdir /my-vm-partition
# mount /dev/gpt/my-vm-partition /my-vm-partition
# bhyve -S -c sockets=2,cores=2,threads=2 -m 8G -w -H -A \
-s 0,hostbridge \
-s 2,virtio-blk,/my-vm-partition/bhyve/os/Linux/photon.img \
-s 3:0,passthru,1/0/0 \
-s 9,virtio-net,tap0 \
-s 10,virtio-9p,sharename=/ \
-s 29,fbuf,tcp=0.0.0.0:5900,w=1440,h=900 \
-s 30,xhci,tablet \
-s 31,lpc \
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_BHF_CODE.fd \
-l com1,stdio \
vm0

You may also want to take a look at Matt Churchyard's excellent vm-bhyve package and its ZFS integration. Mounting filesystems by ZFS pool name or ZFS filesystem name would also save you all the hassle of trying to suss out the right device numbers from your /dev tree.

1
  • 1
    thanks for the good explanation on how the label works. You have explained well and now I can take it in consideration. Regarding the vm-bhyve wrapper,I'm not interested. I found it not useful at all,since I find more comfortable to play directly with the bhyve parameters.
    – Marietto
    Feb 27, 2022 at 10:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .