tap(4) and sudo cleanup for QEMU, from Brad.

This commit is contained in:
sthen 2015-10-28 09:17:31 +00:00
parent 56c73b96b4
commit 783a43b006
4 changed files with 26 additions and 31 deletions

View File

@ -1,11 +1,11 @@
# $OpenBSD: Makefile,v 1.133 2015/10/23 15:15:28 sthen Exp $
# $OpenBSD: Makefile,v 1.134 2015/10/28 09:17:31 sthen Exp $
ONLY_FOR_ARCHS= amd64 i386 powerpc sparc64
COMMENT= multi system emulator
DISTNAME= qemu-2.2.1
REVISION= 9
REVISION= 10
CATEGORIES= emulators
MASTER_SITES= http://wiki.qemu.org/download/
EXTRACT_SUFX= .tar.bz2

View File

@ -8,7 +8,7 @@ _BRIDGE=bridge0
[ "$ETHER" ] || ETHER=${_ETHER}
if test `id -u` -ne 0; then
SUDO=doas
DOAS=doas
fi
$SUDO ifconfig $BRIDGE del $1 > /dev/null 2>&1
$DOAS ifconfig $BRIDGE del $1 > /dev/null 2>&1

View File

@ -9,35 +9,32 @@ _BRIDGE=bridge0
[ "$ETHER" ] || ETHER=${_ETHER}
if test `id -u` -ne 0; then
SUDO=doas
DOAS=doas
fi
echo -n " {$1 ($BRIDGE <-> $ETHER)"
# Set the tun device into layer2 mode
$SUDO ifconfig $1 link0 up
# Set up our bridge
$SUDO ifconfig $1 group tun > /dev/null 2>&1
$SUDO ifconfig $BRIDGE create > /dev/null 2>&1 && {
$DOAS ifconfig $1 group tap > /dev/null 2>&1
$DOAS ifconfig $BRIDGE create > /dev/null 2>&1 && {
# Only add rules if the bridge creation succeeds; otherwise
# duplicate rules get loaded each time qemu starts
# The following two block carp packets from wasting cpu cycles inside the
# qemu sessions, remove if testing carp inside qemu
$SUDO ifconfig $BRIDGE rule block in on $ETHER dst 33:33:0:0:0:12
$SUDO ifconfig $BRIDGE rule block in on $ETHER dst 01:00:5e:00:00:12
$DOAS ifconfig $BRIDGE rule block in on $ETHER dst 33:33:0:0:0:12
$DOAS ifconfig $BRIDGE rule block in on $ETHER dst 01:00:5e:00:00:12
}
# Since we can specify ETHER and BRIDGE above, its possible that
# this tun interface or this physical interface was setup as part of
# this tap interface or this physical interface was setup as part of
# a different bridge earlier, and that is never cleaned up, so we have
# to cleanup here first before we set it up; a physical interface cannot
# be member to more than one bridge, thankfully, or I never would have
# caught this
ifconfig bridge | sed -n '/^bridge[0-9]*/{s/:.*$//;p;}' | while read brif
do
$SUDO ifconfig $brif del $ETHER > /dev/null 2>&1
$SUDO ifconfig $brif del $1 > /dev/null 2>&1
$DOAS ifconfig $brif del $ETHER > /dev/null 2>&1
$DOAS ifconfig $brif del $1 > /dev/null 2>&1
done
$SUDO ifconfig $BRIDGE add $ETHER up
$SUDO ifconfig $BRIDGE add $1 up || true
$DOAS ifconfig $BRIDGE add $ETHER up
$DOAS ifconfig $BRIDGE add $1 up || true
echo "}"

View File

@ -1,4 +1,4 @@
$OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
$OpenBSD: README,v 1.12 2015/10/28 09:17:31 sthen Exp $
+-----------------------------------------------------------------------
| Running ${FULLPKGNAME} on OpenBSD
@ -92,20 +92,19 @@ $OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
Sometimes it is desirable to configure QEMU to access a network
via layer2 directly. One way of doing this without having to run
QEMU as root is to let root open /dev/tunN and pass the file
descriptor to QEMU. The tun(4) interface should preferrably be
QEMU as root is to let root open /dev/tapN and pass the file
descriptor to QEMU. The tap(4) interface should preferrably be
configured before starting QEMU:
$ doas ifconfig tun0 192.168.0.254 link0
$ doas ifconfig tap0 192.168.0.254
The interface can also be configured as part of a bridge(4), in
which case the ip address can be omitted:
$ doas ifconfig tun0 link0
$ doas ifconfig bridge0 add tun0 add em0 up
$ doas ifconfig bridge0 add tap0 add em0 up
The tunnel and bridge interfaces can also be configured at system
startup by editing /etc/hostname.tunN and /etc/hostname.bridgeN,
startup by editing /etc/hostname.tapN and /etc/hostname.bridgeN,
respectively (see hostname.if(5)).
After configuring the virtual network we can use doas to let
@ -113,7 +112,7 @@ $OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
privileges and start QEMU:
$ doas sh -c "doas -u $USER qemu-system-i386 -nographic -net nic \
-net tap,fd=3 -no-fd-bootchk -hda virtual.img 3<>/dev/tun0"
-net tap,fd=3 -no-fd-bootchk -hda virtual.img 3<>/dev/tap0"
NOTE: if you use sudo instead of doas, remember that sudo calls
closefrom(2). In order to have more than one fd passed tap
@ -134,7 +133,7 @@ $OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
# qemu-system-i386 -net nic -net tap -no-fd-bootchk -hda virtual.img
It presumes you wish the tun(4) interface to talk to the interface
It presumes you wish the tap(4) interface to talk to the interface
holding the default IPv4 route (falling back to trunk0 if no
route is found), and that you want 'bridge0' to be used to bridge
the two.
@ -160,7 +159,7 @@ $OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
# qemu-system-i386 -net nic,vlan=0,macaddr=52:54:00:12:35:00 \
-net tap,vlan=0 -vnc :0 -localtime -usb -usbdevice tablet \
-m 256 -no-fd-bootchk -hda virtual.img -monitor stdio
{tun0 (bridge101 <-> trunk101)ifconfig: bridge101: No such process
{tap0 (bridge101 <-> trunk101)ifconfig: bridge101: No such process
ifconfig: bridge101: No such process
}
(qemu)
@ -174,7 +173,7 @@ $OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
groups: bridge
priority 32768 hellotime 2 fwddelay 15 maxage 20 holdcnt 6 proto rstp
designated: id 00:00:00:00:00:00 priority 0
tun0 flags=3<LEARNING,DISCOVER>
tap0 flags=3<LEARNING,DISCOVER>
port 16 ifpriority 0 ifcost 0
trunk101 flags=3<LEARNING,DISCOVER>
port 6 ifpriority 0 ifcost 0
@ -248,8 +247,7 @@ $OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
if [ -x ${TRUEPREFIX}/bin/qemu ]; then
echo -n 'Qemu: vmi386'
(
ifconfig tun0 link0
ifconfig bridge101 add trunk101 add tun0 up
ifconfig bridge101 add trunk101 add tap0 up
sh -c "doas -u $USER \
${TRUEPREFIX}/bin/qemu-system-i386 \
@ -261,7 +259,7 @@ $OpenBSD: README,v 1.11 2015/08/03 19:14:56 jung Exp $
-hda $hddir/virtual.img \
-serial telnet:127.0.0.1:1080,server,nowait \
-monitor telnet:127.0.0.1:1081,server,nowait \
-no-fd-bootchk 3<>/dev/tun0"
-no-fd-bootchk 3<>/dev/tap0"
)
echo "."
fi