Translating Linux code to FreeBSD

Is there anything like a 'lookup table' for translating of Linux code to FreeBSD?

eg something like checking if a disk is mounted

Code:
grep "$DISK" /proc/mounts
 
Anyone know how FreeBSD would handle this:-

Code:
if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
 
FreeBSD doesn't have /sys. In linux emulation there is /compat/linux/sys but it is quite limited. For one thing we don't have block devices any more.

Explain what you want to do and may be we can suggest something. On the other hand, why even bother with FreeBSD if you are comfortable with Linux?
 
Anyone know how FreeBSD would handle this:-

Code:
if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
/sys is a pseudo filesystem presented by the Linux kernel. Natively FreeBSD doesn't have a /sys pseudo filesystem because, architecturally, it's not needed.

However, the Linuxulator does support a /sys pseudeo filesystem, normally mounted at /usr/compat/sys or /compat/sys, for native Linux binaries running emulated on a FreeBSD system.

If you looking for disk information found in /sys/class/block, FreeBSD's camcontrol(8) command very loosely provides similar information. And, it can also perform similar functions, like reset a SCSI or ATA bus, or disk.

Having said this, the need to reset a bus under Linux is because Linux is unaware of changes to, for example, fibre channel and geom will taste each disk and partition as as new disk is presented. Linux doesn't do this -- in the worst case you might need to reboot a Linux server to have it taste a partition table while on FreeBSD this is not necessary.

Short answer is, the Linux sysfs is solely a Linux thing not needed by FreeBSD. In the example above you will need to replace it with a call to camcontrol start to start a device.

As someone who's worked with various UNIX systems such as Solaris, SunOS4, Tru64, DG/UX, HP-UX, and SGI, along with the various BSDs, Linux is the outlier. It does not conform to anything UNIX WRT this topic. Linux is its own thing.
 
I like the idea of presenting all hardware resources & features in a properly secured filesystem namespace but not the way Linux has done it -- for example on a pi4 "find /sys | wc" reveals almost 44K entries! FreeBSD does a little of this with disk & audio devices but not in any systematic manner. The benefits are you can do a lot of system stuff in any language without needing brittle C/C++ APIs. But I don't see FreeBSD going this way.
 
I like the idea of presenting all hardware resources & features in a properly secured filesystem namespace but not the way Linux has done it -- for example on a pi4 "find /sys | wc" reveals almost 44K entries! FreeBSD does a little of this with disk & audio devices but not in any systematic manner. The benefits are you can do a lot of system stuff in any language without needing brittle C/C++ APIs. But I don't see FreeBSD going this way.

I'm not sure I would call a C or C++ interface more brittle than parsing random ASCII strings from a filesystem.
 
FreeBSD doesn't have /sys. In linux emulation there is /compat/linux/sys but it is quite limited. For one thing we don't have block devices any more.

Explain what you want to do and may be we can suggest something. On the other hand, why even bother with FreeBSD if you are comfortable with Linux?
I'm not in the least comfortable with Linux, I'm just trying to find out what this does so that I find a FreeBSD equivalent.
 
tcsh with ls alias to /bin/ls -hF to get the trailing "@" and "/".

/sys/class/block gives you all the block devices in the system and then some easy access to information about them.
Ubuntu system I have at hand, laptop with a single SSD in it (sda).
that specific file looks like it's the block number where the partition starts.

cd /sys/class/block/ ls dm-0@ loop10@ loop14@ loop18@ loop21@ loop25@ loop29@ loop6@ sda@ sr0@ dm-1@ loop11@ loop15@ loop19@ loop22@ loop26@ loop3@ loop7@ sda1@ loop0@ loop12@ loop16@ loop2@ loop23@ loop27@ loop4@ loop8@ sda2@ loop1@ loop13@ loop17@ loop20@ loop24@ loop28@ loop5@ loop9@ sda5@ cd sda ls alignment_offset discard_alignment hidden power/ sda1/ stat bdi@ events holders/ queue/ sda2/ subsystem@ capability events_async inflight range sda5/ trace/ dev events_poll_msecs integrity/ removable size uevent device@ ext_range mq/ ro slaves/ cd sda1 ls alignment_offset discard_alignment inflight power/ size stat trace/ dev holders/ partition ro start subsystem@ uevent cat sda1/start 2048 cat sda2/start 1001470
 
[…] checking if a disk is mounted
Code:
grep "$DISK" /proc/mounts
To check whether something is mounted (in the calling process’ mount namespace) use at least anchors (^ and ) when using grep(1)
Bash:
grep "^${DISK} " /proc/mounts # or pipe the output of mount(8)
or use the findmnt(8) Linux utility​
Bash:
findmnt --source "${DISK}" # returns 1 if no match
Anyone know how FreeBSD would handle this:-
Code:
if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
That doesn't even exist on my Linux machine.
I believe the confusing part is $DISK when it’s meant to read $PARTITION. start contains, as mer just described, the number of the partition’s first block. The info can be obtained using gpart(8).​
I like the idea of presenting all hardware resources & features in a properly secured filesystem namespace but not the way Linux has done it […]
Yeah, Linux’s sysfs is utterly unstable. By that I mean the file system contents (can and do) change (between kernel releases). This makes it unsuitable for developing applications. A sysfs file just provides data, but it does not explain what information these data convey (“parsing random ASCII strings from a filesystem” as cracauer@ described it). The “everything is a file” concept has its limits. (Nevertheless sysfs still nice for solving a task now.)​
 
tcsh with ls alias to /bin/ls -hF to get the trailing "@" and "/".

/sys/class/block gives you all the block devices in the system and then some easy access to information about them.
Ubuntu system I have at hand, laptop with a single SSD in it (sda).
that specific file looks like it's the block number where the partition starts.

cd /sys/class/block/ ls dm-0@ loop10@ loop14@ loop18@ loop21@ loop25@ loop29@ loop6@ sda@ sr0@ dm-1@ loop11@ loop15@ loop19@ loop22@ loop26@ loop3@ loop7@ sda1@ loop0@ loop12@ loop16@ loop2@ loop23@ loop27@ loop4@ loop8@ sda2@ loop1@ loop13@ loop17@ loop20@ loop24@ loop28@ loop5@ loop9@ sda5@ cd sda ls alignment_offset discard_alignment hidden power/ sda1/ stat bdi@ events holders/ queue/ sda2/ subsystem@ capability events_async inflight range sda5/ trace/ dev events_poll_msecs integrity/ removable size uevent device@ ext_range mq/ ro slaves/ cd sda1 ls alignment_offset discard_alignment inflight power/ size stat trace/ dev holders/ partition ro start subsystem@ uevent cat sda1/start 2048 cat sda2/start 1001470
gpart list should give you similar information on FreeBSD for this example.
 
tcsh with ls alias to /bin/ls -hF to get the trailing "@" and "/".

/sys/class/block gives you all the block devices in the system and then some easy access to information about them.
Ubuntu system I have at hand, laptop with a single SSD in it (sda).
that specific file looks like it's the block number where the partition starts.

cd /sys/class/block/ ls dm-0@ loop10@ loop14@ loop18@ loop21@ loop25@ loop29@ loop6@ sda@ sr0@ dm-1@ loop11@ loop15@ loop19@ loop22@ loop26@ loop3@ loop7@ sda1@ loop0@ loop12@ loop16@ loop2@ loop23@ loop27@ loop4@ loop8@ sda2@ loop1@ loop13@ loop17@ loop20@ loop24@ loop28@ loop5@ loop9@ sda5@ cd sda ls alignment_offset discard_alignment hidden power/ sda1/ stat bdi@ events holders/ queue/ sda2/ subsystem@ capability events_async inflight range sda5/ trace/ dev events_poll_msecs integrity/ removable size uevent device@ ext_range mq/ ro slaves/ cd sda1 ls alignment_offset discard_alignment inflight power/ size stat trace/ dev holders/ partition ro start subsystem@ uevent cat sda1/start 2048 cat sda2/start 1001470

OK, but where is this documented?

What does the "start" entry referenced upstairs in this thread do? All I can see is that the entry is readonly, so it obviously is not intended to be "echo"ed into.
 
OK, but where is this documented?

What does the "start" entry referenced upstairs in this thread do? All I can see is that the entry is readonly, so it obviously is not intended to be "echo"ed into.
That is the problem :) Last I mucked with it sysfs is used mostly by device drivers to expose things. Some items are read only others are read write.

My opinion: If it were better documented , it would be useful or at least "not a bad way" to expose information. For example, there is /sys/bus/usb which lets you look at device information for everything on the USB subsystem.

There may be some standard somewhere "man -s5 sysfs" on a linux system gives some basics, the man page references:
SEE ALSO proc(5), udev(7) P. Mochel. (2005). The sysfs filesystem. Proceedings of the 2005 Ot‐ tawa Linux Symposium. The kernel source file Documentation/filesystems/sysfs.txt and various other files in Documentation/ABI and Documentation/*/sysfs.txt

Specifically related to the "start" entry, I poked around looking at it on a live Ubuntu system and guessed it represents the starting block/location of a partition. There are tools similar to gpart that you can get partition start/end info. Back up in my post you referenced I said "that specific file looks like it's the block number where the partition starts.".

More My Opinion:
It contains useful information that lets you look at a lot of information, but it's presented in an inconsistent manner and pretty much requires you to read kernel source to find out what the entries actually mean.
 
Well, this is one reason why I like FreeBSD's sysctl: for every variable there is documentation built into the kernel and the systcl command can display it.

It would be like Linux having for every
/sys/class/foo/bar/start
a
/sys/class/foo/bar/start.doc

which you could `cat` to learn about it.
 
Well, this is one reason why I like FreeBSD's sysctl: for every variable there is documentation built into the kernel and the systcl command can display it.

It would be like Linux having for every
/sys/class/foo/bar/start
a
/sys/class/foo/bar/start.doc

which you could `cat` to learn about it.
Absolutely agree with sysctl description. I'd love to see default, allowed values or ranges too, but that's just me and yes, I know "patches welcome".
 
Back
Top