summaryrefslogtreecommitdiff
path: root/subr_virt.sh
blob: ac6e2143153157518f748bb76e17b6f2ad4841e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
do_virt () {
    local o console initdest debug
    OPTIND=1
    while getopts cdi o; do
	case "$o" in
	    c) console="true" ;;
	    d) debug="true" ;;
	    i) initdest="true" ;;
	esac
    done
    shift `expr $OPTIND - 1 || true`

    local basevol="$1"
    local destvol="$2"
    local cmd="$3"
    local env="$4"
    local log="$5"
    
    local instn n0 n1 l e
    local bhparms tempdir result jobid mac ip
    local osrelnum iodisk ionet namedisk namenet

    local mul mem=${conf_Vmem%% *}
    if test "${conf_Vmem#* }"; then
	if echo $mem | grep -q '[kKmMgGtT]'; then
	    mul=`echo $mem | sed "s/.*\(.\)/\1/"`
	    mem=`echo $mem | sed "s/\(.*\)./\1/"`
	fi
	mem=`expr $mem + ${conf_Vmem#* } "*" $conf_ncpu`$mul
    fi

    osrelnum=`eval echo \\$conf_${Parm_Orig}_src`
    osrelnum=${osrelnum##*/}
    osrelnum=${osrelnum%.*}
    if test "$osrelnum" -lt 12; then
	iodisk=ahci-hd
	namedisk=ada
	ionet=e1000
	namenet=em
    else
	iodisk=virtio-blk
	namedisk=vtbd
	ionet=virtio-net
	namenet=vtnet
    fi
    env="$env osrelnum=$osrelnum"

    env="$env $conf_Virtenv"

    env="$env LC_CTYPE=en_US.UTF-8"
    env="$env TERM=xterm-mono"
    # port-make sucht die prereq-executables im PATH:
    env="$env PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"    
    env="$env conf_ncpu=$conf_ncpu"
    env="$env conf_Localname=$conf_Localname"
    env="$env conf_Vwdir=$conf_Vwdir"
    env="$env conf_Mediauri=$conf_Mediauri"

    env="$env Parm_Dialog=$Parm_Dialog"
    env="$env Parm_Dest=$Parm_Dest"
    env="$env Parm_Plvl=$Parm_Plvl"

    instn=0
    
    do_lock vmmselect
    while test -e /dev/vmm/build-$instn; do
	instn=`expr $instn + 1`
    done
    n0=`expr $instn "*" 2 + $conf_Devoffs`
    n1=`expr $n0 + 1`
    logmsg "virt: setup $instn"
    
    # in R.13 gibt es eine direkte netgraph-integration von bhyve,
    # also kein tap0 mehr nötig.
    ngctl info ${conf_Vbridge}: > /dev/null 2>&1 || \
	kill_prog "netgraph bridge not found"
    tapnr=`expr $instn + $conf_Devoffs`
    if ! ifconfig tap$tapnr create; then
	ifconfig tap$tapnr destroy
	ifconfig tap$tapnr create || \
	    kill_prog "failed to create tap device"
    fi
    add_cleanup "ifconfig tap$tapnr destroy"

    l=$instn
    while ngctl msg ${conf_Vbridge}: getstats $l > /dev/null 2>&1; do
	l=`expr $l + 1`
    done
    ngctl connect ${conf_Vbridge}: tap${tapnr}: link$l lower || \
	kill_prog "failed to attach to bridge"
    ngctl msg tap${tapnr}: setpromisc 1
    ngctl msg tap${tapnr}: setautosrc 0

    mac=`printf $conf_Vmacbase $instn`
    bhparms="-c $conf_ncpu -m ${mem} \
		    -H -A -P -u \
		    -s 0:0,hostbridge \
		    -s 1:0,lpc \
		    -s 2:0,$ionet,tap$tapnr,mac=$mac \
		    -s 3:0,$iodisk,/dev/zvol/$basevol \
		    -l com1,/dev/nmdm${n0}A \
		    -l com2,/dev/nmdm${n1}A"
    if test "$conf_Vclass"; then
	bhparms="$bhparms -L $conf_Vclass"
    fi

    # prepare volume for guest's extra zfs
    stk_do zfs-crvol $conf_Vzfs \
	   "refreservation=none volblocksize=131072"
    local vzfs=/dev/zvol/`stk_return`
    bhparms="$bhparms -s 4:0,$iodisk,$vzfs"

    stk_do mount ufs /dev/zvol/${basevol}p3
    tempdir=`stk_return`
    echo "/dev/${namedisk}0p2 none swap sw,trimonce,late 0 0" \
	 > $tempdir/etc/fstab
    echo "/dev/${namedisk}0p3 /    ufs  rw               1 1" \
	 >> $tempdir/etc/fstab
    tzsetup -C $tempdir -s Europe/Berlin
    
    echo 'vfs.zfs.arc_max="900M"' > $tempdir/boot/loader.conf
    ip=${conf_Vipbase%.*}.`expr "${conf_Vipbase##*.}" + $instn`
    echo 'hostname="'`printf $conf_Vname $instn`'"' > $tempdir/etc/rc.conf
    echo "ifconfig_${namenet}0="'"inet'" $ip netmask $conf_Vipmask"'"' \
	 >> $tempdir/etc/rc.conf
    echo 'defaultrouter="'"$conf_Vipgw"'"' >> $tempdir/etc/rc.conf
    echo 'cron_enable="NO"' >> $tempdir/etc/rc.conf
    echo 'ntpd_enable="YES"' >> $tempdir/etc/rc.conf
    echo 'ntpd_sync_on_start="YES"' >> $tempdir/etc/rc.conf
    if test "$conf_Vntp"; then
	echo "server $conf_Vntp iburst" > $tempdir/etc/ntp.conf
    fi
    echo "nameserver $conf_Vdns" > $tempdir/etc/resolv.conf
    echo "me:setenv=TERM=xterm-mono,LC_CTYPE=en_US.UTF-8:" \
	 > $tempdir/root/.login_conf
    echo "vfs.zfs.txg.timeout=60" > $tempdir/etc/sysctl.conf
    # graphics/oidn gibt oom-kill, scheint damit zu klappen:
    echo "vm.panic_on_oom=1" >> $tempdir/etc/sysctl.conf
    echo "vm.pfault_oom_attempts=60" >> $tempdir/etc/sysctl.conf
    echo "vm.pfault_oom_wait=120" >> $tempdir/etc/sysctl.conf    
    sed -I "" "/^ttyu0[ 	]/s/[ 	]off[ 	]/ on /" $tempdir/etc/ttys

    # put tasks and job into guest's root filesystem
    rm -rf $tempdir/$conf_Vwdir
    mkdir -p $tempdir/$conf_Vwdir
    cp $conf_Incdir/virtlib.sh $tempdir/$conf_Vwdir
    cp $conf_Incdir/subr-common.sh $tempdir/$conf_Vwdir
    for e in $env; do
	echo "export $e" >> $tempdir/$conf_Vwdir/Job
    done
    echo ". $conf_Vwdir/subr-common.sh" >> $tempdir/$conf_Vwdir/Job
    echo ". $conf_Vwdir/virtlib.sh" >> $tempdir/$conf_Vwdir/Job

    if test "$destvol"; then
	bhparms="$bhparms -s 5:0,$iodisk,/dev/zvol/$destvol"
	if test "$initdest"; then
	    echo "prepare_vol /dev/${namedisk}2 $conf_Vswap && \\" \
		 >> $tempdir/$conf_Vwdir/Job
	fi
	echo "v_mount ufs /dev/${namedisk}2p3 /mnt && \\" \
	     >> $tempdir/$conf_Vwdir/Job
    fi
    echo "v_init && \\" >> $tempdir/$conf_Vwdir/Job    
    echo "$cmd" >> $tempdir/$conf_Vwdir/Job
    echo "echo \$? > $conf_Vwdir/Result" >> $tempdir/$conf_Vwdir/Job
    echo "shutdown -p now" >> $tempdir/$conf_Vwdir/Job

    # braucht -f weil in rc.local gar kein stdio (0x0, nichtmal /dev/null)
    echo "/usr/sbin/daemon -f sh -c 'time -lpo $conf_Vwdir/Sst \
          sh $conf_Vwdir/Job > $conf_Vwdir/Log 2>&1'" > $tempdir/etc/rc.local
    stk_undo # mount

    # extremer performance-verlust mit IBRS (sollte eh RETPOLINE machen):
    if test "$debug"; then
	sysctl hw.ibrs_disable=1
	sysctl net.link.tap.up_on_open=1
    else
	sysctl hw.ibrs_disable=1 >> $log.log
	sysctl net.link.tap.up_on_open=1 >> $log.log
    fi

    logmsg "virt: startup $conf_ncpu $conf_Vmem"
    /usr/sbin/bhyveload -d /dev/zvol/$basevol \
			-m $mem \
			-c /dev/nmdm${n0}A \
			-e autoboot_delay=1 \
			build-$instn || {
	kill_prog "failed to init bhyve"
    }
    add_cleanup "/usr/sbin/bhyvectl --vm=build-$instn --destroy"
    do_unlock vmmselect
    if test "$console" -a "$Parm_Dialog" = "true"; then
	if test -z "$conf_Console"; then
	    set -m
	    cu -l /dev/nmdm${n1}B &
	    jobid=$!
	    ( /usr/sbin/bhyve $bhparms build-$instn
	      kill $jobid
	    ) &
	    fg $jobid
	    set +m
	else
	    eval `printf "$conf_Console" "cu -l /dev/nmdm${n1}B"` &
	    jobid=$!
	    /usr/sbin/bhyve $bhparms build-$instn
	    kill $jobid
	fi
    else
	/usr/sbin/bhyve $bhparms build-$instn
    fi
    /usr/sbin/bhyvectl --vm=build-$instn --destroy
    cleanup=`expr $cleanup - 1 || true`
    ifconfig tap$tapnr destroy
    cleanup=`expr $cleanup - 1 || true`

    stk_do mount ufs /dev/zvol/${basevol}p3
    tempdir=`stk_return`
    result=`cat $tempdir/$conf_Vwdir/Result || echo 1`
    if test -z "$debug"; then
	cat $tempdir/$conf_Vwdir/Log >> $log.log
	cp $tempdir/$conf_Vwdir/Sst $log.sst
	if test -f $tempdir/$conf_Vwdir/Message; then
	    cat $tempdir/$conf_Vwdir/Message | while read M; do
		echo "$M"
		logmsg "$M"
	    done    
	fi
    fi
    stk_undo # mount
    
    stk_undo # vzfs
    logmsg "virt: terminated $instn"
    return $result
}