0

I'm learning how to passthrough my NIC to my KVM.

This guide says to add the following code to the file ~/.bashrc

#!/bin/bash
# change the 999 if needed
shopt -s nullglob
for d in /sys/kernel/iommu_groups/{0..999}/devices/*; do
    n=${d#*/iommu_groups/*}; n=${n%%/*}
    printf 'IOMMU Group %s ' "$n"
    lspci -nns "${d##*/}"
done;

I'm unclear where the code should be inserted, so it's in lines 1-8.

  1 #!/bin/bash
  2 # change the 999 if needed
  3 shopt -s nullglob
  4 for d in /sys/kernel/iommu_groups/{0..999}/devices/*; do
  5         n=${d#*/iommu_groups/*}; n=${n%%/*}
  6         printf 'IOMMU Group %s ' "$n"
  7         lspci -nns "${d##*/}"
  8 done;
  9 
 10 # ~/.bashrc: executed by bash(1) for non-login shells.
 11 # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
 12 # for examples
 13 
 14 # If not running interactively, don't do anything
 15 case $- in
 16     *i*) ;;
 17       *) return;;
 18 esac
 19 
 20 # don't put duplicate lines or lines starting with space in the history.
 21 # See bash(1) for more options
 22 HISTCONTROL=ignoreboth
 23 
 24 # append to the history file, don't overwrite it
 25 shopt -s histappend
 26 
 27 # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
 28 HISTSIZE=1000
 29 HISTFILESIZE=2000
 30 
 31 # check the window size after each command and, if necessary,
 32 # update the values of LINES and COLUMNS.
 33 shopt -s checkwinsize
 34 
 35 # If set, the pattern "**" used in a pathname expansion context will
 36 # match all files and zero or more directories and subdirectories.
 37 #shopt -s globstar
 38 
 39 # make less more friendly for non-text input files, see lesspipe(1)
 40 [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
 41 
 42 # set variable identifying the chroot you work in (used in the prompt below)
 43 if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
 44     debian_chroot=$(cat /etc/debian_chroot)
 45 fi
 46 
 47 # set a fancy prompt (non-color, unless we know we "want" color)
 48 case "$TERM" in
 49     xterm-color|*-256color) color_prompt=yes;;
 50 esac
 51 
 52 # uncomment for a colored prompt, if the terminal has the capability; turned
 53 # off by default to not distract the user: the focus in a terminal window
 54 # should be on the output of commands, not on the prompt
 55 #force_color_prompt=yes
 56 
 57 if [ -n "$force_color_prompt" ]; then
 58     if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
 59         # We have color support; assume it's compliant with Ecma-48
 60         # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
 61         # a case would tend to support setf rather than setaf.)
 62         color_prompt=yes
 63     else
 64         color_prompt=
 65     fi
 66 fi
 67 
 68 if [ "$color_prompt" = yes ]; then
 69     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$     '
 70 else
 71     PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 72 fi
 73 unset color_prompt force_color_prompt
 74 
 75 # If this is an xterm set the title to user@host:dir
 76 case "$TERM" in
 77 xterm*|rxvt*)
 78     PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
 79     ;;
 80 *)
 81     ;;
 82 esac
 83 
 84 # enable color support of ls and also add handy aliases
 85 if [ -x /usr/bin/dircolors ]; then
 86     test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
 87     alias ls='ls --color=auto'
 88     #alias dir='dir --color=auto'
 89     #alias vdir='vdir --color=auto'
 90 
 91     alias grep='grep --color=auto'
 92     alias fgrep='fgrep --color=auto'
 93     alias egrep='egrep --color=auto'
 94 fi
 95 
 96 # colored GCC warnings and errors
 97 #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
 98 
 99 # some more ls aliases
100 alias ll='ls -alF'
101 alias la='ls -A'
102 alias l='ls -CF'
103 
104 # Add an "alert" alias for long running commands.  Use like so:
105 #   sleep 10; alert
106 alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1    |sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
107 
108 # Alias definitions.
109 # You may want to put all your additions into a separate file like
110 # ~/.bash_aliases, instead of adding them here directly.
111 # See /usr/share/doc/bash-doc/examples in the bash-doc package.
112 
113 if [ -f ~/.bash_aliases ]; then
114     . ~/.bash_aliases
115 fi
116 
117 # enable programmable completion features (you don't need to enable
118 # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
119 # sources /etc/bash.bashrc).
120 if ! shopt -oq posix; then
121   if [ -f /usr/share/bash-completion/bash_completion ]; then
122     . /usr/share/bash-completion/bash_completion
123   elif [ -f /etc/bash_completion ]; then
124     . /etc/bash_completion
125   fi
126 fi

However this causes an error when running .bashrc with the command ./.bashrc

ubuntu@ubuntu:~$ ./.bashrc 
./.bashrc: line 17: return: can only `return' from a function or sourced script

1 Answers1

6

The guide you are following does not tell you to add this code to ~/.bashrc, it tells you to run it as a script. There is no mention of ~/.bashrc anywhere in that page. And that makes sense. Any commands in ~/.bashrc are executed every time you start a new interactive non-login shell session. So every time you open a terminal. You don't want to be running this code every time!

And there's no need to, the code isn't designed for that, note how it will print out a message for every one of the files it finds.

Finally, the code itself is written as a script, starting with a #!/bin/bash shebang line, further indicating that it has no business being in ~/.bashrc, and in any case, that code doesn't do anything except print out information:

  • shopt -s nullglob : turn on the nullglob option.
  • for d in /sys/kernel/iommu_groups/{0..999}/devices/*; do: iterate over all files in /sys/kernel/iommu_groups/{0..999}/devices/, meaning all directories in /sys/kernel/iommu_groups/ whose name is a number between 0 and 999, and then all files in all devices subditctories under them.
  • n=${d#*/iommu_groups/*}; n=${n%%/*}: use shell string manipulation to extract the name of the IOMMU group, the name of the current /sys/kernel/iommu_groups/ subdirectory (this is a strangely convoluted way of doing this, but that's what the guide recommends).
  • lspci -nns "${d##*/}": use the lspci command to print out the textual and numeric IDs of the devices these point to.

So this is nothing but an easy way of extracting information for you to see and then use. There is nothing automatic here, it just prints stuff to the terminal.

For the sake of completion, here's a simpler way of doing the same thing, but don't put this in your ~/.bashrc, just run it, once, and look at the output then follow the guide. But please try to read the guide more carefully this time!

$ for d in /sys/kernel/iommu_groups/[0-9]*; do 
  [ -e "$d"/devices ] && printf 'IOMMU Group %s\n' "${d##*/}" ; 
done 

On my system, that returns:

IOMMU Group 0
IOMMU Group 1
IOMMU Group 10
IOMMU Group 11
IOMMU Group 12
IOMMU Group 13
IOMMU Group 14
IOMMU Group 15
IOMMU Group 16
IOMMU Group 17
IOMMU Group 18
IOMMU Group 2
IOMMU Group 3
IOMMU Group 4
IOMMU Group 5
IOMMU Group 6
IOMMU Group 7
IOMMU Group 8
IOMMU Group 9
terdon
  • 100,812
  • Thanks for explaining and not downvoting a learner with a well crafted question or leaving feedback how a question can be better crafted to not be downvoted for asking a question. As a learner the confusion was the link to 'one can use a bashscript' https://askubuntu.com/questions/471285/how-to-create-execute-a-script-file/471286#471286. As a learner looking for the location, this link's guide shows ~/bin, but I found .bashrc in ~/, so I assumed yet another guide with typos and worked in the ~/.bashrc location and file. – user1416486 Oct 02 '23 at 00:22