summaryrefslogtreecommitdiff
path: root/Tools/portbuild/scripts/cleanup-chroots
blob: e2b8e1ceefbb7746ba1b8a69a1eddccda017e9dd (plain)
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
#!/bin/sh
#
# To be run on the client, this script looks for chroot directories that have not been
# used in 20 minutes, as well as directories listed as 'in use' that have not been touched
# in 24 hours (corresponding to port builds that have timed out or shut down uncleanly)
# and prunes them to reclaim space.

kill_procs()
{
  dir=$1

  pids="XXX"
  while [ ! -z "${pids}" ]; do
    pids=$(fstat -f "$dir" | tail +2 | awk '{print $3}' | sort -u)
    if [ ! -z "${pids}" ]; then
      echo "Killing off pids in ${dir}"
      ps -p $pids
      kill -KILL ${pids} 2> /dev/null
      sleep 2
    fi
  done
}

cleanup_mount() {
  chroot=$1
  mount=$2

  if [ -d ${chroot}${mount} ]; then
    mdir=$(fstat -f ${chroot}${mount} | head -2 | tail -1 | awk '{print $5}')
    if [ "${mdir}" = "MOUNT" ]; then
      umount ${chroot}${mount} || echo "Cleanup of ${chroot}${mount} failed!"
    fi
    if [ "${mdir}" = "${chroot}${mount}" ]; then
      kill_procs ${chroot}${mount}
      umount ${chroot}${mount} || echo "Cleanup of ${chroot}${mount} failed!"
    fi
  fi
}

pb=/var/portbuild
arch=$(cat /etc/arch)

. ${pb}/${arch}/portbuild.conf

old=$(find ${scratchdir}/*/chroot/* -prune -mmin +20 2> /dev/null)

if [ -z "${old}" ]; then
	exit 0
fi

# Prune out chroots with active builds
for i in ${old}; do
	if [ ! -d ${i}/used ]; then
		old2="${i} ${old2}"
	# Also remove "in use" chroots that were set up more than 48 hours ago
	elif [ ! -z "`find $i/used -prune -mmin +2880`" ]; then
		echo "cleanup-chroots: Found old files on `hostname`:"
		ls -l ${i}/tmp ${i}/used
		echo "${i} allegedly in use but >48 hours old"
		old2="${i} ${old2}"
	fi
done

if [ -z "${old2}" ]; then
	exit 0
fi

# cleanup old NFS and devfs mounts
for i in ${old2}; do
	mounts=$(mount | grep $i | awk '{print $3}')
	if [ ! -z "${mounts}" ]; then
	  for j in ${mounts}; do
	    umount ${j} || cleanup_mount ${j}
	  done
	  umount ${i}/compat/linux/proc || cleanup_mount ${i}/compat/linux/proc
	fi
	if [ "${use_md_swap}" = "1" ]; then
		chrootnum=$(basename $i)
		umount -f /dev/md${i}
		mdconfig -d -u ${chrootnum}
	fi
done

mkdir -p ${scratchdir}/old
mv ${old2} ${scratchdir}/old
rm -rf ${scratchdir}/old 2> /dev/null
chflags -R noschg ${scratchdir}/old
rm -rf ${scratchdir}/old