Query:
various useful util-linux fn/headers that I cn use to make my c/cpp prg easy
Reply:
Excellent and very practical question 👏 — especially for Linux system-level C/C++ work.
util-linux is a huge, battle-tested toolkit of system utilities (mount, fdisk, uuidgen, etc.) and library functions you can use to make your programs more powerful and consistent with Linux internals.
Below is a compact developer-oriented reference of the useful functions and headers you can directly leverage from util-linux (and related system libraries that ship with it).
🧩 1️⃣ Core Library — <uuid/uuid.h> (libuuid)
Library: libuuid
Link: -luuid
Purpose: UUID generation, parsing, and comparison.
Useful Functions:
Use: Unique IDs, session keys, distributed object IDs, etc.
🧩 2️⃣ <blkid/blkid.h> — libblkid
Library: libblkid
Link: -lblkid
Purpose: Identify and query block devices (disks, partitions, filesystems).
Useful Functions:
Use:
Detect filesystem types, UUIDs, labels, mount points — great for system monitors or installers.
🧩 3️⃣ <mount.h> — libmount
Library: libmount
Link: -lmount
Purpose: Manage and parse mount tables (/etc/mtab, /proc/self/mountinfo).
Useful Functions:
Use:
Check what’s mounted, unmount/mount filesystems programmatically.
🧩 4️⃣ <smartcols.h> — libsmartcols
Library: libsmartcols
Link: -lsmartcols
Purpose: Nicely format tables/columns for CLI tools (used by lsblk, lsns, etc.).
Useful Functions:
Use:
Pretty CLI tabular outputs (aligned like system tools).
🧩 5️⃣ <fdisk.h> — libfdisk
Library: libfdisk
Link: -lfdisk
Purpose: Low-level disk partitioning and metadata operations.
Useful Functions:
Use:
Writing partitioning tools, installers, or storage management scripts.
🧩 6️⃣ <uuid/uuid.h> + <blkid/blkid.h> combo
Use together to correlate filesystem and device UUIDs.
Example: map /dev/sda1 → filesystem UUID.
🧩 7️⃣ <libsmartcols> + <mntent.h> combo
Create lsblk-like table of currently mounted filesystems.
🧩 8️⃣ <pathnames.h>, <sys/mount.h>, <sys/statvfs.h>
These are standard Linux headers but complement util-linux libs.
Use:
-
mount(),umount() -
statvfs()for filesystem stats -
constants like
_PATH_MOUNTED
🧩 9️⃣ <libfdisk> Helpers for GPT/MBR parsing
Purpose: inspect partition types, sizes, alignments, GPT GUIDs.
🧩 🔟 <libsmartcols> with color support
Purpose: build colorful CLI outputs with auto-width columns — like lsblk.
⚙️ Install dev headers (if not present)
⚙️ Example: Combining UUID + Smartcols
Compile:
Output:
✅ Summary Table
| Lib | Header | Link | Purpose |
|---|---|---|---|
| libuuid | <uuid/uuid.h> | -luuid | UUID generation |
| libblkid | <blkid/blkid.h> | -lblkid | Detect block devices/filesystems |
| libmount | <mount.h> | -lmount | Manage mount tables |
| libsmartcols | <smartcols.h> | -lsmartcols | Pretty-print tables |
| libfdisk | <fdisk.h> | -lfdisk | Partition table manipulation |
Would you like me to generate a single C++ “util-linux helper header” (e.g. util_linux_helpers.hpp) wrapping the above in a modern, RAII-safe C++23 interface (namespace util::linuxsys)?
It’ll give you functions like listMountedFS(), generateUUID(), prettyPrintTable(), etc.
-------------------------------------============
No comments:
Post a Comment
If you have any doubts, please let me know.