Proxmox forum administrators, please increase maximum character count per post from 16KB to 128KB, thanks !

shodan

Well-Known Member
Sep 1, 2022
331
78
48
Hi,
I wish to paste a command script
Unfortunately, it is 17579 characters so it is over the limit of 16384 characters.

I know SSDs went up but the storage bill for these forum is still going to be alright if you spare me a couple more KBs !
 
Hi @shodan ,why not post this script on github and provide a link?


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
Because that's a lot more work including agreeing to microsoft's terms and including third parties.

Versus pressing CTRL+V, I think it's it's to see why not.
Plus with random free hosts, well, we remember what happened to photobucket.

If I post the script here's it's going to reliably stay here as long as the forum exists.
But if I paste it to a 3rd party website, who knows how long that will remain functioning.
 
It sounds like we may have different views on the intended use of the forum. In my view, forum posts are not the ideal place for large code dumps, particularly ones approaching 16 KB, let alone 128 KB.

For larger code samples, a repository or another suitable hosting platform would make the code easier to read, maintain, and share. Providing a link also gives others a better opportunity to review and reuse the work.

Ultimately, the goal is to make contributions as accessible and useful to the community as possible.


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
  • Like
Reactions: fba
Sure and I might, but also, maybe now that it's out of my clipboard and my thingy work, I might move on to other thing and forget to come back and share it.
And having a thing were you're reading about it, is also easier than having to go to another place to get it.
 
I wish to paste a command script
Unfortunately, it is 17579 characters so it is over the limit of 16384 characters.
You can attach a file to a post (the "Attach files" button below the composing window).

I can see attachments easily exceeding a few hundreds of KB in this Forum.
 
I understand there are workaround, ok, yes I can save it to a file, then other users could download the file and open it.

Or they could also just, copy this text

Code:
init_colors(){ if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then RED=$(printf '\033[31m'); GREEN=$(printf '\033[32m'); CYAN=$(printf '\033[36m'); BOLD=$(printf '\033[1m'); RESET=$(printf '\033[0m'); else RED=; GREEN=; CYAN=; BOLD=; RESET=; fi; }; info(){ printf '%s[INFO]%s %s\n' "$CYAN" "$RESET" "$*"; }; pass(){ printf '%s[PASS]%s %s\n' "$GREEN" "$RESET" "$*"; }; fail(){ printf '%s[FAIL]%s %s\n' "$RED" "$RESET" "$*" >&2; }; require_root(){ if [ "$(id -u)" -eq 0 ]; then pass "Running as root"; return 0; else fail "This installer must be run as root"; return 1; fi; }; create_iommu_passthrough_service(){ if printf '%s\n' '[Unit]' 'Description=Set IOMMU mode for next boot - Pass-Through' '' '[Service]' Type=oneshot SyslogIdentifier=iommu-passthrough 'ExecStart=/bin/sh -c '"'"'find_tty(){ tty=; i=0; while [ "$$i" -lt 10 ] && [ -z "$$tty" ]; do for p in /proc/[0-9]*; do case "$$(readlink "$$p/exe" 2>/dev/null)" in */systemctl) c=$$(tr "\0" " " <"$$p/cmdline" 2>/dev/null); case "$$c" in *iommu-passthrough*) t=$$(readlink "$$p/fd/1" 2>/dev/null); case "$$t" in /dev/pts/*|/dev/tty*) tty=$$t; break;; esac;; esac;; esac; done; i=$$((i+1)); [ -n "$$tty" ] || sleep 0.05; done; }; emit(){ printf "%%s\n" "$$1"; [ -n "$$tty" ] && [ -w "$$tty" ] && printf "%%b\n" "$${2:-$$1}" >"$$tty"; }; clean_args(){ out=; for x in $$1; do case "$$x" in iommu=pt|iommu.strict=*|iommu.passthrough=*) :;; *) out="$${out:+$$out }$$x";; esac; done; printf "%%s" "$$out"; }; find_tty; C=$$(printf "\033[1;36m"); Y=$$(printf "\033[1;33m"); R=$$(printf "\033[1;31m"); Z=$$(printf "\033[0m"); M=$$(printf "\033[1;32m"); mode="iommu=pt"; name="Pass-Through"; [ "$$(id -u)" -eq 0 ] || { emit "ERROR: run as root" "$${R}ERROR:$${Z} run as root"; exit 1; }; src=; if [ -f /etc/kernel/cmdline ] && [ -s /etc/kernel/proxmox-boot-uuids ]; then old=$$(cat /etc/kernel/cmdline) || exit 1; clean=$$(clean_args "$$old"); printf "%%s\n" "$${clean:+$$clean }$$mode" >/etc/kernel/cmdline || { emit "ERROR: could not update /etc/kernel/cmdline" "$${R}ERROR:$${Z} could not update /etc/kernel/cmdline"; exit 1; }; command -v proxmox-boot-tool >/dev/null 2>&1 || { emit "ERROR: proxmox-boot-tool not found" "$${R}ERROR:$${Z} proxmox-boot-tool not found"; exit 1; }; proxmox-boot-tool refresh || exit 1; src=/etc/kernel/cmdline; elif [ -f /etc/default/grub ] && command -v update-grub >/dev/null 2>&1; then tmp=$$(mktemp) || exit 1; found=0; : >"$$tmp" || exit 1; while IFS= read -r line || [ -n "$$line" ]; do trim=$${line#"$${line%%%%[![:space:]]*}"}; case "$$trim" in GRUB_CMDLINE_LINUX_DEFAULT=*) val=$${trim#*=}; val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); clean=$$(clean_args "$$val"); printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$${clean:+$$clean }$$mode" >>"$$tmp"; found=1;; GRUB_CMDLINE_LINUX=*) val=$${trim#*=}; val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); clean=$$(clean_args "$$val"); printf "GRUB_CMDLINE_LINUX=\"%%s\"\n" "$$clean" >>"$$tmp";; *) printf "%%s\n" "$$line" >>"$$tmp";; esac; done </etc/default/grub; [ "$$found" -eq 1 ] || printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$$mode" >>"$$tmp"; cat "$$tmp" >/etc/default/grub; r=$$?; rm -f "$$tmp"; [ "$$r" -eq 0 ] || { emit "ERROR: could not update /etc/default/grub" "$${R}ERROR:$${Z} could not update /etc/default/grub"; exit "$$r"; }; update-grub || exit 1; src=/etc/default/grub; else emit "ERROR: no supported Proxmox boot configuration found" "$${R}ERROR:$${Z} no supported Proxmox boot configuration found"; exit 1; fi; emit "Next boot configured: $$name" "$${C}Next boot configured:$${Z} $${M}$$name$${Z}"; emit "Boot config: $$src" "$${C}Boot config:$${Z} $$src"; emit "Reboot required for the change to take effect." "$${Y}Reboot required for the change to take effect.$${Z}"; emit "Status: systemctl start iommu-status" "$${C}Status:$${Z} systemctl start iommu-status"'"'"'' StandardOutput=journal StandardError=journal >'/etc/systemd/system/iommu-passthrough.service'; then pass 'Created iommu-passthrough.service'; else fail 'Failed to create iommu-passthrough.service'; return 1; fi; }; create_iommu_translated_lazy_service(){ if printf '%s\n' '[Unit]' 'Description=Set IOMMU mode for next boot - Translated - Lazy' '' '[Service]' Type=oneshot SyslogIdentifier=iommu-translated-lazy 'ExecStart=/bin/sh -c '"'"'find_tty(){ tty=; i=0; while [ "$$i" -lt 10 ] && [ -z "$$tty" ]; do for p in /proc/[0-9]*; do case "$$(readlink "$$p/exe" 2>/dev/null)" in */systemctl) c=$$(tr "\0" " " <"$$p/cmdline" 2>/dev/null); case "$$c" in *iommu-translated-lazy*) t=$$(readlink "$$p/fd/1" 2>/dev/null); case "$$t" in /dev/pts/*|/dev/tty*) tty=$$t; break;; esac;; esac;; esac; done; i=$$((i+1)); [ -n "$$tty" ] || sleep 0.05; done; }; emit(){ printf "%%s\n" "$$1"; [ -n "$$tty" ] && [ -w "$$tty" ] && printf "%%b\n" "$${2:-$$1}" >"$$tty"; }; clean_args(){ out=; for x in $$1; do case "$$x" in iommu=pt|iommu.strict=*|iommu.passthrough=*) :;; *) out="$${out:+$$out }$$x";; esac; done; printf "%%s" "$$out"; }; find_tty; C=$$(printf "\033[1;36m"); Y=$$(printf "\033[1;33m"); R=$$(printf "\033[1;31m"); Z=$$(printf "\033[0m"); M=$$(printf "\033[1;33m"); mode="iommu.strict=0"; name="Translated - Lazy"; [ "$$(id -u)" -eq 0 ] || { emit "ERROR: run as root" "$${R}ERROR:$${Z} run as root"; exit 1; }; src=; if [ -f /etc/kernel/cmdline ] && [ -s /etc/kernel/proxmox-boot-uuids ]; then old=$$(cat /etc/kernel/cmdline) || exit 1; clean=$$(clean_args "$$old"); printf "%%s\n" "$${clean:+$$clean }$$mode" >/etc/kernel/cmdline || { emit "ERROR: could not update /etc/kernel/cmdline" "$${R}ERROR:$${Z} could not update /etc/kernel/cmdline"; exit 1; }; command -v proxmox-boot-tool >/dev/null 2>&1 || { emit "ERROR: proxmox-boot-tool not found" "$${R}ERROR:$${Z} proxmox-boot-tool not found"; exit 1; }; proxmox-boot-tool refresh || exit 1; src=/etc/kernel/cmdline; elif [ -f /etc/default/grub ] && command -v update-grub >/dev/null 2>&1; then tmp=$$(mktemp) || exit 1; found=0; : >"$$tmp" || exit 1; while IFS= read -r line || [ -n "$$line" ]; do trim=$${line#"$${line%%%%[![:space:]]*}"}; case "$$trim" in GRUB_CMDLINE_LINUX_DEFAULT=*) val=$${trim#*=}; val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); clean=$$(clean_args "$$val"); printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$${clean:+$$clean }$$mode" >>"$$tmp"; found=1;; GRUB_CMDLINE_LINUX=*) val=$${trim#*=}; val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); clean=$$(clean_args "$$val"); printf "GRUB_CMDLINE_LINUX=\"%%s\"\n" "$$clean" >>"$$tmp";; *) printf "%%s\n" "$$line" >>"$$tmp";; esac; done </etc/default/grub; [ "$$found" -eq 1 ] || printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$$mode" >>"$$tmp"; cat "$$tmp" >/etc/default/grub; r=$$?; rm -f "$$tmp"; [ "$$r" -eq 0 ] || { emit "ERROR: could not update /etc/default/grub" "$${R}ERROR:$${Z} could not update /etc/default/grub"; exit "$$r"; }; update-grub || exit 1; src=/etc/default/grub; else emit "ERROR: no supported Proxmox boot configuration found" "$${R}ERROR:$${Z} no supported Proxmox boot configuration found"; exit 1; fi; emit "Next boot configured: $$name" "$${C}Next boot configured:$${Z} $${M}$$name$${Z}"; emit "Boot config: $$src" "$${C}Boot config:$${Z} $$src"; emit "Reboot required for the change to take effect." "$${Y}Reboot required for the change to take effect.$${Z}"; emit "Status: systemctl start iommu-status" "$${C}Status:$${Z} systemctl start iommu-status"'"'"'' StandardOutput=journal StandardError=journal >'/etc/systemd/system/iommu-translated-lazy.service'; then pass 'Created iommu-translated-lazy.service'; else fail 'Failed to create iommu-translated-lazy.service'; return 1; fi; }; create_iommu_translated_strict_service(){ if printf '%s\n' '[Unit]' 'Description=Set IOMMU mode for next boot - Translated - Strict' '' '[Service]' Type=oneshot SyslogIdentifier=iommu-translated-strict 'ExecStart=/bin/sh -c '"'"'find_tty(){ tty=; i=0; while [ "$$i" -lt 10 ] && [ -z "$$tty" ]; do for p in /proc/[0-9]*; do case "$$(readlink "$$p/exe" 2>/dev/null)" in */systemctl) c=$$(tr "\0" " " <"$$p/cmdline" 2>/dev/null); case "$$c" in *iommu-translated-strict*) t=$$(readlink "$$p/fd/1" 2>/dev/null); case "$$t" in /dev/pts/*|/dev/tty*) tty=$$t; break;; esac;; esac;; esac; done; i=$$((i+1)); [ -n "$$tty" ] || sleep 0.05; done; }; emit(){ printf "%%s\n" "$$1"; [ -n "$$tty" ] && [ -w "$$tty" ] && printf "%%b\n" "$${2:-$$1}" >"$$tty"; }; clean_args(){ out=; for x in $$1; do case "$$x" in iommu=pt|iommu.strict=*|iommu.passthrough=*) :;; *) out="$${out:+$$out }$$x";; esac; done; printf "%%s" "$$out"; }; find_tty; C=$$(printf "\033[1;36m"); Y=$$(printf "\033[1;33m"); R=$$(printf "\033[1;31m"); Z=$$(printf "\033[0m"); M=$$(printf "\033[1;31m"); mode="iommu.strict=1"; name="Translated - Strict"; [ "$$(id -u)" -eq 0 ] || { emit "ERROR: run as root" "$${R}ERROR:$${Z} run as root"; exit 1; }; src=; if [ -f /etc/kernel/cmdline ] && [ -s /etc/kernel/proxmox-boot-uuids ]; then old=$$(cat /etc/kernel/cmdline) || exit 1; clean=$$(clean_args "$$old"); printf "%%s\n" "$${clean:+$$clean }$$mode" >/etc/kernel/cmdline || { emit "ERROR: could not update /etc/kernel/cmdline" "$${R}ERROR:$${Z} could not update /etc/kernel/cmdline"; exit 1; }; command -v proxmox-boot-tool >/dev/null 2>&1 || { emit "ERROR: proxmox-boot-tool not found" "$${R}ERROR:$${Z} proxmox-boot-tool not found"; exit 1; }; proxmox-boot-tool refresh || exit 1; src=/etc/kernel/cmdline; elif [ -f /etc/default/grub ] && command -v update-grub >/dev/null 2>&1; then tmp=$$(mktemp) || exit 1; found=0; : >"$$tmp" || exit 1; while IFS= read -r line || [ -n "$$line" ]; do trim=$${line#"$${line%%%%[![:space:]]*}"}; case "$$trim" in GRUB_CMDLINE_LINUX_DEFAULT=*) val=$${trim#*=}; val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); clean=$$(clean_args "$$val"); printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$${clean:+$$clean }$$mode" >>"$$tmp"; found=1;; GRUB_CMDLINE_LINUX=*) val=$${trim#*=}; val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); clean=$$(clean_args "$$val"); printf "GRUB_CMDLINE_LINUX=\"%%s\"\n" "$$clean" >>"$$tmp";; *) printf "%%s\n" "$$line" >>"$$tmp";; esac; done </etc/default/grub; [ "$$found" -eq 1 ] || printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$$mode" >>"$$tmp"; cat "$$tmp" >/etc/default/grub; r=$$?; rm -f "$$tmp"; [ "$$r" -eq 0 ] || { emit "ERROR: could not update /etc/default/grub" "$${R}ERROR:$${Z} could not update /etc/default/grub"; exit "$$r"; }; update-grub || exit 1; src=/etc/default/grub; else emit "ERROR: no supported Proxmox boot configuration found" "$${R}ERROR:$${Z} no supported Proxmox boot configuration found"; exit 1; fi; emit "Next boot configured: $$name" "$${C}Next boot configured:$${Z} $${M}$$name$${Z}"; emit "Boot config: $$src" "$${C}Boot config:$${Z} $$src"; emit "Reboot required for the change to take effect." "$${Y}Reboot required for the change to take effect.$${Z}"; emit "Status: systemctl start iommu-status" "$${C}Status:$${Z} systemctl start iommu-status"'"'"'' StandardOutput=journal StandardError=journal >'/etc/systemd/system/iommu-translated-strict.service'; then pass 'Created iommu-translated-strict.service'; else fail 'Failed to create iommu-translated-strict.service'; return 1; fi; }; set_iommu_to_lazy_tlb_translation(){ systemctl reset-failed iommu-translated-lazy >/dev/null 2>&1 || :; systemctl start iommu-translated-lazy; }; set_iommu_to_strict_tlb_translation(){ systemctl reset-failed iommu-translated-strict >/dev/null 2>&1 || :; systemctl start iommu-translated-strict; }; set_iommu_to_passthrough(){ systemctl reset-failed iommu-passthrough >/dev/null 2>&1 || :; systemctl start iommu-passthrough; }; get_iommu_passthrough_mode(){ systemctl reset-failed iommu-status >/dev/null 2>&1 || :; systemctl start iommu-status; };
install_iommu_service_units 
#set_iommu_to_lazy_tlb_translation
#set_iommu_to_strict_tlb_translation
set_iommu_to_passthrough
get_iommu_passthrough_mode

This one doesn't work, as I had to trunkate it to fit.
And you can see for yourself, downloading and running the attached file, is several steps, compare to copy a paste-to-run script that is ready to go

I don't want a workaround, I just want this forum's admin to increase the max post length, from one arbitrary value to a slightly bigger arbitrary value.
 

Attachments

You expect people to cut-and-paste completely unreadable code just on your say-so? Any sane person should say "no thanks" to that garbage.
Yes, but it's not garbage, it's the solution to the problem, you can make it pretty as you like, I'm just trying not to make people scroll through 17kb of gobbledigook or force them to login or having handles extra files.

This doesn't have to go anywhere other than the clipboard manager

If your infrastructure is brittle then just put it in GLM 5.2 with the following prompt

Code:
Reformat this paste-to-run oneliner to be human readable, indented and commented but do not change the syntax or functioning, give me to directly not as a script file then give me the function call structure as an ASCII diagram and explain the inner functioning of this code

Can't share from openrouter but here's a typical output

https://claude.ai/share/bfe12e37-ffa6-42e8-8bcf-de9395ecd54b

And that's a 45 page explanation BTW

I can imagine how much you would moan if I posted 45 pages of code and explanation for creating a 3-way toggle in systemd

This could have been so easy but no ....

All this does is create 3 systemd service unit setters to change the iommu config and a colorized iommu status checker.

Are you really telling me you have those command line memorized and that you're not copy-pasting them from some other website anyway ?
 
Last edited:
All this does is create 3 systemd service unit setters to change the iommu config and a colorized iommu status checker.
All the LLMs have the very useful "sceptic" mode. I find it invaluable. In fact pitting one against the other often gives you a more refined result.
  • Overengineered: Excessive code for a simple task, including custom TTY detection and ANSI color handling in every service.
  • Code duplication: Three nearly identical scripts instead of one reusable function.
  • Fragile config editing: Parses and rewrites GRUB configuration with basic string manipulation.
  • No rollback: Modifies boot configuration without backing up the original.
  • Questionable validation: systemd-analyze verify checks unit syntax, not whether the IOMMU configuration is correct or safe.
  • Security trade-off: Defaults to iommu=pt without explaining the DMA isolation implications.
  • No evidence of testing: No hardware detection, compatibility checks, or validation that the requested mode works as intended.
Claude ^


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
Yes, but it's not garbage, it's the solution to the problem, you can make it pretty as you like, I'm just trying not to make people scroll through 17kb of gobbledigook or force them to login or having handles extra files.
Some of us like to know what it is we're running as root.

Are you really telling me you have those command line memorized and that you're not copy-pasting them from some other website anyway ?
So you're going to put it on the eventual users to each individually reformat your thing to make sure it is reasonable and that your account hasn't been hacked by someone who is malicious. Thanks.

Personally, I think cutting and pasting a systemd unit file is safer than running untrusted scripts as root. Maybe it is just me, but I kind of doubt it.
 
  • Like
Reactions: bbgeek17
Security trade-off -> yes that is precisely the difference between setting it to lazy strict and passthrough, #notabugwontfix

Overengineered or "needs rollback and extra validation ?" Well, which is it ?

Code duplication
Hmm, ok, this oneliner, prints out a service unit with an embed posix script 4 different ways
So much for "overengineering" but ok

Ok, yes with deduplicate it does clear the 16384 character bar


Code:
init_colors(){ if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then RED=$(printf '\033[31m'); GREEN=$(printf '\033[32m'); CYAN=$(printf '\033[36m'); BOLD=$(printf '\033[1m'); RESET=$(printf '\033[0m'); else RED=; GREEN=; CYAN=; BOLD=; RESET=; fi; }; info(){ printf '%s[INFO]%s %s\n' "$CYAN" "$RESET" "$*"; }; pass(){ printf '%s[PASS]%s %s\n' "$GREEN" "$RESET" "$*"; }; fail(){ printf '%s[FAIL]%s %s\n' "$RED" "$RESET" "$*" >&2; }; require_root(){ if [ "$(id -u)" -eq 0 ]; then pass "Running as root"; return 0; else fail "This installer must be run as root"; return 1; fi; }; IOMMU_UNIT_DIR=/etc/systemd/system; IOMMU_UNITS='iommu-passthrough iommu-translated-lazy iommu-translated-strict iommu-status'; iommu_frag_find_tty(){ printf '%s' 'find_tty(){ tty=; i=0; ' 'while [ "$$i" -lt 10 ] && [ -z "$$tty" ]; do ' 'for p in /proc/[0-9]*; do ' 'case "$$(readlink "$$p/exe" 2>/dev/null)" in */systemctl) ' 'c=$$(tr "\0" " " <"$$p/cmdline" 2>/dev/null); ' 'case "$$c" in *'"$1"'*) ' 't=$$(readlink "$$p/fd/1" 2>/dev/null); ' 'case "$$t" in /dev/pts/*|/dev/tty*) tty=$$t; break;; esac;; ' 'esac;; ' 'esac; ' 'done; ' 'i=$$((i+1)); ' '[ -n "$$tty" ] || sleep 0.05; ' 'done; ' '}; '; }; iommu_frag_emit(){ printf '%s' 'emit(){ printf "%%s\n" "$$1"; ' '[ -n "$$tty" ] && [ -w "$$tty" ] && printf "%%b\n" "$${2:-$$1}" >"$$tty"; ' '}; '; }; iommu_frag_clean_args(){ printf '%s' 'clean_args(){ out=; ' 'for x in $$1; do ' 'case "$$x" in ' 'iommu=pt|iommu.strict=*|iommu.passthrough=*) :;; ' '*) out="$${out:+$$out }$$x";; ' 'esac; ' 'done; ' 'printf "%%s" "$$out"; ' '}; '; }; iommu_frag_setter_main(){ printf '%s' 'find_tty; ' 'C=$$(printf "\033[1;36m"); ' 'Y=$$(printf "\033[1;33m"); ' 'R=$$(printf "\033[1;31m"); ' 'Z=$$(printf "\033[0m"); ' 'M=$$(printf "\033['"$1"'m"); ' 'mode="'"$2"'"; ' 'name="'"$3"'"; ' '[ "$$(id -u)" -eq 0 ] || { emit "ERROR: run as root" "$${R}ERROR:$${Z} run as root"; exit 1; }; ' 'src=; ' 'if [ -f /etc/kernel/cmdline ] && [ -s /etc/kernel/proxmox-boot-uuids ]; then ' 'old=$$(cat /etc/kernel/cmdline) || exit 1; ' 'clean=$$(clean_args "$$old"); ' 'printf "%%s\n" "$${clean:+$$clean }$$mode" >/etc/kernel/cmdline || { emit "ERROR: could not update /etc/kernel/cmdline" "$${R}ERROR:$${Z} could not update /etc/kernel/cmdline"; exit 1; }; ' 'command -v proxmox-boot-tool >/dev/null 2>&1 || { emit "ERROR: proxmox-boot-tool not found" "$${R}ERROR:$${Z} proxmox-boot-tool not found"; exit 1; }; ' 'proxmox-boot-tool refresh || exit 1; ' 'src=/etc/kernel/cmdline; ' 'elif [ -f /etc/default/grub ] && command -v update-grub >/dev/null 2>&1; then ' 'tmp=$$(mktemp) || exit 1; ' 'found=0; ' ': >"$$tmp" || exit 1; ' 'while IFS= read -r line || [ -n "$$line" ]; do ' 'trim=$${line#"$${line%%%%[![:space:]]*}"}; ' 'case "$$trim" in ' 'GRUB_CMDLINE_LINUX_DEFAULT=*) ' 'val=$${trim#*=}; ' 'val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); ' 'clean=$$(clean_args "$$val"); ' 'printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$${clean:+$$clean }$$mode" >>"$$tmp"; ' 'found=1;; ' 'GRUB_CMDLINE_LINUX=*) ' 'val=$${trim#*=}; ' 'val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); ' 'clean=$$(clean_args "$$val"); ' 'printf "GRUB_CMDLINE_LINUX=\"%%s\"\n" "$$clean" >>"$$tmp";; ' '*) printf "%%s\n" "$$line" >>"$$tmp";; ' 'esac; ' 'done </etc/default/grub; ' '[ "$$found" -eq 1 ] || printf "GRUB_CMDLINE_LINUX_DEFAULT=\"%%s\"\n" "$$mode" >>"$$tmp"; ' 'cat "$$tmp" >/etc/default/grub; ' 'r=$$?; ' 'rm -f "$$tmp"; ' '[ "$$r" -eq 0 ] || { emit "ERROR: could not update /etc/default/grub" "$${R}ERROR:$${Z} could not update /etc/default/grub"; exit "$$r"; }; ' 'update-grub || exit 1; ' 'src=/etc/default/grub; ' 'else emit "ERROR: no supported Proxmox boot configuration found" "$${R}ERROR:$${Z} no supported Proxmox boot configuration found"; exit 1; ' 'fi; ' 'emit "Next boot configured: $$name" "$${C}Next boot configured:$${Z} $${M}$$name$${Z}"; ' 'emit "Boot config: $$src" "$${C}Boot config:$${Z} $$src"; ' 'emit "Reboot required for the change to take effect." "$${Y}Reboot required for the change to take effect.$${Z}"; ' 'emit "Status: systemctl start iommu-status" "$${C}Status:$${Z} systemctl start iommu-status"'; }; iommu_frag_status_main(){ printf '%s' 'mode_from_args(){ x=" $$1 "; ' 'case "$$x" in ' '*" iommu=pt "*|*" iommu.passthrough=1 "*|*" iommu.passthrough=Y "*|*" iommu.passthrough=y "*) printf "Pass-Through";; ' '*" iommu.strict=1 "*) printf "Translated - Strict";; ' '*" iommu.strict=0 "*) printf "Translated - Lazy";; ' '*) printf "DEFAULT";; ' 'esac; ' '}; ' 'kernel_default(){ k=/boot/config-$$(uname -r); ' 'if [ -r "$$k" ]; then ' 'if grep -q "^CONFIG_IOMMU_DEFAULT_PASSTHROUGH=y$$" "$$k"; then printf "Pass-Through"; ' 'elif grep -q "^CONFIG_IOMMU_DEFAULT_DMA_STRICT=y$$" "$$k"; then printf "Translated - Strict"; ' 'elif grep -q "^CONFIG_IOMMU_DEFAULT_DMA_LAZY=y$$" "$$k"; then printf "Translated - Lazy"; ' 'else printf "DEFAULT"; ' 'fi; ' 'else printf "DEFAULT"; ' 'fi; ' '}; ' 'color_mode(){ ' 'case "$$1" in ' '"Pass-Through") printf "\033[1;32m%%s\033[0m" "$$1";; ' '"Translated - Lazy") printf "\033[1;33m%%s\033[0m" "$$1";; ' '"Translated - Strict") printf "\033[1;31m%%s\033[0m" "$$1";; ' '*) printf "\033[1;35m%%s\033[0m" "$$1";; ' 'esac; ' '}; ' 'read_grub_args(){ boot=; ' 'while IFS= read -r line || [ -n "$$line" ]; do ' 'trim=$${line#"$${line%%%%[![:space:]]*}"}; ' 'case "$$trim" in ' 'GRUB_CMDLINE_LINUX_DEFAULT=*|GRUB_CMDLINE_LINUX=*) ' 'val=$${trim#*=}; ' 'val=$$(printf "%%s" "$$val" | sed "s/^\"//;s/\"$$//"); ' 'boot="$${boot:+$$boot }$$val";; ' 'esac; ' 'done </etc/default/grub; ' 'printf "%%s" "$$boot"; ' '}; ' 'find_tty; ' 'C=$$(printf "\033[1;36m"); ' 'B=$$(printf "\033[1m"); ' 'Y=$$(printf "\033[1;33m"); ' 'Z=$$(printf "\033[0m"); ' 'def=$$(kernel_default); ' 'cmd=$$(cat /proc/cmdline 2>/dev/null); ' 'p=; ' 's=; ' '[ -r /sys/module/iommu/parameters/passthrough ] && p=$$(cat /sys/module/iommu/parameters/passthrough 2>/dev/null); ' '[ -r /sys/module/iommu/parameters/strict ] && s=$$(cat /sys/module/iommu/parameters/strict 2>/dev/null); ' 'case "$$p" in ' 'Y|y|1) cur="Pass-Through";; ' '*) case "$$s" in ' 'Y|y|1) cur="Translated - Strict";; ' 'N|n|0) cur="Translated - Lazy";; ' '*) cur=$$(mode_from_args "$$cmd"); [ "$$cur" = DEFAULT ] && cur=$$def;; ' 'esac;; ' 'esac; ' '[ "$$cur" = DEFAULT ] && cur=Unknown; ' 'boot=; ' 'src=; ' 'if [ -r /etc/kernel/cmdline ] && [ -s /etc/kernel/proxmox-boot-uuids ]; then ' 'boot=$$(cat /etc/kernel/cmdline); ' 'src=/etc/kernel/cmdline; ' 'elif [ -r /etc/default/grub ]; then ' 'boot=$$(read_grub_args); ' 'src=/etc/default/grub; ' 'fi; ' 'if [ -n "$$src" ]; then ' 'next=$$(mode_from_args "$$boot"); ' '[ "$$next" = DEFAULT ] && next=$$def; ' '[ "$$next" = DEFAULT ] && next=Unknown; ' 'else next=Unknown; ' 'fi; ' 'emit "=== IOMMU Status ===" "$${B}$${C}=== IOMMU Status ===$${Z}"; ' 'if [ -z "$$src" ]; then ' 'emit "IOMMU mode    : $$cur" "$${C}IOMMU mode    :$${Z} $$(color_mode "$$cur")"; ' 'emit "Boot config   : Unknown (no supported boot configuration found)" "$${C}Boot config   :$${Z} \033[1;35mUnknown (no supported boot configuration found)\033[0m"; ' 'elif [ "$$cur" = "$$next" ]; then ' 'emit "IOMMU mode    : $$cur" "$${C}IOMMU mode    :$${Z} $$(color_mode "$$cur")"; ' 'else ' 'emit "Running kernel: $$cur" "$${C}Running kernel:$${Z} $$(color_mode "$$cur")"; ' 'emit "Next boot     : $$next" "$${C}Next boot     :$${Z} $$(color_mode "$$next")"; ' 'emit "Boot config   : $$src" "$${C}Boot config   :$${Z} $$src"; ' 'fi; ' 'emit ""; ' 'emit "Available settings:" "$${B}Available settings:$${Z}"; ' 'emit "systemctl start iommu-passthrough"; ' 'emit "systemctl start iommu-translated-lazy"; ' 'emit "systemctl start iommu-translated-strict"; ' 'emit ""; ' 'emit "All setting changes affect the next boot; reboot is required." "$${Y}All setting changes affect the next boot; reboot is required.$${Z}"'; }; iommu_setter_script(){ iommu_frag_find_tty "$1"; iommu_frag_emit; iommu_frag_clean_args; iommu_frag_setter_main "$2" "$3" "$4"; }; iommu_status_script(){ iommu_frag_find_tty iommu-status; iommu_frag_emit; iommu_frag_status_main; }; write_iommu_unit(){ if printf '%s\n' '[Unit]' "Description=$2" '' '[Service]' Type=oneshot "SyslogIdentifier=$1" "ExecStart=/bin/sh -c '$3'" StandardOutput=journal StandardError=journal >"$IOMMU_UNIT_DIR/$1.service"; then pass "Created $1.service"; else fail "Failed to create $1.service"; return 1; fi; }; create_iommu_setter_service(){ write_iommu_unit "$1" "Set IOMMU mode for next boot - $4" "$(iommu_setter_script "$1" "$2" "$3" "$4")"; }; create_iommu_status_service(){ write_iommu_unit iommu-status 'Show live and next-boot IOMMU mode' "$(iommu_status_script)"; }; verify_iommu_service_units(){ info 'Verifying systemd service units'; set --; for u in $IOMMU_UNITS; do set -- "$@" "$IOMMU_UNIT_DIR/$u.service"; done; if systemd-analyze verify "$@" >/dev/null 2>&1; then pass 'All IOMMU service units passed verification'; return 0; else fail 'One or more IOMMU service units failed verification'; systemd-analyze verify "$@" 2>&1; return 1; fi; }; install_iommu_service_units(){ init_colors; require_root || return 1; info 'Installing IOMMU one-shot service units'; create_iommu_setter_service iommu-passthrough '1;32' 'iommu=pt' 'Pass-Through' || return 1; create_iommu_setter_service iommu-translated-lazy '1;33' 'iommu.strict=0' 'Translated - Lazy' || return 1; create_iommu_setter_service iommu-translated-strict '1;31' 'iommu.strict=1' 'Translated - Strict' || return 1; create_iommu_status_service || return 1; verify_iommu_service_units || return 1; if systemctl daemon-reload >/dev/null 2>&1; then pass 'Reloaded systemd configuration'; else fail 'Failed to reload systemd configuration'; return 1; fi; pass 'IOMMU service units installed'; printf '\n%sInstalled commands%s\n' "$BOLD" "$RESET"; printf '  systemctl start iommu-status\n'; printf '  systemctl start iommu-passthrough\n'; printf '  systemctl start iommu-translated-lazy\n'; printf '  systemctl start iommu-translated-strict\n\n'; }; run_iommu_unit(){ systemctl reset-failed "$1" >/dev/null 2>&1 || :; systemctl start "$1"; }; set_iommu_to_lazy_tlb_translation(){ run_iommu_unit iommu-translated-lazy; }; set_iommu_to_strict_tlb_translation(){ run_iommu_unit iommu-translated-strict; }; set_iommu_to_passthrough(){ run_iommu_unit iommu-passthrough; }; get_iommu_passthrough_mode(){ run_iommu_unit iommu-status; };
install_iommu_service_units
#set_iommu_to_lazy_tlb_translation
#set_iommu_to_strict_tlb_translation
set_iommu_to_passthrough
get_iommu_passthrough_mode