Check-in [ba3ea6d217]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:WIP: Merge DragonFly bits from 2.3 -- untested
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dragonfly
Files: files | file ages | folders
SHA1:ba3ea6d217ff83e9ee9777f140a1864f0a5b9bd0
User & Date: marino 2013-04-20 01:15:11
Context
2013-05-19
08:16
Merge with trunk at branch for 3.0 Resolved conflicts for: poudriere.8 bulk.sh common.sh jail.sh testport.sh Leaf check-in: 469126adaf user: marino tags: dragonfly
2013-04-20
01:15
WIP: Merge DragonFly bits from 2.3 -- untested check-in: ba3ea6d217 user: marino tags: dragonfly
2013-04-17
16:48
Sync with trunk (resolve 1 conflict) check-in: d7a00fbd93 user: marino tags: dragonfly
Changes

Changes to conf/poudriere.conf.sample.

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# export HTTP_PROXY=bla
# export FTP_PROXY=bla
#
# Cleanout the restricted packages
# NO_RESTRICTED=yes

# Set the host for the git update method
GIT_HOST=koan.bondconsult.net

# Set the URL for the dports git repository
DPORTS_URL=git://github.com/jrmarino/DPorts.git

# Set the localhost path for rsync (cpdup) to update dports repository
# DPORTS_RSYNC_LOC=/home/generic/dports-merged








|







112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# export HTTP_PROXY=bla
# export FTP_PROXY=bla
#
# Cleanout the restricted packages
# NO_RESTRICTED=yes

# Set the host for the git update method
DFLY_GIT_URL=git://koan.bondconsult.net/dragonfly.git

# Set the URL for the dports git repository
DPORTS_URL=git://github.com/jrmarino/DPorts.git

# Set the localhost path for rsync (cpdup) to update dports repository
# DPORTS_RSYNC_LOC=/home/generic/dports-merged

Changes to poudriere.8.

58
59
60
61
62
63
64



65
66
67
68
69
70
71
...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
...
227
228
229
230
231
232
233

























234
235
236
237
238
239
240
and get stats and progress back.
.It jail
This command gives you control to manage the jails used by
.Nm
to provide building environment (different
.Dx versions, different
architectures).



.It ports
This command allows you to manage different portstrees which will be used
by
.Nm
(create, update and delete portstrees).
.It testport
This command, mainly targeted at ports developers, launches a
................................................................................
List all available jails.
.It Fl s
Starts a jail.
.It Fl k
Kills a jail (stops it).
.It Fl u
Update a jail using full buildworld.
.It Fl i
Show information about the jail.
.It Fl C
Attempts to remove all poudriere mounts.  It is normally used to clean up
leftover mounts after aborting a run with control-c.
.El
.Pp
Except for
.Fl l ,
all of the subcommands require the
.Fl j
option (see below).
................................................................................
.Ar mountpoint
when creating jail.
.It Fl t Ar version
instead of upgrading to the latest security fix of the jail version, you can
jump to the new specified
.Ar version .
.El

























.Ss ports
.Pp
These subcommands are mutually exclusive.
.Bl -tag -width "-f conffile"
.It Fl c
Creates a ports tree.
.It Fl d







>
>
>







 







<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
...
179
180
181
182
183
184
185





186
187
188
189
190
191
192
...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
and get stats and progress back.
.It jail
This command gives you control to manage the jails used by
.Nm
to provide building environment (different
.Dx versions, different
architectures).
.It combo
This command works on jail-port build combinations.  The options are
kept out of bulk to avoid damage by a command to a running bulk build.
.It ports
This command allows you to manage different portstrees which will be used
by
.Nm
(create, update and delete portstrees).
.It testport
This command, mainly targeted at ports developers, launches a
................................................................................
List all available jails.
.It Fl s
Starts a jail.
.It Fl k
Kills a jail (stops it).
.It Fl u
Update a jail using full buildworld.





.El
.Pp
Except for
.Fl l ,
all of the subcommands require the
.Fl j
option (see below).
................................................................................
.Ar mountpoint
when creating jail.
.It Fl t Ar version
instead of upgrading to the latest security fix of the jail version, you can
jump to the new specified
.Ar version .
.El
.Ss combo
.pp
These subcommands are mutually exclusive.
.Bl -tag -width "-f conffile"
.It Fl C
Attempts to remove all poudriere mounts.  It is normally used to clean up
leftover mounts after aborting a run with control-c.
.It Fl i
Show information about the jail.
.It Fl x
List all failed ports in last or ongoing build
.El
.Pp
Here are the options associated with the
.Cm combo
command.
.Bl -tag -width "-f conffile"
.It Fl j Ar name
If given, reference bulk build using the jail name
.Ar name
.It Fl p Ar tree
This required flag specifies on which ports
.Ar tree
is used for the reference bulk build.
.El
.Ss ports
.Pp
These subcommands are mutually exclusive.
.Bl -tag -width "-f conffile"
.It Fl c
Creates a ports tree.
.It Fl d

Changes to src/share/poudriere/bulk.sh.

40
41
42
43
44
45
46
47
48

49
50
51
52
53
54
55
..
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
..
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
...
150
151
152
153
154
155
156
157

158
159
160
161
162
163
164
165
...
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
235
236
237
238
239
240
...
281
282
283
284
285
286
287


288
289
    -R          -- Clean RESTRICTED packages after building
    -t          -- Add some tests to the package build
    -T          -- Try to build broken ports anyway
    -F          -- Only fetch from orignial master_site (skip FreeBSD mirrors)
    -s          -- Skip sanity checks
    -J n        -- Run n jobs in parallel (Default: to 8)
    -j name     -- Run only on the given jail
    -N          -- Do not build package repository or INDEX when build completed
    -p tree     -- Specify on which ports tree the bulk build will be done

    -v          -- Be verbose; show more information. Use twice to enable debug output
    -w          -- Save WRKDIR on failed builds
    -z set      -- Specify which SET to use
    -a          -- Build the whole ports tree"

	exit 1
}
................................................................................
clean_restricted() {
	msg "Cleaning restricted packages"
	bset status "clean_restricted:"
	# Remount rw
	# mount_nullfs does not support mount -u
	umount ${MASTERMNT}/packages
	mount_packages
	injail make -C /usr/ports -j ${PARALLEL_JOBS} clean-restricted >/dev/null
	# Remount ro
	umount ${MASTERMNT}/packages
	mount_packages -o ro
}

build_repo() {
	if [ $PKGNG -eq 1 ]; then
................................................................................
		INDEXF=${POUDRIERE_DATA}/packages/${MASTERNAME}/INDEX-${OSMAJ}
		rm -f ${INDEXF}.1 2>/dev/null || :
		for pkg_file in ${POUDRIERE_DATA}/packages/${MASTERNAME}/All/*.tbz; do
			# Check for non-empty directory with no packages in it
			[ "${pkg}" = "${POUDRIERE_DATA}/packages/${MASTERNAME}/All/*.tbz" ] && break
			msg_verbose "Extracting description for ${ORIGIN} ..."
			ORIGIN=$(pkg_get_origin ${pkg_file})
			[ -d ${MASTERMNT}/usr/ports/${ORIGIN} ] &&
				injail make -C /usr/ports/${ORIGIN} describe >> ${INDEXF}.1
		done

		msg_n "Generating INDEX..."
		make_index ${INDEXF}.1 ${INDEXF}
		echo " done"

		rm ${INDEXF}.1
................................................................................
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
PTNAME="default"
SKIPSANITY=0
SETNAME=""
CLEAN=0
CLEAN_LISTED=0
ALL=0
BUILD_REPO=1
. ${SCRIPTPREFIX}/common.sh

[ $# -eq 0 ] && usage

while getopts "B:f:j:J:CcNp:RFtTsvwz:a" FLAG; do
	case "${FLAG}" in
		B)
			BUILDNAME="${OPTARG}"
			;;
		t)
			export PORTTESTING=1
			export DEVELOPER_MODE=yes
................................................................................
		j)
			jail_exists ${OPTARG} || err 1 "No such jail: ${OPTARG}"
			JAILNAME=${OPTARG}
			;;
		J)
			PARALLEL_JOBS=${OPTARG}
			;;
		N)

			BUILD_REPO=0
			;;
		p)
			PTNAME=${OPTARG}
			;;
		R)
			NO_RESTRICTED=1
			;;
................................................................................
	[ -n "${LISTPKGS}" -o ${ALL} -eq 1 ] || err 1 "No packages specified"
	[ ${ALL} -eq 1 -o -f "${LISTPKGS}" ] || err 1 "No such list of packages: ${LISTPKGS}"
else
	[ ${ALL} -eq 0 ] || err 1 "command line arguments and -a cannot be used at the same time"
	[ -z "${LISTPKGS}" ] || err 1 "command line arguments and list of ports cannot be used at the same time"
	LISTPORTS="$@"
fi



export POUDRIERE_BUILD_TYPE=bulk

jail_start ${JAILNAME} ${PTNAME} ${SETNAME}

LOGD=`log_path`
if [ -d ${LOGD} -a ${CLEAN} -eq 1 ]; then
	msg "Cleaning up old logs"
	rm -f ${LOGD}/*.log 2>/dev/null
fi

prepare_ports



bset status "building:"

[ -z "${PORTTESTING}" -a -z "${ALLOW_MAKE_JOBS}" ] &&
	echo "DISABLE_MAKE_JOBS=yes" >> ${MASTERMNT}/etc/make.conf




markfs prepkg ${MASTERMNT}

parallel_build ${JAILNAME} ${PTNAME} ${SETNAME} || : # Ignore errors as they are handled below

bset status "done:"

failed=$(bget ports.failed | awk '{print $1 ":" $3 }' | xargs echo)
................................................................................
	msg_n "Skipped ports: "
	echo ${skipped}
	echo ""
fi
msg "[${MASTERNAME}] $nbbuilt packages built, $nbfailed failures, $nbignored ignored, $nbskipped skipped"

set +e



exit $((nbfailed + nbskipped))







<

>







 







|







 







|
|







 







|




|







 







<
>
|







 







>
>













>
>





>
>
>







 







>
>


40
41
42
43
44
45
46

47
48
49
50
51
52
53
54
55
..
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
..
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
...
150
151
152
153
154
155
156

157
158
159
160
161
162
163
164
165
...
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
235
236
237
238
239
240
241
242
243
244
245
246
247
...
288
289
290
291
292
293
294
295
296
297
298
    -R          -- Clean RESTRICTED packages after building
    -t          -- Add some tests to the package build
    -T          -- Try to build broken ports anyway
    -F          -- Only fetch from orignial master_site (skip FreeBSD mirrors)
    -s          -- Skip sanity checks
    -J n        -- Run n jobs in parallel (Default: to 8)
    -j name     -- Run only on the given jail

    -p tree     -- Specify on which ports tree the bulk build will be done
    -r          -- Execute pkg repo command when bulk completes
    -v          -- Be verbose; show more information. Use twice to enable debug output
    -w          -- Save WRKDIR on failed builds
    -z set      -- Specify which SET to use
    -a          -- Build the whole ports tree"

	exit 1
}
................................................................................
clean_restricted() {
	msg "Cleaning restricted packages"
	bset status "clean_restricted:"
	# Remount rw
	# mount_nullfs does not support mount -u
	umount ${MASTERMNT}/packages
	mount_packages
	injail make -C ${PORTSRC} -j ${PARALLEL_JOBS} clean-restricted >/dev/null
	# Remount ro
	umount ${MASTERMNT}/packages
	mount_packages -o ro
}

build_repo() {
	if [ $PKGNG -eq 1 ]; then
................................................................................
		INDEXF=${POUDRIERE_DATA}/packages/${MASTERNAME}/INDEX-${OSMAJ}
		rm -f ${INDEXF}.1 2>/dev/null || :
		for pkg_file in ${POUDRIERE_DATA}/packages/${MASTERNAME}/All/*.tbz; do
			# Check for non-empty directory with no packages in it
			[ "${pkg}" = "${POUDRIERE_DATA}/packages/${MASTERNAME}/All/*.tbz" ] && break
			msg_verbose "Extracting description for ${ORIGIN} ..."
			ORIGIN=$(pkg_get_origin ${pkg_file})
			[ -d ${MASTERMNT}${PORTSRC}/${ORIGIN} ] &&
				injail make -C ${PORTSRC}/${ORIGIN} describe >> ${INDEXF}.1
		done

		msg_n "Generating INDEX..."
		make_index ${INDEXF}.1 ${INDEXF}
		echo " done"

		rm ${INDEXF}.1
................................................................................
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
PTNAME="default"
SKIPSANITY=0
SETNAME=""
CLEAN=0
CLEAN_LISTED=0
ALL=0
BUILD_REPO=0
. ${SCRIPTPREFIX}/common.sh

[ $# -eq 0 ] && usage

while getopts "B:f:j:J:Ccp:RFtTsvwz:ar" FLAG; do
	case "${FLAG}" in
		B)
			BUILDNAME="${OPTARG}"
			;;
		t)
			export PORTTESTING=1
			export DEVELOPER_MODE=yes
................................................................................
		j)
			jail_exists ${OPTARG} || err 1 "No such jail: ${OPTARG}"
			JAILNAME=${OPTARG}
			;;
		J)
			PARALLEL_JOBS=${OPTARG}
			;;

		r)
			BUILD_REPO=1
			;;
		p)
			PTNAME=${OPTARG}
			;;
		R)
			NO_RESTRICTED=1
			;;
................................................................................
	[ -n "${LISTPKGS}" -o ${ALL} -eq 1 ] || err 1 "No packages specified"
	[ ${ALL} -eq 1 -o -f "${LISTPKGS}" ] || err 1 "No such list of packages: ${LISTPKGS}"
else
	[ ${ALL} -eq 0 ] || err 1 "command line arguments and -a cannot be used at the same time"
	[ -z "${LISTPKGS}" ] || err 1 "command line arguments and list of ports cannot be used at the same time"
	LISTPORTS="$@"
fi

check_jobs

export POUDRIERE_BUILD_TYPE=bulk

jail_start ${JAILNAME} ${PTNAME} ${SETNAME}

LOGD=`log_path`
if [ -d ${LOGD} -a ${CLEAN} -eq 1 ]; then
	msg "Cleaning up old logs"
	rm -f ${LOGD}/*.log 2>/dev/null
fi

prepare_ports

run_hook bulk_build_start "${JAILNAME}" "${PTNAME}" `bget stats_queued`

bset status "building:"

[ -z "${PORTTESTING}" -a -z "${ALLOW_MAKE_JOBS}" ] &&
	echo "DISABLE_MAKE_JOBS=yes" >> ${MASTERMNT}/etc/make.conf

[ -n "${JOBS_LIMIT}" ] &&
	echo "MAKE_JOBS_NUMBER=${JOBS_LIMIT}" >> ${MASTERMNT}/etc/make.conf

markfs prepkg ${MASTERMNT}

parallel_build ${JAILNAME} ${PTNAME} ${SETNAME} || : # Ignore errors as they are handled below

bset status "done:"

failed=$(bget ports.failed | awk '{print $1 ":" $3 }' | xargs echo)
................................................................................
	msg_n "Skipped ports: "
	echo ${skipped}
	echo ""
fi
msg "[${MASTERNAME}] $nbbuilt packages built, $nbfailed failures, $nbignored ignored, $nbskipped skipped"

set +e
run_hook bulk_build_ended "${JAILNAME}" "${PTNAME}" "${nbbuilt}" \
	"${nbfailed}" "${nbignored}" "${nbskipped}"

exit $((nbfailed + nbskipped))

Added src/share/poudriere/combo.sh.













































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#!/bin/sh
# 
# Copyright (c) 2011-2013 Baptiste Daroussin <bapt@FreeBSD.org>
# Copyright (c) 2012-2013 Bryan Drewery <bdrewery@FreeBSD.org>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
set -e

usage() {
	echo "poudriere combo [options]

Parameters:
    -C          -- Cleanup jail mounts (contingency cleanup)
    -i          -- Show jail information
    -x          -- List all failed ports in last or ongoing build

Options:
    -j name     -- Run only on the given jail
    -p tree     -- Specify on which ports tree the bulk will be done"
	exit 1
}

INFO=0
LISTFAIL=0
DISMOUNT=0

SCRIPTPATH=`realpath $0`
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
PTNAME="default"

. ${SCRIPTPREFIX}/common.sh

[ $# -eq 0 ] && usage

while getopts "j:p:Cix" FLAG; do
	case "${FLAG}" in
		C)
			DISMOUNT=1
			;;
		j)
			JAILNAME=${OPTARG}
			;;
		p)
			PTNAME=${OPTARG}
			;;
		i)
			INFO=1
			;;
		x)
			LISTFAIL=1
			;;
		*)
			usage
			;;
	esac
done


[ -z "${JAILNAME}" ] && err 1 "Don't know on which jail to run please specify -j"

MASTERNAME=${JAILNAME}-${PTNAME}${SETNAME:+-${SETNAME}}
MASTERMNT=${POUDRIERE_DATA}/build/${MASTERNAME}/ref

export MASTERNAME
export MASTERMNT

list_jail_info () {
	[ $# -ne 2 ] && eargs num_queued num_to_build
	local EPOCH
	local building_started
	local elapsed elapsed_days elapsed_hms elapsed_timestamp
	echo "Jailname:              $(jget name)"
	echo "BSD version:           $(jget version)"
	echo "BSD arch:              $(jget arch)"
	echo "Install/update method: $(jget method)"
	echo "World built:           $(bget timestamp)"
	echo "Status:                $(bget status)"
	EPOCH=$(zget epoch)
	if [ "${EPOCH}" != "-" -a "${EPOCH}" != "0" ]; then
	   building_started=$(date -j -r ${EPOCH} "+%Y-%m-%d %H:%M:%S")
	   elapsed=$(expr `date "+%s"` - ${EPOCH})
	   elapsed_days=$(expr ${elapsed} / 86400)
	   elapsed_hms=$(date -j -u -r ${elapsed} "+%H:%M:%S")
	   case ${elapsed_days} in
	     0) elapsed_timestamp="${elapsed_hms}" ;;
	     1) elapsed_timestamp="1 day, ${elapsed_hms}" ;;
	     *) elapsed_timestamp="${elapsed_days} days, ${elapsed_hms}" ;;
	   esac
	   echo "Building started:      ${building_started}"
	   echo "Elapsed time:          ${elapsed_timestamp}"
	fi
	echo "Packages built:        $(bget stats_built)"
	echo "Packages failed:       $(bget stats_failed)"
	echo "Packages ignored:      $(bget stats_ignored)"
	echo "Packages skipped:      $(bget stats_skipped)"
	echo "Packages queued:       ${1}"
	echo "Packages to be built:  ${2}"
}

info_jail() {
	jail_exists ${JAILNAME} || err 1 "No such jail: ${JAILNAME}"
	nbb=$(bget stats_built|sed -e 's|-|0|g')
	nbf=$(bget stats_failed|sed -e 's|-|0|g')
	nbi=$(bget stats_ignored|sed -e 's|-|0|g')
	nbs=$(bget stats_skipped|sed -e 's|-|0|g')
	nbq=$(bget stats_queued|sed -e 's|-|0|g')
	tobuild=$((nbq - nbb - nbf - nbi - nbs))
	list_jail_info ${nbq} ${tobuild}
}

jail_dismount() {
	[ $# -ne 0 ] && eargs
	local mnt

	cd /
	msg "Umounting file systems"
	mnt=`realpath ${MASTERMNT}`
	mount | awk -v mnt="${mnt}" '{ if ($3 ~ mnt) { print $3 }}' |  sort -r | xargs umount -v || :
}

list_failures() {
	local FLIST=$(log_path)/last_run.failed
	if [ -f ${FLIST} ]; then
	    cat ${FLIST}
	else
	    msg "There are no logged failures."	
	fi
}

case "${INFO}${DISMOUNT}${LISTFAIL}" in
	100)
		test -z ${JAILNAME} && usage
		info_jail
		;;
	010)
		test -z ${JAILNAME} && usage
		jail_dismount
		;;
	001)
		test -z ${JAILNAME} && usage
		list_failures
		;;
	*)
		usage
		;;
esac

Changes to src/share/poudriere/common.sh.

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
..
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
...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
...
354
355
356
357
358
359
360


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
...
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

561
562
563
564





565









566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
...
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
....
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
....
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
....
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
....
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
....
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
....
1566
1567
1568
1569
1570
1571
1572

1573
1574
1575
1576
1577
1578
1579

1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
....
1619
1620
1621
1622
1623
1624
1625

1626
1627
1628
1629
1630
1631
1632
1633
1634

1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646






1647
1648
1649
1650
1651
1652
1653
....
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
....
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
....
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
....
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
....
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
....
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
....
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
....
2235
2236
2237
2238
2239
2240
2241

2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
....
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
....
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# zfs namespace
NS="poudriere"
IPS="$(sysctl -n kern.features.inet 2>/dev/null || echo 0)$(sysctl -n kern.features.inet6 2>/dev/null || echo 0)"
RELDATE=$(sysctl -n kern.osreldate)
JAILED=$(sysctl -n security.jail.jailed)

err() {
	export CRASHED=1
	if [ $# -ne 2 ]; then
		err 1 "err expects 2 arguments: exit_number \"message\""
	fi
	# Try to set status so other processes know this crashed
................................................................................
	fi
}

job_msg_verbose() {
	[ -n "${MY_JOBID}" ] || return 0
	msg_verbose "[${MY_JOBID}] $1" >&5
}














my_path() {
	echo ${MASTERMNT}${MY_JOBID+/../${MY_JOBID}}
}

my_name() {
	echo ${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}}
}

injail() {
	jexec -U root ${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} $@
}

jstart() {
	local network="${localipargs}"
	[ $1 -eq 1 ] && network="${ipargs}"

	jail -c persist name=${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} \
		path=${MASTERMNT}${MY_JOBID+/../${MY_JOBID}} \
		host.hostname=${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} \
		${network} \
		allow.socket_af allow.raw_sockets allow.chflags allow.sysvipc
}

jstop() {
	jail -r ${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} 2>/dev/null || :
}

eargs() {
	case $# in
	0) err 1 "No arguments expected" ;;
	1) err 1 "1 argument expected: $1" ;;
	*) err 1 "$# arguments expected: $*" ;;
	esac
}



run_hook() {
	local hookfile=${HOOKDIR}/${1}.sh
	shift
	[ -f ${hookfile} ] && /bin/sh ${hookfile} $@
	return 0
}

log_start() {
	local logfile=$1

	# Make sure directory exists
	mkdir -p ${logfile%/*}

	exec 3>&1 4>&2
	[ ! -e ${logfile}.pipe ] && mkfifo ${logfile}.pipe
	tee ${logfile} < ${logfile}.pipe >&3 &
	export tpid=$!
	exec > ${logfile}.pipe 2>&1

................................................................................
		queue_width=5
	elif [ ${nbq} -gt 999 ]; then
		queue_width=4
	elif [ ${nbq} -gt 99 ]; then
		queue_width=3
	fi

	printf "[${MASTERNAME}] [${status}] [%0${queue_width}d/%0${queue_width}d] Built: %-${queue_width}d Failed: %-${queue_width}d  Ignored: %-${queue_width}d  Skipped: %-${queue_width}d  \n" \
	  ${ndone} ${nbq} ${nbb} ${nbf} ${nbi} ${nbs}

	# Skip if stopping or starting jobs
	if [ -n "${JOBS}" -a "${status#starting_jobs:}" = "${status}" -a "${status}" != "stopping_jobs:" ]; then
		now=$(date +%s)
		format_origin_phase="\t[%s]: %-30s %-13s (%s)\n"
		format_phase="\t[%s]: %13s\n"

		# Collect build stats into a string with minimal execs
................................................................................
			else
				printf "${format_phase}" ${j} ${phase}
			fi
		done
	fi

	show_log_info


}

jail_exists() {
	[ $# -ne 1 ] && eargs jailname
	local jname=$1
	[ -d ${POUDRIERED}/jails/${jname} ] && return 0
	return 1
}

jail_runs() {
	[ $# -ne 1 ] && eargs jname
	local jname=$1
	jls -j $jname >/dev/null 2>&1 && return 0
	return 1
}

porttree_list() {
	local name method mntpoint
	for p in $(find ${POUDRIERED}/ports -type d -maxdepth 1 -mindepth 1 -print); do
		name=${p##*/}
		mnt=$(pget ${name} mnt)
		method=$(pget ${name} method)
		echo "${name} ${method:--} ${mnt}"
	done
}

................................................................................
}

fetch_file() {
	[ $# -ne 2 ] && eargs destination source
	fetch -p -o $1 $2 || fetch -p -o $1 $2 || err 1 "Failed to fetch from $2"
}

createfs() {
	[ $# -ne 3 ] && eargs name mnt fs
	local name mnt fs
	name=$1
	mnt=$(echo $2 | sed -e "s,//,/,g")
	fs=$3

	if [ -n "${fs}" -a "${fs}" != "none" ]; then
		msg_n "Creating ${name} fs..."
		zfs create -p \
			-o mountpoint=${mnt} ${fs} || err 1 " fail"
		echo " done"
	else
		mkdir -p ${mnt}
	fi
}

rollbackfs() {
	[ $# -ne 2 ] && eargs name mnt
	local name=$1
	local mnt=$2
	local fs=$(zfs_getfs ${mnt})

	if [ -n "${fs}" ]; then
		zfs rollback -r ${fs}@${name}  || err 1 "Unable to rollback ${fs}"
		return
	fi

	mtree -X ${mnt}/poudriere/mtree.${name}exclude \
	-xr -f ${mnt}/poudriere/mtree.${name} -p ${mnt} | \
	while read l ; do
		case "$l" in
		*extra*Directory*) rm -rf ${mnt}/${l%% *} 2>/dev/null ;;
		*changed|*missing) echo ${MASTERMNT}/${l% *} ;;
		esac
	done | pax -rw -p p -s ",${MASTERMNT},,g" ${mnt}
}

umountfs() {
	[ $# -lt 1 ] && eargs mnt childonly
	local mnt=$1
	local childonly=$2
	local pattern

	[ -n "${childonly}" ] && pattern="/"

	[ -d "${mnt}" ] || return 0
	mnt=$(realpath ${mnt})
	mount | sort -r -k 2 | while read dev on pt opts; do
		case ${pt} in
		${mnt}${pattern}*)
			umount -f ${pt} || :
			[ "${dev#/dev/md*}" != "${dev}" ] && mdconfig -d -u ${dev#/dev/md*}
		;;
		esac
	done

	return 0
}

zfs_getfs() {
	[ $# -ne 1 ] && eargs mnt
	local mnt=$(realpath $1)
	mount -t zfs | awk -v n="${mnt}" ' $3 == n { print $1 }'
}

unmarkfs() {
	[ $# -ne 2 ] && eargs name mnt
	local name=$1
	local mnt=$(realpath $2)

	if [ -n "$(zfs_getfs ${mnt})" ]; then
................................................................................
		zfs snapshot ${fs}@${name}
	fi

	[ $domtree -eq 0 ] && return 0
	mkdir -p ${mnt}/poudriere/
	if [ "${name}" = "prepkg" ]; then
		cat > ${mnt}/poudriere/mtree.${name}exclude << EOF
./poudriere/*
./compat/linux/proc
./wrkdirs/*
./packages/*
./new_packages/*
./usr/ports/*
./distfiles/*
./ccache/*
./var/db/ports/*
./proc/*
EOF
	elif [ "${name}" = "preinst" ]; then
		cat >  ${mnt}/poudriere/mtree.${name}exclude << EOF
./poudriere/*
./var/db/pkg/*
./var/run/*
./wrkdirs/*
./new_packages/*
./tmp/*
.${LOCALBASE:-/usr/local}/share/nls/POSIX
.${LOCALBASE:-/usr/local}/share/nls/en_US.US-ASCII
./compat/linux/proc/*
./var/db/*
./var/log/*
.${HOME}/*
./etc/spwd.db
./etc/pwd.db
./etc/group
./etc/make.conf
./etc/make.conf.bak

./etc/passwd
./etc/master.passwd
./etc/shells
./var/mail/*





.${LOCALBASE:-/usr/local}/etc/gconf/gconf.xml.defaults









EOF
	fi
	mtree -X ${mnt}/poudriere/mtree.${name}exclude \
		-xcn -k uid,gid,mode,size \
		-p ${mnt} > ${mnt}/poudriere/mtree.${name}
}

clonefs() {
	[ $# -lt 2 ] && eargs from to snap
	local from=$1
	local to=$2
	local snap=$3
	local name zfs_to
	local fs=$(zfs_getfs ${from})

	[ -d ${to} ] && destroyfs ${to} jail
	mkdir -p ${to}
	to=$(realpath ${to})
	[ ${TMPFS_ALL} -eq 1 ] && unset fs
	if [ -n "${fs}" ]; then
		name=${to##*/}

		if [ "${name}" = "ref" ]; then
			zfs_to=${fs%/*}/${MASTERNAME}-${name}
		else
			zfs_to=${fs}/${name}
		fi

		# Make sure the fs is clean before cloning
		zfs rollback -R ${fs}@${snap} 2>/dev/null || :
		zfs clone -o mountpoint=${to} \
			-o sync=disabled \
			-o atime=off \
			-o compression=off \
			${fs}@${snap} \
			${zfs_to}
	else
		[ ${TMPFS_ALL} -eq 1 ] && mount -t tmpfs tmpfs ${to}
		pax -X -rw -p p -s ",${from},,g" ${from} ${to}
	fi
}

destroyfs() {
	[ $# -ne 2 ] && eargs name type
	local mnt fs type
	mnt=$1
	type=$2
	[ -d ${mnt} ] || return 0
	mnt=$(realpath ${mnt})
	fs=$(zfs_getfs ${mnt})
	umountfs ${mnt} 1
	if [ ${TMPFS_ALL} -eq 1 ]; then
		umount -f ${mnt} 2>/dev/null || :
	elif [ -n "${fs}" -a "${fs}" != "none" ]; then
		zfs destroy -rf ${fs}
		rmdir ${mnt}
	else
		chflags -R noschg ${mnt}
		rm -rf ${mnt}
	fi
}

do_jail_mounts() {
	[ $# -ne 2 ] && eargs mnt arch
	local mnt=$1
	local arch=$2
	local devfspath="null zero random urandom stdin stdout stderr fd fd/*"

	# clone will inherit from the ref jail
	if [ ${mnt##*/} = "ref" ]; then
		mkdir -p ${mnt}/proc
		mkdir -p ${mnt}/dev
		mkdir -p ${mnt}/compat/linux/proc
		mkdir -p ${mnt}/usr/ports
		mkdir -p ${mnt}/wrkdirs
		mkdir -p ${mnt}/${LOCALBASE:-/usr/local}
		mkdir -p ${mnt}/distfiles
		mkdir -p ${mnt}/packages
		mkdir -p ${mnt}/new_packages
		mkdir -p ${mnt}/ccache
		mkdir -p ${mnt}/var/db/ports
	fi

	# ref jail only needs devfs
	mount -t devfs devfs ${mnt}/dev
	devfs -m ${mnt}/dev rule apply hide
	for p in ${devfspath} ; do
		devfs -m ${mnt}/dev/ rule apply path "${p}" unhide
	done
	if [ "${mnt##*/}" != "ref" ]; then
		[ ${JAILED} -eq 0 ] && mount -t fdescfs fdesc ${mnt}/dev/fd
		mount -t procfs proc ${mnt}/proc
		if [ -z "${NOLINUX}" ]; then
			[ "${arch}" = "i386" -o "${arch}" = "amd64" ] &&
				mount -t linprocfs linprocfs ${mnt}/compat/linux/proc
		fi
	fi

	return 0
}

use_options() {
	[ $# -ne 2 ] && eargs mnt optionsdir
	local mnt=$1
	local optionsdir=$2

	if [ "${optionsdir}" = "-" ]; then
................................................................................
	else
		optionsdir="${POUDRIERED}/${optionsdir}-options"
	fi
	[ -d "${optionsdir}" ] || return 1
	optionsdir=$(realpath ${optionsdir} 2>/dev/null)
	[ "${mnt##*/}" = "ref" ] &&
		msg "Mounting /var/db/ports from: ${optionsdir}"
	mount -t nullfs -o ro ${optionsdir} ${mnt}/var/db/ports ||
		err 1 "Failed to mount OPTIONS directory"

	return 0
}

mount_packages() {
	local mnt=$(my_path)
	mount -t nullfs "$@" ${POUDRIERE_DATA}/packages/${MASTERNAME} \
		${mnt}/packages ||
		err 1 "Failed to mount the packages directory "
}

do_portbuild_mounts() {
	[ $# -lt 3 ] && eargs mnt jname ptname setname
	local mnt=$1
	local jname=$2
	local ptname=$3
	local setname=$4
	local portsdir=$(pget ${ptname} mnt)
	local optionsdir

	[ -d ${portsdir}/ports ] && portsdir=${portsdir}/ports

	[ "$(realpath ${DISTFILES_CACHE})" != \
		"$(realpath -q ${portsdir}/distfiles)" ] || err 1 \
		"DISTFILES_CACHE cannot be in the portsdir as the portsdir will be mounted read-only"

	mkdir -p ${POUDRIERE_DATA}/packages/${MASTERNAME}/All
	[ -d "${CCACHE_DIR:-/nonexistent}" ] &&
		mount -t nullfs ${CCACHE_DIR} ${mnt}/ccache
	[ -n "${MFSSIZE}" ] && mdmfs -M -S -o async -s ${MFSSIZE} md ${mnt}/wrkdirs
	[ ${TMPFS_WRKDIR} -eq 1 ] && mount -t tmpfs tmpfs ${mnt}/wrkdirs
	# Only show mounting messages once, not for every builder
	if [ ${mnt##*/} = "ref" ]; then
		[ -d "${CCACHE_DIR:-/nonexistent}" ] &&
			msg "Mounting ccache from: ${CCACHE_DIR}"
		msg "Mounting packages from: ${POUDRIERE_DATA}/packages/${MASTERNAME}"
	fi

	mount -t nullfs -o ro ${portsdir} ${mnt}/usr/ports ||
		err 1 "Failed to mount the ports directory "
	mount_packages -o ro
	mount -t nullfs ${DISTFILES_CACHE} ${mnt}/distfiles ||
		err 1 "Failed to mount the distfiles cache directory"

	optionsdir="${MASTERNAME}"
	[ -n "${setname}" ] && optionsdir="${optionsdir} ${jname}-${setname}"
	optionsdir="${optionsdir} ${jname}-${ptname} ${setname} ${ptname} ${jname} -"

	for opt in ${optionsdir}; do
		use_options ${mnt} ${opt} && break || continue
	done

	return 0
}

jail_start() {
	[ $# -lt 2 ] && eargs name ptname setname
	local name=$1
	local ptname=$2
	local setname=$3
	local portsdir=$(pget ${ptname} mnt)
	local arch=$(jget ${name} arch)
	local mnt=$(jget ${name} mnt)
	local needfs="nullfs procfs"
	local makeconf

	local tomnt=${POUDRIERE_DATA}/build/${MASTERNAME}/ref

	[ -d ${DISTFILES_CACHE:-/nonexistent} ] || err 1 "DISTFILES_CACHE directory does not exist. (c.f. poudriere.conf)"

	if [ -z "${NOLINUX}" ]; then
		if [ "${arch}" = "i386" -o "${arch}" = "amd64" ]; then
			needfs="${needfs} linprocfs linsysfs"
			sysctl -n compat.linux.osrelease >/dev/null 2>&1 || kldload linux
		fi
	fi
	[ -n "${USE_TMPFS}" ] && NEEDFS="${NEEDFS} tmpfs"
	for fs in ${needfs}; do
		if ! lsvfs $fs >/dev/null 2>&1; then
			if [ $JAILED -eq 0 ]; then
				kldload $fs
			else
				err 1 "please load the $fs module on host using \"kldload $fs\""
			fi
		fi
	done
	jail_exists ${name} || err 1 "No such jail: ${name}"
	jail_runs ${MASTERNAME} && err 1 "jail already running: ${MASTERNAME}"
	export HOME=/root
	export USER=root
	export FORCE_PACKAGE=yes
	[ -z "${NO_PACKAGE_BUILDING}" ] && export PACKAGE_BUILDING=yes

	[ ${SET_STATUS_ON_START-1} -eq 1 ] && export STATUS=1
	msg_n "Creating the reference jail..."
	clonefs ${mnt} ${tomnt} clean
	echo " done"

	msg "Mounting system devices for ${MASTERNAME}"
	do_jail_mounts ${tomnt} ${arch}

	[ -d "${portsdir}/ports" ] && portsdir="${portsdir}/ports"
	msg "Mounting ports/packages/distfiles"
	do_portbuild_mounts ${tomnt} ${name} ${ptname} ${setname}

	if [ -n "${POUDRIERE_BUILD_TYPE}" ]; then
		show_log_info
	fi

	if [ -d "${CCACHE_DIR:-/nonexistent}" ]; then
		echo "WITH_CCACHE_BUILD=yes" >> ${tomnt}/etc/make.conf
		echo "MAKE_ENV+= CCACHE_DIR=/ccache" >> ${tomnt}/etc/make.conf
	fi
	echo "PACKAGES=/packages" >> ${tomnt}/etc/make.conf
	echo "DISTDIR=/distfiles" >> ${tomnt}/etc/make.conf

	makeconf="- ${setname} ${ptname} ${name} ${name}-${ptname}"
	[ -n "${setname}" ] && makeconf="${makeconf} ${name}-${setname}"
	makeconf="${makeconf} ${MASTERNAME}"
	for opt in ${makeconf}; do
		append_make ${tomnt} ${opt}
	done

	test -n "${RESOLV_CONF}" && cp -v "${RESOLV_CONF}" "${tomnt}/etc/"
	msg "Starting jail ${MASTERNAME}"
	jstart 0
	# Only set STATUS=1 if not turned off
	# jail -s should not do this or jail will stop on EXIT
	WITH_PKGNG=$(injail make -f /usr/ports/Mk/bsd.port.mk -V WITH_PKGNG)
	if [ -n "${WITH_PKGNG}" ]; then
		export PKGNG=1
		export PKG_EXT="txz"
		export PKG_ADD="${LOCALBASE:-/usr/local}/sbin/pkg add"
		export PKG_DELETE="${LOCALBASE:-/usr/local}/sbin/pkg delete -y -f"
	else
		export PKGNG=0
		export PKG_ADD=pkg_add
		export PKG_DELETE=pkg_delete
		export PKG_EXT="tbz"
	fi

	# 8.3 did not have distrib-dirs ran on it, so various
	# /usr and /var dirs are missing. Namely /var/games
	if [ "$(injail uname -r | cut -d - -f 1 )" = "8.3" ]; then
		injail mtree -eu -f /etc/mtree/BSD.var.dist -p /var >/dev/null 2>&1 || :
		injail mtree -eu -f /etc/mtree/BSD.usr.dist -p /usr >/dev/null 2>&1 || :
	fi
}

jail_stop() {
	[ $# -ne 0 ] && eargs
	jail_runs ${MASTERNAME} || err 1 "No such jail running: ${MASTERNAME}"
	local fs=$(zfs_getfs ${MASTERMNT})

	# err() will set status to 'crashed', don't override.
	[ -n "${CRASHED}" ] || bset status "stop:" 2>/dev/null || :

	jstop
	# Shutdown all builders
	if [ ${PARALLEL_JOBS} -ne 0 ]; then
		# - here to only check for unset, {start,stop}_builders will set this to blank if already stopped
		for j in ${JOBS-$(jot -w %02d ${PARALLEL_JOBS})}; do
			MY_JOBID=${j} jstop
			destroyfs ${MASTERMNT}/../${j} jail || :
		done
	fi
	msg "Umounting file systems"
	destroyfs ${MASTERMNT} jail
	rm -rf ${MASTERMNT}/../
	export STATUS=0
}

cleanup() {
	[ -n "${CLEANED_UP}" ] && return 0
	# Prevent recursive cleanup on error
	if [ -n "${CLEANING_UP}" ]; then
		echo "Failure cleaning up. Giving up." >&2
		return
	fi
	export CLEANING_UP=1
	msg "Cleaning up"

	# If this is a builder, don't cleanup, the master will handle that.
	if [ -n "${MY_JOBID}" ]; then
		[ -n "${PKGNAME}" ] && clean_pool ${PKGNAME} 1 || :
		return 0
	fi

	[ -n "${MASTERNAME}" ] && rm -rf \
		${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages

	# Only bother with this if using jails as this may be being ran
	# from queue.sh or daemon.sh, etc.
	if [ -n "${MASTERMNT}" -a -n "${MASTERNAME}" ]; then
		if [ -d ${MASTERMNT}/poudriere/var/run ]; then
			for pid in ${MASTERMNT}/poudriere/var/run/*.pid; do
				# Ensure there is a pidfile to read or break
				[ "${pid}" = "${MASTERMNT}/poudriere/var/run/*.pid" ] && break
				pkill -15 -F ${pid} >/dev/null 2>&1 || :
			done
		fi
		wait

		jail_stop
	fi

	export CLEANED_UP=1
}

sanity_check_pkgs() {
	local ret=0
	local depfile
	[ ! -d ${POUDRIERE_DATA}/packages/${MASTERNAME}/All ] && return $ret
	dirempty ${POUDRIERE_DATA}/packages/${MASTERNAME}/All && return $ret
	for pkg in ${POUDRIERE_DATA}/packages/${MASTERNAME}/All/*.${PKG_EXT}; do
		# Check for non-empty directory with no packages in it
................................................................................
	return 0
}

# Build+test port and return on first failure
build_port() {
	[ $# -ne 1 ] && eargs portdir
	local portdir=$1
	local port=${portdir##/usr/ports/}
	local targets="check-config pkg-depends fetch-depends fetch checksum \
				   extract-depends extract patch-depends patch build-depends \
				   lib-depends configure build install-mtree run-depends \
				   install package ${PORTTESTING:+deinstall}"
	local mnt=$(my_path)
	local log=$(log_path)
	local listfilecmd network
	local hangstatus

	for phase in ${targets}; do
		bset ${MY_JOBID} status "${phase}:${port}"
		job_msg_verbose "Status for build ${port}: ${phase}"
		if [ "${phase}" = "fetch" ]; then
			jstop
			jstart 1
		fi
		case ${phase} in
		install) [ -n "${PORTTESTING}" ] && markfs preinst ${mnt} ;;
		deinstall)
................................................................................
		print_phase_header ${phase}

		if [ "${phase}" = "package" ]; then
			echo "PACKAGES=/new_packages" >> ${mnt}/etc/make.conf
			# Create sandboxed staging dir for new package for this build
			rm -rf "${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages/${PKGNAME}"
			mkdir -p "${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages/${PKGNAME}"
			mount -t nullfs \
				"${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages/${PKGNAME}" \
				${mnt}/new_packages
		fi

		if [ "${phase#*-}" = "depends" ]; then
			# No need for nohang or PKGENV/PORT_FLAGS for *-depends
			injail make -C ${portdir} ${phase}
................................................................................
			if [ $hangstatus -ne 0 ]; then
				# 1 = cmd failed, not a timeout
				# 2 = log timed out
				# 3 = cmd timeout
				if [ $hangstatus -eq 2 ]; then
					msg "Killing runaway build"
					bset ${MY_JOBID} status "${phase}/runaway:${port}"
					job_msg_verbose "Status for build ${port}: runaway"
				elif [ $hangstatus -eq 3 ]; then
					msg "Killing timed out build"
					bset ${MY_JOBID} status "${phase}/timeout:${port}"
					job_msg_verbose "Status for build ${port}: timeout"
				fi
				return 1
			fi
		fi

		if [ "${phase}" = "checksum" ]; then
			jstop
................................................................................
}

# Save wrkdir and return path to file
save_wrkdir() {
	[ $# -ne 4 ] && eargs mnt port portdir phase
	local mnt=$1
	local port="$2"
	local portdir="$3"
	local phase="$4"
	local tardir=${POUDRIERE_DATA}/wrkdirs/${MASTERNAME}/${PTNAME}
	local tarname=${tardir}/${PKGNAME}.${WRKDIR_ARCHIVE_FORMAT}
	local mnted_portdir=${mnt}/wrkdirs/${portdir}

	[ -n "${SAVE_WRKDIR}" ] || return 0
	# Only save if not in fetch/checksum phase
................................................................................
	case ${WRKDIR_ARCHIVE_FORMAT} in
	tar) COMPRESSKEY="" ;;
	tgz) COMPRESSKEY="z" ;;
	tbz) COMPRESSKEY="j" ;;
	txz) COMPRESSKEY="J" ;;
	esac
	rm -f ${tarname}
	tar -s ",${mnted_portdir},," -c${COMPRESSKEY}f ${tarname} ${mnted_portdir}/work > /dev/null 2>&1

	job_msg "Saved ${port} wrkdir to: ${tarname}"
}

start_builder() {
	local id=$1
	local arch=$2
	local mnt

	export MY_JOBID=${id}
	mnt=$(my_path)

	# Jail might be lingering from previous build. Already recursively
	# destroyed all the builder datasets, so just try stopping the jail
	# and ignore any errors
	jstop
	destroyfs ${mnt} jail
	mkdir -p "${mnt}"
	clonefs ${MASTERMNT} ${mnt} prepkg
	# Create the /poudriere so that on zfs rollback does not nukes it
	mkdir -p ${mnt}/poudriere
	markfs prepkg ${mnt}
	do_jail_mounts ${mnt} ${arch}
	do_portbuild_mounts ${mnt} ${jname} ${ptname} ${setname}
	jstart 0
	bset ${id} status "idle:"
}

start_builders() {
	local arch=$(injail uname -p)

	bset builders "${JOBS}"
	bset status "starting_builders:"
	parallel_start
	for j in ${JOBS}; do
		parallel_run start_builder ${j} ${arch}
	done
	parallel_stop
}

stop_builders() {
	local mnt

	# wait for the last running processes
	cat ${MASTERMNT}/poudriere/var/run/*.pid 2>/dev/null | xargs pwait 2>/dev/null

	msg "Stopping ${PARALLEL_JOBS} builders"

	for j in ${JOBS}; do
		MY_JOBID=${j} jstop
		destroyfs ${MASTERMNT}/../${j} jail
	done

	# No builders running, unset JOBS
	JOBS=""
}

deadlock_detected() {
	local always_fail=${1:-1}
	local mnt=$(my_path)
	local crashed_packages dependency_cycles

	# If there are still packages marked as "building" they have crashed
................................................................................
	local build_failed=0
	local name=${MASTERNAME}-job-${MY_JOBID}
	local mnt=$(my_path)
	local failed_status failed_phase cnt
	local clean_rdepends=0
	local log=$(log_path)
	local ignore


	export PKGNAME="${pkgname}" # set ASAP so cleanup() can use it
	port=$(cache_get_origin ${pkgname})
	portdir="/usr/ports/${port}"

	job_msg "Starting build of ${port}"
	bset ${MY_JOBID} status "starting:${port}"


	if [ ${TMPFS_LOCALBASE} -eq 1 ]; then
		umount -f ${mnt}/${LOCALBASE:-/usr/local} 2>/dev/null || :
		mount -t tmpfs tmpfs ${mnt}/${LOCALBASE:-/usr/local}
	fi

	# Stop everything first
	jstop
	rollbackfs prepkg ${mnt}
	# Make sure we start with no network
	jstart 0

	# If this port is IGNORED, skip it
	# This is checked here instead of when building the queue
	# as the list may start big but become very small, so here
	# is a less-common check
	ignore="$(injail make -C ${portdir} -VIGNORE)"

	msg "Cleaning up wrkdir"
	rm -rf ${mnt}/wrkdirs/*

	log_start ${log}/logs/${PKGNAME}.log
	msg "Building ${port}"
	buildlog_start ${portdir}

	if [ -n "${ignore}" ]; then
		msg "Ignoring ${port}: ${ignore}"
		badd ports.ignored "${port} ${PKGNAME} ${ignore}"
................................................................................
		elif [ -f ${mnt}/${portdir}/.keep ]; then
			save_wrkdir ${mnt} "${port}" "${portdir}" "noneed" ||:
		fi

		injail make -C ${portdir} clean

		if [ ${build_failed} -eq 0 ]; then

			badd ports.built "${port} ${PKGNAME}"
			job_msg "Finished build of ${port}: Success"
			run_hook pkgbuild success "${port}" "${PKGNAME}"
			# Cache information for next run
			pkg_cache_data "${POUDRIERE_DATA}/packages/${MASTERNAME}/All/${PKGNAME}.${PKG_EXT}" ${port} || :
		else
			# Symlink the buildlog into errors/
			ln -s ../${PKGNAME}.log ${log}/logs/errors/${PKGNAME}.log
			badd ports.failed "${port} ${PKGNAME} ${failed_phase}"

			job_msg "Finished build of ${port}: Failed: ${failed_phase}"
			run_hook pkgbuild failed "${port}" "${PKGNAME}" "${failed_phase}" \
				"${log}/logs/errors/${PKGNAME}.log"
			clean_rdepends=1
		fi
	fi

	clean_pool ${PKGNAME} ${clean_rdepends}

	bset ${MY_JOBID} status "done:${port}"

	stop_build ${portdir} ${log}/logs/${PKGNAME}.log







	echo ${MY_JOBID} >&6
}

stop_build() {
	[ $# -eq 2 ] || eargs portdir logfile
	local portdir="$1"
................................................................................
	}

	[ $xtrace -eq 1 ] && set -x
}

list_deps() {
	[ $# -ne 1 ] && eargs directory
	local dir="/usr/ports/$1"
	local makeargs="-VPKG_DEPENDS -VBUILD_DEPENDS -VEXTRACT_DEPENDS -VLIB_DEPENDS -VPATCH_DEPENDS -VFETCH_DEPENDS -VRUN_DEPENDS"

	mangle_stderr "WARNING" "($1)" injail make -C ${dir} $makeargs | \
		tr '\n' ' ' | sed -e "s,[[:graph:]]*/usr/ports/,,g" \
		-e "s,:[[:graph:]]*,,g" | \
		sort -u || err 1 "Makefile broken: $1"
}

deps_file() {
	[ $# -ne 1 ] && eargs pkg
	local pkg=$1
................................................................................
	fi

	mkdir -p $(pkg_cache_dir ${pkg})

	o=$(pkg_get_origin ${pkg})
	v=${pkg##*-}
	v=${v%.*}
	if [ ! -d "${mnt}/usr/ports/${o}" ]; then
		msg "${o} does not exist anymore. Deleting stale ${pkg##*/}"
		delete_pkg ${pkg}
		return 0
	fi
	v2=$(cache_get_pkgname ${o})
	v2=${v2##*-}
	if [ "$v" != "$v2" ]; then
		msg "Deleting old version: ${pkg##*/}"
		delete_pkg ${pkg}
		return 0
	fi

	current_deps=$(injail make -C /usr/ports/${o} run-depends-list | sed 's,/usr/ports/,,g' | tr '\n' ' ')
	compiled_deps=$(pkg_get_dep_origin ${pkg})
	for d in ${current_deps}; do
		case " $compiled_deps " in
		*\ $d\ *) ;;
		*)
			msg "Direct dependency change, deleting: ${pkg##*/}"
			delete_pkg ${pkg}
................................................................................
			return 0
			;;
		esac
	done

	# Check if the compiled options match the current options from make.conf and /var/db/options
	if [ "${CHECK_CHANGED_OPTIONS:-no}" != "no" ]; then
		current_options=$(injail make -C /usr/ports/${o} pretty-print-config | \
			tr ' ' '\n' | sed -n 's/^\+\(.*\)/\1/p' | sort | tr '\n' ' ')
		compiled_options=$(pkg_get_options ${pkg})

		if [ "${compiled_options}" != "${current_options}" ]; then
			msg "Options changed, deleting: ${pkg##*/}"
			if [ "${CHECK_CHANGED_OPTIONS}" = "verbose" ]; then
				msg "Pkg: ${compiled_options}"
................................................................................
	local cache_origin_pkgname=${MASTERMNT}/poudriere/var/cache/origin-pkgname/${origin%%/*}_${origin##*/}
	local cache_pkgname_origin

	[ -f ${cache_origin_pkgname} ] && read pkgname < ${cache_origin_pkgname}

	# Add to cache if not found.
	if [ -z "${pkgname}" ]; then
		[ -d "${MASTERMNT}/usr/ports/${origin}" ] ||
			err 1 "Invalid port origin '${origin}' not found."
		pkgname=$(injail make -C /usr/ports/${origin} -VPKGNAME ||
			err 1 "Error getting PKGNAME for ${origin}")
		# Make sure this origin did not already exist
		existing_origin=$(cache_get_origin "${pkgname}" 2>/dev/null || :)
		# It may already exist due to race conditions, it is not harmful. Just ignore.
		if [ "${existing_origin}" != "${origin}" ]; then
			[ -n "${existing_origin}" ] &&
				err 1 "Duplicated origin for ${pkgname}: ${origin} AND ${existing_origin}. Rerun with -vv to see which ports are depending on these."
................................................................................
			"${mnt}/poudriere/rdeps/${dep_pkgname}/${pkgname}"
	done
}

listed_ports() {
	if [ ${ALL:-0} -eq 1 ]; then
		PORTSDIR=$(pget ${PTNAME} mnt)
		[ -d "${PORTSDIR}/ports" ] && PORTSDIR="${PORTSDIR}/ports"
		for cat in $(awk '$1 == "SUBDIR" { print $3}' ${PORTSDIR}/Makefile); do
			awk -v cat=${cat} '$1 == "SUBDIR" { print cat"/"$3}' ${PORTSDIR}/${cat}/Makefile
		done
		return 0
	fi
	if [ -z "${LISTPORTS}" ]; then
		[ -n "${LISTPKGS}" ] &&
................................................................................
cache_get_key() {
	if [ -z "${CACHE_KEY}" ]; then
		CACHE_KEY=$({
			injail env
			cat ${MASTERMNT}/etc/make.conf
			injail find /var/db/ports -exec sha256 {} +
			echo ${MASTERNAME}
			if [ -f ${MASTERMNT}/usr/ports/poudriere.stamp ]; then
				cat ${MASTERMNT}/usr/ports/poudriere.stamp
			else
				# This is not a poudriere-managed ports tree.
				# Just toss in getpid() to invalidate the cache
				# as there is no quick way to hash the tree without
				# taking possibly minutes+
				echo $$
			fi
................................................................................
	mkdir -p ${log}/logs ${log}/logs/errors
	ln -sfh ${BUILDNAME} ${log%/*}/latest
	cp ${HTMLPREFIX}/* ${log}

	bset status "computingdeps:"
	parallel_start
	for port in $(listed_ports); do
		[ -d "${MASTERMNT}/usr/ports/${port}" ] ||
			err 1 "Invalid port origin: ${port}"
		parallel_run compute_deps ${port}
	done
	parallel_stop

	bset status "sanity:"

................................................................................
		msg "Sanity checking the repository"
		delete_stale_pkg_cache
		delete_old_pkgs

		while :; do
			sanity_check_pkgs && break
		done


		msg "Deleting stale symlinks"
		find -L ${POUDRIERE_DATA}/packages/${MASTERNAME} -type l \
			-exec rm -f {} +

		msg "Deleting empty directories"
		find ${POUDRIERE_DATA}/packages/${MASTERNAME} -type d -mindepth 1 \
			-empty -delete
	fi

	bset status "cleaning:"
	msg "Cleaning the build queue"
	export LOCALBASE=${LOCALBASE:-/usr/local}
	for pn in $(ls ${MASTERMNT}/poudriere/deps/); do
		if [ -f "${MASTERMNT}/packages/All/${pn}.${PKG_EXT}" ]; then
			find_all_pool_references "${pn}"
................................................................................
		done
		mv ${pkg_dir} ${MASTERMNT}/poudriere/pool/${dep_count##* }/ 2>/dev/null || :
	done

	rmdir ${lock}
}

append_make() {
	[ $# -ne 2 ] && eargs mnt makeconf
	local mnt=$1
	local makeconf=$2

	if [ "${makeconf}" = "-" ]; then
		makeconf="${POUDRIERED}/make.conf"
	else
		makeconf="${POUDRIERED}/${makeconf}-make.conf"
	fi

	[ -f "${makeconf}" ] || return 0
	makeconf="$(realpath ${makeconf} 2>/dev/null)"
	msg "Appending to /etc/make.conf: ${makeconf}"
	echo "#### ${makeconf} ####" >> ${mnt}/etc/make.conf
	cat "${makeconf}" >> ${mnt}/etc/make.conf
}

RESOLV_CONF=""
STATUS=0 # out of jail #

[ -f ${SCRIPTPREFIX}/../../etc/poudriere.conf ] ||
	err 1 "Unable to find ${SCRIPTPREFIX}/../../etc/poudriere.conf"

. ${SCRIPTPREFIX}/../../etc/poudriere.conf
................................................................................
	case ${ZROOTFS} in
	[!/]*) err 1 "ZROOTFS shoud start with a /" ;;
	esac
fi

[ -n "${MFSSIZE}" -a -n "${USE_TMPFS}" ] && err 1 "You can't use both tmpfs and mdmfs"

TMPFS_WRKDIR=0
TMPFS_DATA=0
TMPFS_ALL=0
TMPFS_LOCALBASE=0
for val in ${USE_TMPFS}; do
	case ${val} in
	wrkdir|yes) TMPFS_WRKDIR=1 ;;
	data) TMPFS_DATA=1 ;;
	all) TMPFS_ALL=1 ;;
	localbase) TMPFS_LOCALBASE=1 ;;
	*) err 1 "Unknown value for USE_TMPFS can be a combination of wrkdir,data,all,yes,localbase" ;;
	esac
done

case ${TMPFS_WRKDIR}${TMPFS_DATA}${TMPFS_LOCALBASE}${TMPFS_ALL} in
1**1|*1*1|**11)
	TMPFS_WRKDIR=0
	TMPFS_DATA=0
	TMPFS_LOCALBASE=0
	;;
esac

POUDRIERE_DATA=`get_data_dir`
: ${WRKDIR_ARCHIVE_FORMAT="tbz"}
case "${WRKDIR_ARCHIVE_FORMAT}" in
	tar|tgz|tbz|txz);;
	*) err 1 "invalid format for WRKDIR_ARCHIVE_FORMAT: ${WRKDIR_ARCHIVE_FORMAT}" ;;
esac

#Converting portstree if any
if [ ! -d ${POUDRIERED}/ports ]; then
	mkdir -p ${POUDRIERED}/ports
	[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
		-o ${NS}:type,${NS}:name,${NS}:method,mountpoint,name | \
		grep "^ports" | \
		while read t name method mnt fs; do
			msg "Converting the ${name} ports tree"
			pset ${name} method ${method}
			pset ${name} mnt ${mnt}
			pset ${name} fs ${fs}
			# Delete the old properties
			zfs inherit -r ${NS}:type ${fs}
			zfs inherit -r ${NS}:name ${fs}
			zfs inherit -r ${NS}:method ${fs}
		done
	if [ -f ${POUDRIERED}/portstrees ]; then
		while read name method mnt; do
			[ -z "${name###*}" ] && continue # Skip comments
			msg "Converting the ${name} ports tree"
			mkdir ${POUDRIERED}/ports/${name}
			echo ${method} > ${POUDRIERED}/ports/${name}/method
			echo ${mnt} > ${POUDRIERED}/ports/${name}/mnt
		done < ${POUDRIERED}/portstrees
		rm -f ${POUDRIERED}/portstrees
	fi
fi

#Converting jails if any
if [ ! -d ${POUDRIERED}/jails ]; then
	mkdir -p ${POUDRIERED}/jails
	[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
		-o ${NS}:type,${NS}:name,${NS}:version,${NS}:arch,${NS}:method,mountpoint,name | \
		grep "^rootfs" | \
		while read t name version arch method mnt fs; do
			msg "Converting the ${name} jail"
			jset ${name} version ${version}
			jset ${name} arch ${arch}
			jset ${name} method ${method}
			jset ${name} mnt ${mnt}
			jset ${name} fs ${fs}
			# Delete the old properties
			zfs inherit -r ${NS}:type ${fs}
			zfs inherit -r ${NS}:name ${fs}
			zfs inherit -r ${NS}:method ${fs}
			zfs inherit -r ${NS}:version ${fs}
			zfs inherit -r ${NS}:arch ${fs}
			zfs inherit -r ${NS}:stats_built ${fs}
			zfs inherit -r ${NS}:stats_failed ${fs}
			zfs inherit -r ${NS}:stats_skipped ${fs}
			zfs inherit -r ${NS}:stats_ignored ${fs}
			zfs inherit -r ${NS}:stats_queued ${fs}
			zfs inherit -r ${NS}:status ${fs}
		done
fi

case $IPS in
01)
	localipargs="ip6.addr=::1"
	ipargs="ip6.addr=inherit"
	;;
10)
	localipargs="ip4.addr=127.0.0.1"
	ipargs="ip4=inherit"
	;;
11)
	localipargs="ip4.addr=127.0.0.1 ip6.addr=::1"
	ipargs="ip4=inherit ip6=inherit"
	;;
esac


case ${PARALLEL_JOBS} in
''|*[!0-9]*)
	PARALLEL_JOBS=$(sysctl -n hw.ncpu)
	;;
esac

case ${POOL_BUCKETS} in
''|*[!0-9]*)
	POOL_BUCKETS=10
	;;
esac

: ${WATCHDIR:=${POUDRIERE_DATA}/queue}
: ${PIDFILE:=${POUDRIERE_DATA}/daemon.pid}

BUILDNAME=$(date +%Y-%m-%d_%H:%M:%S)

[ -d ${WATCHDIR} ] || mkdir -p ${WATCHDIR}







|
|
|
|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>









<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







>
>












|







 







<
<
<







 







>
>









<
<
<
<
<
<



|







 







<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|

|
|
|
|
|
|

|



<
<
<
<
<
<
<
<

<
<
<
<
<



>

|

<
>
>
>
>
>

>
>
>
>
>
>
>
>
>







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|












<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|











|







 







|







 







|



|







 







|







 







|




<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>



|



>






<
<
<
<
<
<






<
<
<







 







>

|







>
|











>
>
>
>
>
>







 







|



|







 







|












|







 







|







 







|

|







 







|







 







|
|







 







|







 







>

|
|
|

|
|
|
<







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<













22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
..
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
...
310
311
312
313
314
315
316



317
318
319
320
321
322
323
...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364






365
366
367
368
369
370
371
372
373
374
375
...
407
408
409
410
411
412
413






414


























































415
416
417
418
419
420
421
...
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468








469





470
471
472
473
474
475
476

477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498





























































































499
500
501
502
503
504
505
...
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526






































































































































































































527
528
529
530
531
532
533
...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
...
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
...
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
...
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
...
882
883
884
885
886
887
888
889
890
891
892
893




















































894
895
896
897
898
899
900
....
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170






1171
1172
1173
1174
1175
1176



1177
1178
1179
1180
1181
1182
1183
....
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
....
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
....
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
....
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
....
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
....
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
....
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
....
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
....
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834

1835
1836
1837
1838
1839
1840
1841
....
1886
1887
1888
1889
1890
1891
1892


















1893
1894
1895
1896
1897
1898
1899
....
1929
1930
1931
1932
1933
1934
1935






















1936
1937
1938
1939
1940
1941
1942













































































1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

BSDPLATFORM=`uname -s | tr '[:upper:]' '[:lower:]'`

dir_empty() {
	find $1 -maxdepth 0 -empty
}

err() {
	export CRASHED=1
	if [ $# -ne 2 ]; then
		err 1 "err expects 2 arguments: exit_number \"message\""
	fi
	# Try to set status so other processes know this crashed
................................................................................
	fi
}

job_msg_verbose() {
	[ -n "${MY_JOBID}" ] || return 0
	msg_verbose "[${MY_JOBID}] $1" >&5
}

check_jobs() {
	case ${PARALLEL_JOBS} in
	''|*[!0-9]*)
		local nslaves=$(sysctl -n hw.ncpu)
		if [ -n "${JOBS_LIMIT}" ]; then
			PARALLEL_JOBS=`expr ${nslaves} / ${JOBS_LIMIT}`
		else
			PARALLEL_JOBS=${nslaves}
		fi
		;;
	esac
}

my_path() {
	echo ${MASTERMNT}${MY_JOBID+/../${MY_JOBID}}
}

my_name() {
	echo ${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}}
}




















eargs() {
	case $# in
	0) err 1 "No arguments expected" ;;
	1) err 1 "1 argument expected: $1" ;;
	*) err 1 "$# arguments expected: $*" ;;
	esac
}

. $(dirname ${0})/common.sh.${BSDPLATFORM}

run_hook() {
	local hookfile=${HOOKDIR}/${1}.sh
	shift
	[ -f ${hookfile} ] && /bin/sh ${hookfile} $@
	return 0
}

log_start() {
	local logfile=$1

	# Make sure directory exists
	mkdir -p ${logfile%/*}/success

	exec 3>&1 4>&2
	[ ! -e ${logfile}.pipe ] && mkfifo ${logfile}.pipe
	tee ${logfile} < ${logfile}.pipe >&3 &
	export tpid=$!
	exec > ${logfile}.pipe 2>&1

................................................................................
		queue_width=5
	elif [ ${nbq} -gt 999 ]; then
		queue_width=4
	elif [ ${nbq} -gt 99 ]; then
		queue_width=3
	fi




	# Skip if stopping or starting jobs
	if [ -n "${JOBS}" -a "${status#starting_jobs:}" = "${status}" -a "${status}" != "stopping_jobs:" ]; then
		now=$(date +%s)
		format_origin_phase="\t[%s]: %-30s %-13s (%s)\n"
		format_phase="\t[%s]: %13s\n"

		# Collect build stats into a string with minimal execs
................................................................................
			else
				printf "${format_phase}" ${j} ${phase}
			fi
		done
	fi

	show_log_info
	printf "[${MASTERNAME}] [${status}] [%0${queue_width}d/%0${queue_width}d]\nBuilt: %-${queue_width}d Failed: %-${queue_width}d  Ignored: %-${queue_width}d  Skipped: %-${queue_width}d\n" \
	  ${ndone} ${nbq} ${nbb} ${nbf} ${nbi} ${nbs}
}

jail_exists() {
	[ $# -ne 1 ] && eargs jailname
	local jname=$1
	[ -d ${POUDRIERED}/jails/${jname} ] && return 0
	return 1
}








porttree_list() {
	local name method mntpoint
	for p in $(find ${POUDRIERED}/${PORTLOC} -type d -maxdepth 1 -mindepth 1 -print); do
		name=${p##*/}
		mnt=$(pget ${name} mnt)
		method=$(pget ${name} method)
		echo "${name} ${method:--} ${mnt}"
	done
}

................................................................................
}

fetch_file() {
	[ $# -ne 2 ] && eargs destination source
	fetch -p -o $1 $2 || fetch -p -o $1 $2 || err 1 "Failed to fetch from $2"
}



































































unmarkfs() {
	[ $# -ne 2 ] && eargs name mnt
	local name=$1
	local mnt=$(realpath $2)

	if [ -n "$(zfs_getfs ${mnt})" ]; then
................................................................................
		zfs snapshot ${fs}@${name}
	fi

	[ $domtree -eq 0 ] && return 0
	mkdir -p ${mnt}/poudriere/
	if [ "${name}" = "prepkg" ]; then
		cat > ${mnt}/poudriere/mtree.${name}exclude << EOF
./ccache/*
./compat/linux/proc
./distfiles/*
./new_packages/*
./packages/*
./poudriere/*
./proc/*
.${PORTSRC}/*
./var/db/ports/*
./wrkdirs/*
EOF
	elif [ "${name}" = "preinst" ]; then
		cat >  ${mnt}/poudriere/mtree.${name}exclude << EOF








./compat/linux/proc/*





./etc/group
./etc/make.conf
./etc/make.conf.bak
./etc/master.passwd
./etc/passwd
./etc/pwd.db
./etc/shells

./etc/spwd.db
./poudriere/*
./new_packages/*
.${HOME}/*
./tmp/*
.${LOCALBASE:-/usr/local}/etc/gconf/gconf.xml.defaults
.${LOCALBASE:-/usr/local}/lib/charset.alias
.${LOCALBASE:-/usr/local}/share/applications/mimeinfo.cache
.${LOCALBASE:-/usr/local}/share/nls/POSIX
.${LOCALBASE:-/usr/local}/share/nls/en_US.US-ASCII
./var/db/*
./var/log/*
./var/mail/*
./var/run/*
./wrkdirs/*
EOF
	fi
	mtree -X ${mnt}/poudriere/mtree.${name}exclude \
		-xcn -k uid,gid,mode,size \
		-p ${mnt} > ${mnt}/poudriere/mtree.${name}
}































































































use_options() {
	[ $# -ne 2 ] && eargs mnt optionsdir
	local mnt=$1
	local optionsdir=$2

	if [ "${optionsdir}" = "-" ]; then
................................................................................
	else
		optionsdir="${POUDRIERED}/${optionsdir}-options"
	fi
	[ -d "${optionsdir}" ] || return 1
	optionsdir=$(realpath ${optionsdir} 2>/dev/null)
	[ "${mnt##*/}" = "ref" ] &&
		msg "Mounting /var/db/ports from: ${optionsdir}"
	${NULLMOUNT} -o ro ${optionsdir} ${mnt}/var/db/ports ||
		err 1 "Failed to mount OPTIONS directory"

	return 0
}

mount_packages() {
	local mnt=$(my_path)
	mount -t nullfs "$@" ${POUDRIERE_DATA}/packages/${MASTERNAME} \
		${mnt}/packages ||
		err 1 "Failed to mount the packages directory "
}







































































































































































































sanity_check_pkgs() {
	local ret=0
	local depfile
	[ ! -d ${POUDRIERE_DATA}/packages/${MASTERNAME}/All ] && return $ret
	dirempty ${POUDRIERE_DATA}/packages/${MASTERNAME}/All && return $ret
	for pkg in ${POUDRIERE_DATA}/packages/${MASTERNAME}/All/*.${PKG_EXT}; do
		# Check for non-empty directory with no packages in it
................................................................................
	return 0
}

# Build+test port and return on first failure
build_port() {
	[ $# -ne 1 ] && eargs portdir
	local portdir=$1
	local port=${portdir##${PORTSRC}/}
	local targets="check-config pkg-depends fetch-depends fetch checksum \
				   extract-depends extract patch-depends patch build-depends \
				   lib-depends configure build install-mtree run-depends \
				   install package ${PORTTESTING:+deinstall}"
	local mnt=$(my_path)
	local log=$(log_path)
	local listfilecmd network
	local hangstatus

	for phase in ${targets}; do
		bset ${MY_JOBID} status "${phase}:${port}"
		job_msg_verbose "Status   ${port}: ${phase}"
		if [ "${phase}" = "fetch" ]; then
			jstop
			jstart 1
		fi
		case ${phase} in
		install) [ -n "${PORTTESTING}" ] && markfs preinst ${mnt} ;;
		deinstall)
................................................................................
		print_phase_header ${phase}

		if [ "${phase}" = "package" ]; then
			echo "PACKAGES=/new_packages" >> ${mnt}/etc/make.conf
			# Create sandboxed staging dir for new package for this build
			rm -rf "${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages/${PKGNAME}"
			mkdir -p "${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages/${PKGNAME}"
			${NULLMOUNT} \
				"${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages/${PKGNAME}" \
				${mnt}/new_packages
		fi

		if [ "${phase#*-}" = "depends" ]; then
			# No need for nohang or PKGENV/PORT_FLAGS for *-depends
			injail make -C ${portdir} ${phase}
................................................................................
			if [ $hangstatus -ne 0 ]; then
				# 1 = cmd failed, not a timeout
				# 2 = log timed out
				# 3 = cmd timeout
				if [ $hangstatus -eq 2 ]; then
					msg "Killing runaway build"
					bset ${MY_JOBID} status "${phase}/runaway:${port}"
					job_msg_verbose "Status   ${port}: runaway"
				elif [ $hangstatus -eq 3 ]; then
					msg "Killing timed out build"
					bset ${MY_JOBID} status "${phase}/timeout:${port}"
					job_msg_verbose "Status   ${port}: timeout"
				fi
				return 1
			fi
		fi

		if [ "${phase}" = "checksum" ]; then
			jstop
................................................................................
}

# Save wrkdir and return path to file
save_wrkdir() {
	[ $# -ne 4 ] && eargs mnt port portdir phase
	local mnt=$1
	local port="$2"
	local portdir="$(echo "$3" | sed -e "s|${PORTSRC}/||g")"
	local phase="$4"
	local tardir=${POUDRIERE_DATA}/wrkdirs/${MASTERNAME}/${PTNAME}
	local tarname=${tardir}/${PKGNAME}.${WRKDIR_ARCHIVE_FORMAT}
	local mnted_portdir=${mnt}/wrkdirs/${portdir}

	[ -n "${SAVE_WRKDIR}" ] || return 0
	# Only save if not in fetch/checksum phase
................................................................................
	case ${WRKDIR_ARCHIVE_FORMAT} in
	tar) COMPRESSKEY="" ;;
	tgz) COMPRESSKEY="z" ;;
	tbz) COMPRESSKEY="j" ;;
	txz) COMPRESSKEY="J" ;;
	esac
	rm -f ${tarname}
	tar -s ",${mnt}/wrkdirs,," -c${COMPRESSKEY}f ${tarname} ${mnted_portdir}/work > /dev/null 2>&1

	job_msg "Saved ${port} wrkdir to: ${tarname}"
}






















































deadlock_detected() {
	local always_fail=${1:-1}
	local mnt=$(my_path)
	local crashed_packages dependency_cycles

	# If there are still packages marked as "building" they have crashed
................................................................................
	local build_failed=0
	local name=${MASTERNAME}-job-${MY_JOBID}
	local mnt=$(my_path)
	local failed_status failed_phase cnt
	local clean_rdepends=0
	local log=$(log_path)
	local ignore
	local zip_log=0

	export PKGNAME="${pkgname}" # set ASAP so cleanup() can use it
	port=$(cache_get_origin ${pkgname})
	portdir="${PORTSRC}/${port}"

	job_msg "Starting build of ${port}"
	bset ${MY_JOBID} status "starting:${port}"
	create_slave ${MASTERMNT} ${MY_JOBID}

	if [ ${TMPFS_LOCALBASE} -eq 1 ]; then
		umount -f ${mnt}/${LOCALBASE:-/usr/local} 2>/dev/null || :
		mount -t tmpfs tmpfs ${mnt}/${LOCALBASE:-/usr/local}
	fi







	# If this port is IGNORED, skip it
	# This is checked here instead of when building the queue
	# as the list may start big but become very small, so here
	# is a less-common check
	ignore="$(injail make -C ${portdir} -VIGNORE)"




	log_start ${log}/logs/${PKGNAME}.log
	msg "Building ${port}"
	buildlog_start ${portdir}

	if [ -n "${ignore}" ]; then
		msg "Ignoring ${port}: ${ignore}"
		badd ports.ignored "${port} ${PKGNAME} ${ignore}"
................................................................................
		elif [ -f ${mnt}/${portdir}/.keep ]; then
			save_wrkdir ${mnt} "${port}" "${portdir}" "noneed" ||:
		fi

		injail make -C ${portdir} clean

		if [ ${build_failed} -eq 0 ]; then
			zip_log=1
			badd ports.built "${port} ${PKGNAME}"
			job_msg "Finished ${port}: Success"
			run_hook pkgbuild success "${port}" "${PKGNAME}"
			# Cache information for next run
			pkg_cache_data "${POUDRIERE_DATA}/packages/${MASTERNAME}/All/${PKGNAME}.${PKG_EXT}" ${port} || :
		else
			# Symlink the buildlog into errors/
			ln -s ../${PKGNAME}.log ${log}/logs/errors/${PKGNAME}.log
			badd ports.failed "${port} ${PKGNAME} ${failed_phase}"
			echo "${port} ${failed_phase}" >> $(log_path)/last_run.failed
			job_msg "Finished ${port}: Failed: ${failed_phase}"
			run_hook pkgbuild failed "${port}" "${PKGNAME}" "${failed_phase}" \
				"${log}/logs/errors/${PKGNAME}.log"
			clean_rdepends=1
		fi
	fi

	clean_pool ${PKGNAME} ${clean_rdepends}

	bset ${MY_JOBID} status "done:${port}"

	stop_build ${portdir} ${log}/logs/${PKGNAME}.log

	destroy_slave ${MASTERMNT} ${MY_JOBID}
	if [ ${zip_log} -eq 1 ]; then
		bzip2 ${log}/logs/${PKGNAME}.log
		mv ${log}/logs/${PKGNAME}.log.bz2 $(log_path)/success/
	fi

	echo ${MY_JOBID} >&6
}

stop_build() {
	[ $# -eq 2 ] || eargs portdir logfile
	local portdir="$1"
................................................................................
	}

	[ $xtrace -eq 1 ] && set -x
}

list_deps() {
	[ $# -ne 1 ] && eargs directory
	local dir="${PORTSRC}/$1"
	local makeargs="-VPKG_DEPENDS -VBUILD_DEPENDS -VEXTRACT_DEPENDS -VLIB_DEPENDS -VPATCH_DEPENDS -VFETCH_DEPENDS -VRUN_DEPENDS"

	mangle_stderr "WARNING" "($1)" injail make -C ${dir} $makeargs | \
		tr '\n' ' ' | sed -e "s,[[:graph:]]*${PORTSRC}/,,g" \
		-e "s,:[[:graph:]]*,,g" | \
		sort -u || err 1 "Makefile broken: $1"
}

deps_file() {
	[ $# -ne 1 ] && eargs pkg
	local pkg=$1
................................................................................
	fi

	mkdir -p $(pkg_cache_dir ${pkg})

	o=$(pkg_get_origin ${pkg})
	v=${pkg##*-}
	v=${v%.*}
	if [ ! -d "${mnt}${PORTSRC}/${o}" ]; then
		msg "${o} does not exist anymore. Deleting stale ${pkg##*/}"
		delete_pkg ${pkg}
		return 0
	fi
	v2=$(cache_get_pkgname ${o})
	v2=${v2##*-}
	if [ "$v" != "$v2" ]; then
		msg "Deleting old version: ${pkg##*/}"
		delete_pkg ${pkg}
		return 0
	fi

	current_deps=$(injail make -C ${PORTSRC}/${o} run-depends-list | sed 's,${PORTSRC}/,,g' | tr '\n' ' ')
	compiled_deps=$(pkg_get_dep_origin ${pkg})
	for d in ${current_deps}; do
		case " $compiled_deps " in
		*\ $d\ *) ;;
		*)
			msg "Direct dependency change, deleting: ${pkg##*/}"
			delete_pkg ${pkg}
................................................................................
			return 0
			;;
		esac
	done

	# Check if the compiled options match the current options from make.conf and /var/db/options
	if [ "${CHECK_CHANGED_OPTIONS:-no}" != "no" ]; then
		current_options=$(injail make -C ${PORTSRC}/${o} pretty-print-config | \
			tr ' ' '\n' | sed -n 's/^\+\(.*\)/\1/p' | sort | tr '\n' ' ')
		compiled_options=$(pkg_get_options ${pkg})

		if [ "${compiled_options}" != "${current_options}" ]; then
			msg "Options changed, deleting: ${pkg##*/}"
			if [ "${CHECK_CHANGED_OPTIONS}" = "verbose" ]; then
				msg "Pkg: ${compiled_options}"
................................................................................
	local cache_origin_pkgname=${MASTERMNT}/poudriere/var/cache/origin-pkgname/${origin%%/*}_${origin##*/}
	local cache_pkgname_origin

	[ -f ${cache_origin_pkgname} ] && read pkgname < ${cache_origin_pkgname}

	# Add to cache if not found.
	if [ -z "${pkgname}" ]; then
		[ -d "${MASTERMNT}${PORTSRC}/${origin}" ] ||
			err 1 "Invalid port origin '${origin}' not found."
		pkgname=$(injail make -C ${PORTSRC}/${origin} -VPKGNAME ||
			err 1 "Error getting PKGNAME for ${origin}")
		# Make sure this origin did not already exist
		existing_origin=$(cache_get_origin "${pkgname}" 2>/dev/null || :)
		# It may already exist due to race conditions, it is not harmful. Just ignore.
		if [ "${existing_origin}" != "${origin}" ]; then
			[ -n "${existing_origin}" ] &&
				err 1 "Duplicated origin for ${pkgname}: ${origin} AND ${existing_origin}. Rerun with -vv to see which ports are depending on these."
................................................................................
			"${mnt}/poudriere/rdeps/${dep_pkgname}/${pkgname}"
	done
}

listed_ports() {
	if [ ${ALL:-0} -eq 1 ]; then
		PORTSDIR=$(pget ${PTNAME} mnt)
		[ -d "${PORTSDIR}/${PORTLOC}" ] && PORTSDIR="${PORTSDIR}/${PORTLOC}"
		for cat in $(awk '$1 == "SUBDIR" { print $3}' ${PORTSDIR}/Makefile); do
			awk -v cat=${cat} '$1 == "SUBDIR" { print cat"/"$3}' ${PORTSDIR}/${cat}/Makefile
		done
		return 0
	fi
	if [ -z "${LISTPORTS}" ]; then
		[ -n "${LISTPKGS}" ] &&
................................................................................
cache_get_key() {
	if [ -z "${CACHE_KEY}" ]; then
		CACHE_KEY=$({
			injail env
			cat ${MASTERMNT}/etc/make.conf
			injail find /var/db/ports -exec sha256 {} +
			echo ${MASTERNAME}
			if [ -f ${MASTERMNT}/${PORTSRC}/poudriere.stamp ]; then
				cat ${MASTERMNT}/${PORTSRC}/poudriere.stamp
			else
				# This is not a poudriere-managed ports tree.
				# Just toss in getpid() to invalidate the cache
				# as there is no quick way to hash the tree without
				# taking possibly minutes+
				echo $$
			fi
................................................................................
	mkdir -p ${log}/logs ${log}/logs/errors
	ln -sfh ${BUILDNAME} ${log%/*}/latest
	cp ${HTMLPREFIX}/* ${log}

	bset status "computingdeps:"
	parallel_start
	for port in $(listed_ports); do
		[ -d "${MASTERMNT}${PORTSRC}/${port}" ] ||
			err 1 "Invalid port origin: ${port}"
		parallel_run compute_deps ${port}
	done
	parallel_stop

	bset status "sanity:"

................................................................................
		msg "Sanity checking the repository"
		delete_stale_pkg_cache
		delete_old_pkgs

		while :; do
			sanity_check_pkgs && break
		done
	fi

	msg "Deleting stale symlinks"
	find -L ${POUDRIERE_DATA}/packages/${MASTERNAME} -type l \
		-exec rm -f {} +

	msg "Deleting empty directories"
	find ${POUDRIERE_DATA}/packages/${MASTERNAME} -type d \
		-mindepth 1 -empty -delete


	bset status "cleaning:"
	msg "Cleaning the build queue"
	export LOCALBASE=${LOCALBASE:-/usr/local}
	for pn in $(ls ${MASTERMNT}/poudriere/deps/); do
		if [ -f "${MASTERMNT}/packages/All/${pn}.${PKG_EXT}" ]; then
			find_all_pool_references "${pn}"
................................................................................
		done
		mv ${pkg_dir} ${MASTERMNT}/poudriere/pool/${dep_count##* }/ 2>/dev/null || :
	done

	rmdir ${lock}
}



















RESOLV_CONF=""
STATUS=0 # out of jail #

[ -f ${SCRIPTPREFIX}/../../etc/poudriere.conf ] ||
	err 1 "Unable to find ${SCRIPTPREFIX}/../../etc/poudriere.conf"

. ${SCRIPTPREFIX}/../../etc/poudriere.conf
................................................................................
	case ${ZROOTFS} in
	[!/]*) err 1 "ZROOTFS shoud start with a /" ;;
	esac
fi

[ -n "${MFSSIZE}" -a -n "${USE_TMPFS}" ] && err 1 "You can't use both tmpfs and mdmfs"























POUDRIERE_DATA=`get_data_dir`
: ${WRKDIR_ARCHIVE_FORMAT="tbz"}
case "${WRKDIR_ARCHIVE_FORMAT}" in
	tar|tgz|tbz|txz);;
	*) err 1 "invalid format for WRKDIR_ARCHIVE_FORMAT: ${WRKDIR_ARCHIVE_FORMAT}" ;;
esac















































































case ${POOL_BUCKETS} in
''|*[!0-9]*)
	POOL_BUCKETS=10
	;;
esac

: ${WATCHDIR:=${POUDRIERE_DATA}/queue}
: ${PIDFILE:=${POUDRIERE_DATA}/daemon.pid}

BUILDNAME=$(date +%Y-%m-%d_%H:%M:%S)

[ -d ${WATCHDIR} ] || mkdir -p ${WATCHDIR}

Added src/share/poudriere/common.sh.dragonfly.













































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/sh
# DRAGONFLY SPECIFIC DEFINITIONS

PORTSRC=/usr/dports
PORTLOC=dports
STD_DISTFILES=/usr/distfiles
STD_PACKAGES=/usr/packages
NULLMOUNT="/sbin/mount_null"
NULLFSREF="null"
NO_ZFS=TRUE
USE_TMPFS=true
TMPFS_WRKDIR=0
TMPFS_DATA=0
TMPFS_ALL=0
TMPFS_LOCALBASE=0

injail()  {
	chroot ${MASTERMNT}${MY_JOBID+/../${MY_JOBID} $@
}

append_make() {
	[ $# -ne 2 ] && eargs mnt makeconf
	local mnt=$1
	local makeconf=$2

	if [ "${makeconf}" = "-" ]; then
		makeconf="${POUDRIERED}/make.conf"
	else
		makeconf="${POUDRIERED}/${makeconf}-make.conf"
	fi

	[ -f "${makeconf}" ] || return 0
	makeconf="$(realpath ${makeconf} 2>/dev/null)"
	msg "Appending to /etc2/make.conf: ${makeconf}"
	echo "#### ${makeconf} ####" >> ${mnt}/etc2/make.conf
	cat "${makeconf}" >> ${mnt}/etc2/make.conf
}

jail_runs() {
	[ $# -ne 1 ] && eargs jname
	local check=$(jget ${1} status)
	[ "${check}" = "idle:" ] && return 0
	return 1
}

jail_start() {
	[ $# -lt 2 ] && eargs name ptname setname
	local name=$1
	local ptname=$2
	local setname=$3
	local arch=$(jget ${name} arch)
	local mnt=$(jget ${name} mnt)
	local NEEDFS="${NULLFSREF} procfs tmpfs"
	local BASEIMG=${MASTERMNT}/ref

	jail_exists ${name} || err 1 "No such jail: ${name}"
	jail_runs ${MASTERNAME} && err 1 "jail already running: ${MASTERNAME}"

	if [ -z "${NOLINUX}" ]; then
		if [ "${arch}" = "i386" ]; then
			NEEDFS="${NEEDFS} linprocfs"
			sysctl -n compat.linux.osrelease >/dev/null 2>&1 || kldload linux
		fi
	fi
	for fs in ${NEEDFS}; do
		lsvfs $fs >/dev/null 2>&1 || kldload $fs
	done
	bset status "initializing:"

	mkdir -p ${BASEIMG}
	mount_tmpfs -s 400M tmpfs ${BASEIMG}
	cpdup -x -VV -i0 ${mnt}/ ${BASEIMG}/
	mv ${BASEIMG}/etc ${BASEIMG}/etc2
	mv ${BASEIMG}/var ${BASEIMG}/var2
	mkdir ${BASEIMG}/etc \
	      ${BASEIMG}/var \
	      ${BASEIMG}/usr/local

	[ -n "${RESOLV_CONF}" ] && cp -v "${RESOLV_CONF}" "${BASEIMG}/etc2/"

	if [ -d "${CCACHE_DIR:-/nonexistent}" ]; then
		echo "WITH_CCACHE_BUILD=yes" >> ${tomnt}/etc2/make.conf
		echo "MAKE_ENV+= CCACHE_DIR=/ccache" >> ${tomnt}/etc2/make.conf
	fi
	echo "PACKAGES=/packages" >> ${tomnt}/etc2/make.conf
	echo "DISTDIR=/distfiles" >> ${tomnt}/etc2/make.conf

	makeconf="- ${setname} ${ptname} ${name} ${name}-${ptname}"
	[ -n "${setname}" ] && makeconf="${makeconf} ${name}-${setname}"
	makeconf="${makeconf} ${MASTERNAME}"
	for opt in ${makeconf}; do
		append_make ${tomnt} ${opt}
	done

	# The master jail is malleable so /etc/ can be modified later
	# The slave jails are read only.  All have same source

	[ -z "${NO_PACKAGE_BUILDING}" ] && export PACKAGE_BUILDING=yes
	[ ${SET_STATUS_ON_START-1} -eq 1 ] && export STATUS=1

	export HOME=/root
	export USER=root
	export FORCE_PACKAGE=yes
	export PKGNG=1
	export PKG_EXT="txz"
	export PKG_ADD="${LOCALBASE:-/usr/local}/sbin/pkg add"
	export PKG_DELETE="${LOCALBASE:-/usr/local}/sbin/pkg delete -y -f"
}

jail_stop() {
	[ $# -ne 0 ] && eargs
	local mnt
	bset status "stop:"

	cd /
	msg "Umounting file systems"
	mnt=`realpath ${MASTERMNT:-${JAILMNT}}`
	mount | awk -v mnt="${mnt}/" 'BEGIN{ gsub(/\//, "\\\/", mnt); } { if ($3 ~ mnt && $1 !~ /\/dev\/md/ ) { print $3 }}' |  sort -r | xargs umount || :
	umount ${mnt} || :

	export STATUS=0
}

jail_dismount() {
	[ $# -ne 0 ] && eargs
	local mnt

	cd /
	msg "Umounting file systems"
	mnt=`realpath ${JAILMNT}`
	mount | awk -v mnt="${mnt}" '{ if ($3 ~ mnt && $1 !~ /\/dev\/md/ ) { print $3 }}' |  sort -r | xargs umount -v || :
}

dismount() {
	[ $# -ne 1 ] && eargs mountpoint
	umount ${1} || :
}

create_slave() {
	[ $# -ne 1 ] && eargs job-number
	local task="${1}"
	local mnt="${MASTERMNT}/${task}"
	local arch=$(jget ${jname} arch)
	local BASEIMG=${MASTERMNT}/ref

	bset ${task} status "genesis:"
	if [ -f ${mnt}/virgin ]; then
	    job_msg "Jail     Mounting"
	    # Make one-time readonly duplicate of base image, virgin hidden
	    ${NULLMOUNT} -o rdonly ${BASEIMG} ${mnt}
	    do_jail_mounts ${mnt} ${arch}
	    do_portbuild_mounts ${mnt} ${jname} ${ptname} ${setname}
	else
	    job_msg "Jail     Renovating"
	fi
	# Refresh /usr/local and /etc and /var
	mount_tmpfs -s 100M  tmpfs ${mnt}/tmp
	mount_tmpfs -s 7G    tmfps ${mnt}/wrkdirs
	mount_tmpfs -s 12M   tmpfs ${mnt}/etc
	mount_tmpfs -s 100M  tmpfs ${mnt}/var
	mount_tmpfs -s 64K   tmpfs ${mnt}/compat
	mount_tmpfs -s 2500M tmpfs ${mnt}/usr/local
	mkdir -p ${mnt}/compat/linprocfs \
		 ${mnt}/compat/linux/proc \
		 ${mnt}/compat/svr4
	cp -a ${mnt}/etc2/* ${mnt}/etc/
	cp -a ${mnt}/var2/* ${mnt}/var/

	JAILMNT=${mnt} injail /usr/sbin/mtree -q -U -f \
	   ${PORTSRC}/Templates/BSD.local.dist -d -e -p /usr/local >/dev/null

	job_msg "Jail     Ready"
}

do_jail_mounts() {
	[ $# -ne 2 ] && eargs mnt arch
	local mnt=$1
	local arch=$2

	mount -t devfs devfs ${mnt}/dev
	mount -t procfs proc ${mnt}/proc

	if [ -z "${NOLINUX}" ]; then
		if [ "${arch}" = "i386" ]; then
			mount -t linprocfs linprocfs ${mnt}/compat/linux/proc
		fi
	fi
}

do_portbuild_mounts() {
	[ $# -lt 3 ] && eargs mnt jname ptname setname
	local mnt=$1
	local jname=$2
	local ptname=$3
	local setname=$4
	local optionsdir

	mkdir -p ${POUDRIERE_DATA}/packages/${MASTERNAME}/All
	[ -d "${CCACHE_DIR:-/nonexistent}" ] &&
		${NULLMOUNT} ${CCACHE_DIR} ${mnt}/ccache

	${NULLMOUNT} -o ro ${PORTSDIR} ${mnt}/${PORTSRC} || 
		err 1 "Failed to mount the ports directory "
	${NULLMOUNT} ${POUDRIERE_DATA}/packages/${MASTERNAME} ${mnt}/${STD_PACKAGES} ||
		err 1 "Failed to mount the packages directory "
	${NULLMOUNT} ${DISTFILES_CACHE} ${mnt}/${STD_DISTFILES} ||
		err 1 "Failed to mount the distfiles cache directory"

	optionsdir="${MASTERNAME}"
	[ -n "${setname}" ] && optionsdir="${optionsdir} ${jname}-${setname}"
	optionsdir="${optionsdir} ${jname}-${ptname} ${setname} ${ptname} ${jname} -"

	for opt in ${optionsdir}; do
		use_options ${mnt} ${opt} && break || continue
	done

	return 0
}

destroy_slave() {
	[ $# -ne 2 ] && eargs job-number
	local task="${1}"
	local mnt="${MASTERMNT}/${task}"
	bset ${task} status "dismounting:${task}"

	# make sure cwd is not on mount
	cd /

	# Repeat dismount attempts in case 1 or more failed
	# This also helps reap mount on previous failed attempts
	for x in 1 2; do
	   [ ${x} -eq 1 ] && sleep 3
	   dismount ${mnt}/tmp
	   dismount ${mnt}/wrkdirs
	   dismount ${mnt}/etc
	   dismount ${mnt}/var
	   dismount ${mnt}/compat/linprocfs
	   dismount ${mnt}/compat/srv4
	   dismount ${mnt}/compat
	   dismount ${mnt}/usr/local
	done

	bset ${task} status "available:"
}

start_builders() {
	local task mnt

	bset builders "${JOBS}"
	bset status "starting_builders:"
	for task in ${JOBS}; do
		local mnt="${MASTERMNT}/${task}"
		mkdir -p ${mnt}
		touch ${mnt}/virgin
	done
	bset epoch "$(date '+%s')"
}


stop_builders() {
	local mnt

	# wait for the last running processes
	cat ${MASTERMNT}/poudriere/var/run/*.pid 2>/dev/null | xargs pwait 2>/dev/null

	msg "Stopping ${PARALLEL_JOBS} builders"

	# wait for slaves to finish dismounting
	sleep 3

	cd /
	mnt=`realpath ${JAILMNT}`
	mount | awk -v mnt="${MASTERMNT}/" 'BEGIN{ gsub(/\//, "\\\/", mnt); } { if ($3 ~ mnt) { print $3 }}' |  sort -r | xargs umount || :

	# No builders running, unset JOBS
	JOBS=""
}

zfs_getfs() {
	# NO-OP always
}

jstop() {
	# NO-OP for chroot
}

jstart() {
	# NO-OP for chroot
}

killtree() {
	local _pid=${1}
	local _sig=${2}
	local _child
	local _children

	# stop parent from attempting to folk
	kill -17 ${_pid} 2>/dev/null
	if [ $? -eq 0 ]; then
	   _children=$(pgrep -P ${_pid})
	   for _child in ${children}; do
	      killtree ${_child} ${_sig}
	   done
	   kill -${_sig} ${_pid}
	fi
}

cleanup() {
	[ -n "${CLEANED_UP}" ] && return 0
	# Prevent recursive cleanup on error
	if [ -n "${CLEANING_UP}" ]; then
		echo "Failure cleaning up. Giving up." >&2
		return
	fi
	export CLEANING_UP=1
	msg "Cleaning up"

	# If this is a builder, don't cleanup, the master will handle that.
	if [ -n "${MY_JOBID}" ]; then
		[ -n "${PKGNAME}" ] && clean_pool ${PKGNAME} 1 || :
		return 0
	fi

	[ -n "${MASTERNAME}" ] && rm -rf \
		${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages

	bset epoch "0"
	# Only bother with this if using jails as this may be being ran
	# from queue.sh or daemon.sh, etc.
	if [ -n "${MASTERMNT}" -a -n "${MASTERNAME}" ]; then
		if [ -d ${MASTERMNT}/poudriere/var/run ]; then
			for pid in ${MASTERMNT}/poudriere/var/run/*.pid; do
				# Ensure there is a pidfile to read or break
				[ "${pid}" = "${MASTERMNT}/poudriere/var/run/*.pid" ] && break
				killtree `cat ${pid}` 9 || :
			done
		fi
		wait
	fi

	export CLEANED_UP=1
}

Added src/share/poudriere/common.sh.freebsd.





























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/bin/sh
# FREEBSD SPECIFIC DEFINITIONS

PORTSRC=/usr/ports
PORTLOC=ports
STD_DISTFILES=${PORTSRC}/distfiles
STD_PACKAGES=${PORTSRC}/packages
NULLMOUNT="/sbin/mount -t nullfs"
NULLFSREF="nullfs"
TMPFS_WRKDIR=0
TMPFS_DATA=0
TMPFS_ALL=0
TMPFS_LOCALBASE=0

# zfs namespace
NS="poudriere"
IPS="$(sysctl -n kern.features.inet 2>/dev/null || echo 0)$(sysctl -n kern.features.inet6 2>/dev/null || echo 0)"
RELDATE=$(sysctl -n kern.osreldate)
JAILED=$(sysctl -n security.jail.jailed)

injail() {
	jexec -U root ${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} $@
}

append_make() {
	[ $# -ne 2 ] && eargs mnt makeconf
	local mnt=$1
	local makeconf=$2

	if [ "${makeconf}" = "-" ]; then
		makeconf="${POUDRIERED}/make.conf"
	else
		makeconf="${POUDRIERED}/${makeconf}-make.conf"
	fi

	[ -f "${makeconf}" ] || return 0
	makeconf="$(realpath ${makeconf} 2>/dev/null)"
	msg "Appending to /etc/make.conf: ${makeconf}"
	echo "#### ${makeconf} ####" >> ${mnt}/etc/make.conf
	cat "${makeconf}" >> ${mnt}/etc/make.conf
}

jstart() {
	local network="${localipargs}"
	[ $1 -eq 1 ] && network="${ipargs}"

	jail -c persist name=${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} \
		path=${MASTERMNT}${MY_JOBID+/../${MY_JOBID}} \
		host.hostname=${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} \
		${network} \
		allow.socket_af allow.raw_sockets allow.chflags allow.sysvipc
}

jstop() {
	jail -r ${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}} 2>/dev/null || :
}

jail_runs() {
	[ $# -ne 1 ] && eargs jname
	local jname=$1
	jls -j $jname >/dev/null 2>&1 && return 0
	return 1
}

jail_start() {
	[ $# -lt 2 ] && eargs name ptname setname
	local name=$1
	local ptname=$2
	local setname=$3
	local portsdir=$(pget ${ptname} mnt)
	local arch=$(jget ${name} arch)
	local mnt=$(jget ${name} mnt)
	local needfs="${NULLFSREF} procfs"
	local makeconf

	local tomnt=${POUDRIERE_DATA}/build/${MASTERNAME}/ref

	[ -d ${DISTFILES_CACHE:-/nonexistent} ] || err 1 "DISTFILES_CACHE directory does not exist. (c.f. poudriere.conf)"

	if [ -z "${NOLINUX}" ]; then
		if [ "${arch}" = "i386" -o "${arch}" = "amd64" ]; then
			needfs="${needfs} linprocfs linsysfs"
			sysctl -n compat.linux.osrelease >/dev/null 2>&1 || kldload linux
		fi
	fi
	[ -n "${USE_TMPFS}" ] && NEEDFS="${NEEDFS} tmpfs"
	for fs in ${needfs}; do
		if ! lsvfs $fs >/dev/null 2>&1; then
			if [ $JAILED -eq 0 ]; then
				kldload $fs
			else
				err 1 "please load the $fs module on host using \"kldload $fs\""
			fi
		fi
	done
	jail_exists ${name} || err 1 "No such jail: ${name}"
	jail_runs ${MASTERNAME} && err 1 "jail already running: ${MASTERNAME}"
	export HOME=/root
	export USER=root
	export FORCE_PACKAGE=yes
	if [ -z "${NO_PACKAGE_BUILDING}" ]; then
		export PACKAGE_BUILDING=yes
	fi

	[ ${SET_STATUS_ON_START-1} -eq 1 ] && export STATUS=1
	msg_n "Creating the reference jail..."
	clonefs ${mnt} ${tomnt} clean
	echo " done"

	msg "Mounting system devices for ${MASTERNAME}"
	do_jail_mounts ${tomnt} ${arch}

	[ -d "${portsdir}/ports" ] && portsdir="${portsdir}/ports"
	msg "Mounting ports/packages/distfiles"
	do_portbuild_mounts ${tomnt} ${name} ${ptname} ${setname}

	if [ -n "${POUDRIERE_BUILD_TYPE}" ]; then
		show_log_info
	fi

	if [ -d "${CCACHE_DIR:-/nonexistent}" ]; then
		echo "WITH_CCACHE_BUILD=yes" >> ${tomnt}/etc/make.conf
		echo "MAKE_ENV+= CCACHE_DIR=/ccache" >> ${tomnt}/etc/make.conf
	fi
	echo "PACKAGES=/packages" >> ${tomnt}/etc/make.conf
	echo "DISTDIR=/distfiles" >> ${tomnt}/etc/make.conf

	makeconf="- ${setname} ${ptname} ${name} ${name}-${ptname}"
	[ -n "${setname}" ] && makeconf="${makeconf} ${name}-${setname}"
	makeconf="${makeconf} ${MASTERNAME}"
	for opt in ${makeconf}; do
		append_make ${tomnt} ${opt}
	done

	test -n "${RESOLV_CONF}" && cp -v "${RESOLV_CONF}" "${tomnt}/etc/"
	msg "Starting jail ${MASTERNAME}"
	jstart 0
	# Only set STATUS=1 if not turned off
	# jail -s should not do this or jail will stop on EXIT
	WITH_PKGNG=$(injail make -f ${PORTSRC}/Mk/bsd.port.mk -V WITH_PKGNG)
	if [ -n "${WITH_PKGNG}" ]; then
		export PKGNG=1
		export PKG_EXT="txz"
		export PKG_ADD="${LOCALBASE:-/usr/local}/sbin/pkg add"
		export PKG_DELETE="${LOCALBASE:-/usr/local}/sbin/pkg delete -y -f"
	else
		export PKGNG=0
		export PKG_ADD=pkg_add
		export PKG_DELETE=pkg_delete
		export PKG_EXT="tbz"
	fi

	# 8.3 did not have distrib-dirs ran on it, so various
	# /usr and /var dirs are missing. Namely /var/games
	if [ "$(injail uname -r | cut -d - -f 1 )" = "8.3" ]; then
		injail mtree -eu -f /etc/mtree/BSD.var.dist -p /var >/dev/null 2>&1 || :
		injail mtree -eu -f /etc/mtree/BSD.usr.dist -p /usr >/dev/null 2>&1 || :
	fi
}

jail_stop() {
	[ $# -ne 0 ] && eargs
	jail_runs ${MASTERNAME} || err 1 "No such jail running: ${MASTERNAME}"
	local fs=$(zfs_getfs ${MASTERMNT})

	# err() will set status to 'crashed', don't override.
	[ -n "${CRASHED}" ] || bset status "stop:" 2>/dev/null || :

	jstop
	# Shutdown all builders
	if [ ${PARALLEL_JOBS} -ne 0 ]; then
		# - here to only check for unset, {start,stop}_builders will set this to blank if already stopped
		for j in ${JOBS-$(jot -w %02d ${PARALLEL_JOBS})}; do
			MY_JOBID=${j} jstop
			destroyfs ${MASTERMNT}/../${j} jail || :
		done
	fi
	msg "Umounting file systems"
	destroyfs ${MASTERMNT} jail
	rm -rf ${MASTERMNT}/../
	export STATUS=0
}

do_jail_mounts() {
	[ $# -ne 2 ] && eargs mnt arch
	local mnt=$1
	local arch=$2
	local devfspath="null zero random urandom stdin stdout stderr fd fd/*"

	# clone will inherit from the ref jail
	if [ ${mnt##*/} = "ref" ]; then
		mkdir -p ${mnt}/proc
		mkdir -p ${mnt}/dev
		mkdir -p ${mnt}/compat/linux/proc
		mkdir -p ${mnt}${PORTSRC}
		mkdir -p ${mnt}/wrkdirs
		mkdir -p ${mnt}/${LOCALBASE:-/usr/local}
		mkdir -p ${mnt}/distfiles
		mkdir -p ${mnt}/packages
		mkdir -p ${mnt}/new_packages
		mkdir -p ${mnt}/ccache
		mkdir -p ${mnt}/var/db/ports
	fi

	# ref jail only needs devfs
	mount -t devfs devfs ${mnt}/dev
	devfs -m ${mnt}/dev rule apply hide
	for p in ${devfspath} ; do
		devfs -m ${mnt}/dev/ rule apply path "${p}" unhide
	done
	if [ "${mnt##*/}" != "ref" ]; then
		[ ${JAILED} -eq 0 ] && mount -t fdescfs fdesc ${mnt}/dev/fd
		mount -t procfs proc ${mnt}/proc
		if [ -z "${NOLINUX}" ]; then
			[ "${arch}" = "i386" -o "${arch}" = "amd64" ] &&
				mount -t linprocfs linprocfs ${mnt}/compat/linux/proc
			fi
		fi

	return 0
}

mount_packages() {
	local mnt=$(my_path)
	${NULLMOUNT} "$@" ${POUDRIERE_DATA}/packages/${MASTERNAME} \
		${mnt}/packages ||
		err 1 "Failed to mount the packages directory "
}

do_portbuild_mounts() {
	[ $# -lt 3 ] && eargs mnt jname ptname setname
	local mnt=$1
	local jname=$2
	local ptname=$3
	local setname=$4
	local portsdir=$(pget ${ptname} mnt)
	local optionsdir

	[ -d ${portsdir}/${PORTLOC} ] && portsdir=${portsdir}/${PORTLOC}

	[ "$(realpath ${DISTFILES_CACHE})" != \
		"$(realpath -q ${portsdir}/distfiles)" ] || err 1 \
		"DISTFILES_CACHE cannot be in the portsdir as the portsdir will be mounted read-only"

	mkdir -p ${POUDRIERE_DATA}/packages/${MASTERNAME}/All
	[ -d "${CCACHE_DIR:-/nonexistent}" ] &&
		${NULLMOUNT} ${CCACHE_DIR} ${mnt}/ccache
	[ -n "${MFSSIZE}" ] && mdmfs -M -S -o async -s ${MFSSIZE} md ${mnt}/wrkdirs
	[ ${TMPFS_WRKDIR} -eq 1 ] && mount -t tmpfs tmpfs ${mnt}/wrkdirs
	# Only show mounting messages once, not for every builder
	if [ ${mnt##*/} = "ref" ]; then
		[ -d "${CCACHE_DIR:-/nonexistent}" ] &&
			msg "Mounting ccache from: ${CCACHE_DIR}"
		msg "Mounting packages from: ${POUDRIERE_DATA}/packages/${MASTERNAME}"
	fi

	${NULLMOUNT} -o ro ${portsdir} ${mnt}${PORTSRC} ||
		err 1 "Failed to mount the ports directory "
	mount_packages -o ro
	${NULLMOUNT} ${DISTFILES_CACHE} ${mnt}/distfiles ||
		err 1 "Failed to mount the distfiles cache directory"

	optionsdir="${MASTERNAME}"
	[ -n "${setname}" ] && optionsdir="${optionsdir} ${jname}-${setname}"
	optionsdir="${optionsdir} ${jname}-${ptname} ${setname} ${ptname} ${jname} -"

	for opt in ${optionsdir}; do
		use_options ${mnt} ${opt} && break || continue
	done

	return 0
}

createfs() {
	[ $# -ne 3 ] && eargs name mnt fs
	local name mnt fs
	name=$1
	mnt=$(echo $2 | sed -e "s,//,/,g")
	fs=$3

	if [ -n "${fs}" -a "${fs}" != "none" ]; then
		msg_n "Creating ${name} fs..."
		zfs create -p \
			-o mountpoint=${mnt} ${fs} || err 1 " fail"
		echo " done"
	else
		mkdir -p ${mnt}
	fi
}

rollbackfs() {
	[ $# -ne 2 ] && eargs name mnt
	local name=$1
	local mnt=$2
	local fs=$(zfs_getfs ${mnt})

	if [ -n "${fs}" ]; then
		zfs rollback -r ${fs}@${name}  || err 1 "Unable to rollback ${fs}"
		return
	fi

	mtree -X ${mnt}/poudriere/mtree.${name}exclude \
	-xr -f ${mnt}/poudriere/mtree.${name} -p ${mnt} | \
	while read l ; do
		case "$l" in
		*extra*Directory*) rm -rf ${mnt}/${l%% *} 2>/dev/null ;;
		*changed|*missing) echo ${MASTERMNT}/${l% *} ;;
		esac
	done | pax -rw -p p -s ",${MASTERMNT},,g" ${mnt}
}

umountfs() {
	[ $# -lt 1 ] && eargs mnt childonly
	local mnt=$1
	local childonly=$2
	local pattern

	[ -n "${childonly}" ] && pattern="/"

	[ -d "${mnt}" ] || return 0
	mnt=$(realpath ${mnt})
	mount | sort -r -k 2 | while read dev on pt opts; do
		case ${pt} in
		${mnt}${pattern}*)
			umount -f ${pt} || :
			[ "${dev#/dev/md*}" != "${dev}" ] && mdconfig -d -u ${dev#/dev/md*}
		;;
		esac
	done

	return 0
}

zfs_getfs() {
	[ $# -ne 1 ] && eargs mnt
	local mnt=$(realpath $1)
	mount -t zfs | awk -v n="${mnt}" ' $3 == n { print $1 }'
}

clonefs() {
	[ $# -lt 2 ] && eargs from to snap
	local from=$1
	local to=$2
	local snap=$3
	local name zfs_to
	local fs=$(zfs_getfs ${from})

	[ -d ${to} ] && destroyfs ${to} jail
	mkdir -p ${to}
	to=$(realpath ${to})
	[ ${TMPFS_ALL} -eq 1 ] && unset fs
	if [ -n "${fs}" ]; then
		name=${to##*/}

		if [ "${name}" = "ref" ]; then
			zfs_to=${fs%/*}/${MASTERNAME}-${name}
		else
			zfs_to=${fs}/${name}
		fi

		# Make sure the fs is clean before cloning
		zfs rollback -R ${fs}@${snap} 2>/dev/null || :
		zfs clone -o mountpoint=${to} \
			-o sync=disabled \
			-o atime=off \
			-o compression=off \
			${fs}@${snap} \
			${zfs_to}
	else
		[ ${TMPFS_ALL} -eq 1 ] && mount -t tmpfs tmpfs ${to}
		pax -X -rw -p p -s ",${from},,g" ${from} ${to}
	fi
}

destroyfs() {
	[ $# -ne 2 ] && eargs name type
	local mnt fs type
	mnt=$1
	type=$2
	[ -d ${mnt} ] || return 0
	mnt=$(realpath ${mnt})
	fs=$(zfs_getfs ${mnt})
	umountfs ${mnt} 1
	if [ ${TMPFS_ALL} -eq 1 ]; then
		umount -f ${mnt} 2>/dev/null || :
	elif [ -n "${fs}" -a "${fs}" != "none" ]; then
		zfs destroy -rf ${fs}
		rmdir ${mnt}
	else
		chflags -R noschg ${mnt}
		rm -rf ${mnt}
	fi
}

cleanup() {
	[ -n "${CLEANED_UP}" ] && return 0
	# Prevent recursive cleanup on error
	if [ -n "${CLEANING_UP}" ]; then
		echo "Failure cleaning up. Giving up." >&2
		return
	fi
	export CLEANING_UP=1
	msg "Cleaning up"

	# If this is a builder, don't cleanup, the master will handle that.
	if [ -n "${MY_JOBID}" ]; then
		[ -n "${PKGNAME}" ] && clean_pool ${PKGNAME} 1 || :
		return 0
	fi

	[ -n "${MASTERNAME}" ] && rm -rf \
		${POUDRIERE_DATA}/packages/${MASTERNAME}/.new_packages

	# Only bother with this if using jails as this may be being ran
	# from queue.sh or daemon.sh, etc.
	if [ -n "${MASTERMNT}" -a -n "${MASTERNAME}" ]; then
		if [ -d ${MASTERMNT}/poudriere/var/run ]; then
			for pid in ${MASTERMNT}/poudriere/var/run/*.pid; do
				# Ensure there is a pidfile to read or break
				[ "${pid}" = "${MASTERMNT}/poudriere/var/run/*.pid" ] && break
				pkill -15 -F ${pid} >/dev/null 2>&1 || :
			done
		fi
		wait

		jail_stop
	fi

	export CLEANED_UP=1
}

#Converting portstree if any
if [ ! -d ${POUDRIERED}/ports ]; then
	mkdir -p ${POUDRIERED}/ports
	[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
		-o ${NS}:type,${NS}:name,${NS}:method,mountpoint,name | \
		grep "^ports" | \
		while read t name method mnt fs; do
			msg "Converting the ${name} ports tree"
			pset ${name} method ${method}
			pset ${name} mnt ${mnt}
			pset ${name} fs ${fs}
			# Delete the old properties
			zfs inherit -r ${NS}:type ${fs}
			zfs inherit -r ${NS}:name ${fs}
			zfs inherit -r ${NS}:method ${fs}
		done
	if [ -f ${POUDRIERED}/portstrees ]; then
		while read name method mnt; do
			msg "Converting the ${name} ports tree"
			mkdir ${POUDRIERED}/ports/${name}
			echo ${method} > ${POUDRIERED}/ports/${name}/method
			echo ${mnt} > ${POUDRIERED}/ports/${name}/mnt
		done < ${POUDRIERED}/portstrees
		rm -f ${POUDRIERED}/portstrees
	fi
fi

#Converting jails if any
if [ ! -d ${POUDRIERED}/jails ]; then
	mkdir -p ${POUDRIERED}/jails
	[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
		-o ${NS}:type,${NS}:name,${NS}:version,${NS}:arch,${NS}:method,mountpoint,name | \
		grep "^rootfs" | \
		while read t name version arch method mnt fs; do
			msg "Converting the ${name} jail"
			jset ${name} version ${version}
			jset ${name} arch ${arch}
			jset ${name} method ${method}
			jset ${name} mnt ${mnt}
			jset ${name} fs ${fs}
			# Delete the old properties
			zfs inherit -r ${NS}:type ${fs}
			zfs inherit -r ${NS}:name ${fs}
			zfs inherit -r ${NS}:method ${fs}
			zfs inherit -r ${NS}:version ${fs}
			zfs inherit -r ${NS}:arch ${fs}
			zfs inherit -r ${NS}:stats_built ${fs}
			zfs inherit -r ${NS}:stats_failed ${fs}
			zfs inherit -r ${NS}:stats_skipped ${fs}
			zfs inherit -r ${NS}:stats_ignored ${fs}
			zfs inherit -r ${NS}:stats_queued ${fs}
			zfs inherit -r ${NS}:status ${fs}
		done
fi

#Converting portstree if any
if [ ! -d ${POUDRIERED}/ports ]; then
	mkdir -p ${POUDRIERED}/ports
	[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
		-o ${NS}:type,${NS}:name,${NS}:method,mountpoint,name | \
		grep "^ports" | \
		while read t name method mnt fs; do
			msg "Converting the ${name} ports tree"
			pset ${name} method ${method}
			pset ${name} mnt ${mnt}
			pset ${name} fs ${fs}
			# Delete the old properties
			zfs inherit -r ${NS}:type ${fs}
			zfs inherit -r ${NS}:name ${fs}
			zfs inherit -r ${NS}:method ${fs}
		done
	if [ -f ${POUDRIERED}/portstrees ]; then
		while read name method mnt; do
			[ -z "${name###*}" ] && continue # Skip comments
			msg "Converting the ${name} ports tree"
			mkdir ${POUDRIERED}/ports/${name}
			echo ${method} > ${POUDRIERED}/ports/${name}/method
			echo ${mnt} > ${POUDRIERED}/ports/${name}/mnt
		done < ${POUDRIERED}/portstrees
		rm -f ${POUDRIERED}/portstrees
	fi
fi

#Converting jails if any
if [ ! -d ${POUDRIERED}/jails ]; then
	mkdir -p ${POUDRIERED}/jails
	[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
		-o ${NS}:type,${NS}:name,${NS}:version,${NS}:arch,${NS}:method,mountpoint,name | \
		grep "^rootfs" | \
		while read t name version arch method mnt fs; do
			msg "Converting the ${name} jail"
			jset ${name} version ${version}
			jset ${name} arch ${arch}
			jset ${name} method ${method}
			jset ${name} mnt ${mnt}
			jset ${name} fs ${fs}
			# Delete the old properties
			zfs inherit -r ${NS}:type ${fs}
			zfs inherit -r ${NS}:name ${fs}
			zfs inherit -r ${NS}:method ${fs}
			zfs inherit -r ${NS}:version ${fs}
			zfs inherit -r ${NS}:arch ${fs}
			zfs inherit -r ${NS}:stats_built ${fs}
			zfs inherit -r ${NS}:stats_failed ${fs}
			zfs inherit -r ${NS}:stats_skipped ${fs}
			zfs inherit -r ${NS}:stats_ignored ${fs}
			zfs inherit -r ${NS}:stats_queued ${fs}
			zfs inherit -r ${NS}:status ${fs}
		done
fi

case $IPS in
01)
	localipargs="ip6.addr=::1"
	ipargs="ip6.addr=inherit"
	;;
10)
	localipargs="ip4.addr=127.0.0.1"
	ipargs="ip4=inherit"
	;;
11)
	localipargs="ip4.addr=127.0.0.1 ip6.addr=::1"
	ipargs="ip4=inherit ip6=inherit"
	;;
esac

for val in ${USE_TMPFS}; do
	case ${val} in
	wrkdir|yes) TMPFS_WRKDIR=1 ;;
	data) TMPFS_DATA=1 ;;
	all) TMPFS_ALL=1 ;;
	localbase) TMPFS_LOCALBASE=1 ;;
	*) err 1 "Unknown value for USE_TMPFS can be a combination of wrkdir,data,all,yes,localbase" ;;
	esac
done

case ${TMPFS_WRKDIR}${TMPFS_DATA}${TMPFS_LOCALBASE}${TMPFS_ALL} in
1**1|*1*1|**11)
	TMPFS_WRKDIR=0
	TMPFS_DATA=0
	TMPFS_LOCALBASE=0
	;;
esac

Changes to src/share/poudriere/jail.sh.

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
..
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465




466
467
468
469
470
471

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494



495
496
497
498

499
500
501
502
503
504
505
506
507
508
509
...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545






546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574




575
576
577
578
usage() {
	echo "poudriere jail [parameters] [options]

Parameters:
    -c            -- create a jail
    -d            -- delete a jail
    -l            -- list all available jails
    -s            -- start a jail
    -k            -- kill (stop) a jail
    -u            -- update a jail


Options:
    -q            -- quiet (Do not print the header)
    -J n          -- Run buildworld in parallel with n jobs.
    -j jailname   -- Specifies the jailname
    -v version    -- Specifies which version of FreeBSD we want in jail
    -a arch       -- Indicates architecture of the jail: i386 or amd64
                     (Default: same as host)
    -f fs         -- FS name (tank/jails/myjail) if fs is \"none\" then do not
                     create on zfs
    -M mountpoint -- mountpoint

    -m method     -- When used with -c, overrides the method to use by default.
                     Could also be \"http\", \"svn\", \"svn+http\",
                     \"svn+https\", \"svn+file\", \"svn+ssh\", \"csup\".
                     Please note that with svn and csup the world will be
                     built. Note that building from sources can use src.conf
		     and jail-src.conf from /usr/local/etc/poudriere.d/.
		     Other possible method are: \"allbsd\" retrieve a
		     snapshot from allbsd.org's website or \"ftp-archive\"
		     for old releases that're no longer available on \"ftp\".
    -p tree       -- Specify which ports tree the jail to start/stop with
    -P patch      -- Specify a patch file to apply to the source before committing.
    -t version    -- version to upgrade to
    -z set        -- Specify which SET the jail to start/stop with
"
	exit 1
}

list_jail() {
	[ ${QUIET} -eq 0 ] &&
		printf '%-20s %-20s %-7s %-7s\n' "JAILNAME" "VERSION" "ARCH" "METHOD"
	for j in $(find ${POUDRIERED}/jails -type d -maxdepth 1 -mindepth 1 -print); do
................................................................................
		version=$(jget ${name} version)
		arch=$(jget ${name} arch)
		method=$(jget ${name} method)
		printf '%-20s %-20s %-7s %-7s\n' "${name}" "${version}" "${arch}" "${method}"
	done
}

delete_jail() {
	test -z ${JAILNAME} && usage
	jail_exists ${JAILNAME} || err 1 "No such jail: ${JAILNAME}"
	msg_n "Removing ${JAILNAME} jail..."
	destroyfs ${JAILMNT} jail
	rm -rf ${POUDRIERED}/jails/${JAILNAME} || :
	echo " done"
}

cleanup_new_jail() {
	msg "Error while creating jail, cleaning up." >&2
	delete_jail
}

update_version() {
	local release="$1"
	local login_env osversion

	osversion=`awk '/\#define __FreeBSD_version/ { print $3 }' ${JAILMNT}/usr/include/sys/param.h`
	login_env=",UNAME_r=${release},UNAME_v=FreeBSD ${release},OSVERSION=${osversion}"

	[ "${ARCH}" = "i386" -a "${REALARCH}" = "amd64" ] &&
		login_env="${login_env},UNAME_p=i386,UNAME_m=i386"

	sed -i "" -e "s/:\(setenv.*\):/:\1${login_env}:/" ${JAILMNT}/etc/login.conf
	cap_mkdb ${JAILMNT}/etc/login.conf
}

update_jail() {
	local netargs
	jail_exists ${JAILNAME} || err 1 "No such jail: ${JAILNAME}"
	jail_runs ${JAILNAME} &&
		err 1 "Unable to remove jail ${JAILNAME}: it is running"

	METHOD=$(jget ${JAILNAME} method)
	if [ -z "${METHOD}" -o "${METHOD}" = "-" ]; then
		METHOD="ftp"
		jset ${JAILNAME} method ${METHOD}
	fi
	msg "Upgrading using ${METHOD}"
	case ${METHOD} in
	ftp|ftp-archive)
		MASTERMNT=$(jget ${JAILNAME} mnt)
		MASTERNAME=${JAILNAME}
		jstart 1
		if [ -z "${TORELEASE}" ]; then
			injail /usr/sbin/freebsd-update fetch install
		else
			yes | injail env PAGER=/bin/cat /usr/sbin/freebsd-update -r ${TORELEASE} upgrade install ||
				err 1 "Fail to upgrade system"
			yes | injail env PAGER=/bin/cat /usr/sbin/freebsd-update install ||
				err 1 "Fail to upgrade system"
			jset ${JAILNAME} version ${TORELEASE}
		fi
		jstop
		markfs clean ${JAILMNT}
		;;
	csup)
		install_from_csup
		update_version $(jget ${JAILNAME} version)
		yes | make -C ${JAILMNT}/usr/src delete-old delete-old-libs DESTDIR=${JAILMNT}
		markfs clean ${JAILMNT}
		;;
	svn*)
		install_from_svn
		update_version $(jget ${JAILNAME} version)
		yes | make -C ${JAILMNT}/usr/src delete-old delete-old-libs DESTDIR=${JAILMNT}
		markfs clean ${JAILMNT}
		;;
	allbsd|gjb)
		err 1 "Upgrade is not supported with allbsd, to upgrade, please delete and recreate the jail"
		;;
	*)
		err 1 "Unsupported method"
		;;
	esac

}

build_and_install_world() {
	export TARGET_ARCH=${ARCH}
	export SRC_BASE=${JAILMNT}/usr/src
	mkdir -p ${JAILMNT}/etc
	[ -f ${JAILMNT}/etc/src.conf ] && rm -f ${JAILMNT}/etc/src.conf
	touch ${JAILMNT}/etc/src.conf
	[ -f ${POUDRIERED}/src.conf ] && cat ${POUDRIERED}/src.conf > ${JAILMNT}/etc/src.conf
	[ -f ${POUDRIERED}/${JAILNAME}-src.conf ] && cat ${POUDRIERED}/${JAILNAME}-src.conf >> ${JAILMNT}/etc/src.conf
	unset MAKEOBJPREFIX
	export __MAKE_CONF=/dev/null
	export SRCCONF=${JAILMNT}/etc/src.conf
	MAKE_JOBS="-j${PARALLEL_JOBS}"

	: ${CCACHE_PATH:="/usr/local/libexec/ccache"}
	if [ -n "${CCACHE_DIR}" -a -d ${CCACHE_PATH}/world ]; then
		export CCACHE_DIR
		# Fix building world when CC is clang
		export CCACHE_CPP2=yes
		export CC="${CCACHE_PATH}/world/cc"
		export CXX="${CCACHE_PATH}/world/c++"
		unset CCACHE_TEMPDIR
	fi

	msg "Starting make buildworld with ${PARALLEL_JOBS} jobs"
	make -C ${JAILMNT}/usr/src buildworld ${MAKE_JOBS} ${MAKEWORLDARGS} || err 1 "Fail to build world"
	msg "Starting make installworld"
	make -C ${JAILMNT}/usr/src installworld DESTDIR=${JAILMNT} || err 1 "Fail to install world"
	make -C ${JAILMNT}/usr/src DESTDIR=${JAILMNT} distrib-dirs &&
	make -C ${JAILMNT}/usr/src DESTDIR=${JAILMNT} distribution
}

install_from_svn() {
	local UPDATE=0
	local proto
	[ -d ${JAILMNT}/usr/src ] && UPDATE=1
	mkdir -p ${JAILMNT}/usr/src
	case ${METHOD} in
	svn+http) proto="http" ;;
	svn+https) proto="https" ;;
	svn+ssh) proto="svn+ssh" ;;
	svn+file) proto="file" ;;
	svn) proto="svn" ;;
	esac
	if [ ${UPDATE} -eq 0 ]; then
		msg_n "Checking out the sources from svn..."
		svn -q co ${proto}://${SVN_HOST}/base/${VERSION} ${JAILMNT}/usr/src || err 1 " fail"
		echo " done"
		if [ -n "${SRCPATCHFILE}" ]; then
			msg_n "Patching the sources with ${SRCPATCHFILE}"
			svn -q patch ${SRCPATCHFILE} ${JAILMNT}/usr/src || err 1 " fail"
			echo done
		fi
	else
		msg_n "Updating the sources from svn..."
		svn -q update ${JAILMNT}/usr/src || err 1 " fail"
		echo " done"
	fi
	build_and_install_world
}

install_from_csup() {
	local UPDATE=0
	[ -d ${JAILMNT}/usr/src ] && UPDATE=1
	mkdir -p ${JAILMNT}/etc
	mkdir -p ${JAILMNT}/var/db
	mkdir -p ${JAILMNT}/usr
	[ -z ${CSUP_HOST} ] && err 2 "CSUP_HOST has to be defined in the configuration to use csup"
	if [ "${UPDATE}" -eq 0 ]; then
		echo "*default base=${JAILMNT}/var/db
*default prefix=${JAILMNT}/usr
*default release=cvs tag=${VERSION}
*default delete use-rel-suffix
src-all" > ${JAILMNT}/etc/supfile
	fi
	csup -z -h ${CSUP_HOST} ${JAILMNT}/etc/supfile || err 1 "Fail to fetch sources"
	build_and_install_world
}

install_from_ftp() {
	mkdir ${JAILMNT}/fromftp
	local URL V

	V=${ALLBSDVER:-${VERSION}}
	case $V in
	[0-4].*) HASH=MD5 ;;
	5.[0-4]*) HASH=MD5 ;;
	*) HASH=SHA256 ;;
	esac
	if [ ${V%%.*} -lt 9 ]; then
		msg "Fetching sets for FreeBSD ${V} ${ARCH}"
		case ${METHOD} in
		ftp|gjb)
			case ${VERSION} in
				*-PRERELEASE|*-STABLE) type=snapshots ;;
				*) type=releases ;;
			esac

			# Check that the defaults have been changed
			echo ${FREEBSD_HOST} | egrep -E "(_PROTO_|_CHANGE_THIS_)" > /dev/null
			if [ $? -eq 0 ]; then
				msg "FREEBSD_HOST from config invalid; defaulting to http://ftp.freebsd.org"
				FREEBSD_HOST="http://ftp.freebsd.org"
			fi
			URL="${FREEBSD_HOST}/pub/FreeBSD/${type}/${ARCH}/${V}" ;;
		allbsd) URL="https://pub.allbsd.org/FreeBSD-snapshots/${ARCH}-${ARCH}/${V}-JPSNAP/ftp" ;;
		ftp-archive) URL="ftp://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${ARCH}/${V}" ;;
		esac
		DISTS="base dict src games"
		[ ${ARCH} = "amd64" ] && DISTS="${DISTS} lib32"
		for dist in ${DISTS}; do
			fetch_file ${JAILMNT}/fromftp/ ${URL}/$dist/CHECKSUM.${HASH} ||
				err 1 "Fail to fetch checksum file"
			sed -n "s/.*(\(.*\...\)).*/\1/p" \
				${JAILMNT}/fromftp/CHECKSUM.${HASH} | \
				while read pkg; do
				[ ${pkg} = "install.sh" ] && continue
				# Let's retry at least one time
				fetch_file ${JAILMNT}/fromftp/ ${URL}/${dist}/${pkg}
			done
		done

		msg "Extracting sets:"
		for SETS in ${JAILMNT}/fromftp/*.aa; do
			SET=`basename $SETS .aa`
			echo -e "\t- $SET...\c"
			case ${SET} in
				s*)
					APPEND="usr/src"
					;;
				*)
					APPEND=""
					;;
			esac
			(cat ${JAILMNT}/fromftp/${SET}.* || echo Error) | \
				tar --unlink -xpf - -C ${JAILMNT}/${APPEND} || err 1 " fail"
			echo " done"
		done
	else
		local type
		case ${METHOD} in
			ftp|gjb|ftp-archive)
				case ${VERSION} in
					*-CURRENT|*-PRERELEASE|*-STABLE) type=snapshots ;;
					*) type=releases ;;
				esac

				# Check that the defaults have been changed
				echo ${FREEBSD_HOST} | egrep -E "(_PROTO_|_CHANGE_THIS_)" > /dev/null
				if [ $? -eq 0 ]; then
					msg "FREEBSD_HOST from config invalid; defaulting to http://ftp.freebsd.org"
					FREEBSD_HOST="http://ftp.freebsd.org"
				fi

				URL="${FREEBSD_HOST}/pub/FreeBSD/${type}/${ARCH}/${ARCH}/${V}"
				;;
			allbsd) URL="https://pub.allbsd.org/FreeBSD-snapshots/${ARCH}-${ARCH}/${V}-JPSNAP/ftp" ;;
		esac
		DISTS="base.txz src.txz games.txz"
		[ ${ARCH} = "amd64" ] && DISTS="${DISTS} lib32.txz"
		for dist in ${DISTS}; do
			msg "Fetching ${dist} for FreeBSD ${V} ${ARCH}"
			fetch_file ${JAILMNT}/fromftp/${dist} ${URL}/${dist}
			msg_n "Extracting ${dist}..."
			tar -xpf ${JAILMNT}/fromftp/${dist} -C  ${JAILMNT}/ || err 1 " fail"
			echo " done"
		done
	fi

	msg_n "Cleaning up..."
	rm -rf ${JAILMNT}/fromftp/
	echo " done"
}

create_jail() {
	jail_exists ${JAILNAME} && err 2 "The jail ${JAILNAME} already exists"

	test -z ${VERSION} && usage

	[ "${JAILNAME#*.*}" = "${JAILNAME}" ] ||
		err 1 "The jailname can not contain a period (.). See jail(8)"

	if [ -z ${JAILMNT} ]; then
		[ -z ${BASEFS} ] && err 1 "Please provide a BASEFS variable in your poudriere.conf"
		JAILMNT=${BASEFS}/jails/${JAILNAME}
	fi

	if [ -z ${JAILFS} ]; then
		[ -z ${ZPOOL} ] && err 1 "Please provide a ZPOOL variable in your poudriere.conf"
		JAILFS=${ZPOOL}${ZROOTFS}/jails/${JAILNAME}
	fi

	case ${METHOD} in
	ftp|gjb|ftp-archive)
		FCT=install_from_ftp
		;;
	allbsd)
		FCT=install_from_ftp
		ALLBSDVER=`fetch -qo - \
			https://pub.allbsd.org/FreeBSD-snapshots/${ARCH}-${ARCH}/ | \
			sed -n "s,.*href=\"\(.*${VERSION}.*\)-JPSNAP/\".*,\1,p" | \
			sort -k 3 -t - -r | head -n 1 `
		[ -z ${ALLBSDVER} ] && err 1 "Unknown version $VERSION"

		OIFS=${IFS}
		IFS=-
		set -- ${ALLBSDVER}
		IFS=${OIFS}
		RELEASE="${ALLBSDVER}-JPSNAP/ftp"
		;;
	svn*)
		SVN=`which svn`
		test -z ${SVN} && err 1 "You need svn on your host to use svn method"
		case ${VERSION} in
			stable/*![0-9]*)
				err 1 "bad version number for stable version"
				;;
			head@*![0-9]*)
				err 1 "bad revision number for head version"
				;;
			release/*![0-9]*.[0-9].[0-9])
				err 1 "bad version number for release version"
				;;
			releng/*![0-9]*.[0-9])
				err 1 "bad version number for releng version"
				;;
			stable/*|head*|release/*|releng/*.[0-9]) ;;
			*)
				err 1 "version with svn should be: head[@rev] or stable/N or release/N or releng/N"
				;;
		esac
		FCT=install_from_svn
		;;
	csup)
		case ${VERSION} in
			.)
				;;
			RELENG_*![0-9]*_[0-9])
				err 1 "bad version number for RELENG"
				;;
			RELENG_*![0-9]*)
				err 1 "bad version number for RELENG"
				;;
			RELENG_*|.) ;;
			*)
				err 1 "version with svn should be: head or stable/N or release/N or releng/N"
				;;
		esac
		FCT=install_from_csup
		;;
	*)
		err 2 "Unknown method to create the jail"
		;;
	esac

	createfs ${JAILNAME} ${JAILMNT} ${JAILFS:-none}
	[ -n "${JAILFS}" -a "${JAILFS}" != "none" ] && jset ${JAILNAME} fs ${JAILFS}
	jset ${JAILNAME} version ${VERSION}
	jset ${JAILNAME} arch ${ARCH}
	jset ${JAILNAME} mnt ${JAILMNT}

	# Wrap the jail creation in a special cleanup hook that will remove the jail
	# if any error is encountered
	CLEANUP_HOOK=cleanup_new_jail
	jset ${JAILNAME} method ${METHOD}
	${FCT}

	eval `grep "^[RB][A-Z]*=" ${JAILMNT}/usr/src/sys/conf/newvers.sh `
	RELEASE=${REVISION}-${BRANCH}
	jset ${JAILNAME} version ${RELEASE}
	update_version ${RELEASE}

	if [ "${ARCH}" = "i386" -a "${REALARCH}" = "amd64" ]; then
		cat > ${JAILMNT}/etc/make.conf << EOF
ARCH=i386
MACHINE=i386
MACHINE_ARCH=i386
EOF

	fi

	pwd_mkdb -d ${JAILMNT}/etc/ -p ${JAILMNT}/etc/master.passwd

	cat >> ${JAILMNT}/etc/make.conf << EOF
USE_PACKAGE_DEPENDS=yes
BATCH=yes
WRKDIRPREFIX=/wrkdirs
EOF

	mkdir -p ${JAILMNT}/usr/ports
	mkdir -p ${JAILMNT}/wrkdirs
	mkdir -p ${POUDRIERE_DATA}/logs

	jail -U root -c path=${JAILMNT} command=/sbin/ldconfig -m /lib /usr/lib /usr/lib/compat

	markfs clean ${JAILMNT}
	unset CLEANUP_HOOK
	msg "Jail ${JAILNAME} ${VERSION} ${ARCH} is ready to be used"
}

ARCH=`uname -m`
REALARCH=${ARCH}
START=0
STOP=0
LIST=0
DELETE=0
CREATE=0
QUIET=0
INFO=0
UPDATE=0




PTNAME=default
SETNAME=""

SCRIPTPATH=`realpath $0`
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
. ${SCRIPTPREFIX}/common.sh


while getopts "J:j:v:a:z:m:n:f:M:sdklqcip:ut:z:P:" FLAG; do
	case "${FLAG}" in
		j)
			JAILNAME=${OPTARG}
			;;
		J)
			PARALLEL_JOBS=${OPTARG}
			;;
		v)
			VERSION=${OPTARG}
			;;
		a)
			[ "${REALARCH}" != "amd64" -a "${REALARCH}" != ${OPTARG} ] &&
				err 1 "Only amd64 host can choose another architecture"
			ARCH=${OPTARG}
			;;
		m)
			METHOD=${OPTARG}
			;;
		f)
			JAILFS=${OPTARG}
			;;



		M)
			JAILMNT=${OPTARG}
			;;
		s)

			START=1
			;;
		k)
			STOP=1
			;;
		l)
			LIST=1
			;;
		c)
			CREATE=1
			;;
................................................................................
			;;
		u)
			UPDATE=1
			;;
		t)
			TORELEASE=${OPTARG}
			;;
		z)
			[ -n "${OPTARG}" ] || err 1 "Empty set name"
			SETNAME="${OPTARG}"
			;;
		*)
			usage
			;;
	esac
done

METHOD=${METHOD:-ftp}
if [ -n "${JAILNAME}" -a ${CREATE} -eq 0 ]; then
	ARCH=$(jget ${JAILNAME} arch)
	JAILFS=$(jget ${JAILNAME} fs)
	JAILMNT=$(jget ${JAILNAME} mnt)
fi







case "${CREATE}${LIST}${STOP}${START}${DELETE}${UPDATE}" in
	100000)
		test -z ${JAILNAME} && usage
		create_jail
		;;
	010000)
		list_jail
		;;
	001000)
		test -z ${JAILNAME} && usage
		export MASTERNAME=${JAILNAME}-${PTNAME}${SETNAME:+-${SETNAME}}
		export MASTERMNT=${POUDRIERE_DATA}/build/${MASTERNAME}/ref
		jail_stop
		;;
	000100)
		export SET_STATUS_ON_START=0
		test -z ${JAILNAME} && usage
		export MASTERNAME=${JAILNAME}-${PTNAME}${SETNAME:+-${SETNAME}}
		export MASTERMNT=${POUDRIERE_DATA}/build/${MASTERNAME}/ref
		jail_start ${JAILNAME} ${PTNAME} ${SETNAME}
		;;
	000010)
		test -z ${JAILNAME} && usage
		delete_jail
		;;
	000001)
		test -z ${JAILNAME} && usage
		update_jail
		;;




	*)
		usage
		;;
esac







<
<

>





|
|
|
|
<

>
|
|
|
<
<
<
<
<
<


|
<
<







 







<
<
<
<
<
<
<
<
<





<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<










>
>
>
>






>

|





|





|
<
<







>
>
>



<
>
|
<
<
<







 







<
<
<
<






<






>
>
>
>
>
>
|
|



|


<
<
<
<
<
<
|
<
<
<
<
<
<
<



|



>
>
>
>




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
..
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
...
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
usage() {
	echo "poudriere jail [parameters] [options]

Parameters:
    -c            -- create a jail
    -d            -- delete a jail
    -l            -- list all available jails


    -u            -- update a jail
    -C            -- Cleanup jail mounts (contingency cleanup)

Options:
    -q            -- quiet (Do not print the header)
    -J n          -- Run buildworld in parallel with n jobs.
    -j jailname   -- Specifies the jailname
    -v version    -- Specifies which version of DragonFly we want in jail
                     e.g. \"3.4\", \"3.6\", or \"master\"
    -a arch       -- Does nothing - set to be same as host
    -f fs         -- FS name (\$BASEFS/worlds/myjail)

    -M mountpoint -- mountpoint
    -Q quickworld -- when used with -u jail is incrementally updated
    -m method     -- when used with -c forces the method to use by default
                     \"git\" to build world from source.  There are no other
                     method options at this time.






    -p tree       -- Specify which ports tree the jail to start/stop with
    -P patch      -- Specify a patch file to apply to the source before committing.
    -t version    -- version to upgrade to"


	exit 1
}

list_jail() {
	[ ${QUIET} -eq 0 ] &&
		printf '%-20s %-20s %-7s %-7s\n' "JAILNAME" "VERSION" "ARCH" "METHOD"
	for j in $(find ${POUDRIERED}/jails -type d -maxdepth 1 -mindepth 1 -print); do
................................................................................
		version=$(jget ${name} version)
		arch=$(jget ${name} arch)
		method=$(jget ${name} method)
		printf '%-20s %-20s %-7s %-7s\n' "${name}" "${version}" "${arch}" "${method}"
	done
}










cleanup_new_jail() {
	msg "Error while creating jail, cleaning up." >&2
	delete_jail
}













































































































































































































































































































































































ARCH=`uname -m`
REALARCH=${ARCH}
START=0
STOP=0
LIST=0
DELETE=0
CREATE=0
QUIET=0
INFO=0
UPDATE=0
QUICK=0
DISMOUNT=0
METHOD=git
JOB_OVERRIDE="0"
PTNAME=default
SETNAME=""

SCRIPTPATH=`realpath $0`
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
. ${SCRIPTPREFIX}/common.sh
. ${SCRIPTPREFIX}/jail.sh.${BSDPLATFORM}

while getopts "J:j:v:a:z:m:n:f:M:Cdlqcip:iut:z:P:Q" FLAG; do
	case "${FLAG}" in
		j)
			JAILNAME=${OPTARG}
			;;
		J)
			JOB_OVERRIDE=${OPTARG}
			;;
		v)
			VERSION=${OPTARG}
			;;
		a)
			# Force it to stay on host's arch


			;;
		m)
			METHOD=${OPTARG}
			;;
		f)
			JAILFS=${OPTARG}
			;;
		C)
			DISMOUNT=1
			;;
		M)
			JAILMNT=${OPTARG}
			;;

		Q)
			QUICK=1



			;;
		l)
			LIST=1
			;;
		c)
			CREATE=1
			;;
................................................................................
			;;
		u)
			UPDATE=1
			;;
		t)
			TORELEASE=${OPTARG}
			;;




		*)
			usage
			;;
	esac
done


if [ -n "${JAILNAME}" -a ${CREATE} -eq 0 ]; then
	ARCH=$(jget ${JAILNAME} arch)
	JAILFS=$(jget ${JAILNAME} fs)
	JAILMNT=$(jget ${JAILNAME} mnt)
fi

if [ "${JOB_OVERRIDE}" = "0" ]; then
	PARALLEL_JOBS=$(sysctl -n hw.ncpu)
else
	PARALLEL_JOBS=${JOB_OVERRIDE}
fi

case "${CREATE}${LIST}${DELETE}${UPDATE}${INFO}${DISMOUNT}" in
	10000)
		test -z ${JAILNAME} && usage
		create_jail
		;;
	01000)
		list_jail
		;;






	00100)







		test -z ${JAILNAME} && usage
		delete_jail
		;;
	00010)
		test -z ${JAILNAME} && usage
		update_jail
		;;
	00001)
		test -z ${JAILNAME} && usage
		jail_dismount
		;;
	*)
		usage
		;;
esac

Added src/share/poudriere/jail.sh.dragonfly.





























































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/sh
#
# DragonFly specific

delete_jail() {
	test -z ${JAILNAME} && usage
	jail_exists ${JAILNAME} || err 1 "No such jail: ${JAILNAME}"
	jail_runs ${JAILNAME} &&
		err 1 "Unable to remove jail ${JAILNAME}: it is running"

	msg_n "Removing ${JAILNAME} jail..."
	destroyfs ${JAILMNT} jail
	rm -rf ${POUDRIERED}/jails/${JAILNAME} || :
	echo "done"
}

update_version() {
	local release="$1"
	local login_env osversion

	osversion=`awk '/\#define __DragonFly_version/ { print $3 }' ${JAILMNT}/usr/include/sys/param.h`
	login_env=",UNAME_r=${release},UNAME_v=DragonFly ${release},OSVERSION=${osversion}"

	sed -i "" -e "s/:\(setenv.*\):/:\1${login_env}:/" ${JAILMNT}/etc/login.conf
	cap_mkdb ${JAILMNT}/etc/login.conf
}

establish_jail() {
# REMOVE THIS.
	[ $# -ne 5 ] && eargs name version arch mountpoint filesys
	local name=$1
	local version=$2
	local arch=$3
	local mnt=$( echo $4 | sed -e "s,//,/,g")
	local filesys=$5
	local confdir="${POUDRIERE_DATA}/meta/jail/${name}"
	local memdir="${BASEFS}/memory/${name}"
	local numcpu=$(sysctl -n hw.ncpu)
	local JOBX="$(jot -w %02d ${numcpu})"
	msg_n "Creating ${name} fs..."

	mkdir -p ${mnt}
	mkdir -p ${confdir}
	mkdir -p ${memdir}
	mkdir -p ${filesys}

	echo "rootfs"     > ${confdir}/type
	echo "${name}"    > ${confdir}/name
	echo "${version}" > ${confdir}/version
	echo "${arch}"    > ${confdir}/arch
	echo "${mnt}"     > ${confdir}/mountpoint
	echo "${filesys}" > ${confdir}/filesys
	echo "${numcpu}"  > ${confdir}/slaves
	echo " done"
}

update_jail() {
	# DragonFly expects 1 argument (quick), FreeBSD doesn't
	jail_exists ${JAILNAME} || err 1 "No such jail: ${JAILNAME}"
	jail_runs ${JAILNAME} &&
		err 1 "Unable to remove jail ${JAILNAME}: it is running"

	METHOD=$(jget ${JAILNAME} method)
	if [ -z "${METHOD}" -o "${METHOD}" = "-" ]; then
		METHOD="git"
		jset ${JAILNAME} method ${METHOD}
	fi
	msg "Upgrading using ${METHOD}"
	case ${METHOD} in
	git)
		install_from_git ${1}
		;;
	*)
		err 1 "Unsupported method"
		;;
	esac

}

build_and_install_world() {
	[ $# -ne 1 ] && eargs quick
	local JLOGBASE=${POUDRIERE_DATA}/jailbuild/${JAILNAME}
	local factor=${PARALLEL_JOBS}
	local options="-DNO_GAMES -DNOPROFILE -DNO_BINUTILS221"
	local directive
	mkdir -p ${JLOGBASE}

	export TARGET_ARCH=${ARCH}
	export __MAKE_CONF=/dev/null
	export DESTDIR=${JAILMNT}

	if [ ${1} -eq 1 ]; then
	   directive=quickworld
	else
	   directive=buildworld
	fi
	msg "${JAILNAME}: making ${directive} with ${factor} jobs"
	make -C ${JAILMNT}/usr/src -j${factor} ${options} ${directive} > \
	  ${JLOGBASE}/build.log 2>&1 || err 1 "Fail to ${directive}"

	msg "${JAILNAME}: making installworld"
	make -C ${JAILMNT}/usr/src ${options} installworld > \
	  ${JLOGBASE}/install.log 2>&1 || err 1 "Fail to install world"

	msg "${JAILNAME}: making distribution"
	make -C ${JAILMNT}/usr/src/etc distribution > \
	  ${JLOGBASE}/distrib.log 2>&1 || err 1 "Fail to make distribution"

	# make upgrade required to link device drives (e.g. </dev/video/...)
	msg "${JAILNAME}: making world upgrade"
	make -C ${JAILMNT}/usr/src upgrade > \
	  ${JLOGBASE}/upgrade.log 2>&1 || err 1 "Fail to make world upgrade"

	# make sure world can be dismounted
	cd /
}

convert_property_version () {
	local GREPPAT='^[0-9]\.[0-9]+$'
	local FILTER=`echo "${1}" | grep -E "${GREPPAT}"`
	if [ -n "${FILTER}" ]; then
	   echo RELEASE-${1}
	else
	   echo master
	fi
}

convert_version () {
	# $1 can be empty; "master" will be returned
	local GREPPAT='^[0-9]\.[0-9]+$'
	local FILTER=`echo "${1}" | grep -E "${GREPPAT}"`
	if [ -n "${FILTER}" ]; then
	   echo DragonFly_RELEASE_`echo ${FILTER} | sed 's|\.|_|'`
	else
	   echo master
	fi
}

install_from_git() {
	[ $# -ne 1 ] && eargs quick
	local UPDATE=0
	local proto
	local bver
	local bverz
	local bhack
	local mounted=$(check_mount "${JAILFS}")

	[ -n "${mounted}" ] && err 1 "Already mounted: ${JAILFS}"
	${NULLMOUNT} ${JAILFS} "${JAILMNT}" || err 1 "Failed to mount JAILFS"
	[ -d ${JAILMNT}/usr/src ] && UPDATE=1
	mkdir -p ${JAILMNT}/usr/src
	cd ${JAILMNT}/usr/src
	if [ ${UPDATE} -eq 0 ]; then
		msg "Checking out the sources via git..."
		bver=$(convert_version ${VERSION})
		git init
		git remote add origin ${DFLY_GIT_URL}
		git fetch --depth=1 origin
		git branch ${bver} origin/${bver}
		git checkout ${bver}
		git pull || err 1 "Fail "
		echo " done"
	else
		msg "Updating the sources via git..."
		if [ -n "${TORELEASE}" ]; then
			bver=$(convert_version "${TORELEASE}")
			bverz=$(convert_property_version "${TORELEASE}")
			BAE=`git branch -l | grep ${bver}`
			if [ -z "${BAE}" ]; then
				git branch ${bver} origin/${bver}
			fi
			git checkout ${bver}
			jset ${JAILNAME} version ${bverz}
		fi
		git pull || err 1 "Fail "
		echo " done"
	fi
	mount -t devfs devfs ${JAILMNT}/dev
	build_and_install_world ${1}
	umount ${JAILMNT}/dev
	umount ${JAILMNT}
	bhack="$(date '+%Y-%m-%d %H:%M:%S')"
	jset ${JAILNAME} timestamp "${bhack}"
}

create_jail() {
	jail_exists ${JAILNAME} && err 2 "The jail ${JAILNAME} already exists"

	[ -z ${VERSION} ] && VERSION=master
	[ -z ${JAILMNT} ] && JAILMNT=${BASEFS}/jails/${JAILNAME}
	[ -z ${JAILFS} ] && JAILFS=${BASEFS}/worlds/${JAILNAME}

	case ${METHOD} in
	git)
		GIT=`which git`
		test -z ${GIT} && \
		  err 1 "You need git on your host to use this method"
		FCT="install_from_git 0"
		;;
	*)
		err 2 "Unknown method to create the jail"
		;;
	esac

	local bverz=$(convert_property_version "${VERSION}")
	createfs ${JAILNAME} ${JAILMNT} ${JAILFS:-none}
	[ -n "${JAILFS}" -a "${JAILFS}" != "none" ] && jset ${JAILNAME} fs ${JAILFS}
	jset ${JAILNAME} version ${bverz}
	jset ${JAILNAME} arch ${ARCH}
	jset ${JAILNAME} mnt ${JAILMNT}

	# Wrap the jail creation in a special cleanup hook that will remove the jail
	# if any error is encountered
	CLEANUP_HOOK=cleanup_new_jail
	jset ${JAILNAME} method ${METHOD}
	${FCT}

	eval `grep -E "^(RELEASE|BRANCH|REVISION)=" ${JAILMNT}/usr/src/sys/conf/newvers.sh `
	jset ${JAILNAME} version ${bverz}
	update_version ${RELEASE}

	pwd_mkdb -d ${JAILMNT}/etc/ -p ${JAILMNT}/etc/master.passwd

	cat > ${JAILMNT}/etc/make.conf << EOF
USE_PACKAGE_DEPENDS=yes
BATCH=yes
WRKDIRPREFIX=/wrkdirs
NO_BACKUP=yes
EOF
	# At least one daemon has to run otherwise jail will exits immediately
	# Let cron run over syslogd (remove "cron_enable=NO")
	cat > ${JAILMNT}/etc/rc.conf << EOF
sendmail_enable=NONE
syslogd_enable=NO
update_motd=NO
EOF
	echo "# dummy" 	            > ${JAILMNT}/etc/fstab
	echo "src"                  > ${JAILMNT}/usr/.cpignore
	echo "local"               >> ${JAILMNT}/usr/.cpignore
	echo "sys"                 >> ${JAILMNT}/.cpignore

	mkdir -p ${JAILMNT}/${PORTSRC}
	mkdir -p ${JAILMNT}/${STD_PACKAGES}
	mkdir -p ${JAILMNT}/${STD_DISTFILES}
	mkdir -p ${JAILMNT}/wrkdirs
	mkdir -p ${JAILMNT}/var/db/pkg
	mkdir -p ${POUDRIERE_DATA}/logs

	markfs clean ${JAILMNT}
	unset CLEANUP_HOOK
	msg "Jail ${JAILNAME} ${VERSION} ${ARCH} is ready to be used"
	umount ${JAILMNT}/dev
	umount ${JAILMNT}
}

Added src/share/poudriere/jail.sh.freebsd.



















































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/bin/sh
#
# FreeBSD specific

delete_jail() {
	test -z ${JAILNAME} && usage
	jail_exists ${JAILNAME} || err 1 "No such jail: ${JAILNAME}"
	msg_n "Removing ${JAILNAME} jail..."
	destroyfs ${JAILMNT} jail
	rm -rf ${POUDRIERED}/jails/${JAILNAME} || :
	echo " done"
}

update_version() {
	local release="$1"
	local login_env osversion

	osversion=`awk '/\#define __FreeBSD_version/ { print $3 }' ${JAILMNT}/usr/include/sys/param.h`
	login_env=",UNAME_r=${release},UNAME_v=FreeBSD ${release},OSVERSION=${osversion}"

	[ "${ARCH}" = "i386" -a "${REALARCH}" = "amd64" ] &&
		login_env="${login_env},UNAME_p=i386,UNAME_m=i386"

	sed -i "" -e "s/:\(setenv.*\):/:\1${login_env}:/" ${JAILMNT}/etc/login.conf
	cap_mkdb ${JAILMNT}/etc/login.conf
}

update_jail() {
	local netargs
	jail_exists ${JAILNAME} || err 1 "No such jail: ${JAILNAME}"
	jail_runs ${JAILNAME} &&
		err 1 "Unable to remove jail ${JAILNAME}: it is running"

	METHOD=$(jget ${JAILNAME} method)
	if [ -z "${METHOD}" -o "${METHOD}" = "-" ]; then
		METHOD="ftp"
		jset ${JAILNAME} method ${METHOD}
	fi
	msg "Upgrading using ${METHOD}"
	case ${METHOD} in
	ftp|ftp-archive)
		MASTERMNT=$(jget ${JAILNAME} mnt)
		MASTERNAME=${JAILNAME}
		jstart 1
		if [ -z "${TORELEASE}" ]; then
			injail /usr/sbin/freebsd-update fetch install
		else
			yes | injail env PAGER=/bin/cat /usr/sbin/freebsd-update -r ${TORELEASE} upgrade install ||
				err 1 "Fail to upgrade system"
			yes | injail env PAGER=/bin/cat /usr/sbin/freebsd-update install ||
				err 1 "Fail to upgrade system"
			jset ${JAILNAME} version ${TORELEASE}
		fi
		jstop
		markfs clean ${JAILMNT}
		;;
	csup)
		install_from_csup
		update_version $(jget ${JAILNAME} version)
		yes | make -C ${JAILMNT}/usr/src delete-old delete-old-libs DESTDIR=${JAILMNT}
		markfs clean ${JAILMNT}
		;;
	svn*)
		install_from_svn
		update_version $(jget ${JAILNAME} version)
		yes | make -C ${JAILMNT}/usr/src delete-old delete-old-libs DESTDIR=${JAILMNT}
		markfs clean ${JAILMNT}
		;;
	allbsd|gjb)
		err 1 "Upgrade is not supported with allbsd, to upgrade, please delete and recreate the jail"
		;;
	*)
		err 1 "Unsupported method"
		;;
	esac

}

build_and_install_world() {
	export TARGET_ARCH=${ARCH}
	export SRC_BASE=${JAILMNT}/usr/src
	mkdir -p ${JAILMNT}/etc
	[ -f ${JAILMNT}/etc/src.conf ] && rm -f ${JAILMNT}/etc/src.conf
	touch ${JAILMNT}/etc/src.conf
	[ -f ${POUDRIERED}/src.conf ] && cat ${POUDRIERED}/src.conf > ${JAILMNT}/etc/src.conf
	[ -f ${POUDRIERED}/${JAILNAME}-src.conf ] && cat ${POUDRIERED}/${JAILNAME}-src.conf >> ${JAILMNT}/etc/src.conf
	unset MAKEOBJPREFIX
	export __MAKE_CONF=/dev/null
	export SRCCONF=${JAILMNT}/etc/src.conf
	MAKE_JOBS="-j${PARALLEL_JOBS}"

	: ${CCACHE_PATH:="/usr/local/libexec/ccache"}
	if [ -n "${CCACHE_DIR}" -a -d ${CCACHE_PATH}/world ]; then
		export CCACHE_DIR
		# Fix building world when CC is clang
		export CCACHE_CPP2=yes
		export CC="${CCACHE_PATH}/world/cc"
		export CXX="${CCACHE_PATH}/world/c++"
		unset CCACHE_TEMPDIR
	fi

	msg "Starting make buildworld with ${PARALLEL_JOBS} jobs"
	make -C ${JAILMNT}/usr/src buildworld ${MAKE_JOBS} ${MAKEWORLDARGS} || err 1 "Fail to build world"
	msg "Starting make installworld"
	make -C ${JAILMNT}/usr/src installworld DESTDIR=${JAILMNT} || err 1 "Fail to install world"
	make -C ${JAILMNT}/usr/src DESTDIR=${JAILMNT} distrib-dirs &&
	make -C ${JAILMNT}/usr/src DESTDIR=${JAILMNT} distribution
}

install_from_svn() {
	local UPDATE=0
	local proto
	[ -d ${JAILMNT}/usr/src ] && UPDATE=1
	mkdir -p ${JAILMNT}/usr/src
	case ${METHOD} in
	svn+http) proto="http" ;;
	svn+https) proto="https" ;;
	svn+ssh) proto="svn+ssh" ;;
	svn+file) proto="file" ;;
	svn) proto="svn" ;;
	esac
	if [ ${UPDATE} -eq 0 ]; then
		msg_n "Checking out the sources from svn..."
		svn -q co ${proto}://${SVN_HOST}/base/${VERSION} ${JAILMNT}/usr/src || err 1 " fail"
		echo " done"
		if [ -n "${SRCPATCHFILE}" ]; then
			msg_n "Patching the sources with ${SRCPATCHFILE}"
			svn -q patch ${SRCPATCHFILE} ${JAILMNT}/usr/src || err 1 " fail"
			echo done
		fi
	else
		msg_n "Updating the sources from svn..."
		svn -q update ${JAILMNT}/usr/src || err 1 " fail"
		echo " done"
	fi
	build_and_install_world
}

install_from_csup() {
	local UPDATE=0
	[ -d ${JAILMNT}/usr/src ] && UPDATE=1
	mkdir -p ${JAILMNT}/etc
	mkdir -p ${JAILMNT}/var/db
	mkdir -p ${JAILMNT}/usr
	[ -z ${CSUP_HOST} ] && err 2 "CSUP_HOST has to be defined in the configuration to use csup"
	if [ "${UPDATE}" -eq 0 ]; then
		echo "*default base=${JAILMNT}/var/db
*default prefix=${JAILMNT}/usr
*default release=cvs tag=${VERSION}
*default delete use-rel-suffix
src-all" > ${JAILMNT}/etc/supfile
	fi
	csup -z -h ${CSUP_HOST} ${JAILMNT}/etc/supfile || err 1 "Fail to fetch sources"
	build_and_install_world
}

install_from_ftp() {
	mkdir ${JAILMNT}/fromftp
	local URL V

	V=${ALLBSDVER:-${VERSION}}
	case $V in
	[0-4].*) HASH=MD5 ;;
	5.[0-4]*) HASH=MD5 ;;
	*) HASH=SHA256 ;;
	esac
	if [ ${V%%.*} -lt 9 ]; then
		msg "Fetching sets for FreeBSD ${V} ${ARCH}"
		case ${METHOD} in
		ftp|gjb)
			case ${VERSION} in
				*-PRERELEASE|*-STABLE) type=snapshots ;;
				*) type=releases ;;
			esac

			# Check that the defaults have been changed
			echo ${FREEBSD_HOST} | egrep -E "(_PROTO_|_CHANGE_THIS_)" > /dev/null
			if [ $? -eq 0 ]; then
				msg "FREEBSD_HOST from config invalid; defaulting to http://ftp.freebsd.org"
				FREEBSD_HOST="http://ftp.freebsd.org"
			fi
			URL="${FREEBSD_HOST}/pub/FreeBSD/${type}/${ARCH}/${V}" ;;
		allbsd) URL="https://pub.allbsd.org/FreeBSD-snapshots/${ARCH}-${ARCH}/${V}-JPSNAP/ftp" ;;
		ftp-archive) URL="ftp://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${ARCH}/${V}" ;;
		esac
		DISTS="base dict src games"
		[ ${ARCH} = "amd64" ] && DISTS="${DISTS} lib32"
		for dist in ${DISTS}; do
			fetch_file ${JAILMNT}/fromftp/ ${URL}/$dist/CHECKSUM.${HASH} ||
				err 1 "Fail to fetch checksum file"
			sed -n "s/.*(\(.*\...\)).*/\1/p" \
				${JAILMNT}/fromftp/CHECKSUM.${HASH} | \
				while read pkg; do
				[ ${pkg} = "install.sh" ] && continue
				# Let's retry at least one time
				fetch_file ${JAILMNT}/fromftp/ ${URL}/${dist}/${pkg}
			done
		done

		msg "Extracting sets:"
		for SETS in ${JAILMNT}/fromftp/*.aa; do
			SET=`basename $SETS .aa`
			echo -e "\t- $SET...\c"
			case ${SET} in
				s*)
					APPEND="usr/src"
					;;
				*)
					APPEND=""
					;;
			esac
			(cat ${JAILMNT}/fromftp/${SET}.* || echo Error) | \
				tar --unlink -xpf - -C ${JAILMNT}/${APPEND} || err 1 " fail"
			echo " done"
		done
	else
		local type
		case ${METHOD} in
			ftp|gjb|ftp-archive)
				case ${VERSION} in
					*-CURRENT|*-PRERELEASE|*-STABLE) type=snapshots ;;
					*) type=releases ;;
				esac

				# Check that the defaults have been changed
				echo ${FREEBSD_HOST} | egrep -E "(_PROTO_|_CHANGE_THIS_)" > /dev/null
				if [ $? -eq 0 ]; then
					msg "FREEBSD_HOST from config invalid; defaulting to http://ftp.freebsd.org"
					FREEBSD_HOST="http://ftp.freebsd.org"
				fi

				URL="${FREEBSD_HOST}/pub/FreeBSD/${type}/${ARCH}/${ARCH}/${V}"
				;;
		allbsd) URL="https://pub.allbsd.org/FreeBSD-snapshots/${ARCH}-${ARCH}/${V}-JPSNAP/ftp" ;;
		esac
		DISTS="base.txz src.txz games.txz"
		[ ${ARCH} = "amd64" ] && DISTS="${DISTS} lib32.txz"
		for dist in ${DISTS}; do
			msg "Fetching ${dist} for FreeBSD ${V} ${ARCH}"
			fetch_file ${JAILMNT}/fromftp/${dist} ${URL}/${dist}
			msg_n "Extracting ${dist}..."
			tar -xpf ${JAILMNT}/fromftp/${dist} -C  ${JAILMNT}/ || err 1 " fail"
			echo " done"
		done
	fi

	msg_n "Cleaning up..."
	rm -rf ${JAILMNT}/fromftp/
	echo " done"
}

create_jail() {
	jail_exists ${JAILNAME} && err 2 "The jail ${JAILNAME} already exists"

	test -z ${VERSION} && usage

	[ "${JAILNAME#*.*}" = "${JAILNAME}" ] ||
		err 1 "The jailname can not contain a period (.). See jail(8)"

	if [ -z ${JAILMNT} ]; then
		[ -z ${BASEFS} ] && err 1 "Please provide a BASEFS variable in your poudriere.conf"
		JAILMNT=${BASEFS}/jails/${JAILNAME}
	fi

	if [ -z ${JAILFS} ] ; then
		[ -z ${ZPOOL} ] && err 1 "Please provide a ZPOOL variable in your poudriere.conf"
		JAILFS=${ZPOOL}${ZROOTFS}/jails/${JAILNAME}
	fi

	case ${METHOD} in
	ftp|gjb|ftp-archive)
		FCT=install_from_ftp
		;;
	allbsd)
		FCT=install_from_ftp
		ALLBSDVER=`fetch -qo - \
			https://pub.allbsd.org/FreeBSD-snapshots/${ARCH}-${ARCH}/ | \
			sed -n "s,.*href=\"\(.*${VERSION}.*\)-JPSNAP/\".*,\1,p" | \
			sort -k 3 -t - -r | head -n 1 `
		[ -z ${ALLBSDVER} ] && err 1 "Unknown version $VERSION"

		OIFS=${IFS}
		IFS=-
		set -- ${ALLBSDVER}
		IFS=${OIFS}
		RELEASE="${ALLBSDVER}-JPSNAP/ftp"
		;;
	svn*)
		SVN=`which svn`
		test -z ${SVN} && err 1 "You need svn on your host to use svn method"
		case ${VERSION} in
			stable/*![0-9]*)
				err 1 "bad version number for stable version"
				;;
			head@*![0-9]*)
				err 1 "bad revision number for head version"
				;;
			release/*![0-9]*.[0-9].[0-9])
				err 1 "bad version number for release version"
				;;
			releng/*![0-9]*.[0-9])
				err 1 "bad version number for releng version"
				;;
			stable/*|head*|release/*|releng/*.[0-9]) ;;
			*)
				err 1 "version with svn should be: head[@rev] or stable/N or release/N or releng/N"
				;;
		esac
		FCT=install_from_svn
		;;
	csup)
		case ${VERSION} in
			.)
				;;
			RELENG_*![0-9]*_[0-9])
				err 1 "bad version number for RELENG"
				;;
			RELENG_*![0-9]*)
				err 1 "bad version number for RELENG"
				;;
			RELENG_*|.) ;;
			*)
				err 1 "version with svn should be: head or stable/N or release/N or releng/N"
				;;
		esac
		FCT=install_from_csup
		;;
	*)
		err 2 "Unknown method to create the jail"
		;;
	esac

	createfs ${JAILNAME} ${JAILMNT} ${JAILFS:-none}
	[ -n "${JAILFS}" -a "${JAILFS}" != "none" ] && jset ${JAILNAME} fs ${JAILFS}
	jset ${JAILNAME} version ${VERSION}
	jset ${JAILNAME} arch ${ARCH}
	jset ${JAILNAME} mnt ${JAILMNT}

	# Wrap the jail creation in a special cleanup hook that will remove the jail
	# if any error is encountered
	CLEANUP_HOOK=cleanup_new_jail
	jset ${JAILNAME} method ${METHOD}
	${FCT}

	eval `grep "^[RB][A-Z]*=" ${JAILMNT}/usr/src/sys/conf/newvers.sh `
	RELEASE=${REVISION}-${BRANCH}
	jset ${JAILNAME} version ${RELEASE}
	update_version ${RELEASE}

	if [ "${ARCH}" = "i386" -a "${REALARCH}" = "amd64" ];then
		cat > ${JAILMNT}/etc/make.conf << EOF
ARCH=i386
MACHINE=i386
MACHINE_ARCH=i386
EOF

	fi

	pwd_mkdb -d ${JAILMNT}/etc/ -p ${JAILMNT}/etc/master.passwd

	cat >> ${JAILMNT}/etc/make.conf << EOF
USE_PACKAGE_DEPENDS=yes
BATCH=yes
WRKDIRPREFIX=/wrkdirs
EOF

	mkdir -p ${JAILMNT}/${PORTSRC}
	mkdir -p ${JAILMNT}/wrkdirs
	mkdir -p ${POUDRIERE_DATA}/logs

	jail -U root -c path=${JAILMNT} command=/sbin/ldconfig -m /lib /usr/lib /usr/lib/compat

	markfs clean ${JAILMNT}
	unset CLEANUP_HOOK
	msg "Jail ${JAILNAME} ${VERSION} ${ARCH} is ready to be used"
}

Changes to src/share/poudriere/testport.sh.

38
39
40
41
42
43
44
45

46
47
48
49
50
51
52
...
106
107
108
109
110
111
112


113
114
115
116
117
118
119
120
121
122



123
124
125
126
127
128
129
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
...
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
    -J n        -- Run n jobs in parallel for dependencies
    -j name     -- Run inside the given jail
    -i          -- Interactive mode. Enter jail for interactive testing and automatically cleanup when done.
    -I          -- Advanced Interactive mode. Leaves jail running with port installed after test.
    -n          -- No custom prefix
    -p tree     -- Specify the path to the portstree
    -s          -- Skip sanity checks
    -v          -- Be verbose; show more information. Use twice to enable debug output"

	exit 1
}

SCRIPTPATH=`realpath $0`
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
CONFIGSTR=0
. ${SCRIPTPREFIX}/common.sh
................................................................................
[ -z "${JAILNAME}" ] && err 1 "Don't know on which jail to run please specify -j"

MASTERNAME=${JAILNAME}-${PTNAME}${SETNAME:+-${SETNAME}}
MASTERMNT=${POUDRIERE_DATA}/build/${MASTERNAME}/ref
export MASTERNAME
export MASTERMNT
export POUDRIERE_BUILD_TYPE=bulk



jail_start ${JAILNAME} ${PTNAME} ${SETNAME}

[ $CONFIGSTR -eq 1 ] && injail env TERM=${SAVED_TERM} make -C /usr/ports/${ORIGIN} config

LISTPORTS=$(list_deps ${ORIGIN} )
prepare_ports

markfs prepkg ${MASTERMNT}
log=$(log_path)




if ! POUDRIERE_BUILD_TYPE=bulk parallel_build ${JAILNAME} ${PTNAME} ${SETNAME} ; then
	failed=$(bget ports.failed | awk '{print $1 ":" $3 }' | xargs echo)
	skipped=$(bget ports.skipped | awk '{print $1}' | sort -u | xargs echo)
	nbignored=$(bget stats_failed)
	nbskipped=$(bget stats_skipped)

................................................................................
	exit 1
fi

unmarkfs prepkg ${MASTERMNT}

bset status "testing:"

PKGNAME=`injail make -C /usr/ports/${ORIGIN} -VPKGNAME`
LOCALBASE=`injail make -C /usr/ports/${ORIGIN} -VLOCALBASE`
: ${PREFIX:=$(injail make -C /usr/ports/${ORIGIN} -VPREFIX)}
if [ "${USE_PORTLINT}" = "yes" ]; then
	[ ! -x `which portlint` ] &&
		err 2 "First install portlint if you want USE_PORTLINT to work as expected"
	msg "Portlint check"
	set +e
	cd ${MASTERMNT}/usr/ports/${ORIGIN} &&
		PORTSDIR="${MASTERMNT}/usr/ports" portlint -C | \
		tee ${log}/logs/${PKGNAME}.portlint.log
	set -e
fi
[ ${NOPREFIX} -ne 1 ] && PREFIX="${BUILDROOT:-/prefix}/`echo ${PKGNAME} | tr '[,+]' _`"
PORT_FLAGS="NO_DEPENDS=yes PREFIX=${PREFIX}"
msg "Building with flags: ${PORT_FLAGS}"

................................................................................
fi

PKGENV="PACKAGES=/tmp/pkgs PKGREPOSITORY=/tmp/pkgs"
mkdir -p ${MASTERMNT}/tmp/pkgs
PORTTESTING=yes
export DEVELOPER_MODE=yes
log_start ${log}/logs/${PKGNAME}.log
buildlog_start /usr/ports/${ORIGIN}
if ! build_port /usr/ports/${ORIGIN}; then
	failed_status=$(bget status)
	failed_phase=${failed_status%:*}

	save_wrkdir ${MASTERMNT} "${PKGNAME}" "/usr/ports/${ORIGIN}" "${failed_phase}" || :



	if [ ${INTERACTIVE_MODE} -eq 0 ]; then
		stop_build /usr/ports/${ORIGIN} ${log}/logs/${PKGNAME}.log
		exit 1
	fi

elif [ -f ${MASTERMNT}/usr/ports/${ORIGIN}/.keep ]; then
	save_wrkdir ${MASTERMNT} "${PKGNAME}" "/usr/ports/${ORIGIN}" "noneed" ||:



fi

msg "Installing from package"
injail ${PKG_ADD} /tmp/pkgs/${PKGNAME}.${PKG_EXT}

# Interactive test mode
if [ $INTERACTIVE_MODE -gt 0 ]; then
	print_phase_header "Interactive"

	msg "Installing run-depends"
	# Install run-depends since this is an interactive test
	echo "PACKAGES=/packages" >> ${MASTERMNT}/etc/make.conf
	injail make -C /usr/ports/${ORIGIN} run-depends ||
		msg "Failed to install RUN_DEPENDS"


	# Enable networking
	jstop
	jstart 1

	if [ $INTERACTIVE_MODE -eq 1 ]; then
		msg "Entering interactive test mode. Type 'exit' when done."
		injail env -i TERM=${SAVED_TERM} \
			PACKAGESITE="file:///packages" /usr/bin/login -fp root
		[ -z "${failed_phase}" ] || err 1 "Build failed in phase: ${failed_phase}"
	elif [ $INTERACTIVE_MODE -eq 2 ]; then
		msg "Leaving jail ${MASTERNAME} running, mounted at ${MASTERMNT} for interactive run testing"
		msg "To enter jail: jexec ${MASTERNAME} /bin/sh"
		msg "To stop jail: poudriere jail -k -j ${MASTERNAME}"
		CLEANING_UP=1
		exit 0















	fi
	print_phase_footer
fi

msg "Cleaning up"
injail make -C /usr/ports/${ORIGIN} clean

msg "Deinstalling package"
injail ${PKG_DELETE} ${PKGNAME}

msg "Removing existing ${PREFIX} dir"
stop_build /usr/ports/${ORIGIN} ${log}/logs/${PKGNAME}.log

cleanup
set +e




exit 0







|
>







 







>
>



|






>
>
>







 







|
|
|





|
|







 







|
|



|
>
>


|


>
|
|
>
>
>












|


>
|
|
|

|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





|





|



>
>
>


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
...
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
...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    -J n        -- Run n jobs in parallel for dependencies
    -j name     -- Run inside the given jail
    -i          -- Interactive mode. Enter jail for interactive testing and automatically cleanup when done.
    -I          -- Advanced Interactive mode. Leaves jail running with port installed after test.
    -n          -- No custom prefix
    -p tree     -- Specify the path to the portstree
    -s          -- Skip sanity checks
    -v          -- Be verbose; show more information. Use twice to enable debug output
    -z          -- Define set name"
	exit 1
}

SCRIPTPATH=`realpath $0`
SCRIPTPREFIX=`dirname ${SCRIPTPATH}`
CONFIGSTR=0
. ${SCRIPTPREFIX}/common.sh
................................................................................
[ -z "${JAILNAME}" ] && err 1 "Don't know on which jail to run please specify -j"

MASTERNAME=${JAILNAME}-${PTNAME}${SETNAME:+-${SETNAME}}
MASTERMNT=${POUDRIERE_DATA}/build/${MASTERNAME}/ref
export MASTERNAME
export MASTERMNT
export POUDRIERE_BUILD_TYPE=bulk

check_jobs

jail_start ${JAILNAME} ${PTNAME} ${SETNAME}

[ $CONFIGSTR -eq 1 ] && injail env TERM=${SAVED_TERM} make -C ${PORTSRC}/${ORIGIN} config

LISTPORTS=$(list_deps ${ORIGIN} )
prepare_ports

markfs prepkg ${MASTERMNT}
log=$(log_path)

run_hook test_port_start "${JAILNAME}" "${PTNAME}" "${MASTERMNT}${PORTSRC}/${ORIGIN}" \
	`bget stats_queued`

if ! POUDRIERE_BUILD_TYPE=bulk parallel_build ${JAILNAME} ${PTNAME} ${SETNAME} ; then
	failed=$(bget ports.failed | awk '{print $1 ":" $3 }' | xargs echo)
	skipped=$(bget ports.skipped | awk '{print $1}' | sort -u | xargs echo)
	nbignored=$(bget stats_failed)
	nbskipped=$(bget stats_skipped)

................................................................................
	exit 1
fi

unmarkfs prepkg ${MASTERMNT}

bset status "testing:"

PKGNAME=`injail make -C ${PORTSRC}/${ORIGIN} -VPKGNAME`
LOCALBASE=`injail make -C ${PORTSRC}/${ORIGIN} -VLOCALBASE`
: ${PREFIX:=$(injail make -C ${PORTSRC}/${ORIGIN} -VPREFIX)}
if [ "${USE_PORTLINT}" = "yes" ]; then
	[ ! -x `which portlint` ] &&
		err 2 "First install portlint if you want USE_PORTLINT to work as expected"
	msg "Portlint check"
	set +e
	cd ${MASTERMNT}${PORTSRC}/${ORIGIN} &&
		PORTSDIR="${MASTERMNT}${PORTSRC}" portlint -C | \
		tee ${log}/logs/${PKGNAME}.portlint.log
	set -e
fi
[ ${NOPREFIX} -ne 1 ] && PREFIX="${BUILDROOT:-/prefix}/`echo ${PKGNAME} | tr '[,+]' _`"
PORT_FLAGS="NO_DEPENDS=yes PREFIX=${PREFIX}"
msg "Building with flags: ${PORT_FLAGS}"

................................................................................
fi

PKGENV="PACKAGES=/tmp/pkgs PKGREPOSITORY=/tmp/pkgs"
mkdir -p ${MASTERMNT}/tmp/pkgs
PORTTESTING=yes
export DEVELOPER_MODE=yes
log_start ${log}/logs/${PKGNAME}.log
buildlog_start ${PORTSRC}/${ORIGIN}
if ! build_port ${PORTSRC}/${ORIGIN}; then
	failed_status=$(bget status)
	failed_phase=${failed_status%:*}

	save_wrkdir ${MASTERMNT} "${PKGNAME}" "${PORTSRC}/${ORIGIN}" "${failed_phase}" || :
	build_result=0
	run_hook port_build_failure "${JAILNAME}" "${PTNAME}" "${MASTERMNT}${PORTSRC}/${ORIGIN}" "${failed_phase}"

	if [ ${INTERACTIVE_MODE} -eq 0 ]; then
		stop_build ${PORTSRC}/${ORIGIN} ${log}/logs/${PKGNAME}.log
		exit 1
	fi
else
	if [ -f ${MASTERMNT}/${PORTSRC}/${ORIGIN}/.keep ]; then
		save_wrkdir ${MASTERMNT} "${PKGNAME}" "${PORTSRC}/${ORIGIN}" "noneed" ||:
	fi
	build_result=1
	run_hook port_build_success "${JAILNAME}" "${PTNAME}" "${MASTERMNT}${PORTSRC}/${ORIGIN}"
fi

msg "Installing from package"
injail ${PKG_ADD} /tmp/pkgs/${PKGNAME}.${PKG_EXT}

# Interactive test mode
if [ $INTERACTIVE_MODE -gt 0 ]; then
	print_phase_header "Interactive"

	msg "Installing run-depends"
	# Install run-depends since this is an interactive test
	echo "PACKAGES=/packages" >> ${MASTERMNT}/etc/make.conf
	injail make -C ${PORTSRC}/${ORIGIN} run-depends ||
		msg "Failed to install RUN_DEPENDS"

	if ${BSDPLATFORM} = "freebsd"; then
		# Enable networking
		jstop
		jstart 1

		if [ $INTERACTIVE_MODE -eq 1 ]; then
			msg "Entering interactive test mode. Type 'exit' when done."
			injail env -i TERM=${SAVED_TERM} \
				PACKAGESITE="file:///packages" /usr/bin/login -fp root
			[ -z "${failed_phase}" ] || err 1 "Build failed in phase: ${failed_phase}"
		elif [ $INTERACTIVE_MODE -eq 2 ]; then
			msg "Leaving jail ${MASTERNAME} running, mounted at ${MASTERMNT} for interactive run testing"
			msg "To enter jail: jexec ${MASTERNAME} /bin/sh"
			msg "To stop jail: poudriere jail -k -j ${MASTERNAME}"
			CLEANING_UP=1
			exit 0
		fi
	else
		if [ $INTERACTIVE_MODE -eq 1 ]; then
			msg "Entering interactive test mode. Type 'exit' when done."
			injail env -i TERM=${SAVED_TERM} \
				PACKAGESITE="file:///packages" /bin/sh
			[ -z "${failed_phase}" ] || 
				err 1 "Build failed in phase: ${failed_phase}"
		else
			msg "Leaving jail ${MASTERNAME} running, mounted at ${MASTERMNT} for interactive run testing"
			msg "To enter jail: chroot ${MASTERMNT} /bin/sh"
			msg "To stop jail: 'exit', 'poudriere jail -C -j ${MASTERNAME}'"
			CLEANING_UP=1
			exit 0
		fi
	fi
	print_phase_footer
fi

msg "Cleaning up"
injail make -C ${PORTSRC}/${ORIGIN} clean

msg "Deinstalling package"
injail ${PKG_DELETE} ${PKGNAME}

msg "Removing existing ${PREFIX} dir"
stop_build ${PORTSRC}/${ORIGIN} ${log}/logs/${PKGNAME}.log

cleanup
set +e
run_hook test_port_ended "${JAILNAME}" "${PTNAME}" "${MASTERMNT}${PORTSRC}/${ORIGIN}" \
	`bget stats_built` `bget stats_failed` `bget stats_ignored` \
	`bget stats_skipped` "${build_result}"

exit 0