aboutsummaryrefslogtreecommitdiffstats
path: root/Tools/portbuild/scripts/claim-chroot
blob: 0a07f57aa7d18a063b2a49771443a724acb38f03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh

# usage: claim-chroot ${arch} ${branch} ${pkgname}

# This script cannot output anything except the name of the successfully
# claimed chroot.  In case of error, just exit.

# XXX Return the string "chroot=*" and look for that in pdispatch to make
# this more robust

# configurable variables
pb=/var/portbuild

arch=$1
shift

. ${pb}/${arch}/portbuild.conf
. ${pb}/${arch}/portbuild.$(hostname)
. ${pb}/scripts/buildenv

buildroot=${scratchdir}

branch=$1
shift

buildenv ${pb} ${arch} ${branch}

pkgname=$(basename $1 ${PKGSUFFIX})

chrootdir=${buildroot}/${branch}/chroot

# Perform initial sanity check

if [ ! -z "${squid_dir}" ]; then
  /usr/local/sbin/squid -k check 2> /dev/null
  status=$?
  if [ "${status}" != "0" ]; then
    touch ${scratchdir}/.squid
    /usr/local/etc/rc.d/squid start > /dev/null &
    exit 1
  else
    rm -f ${scratchdir}/.squid
  fi
fi

# Check for enough disk space
df=$(df -k ${scratchdir} | tail -1 | awk '{print $4}')

if [ ${df} -lt 102400 ]; then
  touch ${scratchdir}/.disk
  exit 1
else
  rm -f ${scratchdir}/.disk
fi

found=0
# Look for pre-existing chroot directories that are populated and unused
for dir in ${chrootdir}/*; do
  if [ -f ${dir}/.ready -o -f ${dir}/.dirty ]; then
    # Atomically claim the directory
    mkdir ${dir}/used 2>/dev/null || continue
    touch ${dir}/used/${pkgname}
    if [ -f ${dir}/.dirty ]; then
      ${pb}/scripts/clean-chroot ${arch} ${branch} ${dir} 2 &
      continue
    fi
    found=1
    chroot=${dir}
    break
  fi
done

chrootnum=$$
# If we didn't find a pre-existing directory, create and claim a new one.
while [ ${found} != 1 ]; do
  if [ "${use_md_swap}" = "1" ]; then
    unit=$(mdconfig -a -t swap -s ${md_size})
    newfs /dev/${unit} > /dev/null
    chrootnum=$(echo ${unit} | sed 's,md,,')
    chroot=${chrootdir}/${chrootnum}
    mkdir -p ${chroot}/used 2>/dev/null || continue
    # Need to make sure that used/ is also present after mounting the fresh md so as to not leave open any races
    mount -o async /dev/${unit} ${chroot}/used
    mkdir ${chroot}/used/used
    touch ${chroot}/used/used/${pkgname}
    umount ${chroot}/used
    mount -o async /dev/${unit} ${chroot}/
  else
    chrootnum=$(($chrootnum+1))
    chroot=${chrootdir}/${chrootnum}
    mkdir -p ${chroot} 2>/dev/null || continue
    mkdir ${chroot}/used 2>/dev/null || continue
  fi
  touch ${chroot}/used/${pkgname}
  touch ${chroot}/.notready
  found=1
done

echo ${chroot}