xref: /freebsd/release/scripts/mm-mtree.sh (revision 81ad6265)
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 updating
9# to -stable.
10
11# Copyright 2009 Douglas Barton
12# dougb@FreeBSD.org
13
14# $FreeBSD$
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=`TMPDIR=/var/tmp mktemp -d -t 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 -DDB_FROM_SRC"
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  ${MM_MAKE} DESTDIR=${TEMPROOT} distrib-dirs &&
118  ${MM_MAKE} _obj SUBDIR_OVERRIDE=etc &&
119  ${MM_MAKE} everything SUBDIR_OVERRIDE=etc &&
120  ${MM_MAKE} DESTDIR=${TEMPROOT} distribution;} ||
121  { echo '';
122    echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
123    echo "      the temproot environment";
124    echo '';
125    exit 1;}
126
127# We really don't want to have to deal with files like login.conf.db, pwd.db,
128# or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
129# Prompt the user to do so below, as needed.
130#
131rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
132
133# We only need to compare things like freebsd.cf once
134find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
135
136# Delete stuff we do not need to keep the mtree database small,
137# and to make the actual comparison faster.
138find ${TEMPROOT}/usr -type l -delete 2>/dev/null
139find ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
140find -d ${TEMPROOT} -type d -empty -delete 2>/dev/null
141
142# Build the mtree database in a temporary location.
143MTREENEW=`mktemp -t mergemaster.mtree`
144mtree -nci -p ${TEMPROOT} -k size,md5digest > ${MTREENEW} 2>/dev/null
145
146if [ -s "${MTREENEW}" ]; then
147  echo "*** Saving mtree database for future upgrades"
148  test -e "${DESTDIR}${MTREEFILE}" && unlink ${DESTDIR}${MTREEFILE}
149  mv ${MTREENEW} ${DESTDIR}${MTREEFILE}
150fi
151
152delete_temproot
153
154exit 0
155