mm-mtree.sh revision 196434
1#!/bin/sh
2
3# mergemaster mtree database generator
4
5# This script is intended to be used as part of the release building
6# process to generate the /var/db/mergemaster.mtree file relevant to
7# the source tree used to create the release so that users can make
8# use of mergemaster's -U option to update their files after csup'ing
9# to -stable.
10
11# Copyright 2009 Douglas Barton
12# dougb@FreeBSD.org
13
14# $FreeBSD: head/release/scripts/mm-mtree.sh 196434 2009-08-23 05:42:50Z dougb $
15
16PATH=/bin:/usr/bin:/usr/sbin
17
18display_usage () {
19  VERSION_NUMBER=`grep "[$]FreeBSD:" $0 | cut -d ' ' -f 4`
20  echo "${0##*/} version ${VERSION_NUMBER}"
21  echo "Usage: ${0##*/} [-m /path] [-t /path] [-A arch] [-F <make args>] [-D /path]"
22  echo "Options:"
23  echo "  -m /path/directory  Specify location of source to do the make in"
24  echo "  -t /path/directory  Specify temp root directory"
25  echo "  -A architecture  Alternative architecture name to pass to make"
26  echo "  -F <arguments for make> Specify what to put on the make command line"
27  echo '  -D /path/directory  Specify the destination directory to install files to'
28  echo ''
29}
30
31# Set the default path for the temporary root environment
32#
33TEMPROOT='/var/tmp/temproot'
34
35# Assign the location of the mtree database
36#
37MTREEDB=${MTREEDB:-/var/db}
38MTREEFILE="${MTREEDB}/mergemaster.mtree"
39
40# Check the command line options
41#
42while getopts "m:t:A:F:D:h" COMMAND_LINE_ARGUMENT ; do
43  case "${COMMAND_LINE_ARGUMENT}" in
44  m)
45    SOURCEDIR=${OPTARG}
46    ;;
47  t)
48    TEMPROOT=${OPTARG}
49    ;;
50  A)
51    ARCHSTRING='TARGET_ARCH='${OPTARG}
52    ;;
53  F)
54    MM_MAKE_ARGS="${OPTARG}"
55    ;;
56  D)
57    DESTDIR=${OPTARG}
58    ;;
59  h)
60    display_usage
61    exit 0
62    ;;
63  *)
64    echo ''
65    display_usage
66    exit 1
67    ;;
68  esac
69done
70
71# Assign the source directory
72#
73SOURCEDIR=${SOURCEDIR:-/usr/src}
74if [ ! -f ${SOURCEDIR}/Makefile.inc1 -a \
75   -f ${SOURCEDIR}/../Makefile.inc1 ]; then
76  echo " *** The source directory you specified (${SOURCEDIR})"
77  echo "     will be reset to ${SOURCEDIR}/.."
78  echo ''
79  sleep 3
80  SOURCEDIR=${SOURCEDIR}/..
81fi
82
83# Setup make to use system files from SOURCEDIR
84MM_MAKE="make ${ARCHSTRING} ${MM_MAKE_ARGS} -m ${SOURCEDIR}/share/mk"
85
86delete_temproot () {
87  rm -rf "${TEMPROOT}" 2>/dev/null
88  chflags -R 0 "${TEMPROOT}" 2>/dev/null
89  rm -rf "${TEMPROOT}" || exit 1
90}
91
92[ -d "${TEMPROOT}" ] && delete_temproot
93
94echo "*** Creating the temporary root environment in ${TEMPROOT}"
95
96if mkdir -p "${TEMPROOT}"; then
97  echo " *** ${TEMPROOT} ready for use"
98fi
99
100if [ ! -d "${TEMPROOT}" ]; then
101  echo ''
102  echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
103  echo ''
104  exit 1
105fi
106
107echo " *** Creating and populating directory structure in ${TEMPROOT}"
108echo ''
109
110{ cd ${SOURCEDIR} || { echo "*** Cannot cd to ${SOURCEDIR}" ; exit 1;}
111  case "${DESTDIR}" in
112  '') ;;
113  *)
114    ${MM_MAKE} DESTDIR=${DESTDIR} distrib-dirs
115    ;;
116  esac
117  od=${TEMPROOT}/usr/obj
118  ${MM_MAKE} DESTDIR=${TEMPROOT} distrib-dirs &&
119  MAKEOBJDIRPREFIX=$od ${MM_MAKE} _obj SUBDIR_OVERRIDE=etc &&
120  MAKEOBJDIRPREFIX=$od ${MM_MAKE} everything SUBDIR_OVERRIDE=etc &&
121  MAKEOBJDIRPREFIX=$od ${MM_MAKE} DESTDIR=${TEMPROOT} distribution;} ||
122  { echo '';
123    echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
124    echo "      the temproot environment";
125    echo '';
126    exit 1;}
127
128# We really don't want to have to deal with files like login.conf.db, pwd.db,
129# or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
130# Prompt the user to do so below, as needed.
131#
132rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
133
134# We only need to compare things like freebsd.cf once
135find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
136
137# Delete stuff we do not need to keep the mtree database small,
138# and to make the actual comparison faster.
139find ${TEMPROOT}/usr -type l -delete 2>/dev/null
140find ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
141find -d ${TEMPROOT} -type d -empty -delete 2>/dev/null
142
143# Build the mtree database in a temporary location.
144MTREENEW=`mktemp -t mergemaster.mtree`
145mtree -ci -p ${TEMPROOT} -k size,md5digest > ${MTREENEW} 2>/dev/null
146
147if [ -s "${MTREENEW}" ]; then
148  echo "*** Saving mtree database for future upgrades"
149  test -e "${DESTDIR}${MTREEFILE}" && unlink ${DESTDIR}${MTREEFILE}
150  mv ${MTREENEW} ${DESTDIR}${MTREEFILE}
151fi
152
153delete_temproot
154
155exit 0
156