Created
December 1, 2024 12:18
-
-
Save memchr/5326aeb379ecb6a9747a646201ef29e1 to your computer and use it in GitHub Desktop.
fstab generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import yaml | |
import sys | |
from pathlib import Path | |
HIDE_OPTIONS = ['x-gvfs-hide'] | |
def generate_fstab(yaml_file: Path): | |
with yaml_file.open('r') as file: | |
config = yaml.safe_load(file) | |
fstab_entries = [] | |
for entry in config: | |
group = [] | |
fs = entry['fs'] | |
fstype = entry['type'] | |
hide = entry.get('hide', False) | |
dump = entry.get('dump', 0) | |
passno = entry.get('pass', 0) | |
mount = entry.get('mount', 'none') | |
options: list = entry.get('options', ['defaults']) | |
subvol = entry.get('subvol') | |
if mount == 'none' and fstype not in ['swap']: | |
raise ValueError(f'mount point for {fs} not specified') | |
# [mount] can be a list of btrfs subvolumes, a list of strings, or just a string | |
if isinstance(mount, list): | |
for mount in entry['mount']: | |
if isinstance(mount, str): | |
path = mount | |
path_subvol = subvol | |
path_options = options.copy() | |
path_hide = hide | |
else: | |
path = mount['path'] | |
path_subvol = mount.get('subvol', subvol) | |
path_options: list[str] = mount.get('options', []) | |
if not mount.get('override', False): | |
path_options.extend(options) | |
path_hide = mount.get('hide', hide) | |
if fstype == "btrfs": | |
path_options.append(f'subvol={path_subvol}') | |
if path_hide: | |
path_options.extend(HIDE_OPTIONS) | |
group.append(f"{fs} {path} {fstype} {','.join(path_options)} {dump} {passno}") | |
else: | |
if hide: | |
options.extend(HIDE_OPTIONS) | |
if fstype == "btrfs": | |
options.append(f'subvol={subvol}') | |
group.append(f"{fs} {mount} {fstype} {','.join(options)} {dump} {passno}") | |
fstab_entries.append(group) | |
return '\n\n'.join('\n'.join(group) for group in fstab_entries) | |
if __name__ == "__main__": | |
yaml_file = Path(sys.argv[1] if len(sys.argv) > 1 else '/etc/fstab.yml') | |
if not yaml_file.exists(): | |
print(f"{yaml_file} does not exist") | |
raise SystemExit | |
fstab_content = generate_fstab(yaml_file) | |
print(fstab_content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- fs: UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
type: btrfs | |
options: | |
- rw | |
- noatime | |
- user_subvol_rm_allowed | |
- compress=zstd:1 | |
- discard=async | |
hide: false | |
mount: | |
- path: / | |
subvol: /arch | |
- path: /home/user | |
subvol: /home/1000 | |
- fs: UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
type: btrfs | |
options: | |
- rw | |
- noatime | |
- user_subvol_rm_allowed | |
- compress=zstd:2 | |
- discard=async | |
mount: | |
- /media/secondary | |
- path: /home/user/src/chromium | |
subvol: /chromium | |
- path: /home/user/src/aosp | |
subvol: /aosp | |
- path: /home/user/steam | |
subvol: /steam | |
hide: true | |
- path: /recovery | |
subvol: /arch | |
hide: true | |
- &esp | |
fs: UUID=FFFF-FFFF | |
type: vfat | |
pass: 2 | |
options: | |
- rw | |
- noatime | |
- fmask=0177 | |
- dmask=0077 | |
- codepage=437 | |
- iocharset=ascii | |
- shortname=mixed | |
- utf8 | |
- errors=remount-ro | |
hide: true | |
mount: | |
- /efi | |
- /recovery/efi | |
- &swap | |
fs: UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
type: swap | |
options: | |
- defaults | |
- pri=10 | |
hide: false | |
- <<: *swap | |
fs: UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment