Whamcloud - gitweb
LU-6051 utils: allow lfs_migrate to handle hard links
[fs/lustre-release.git] / lustre / scripts / lfs_migrate
1 #!/bin/bash
2
3 # lfs_migrate: a simple tool to copy and check files.
4 #
5 # To avoid allocating objects on one or more OSTs, they should be
6 # deactivated on the MDS via "lctl --device {device_number} deactivate",
7 # where {device_number} is from the output of "lctl dl" on the MDS.
8 #
9 # To guard against corruption, the file is compared after migration
10 # to verify the copy is correct and the file has not been modified.
11 # This is not a protection against the file being open by another
12 # process, but it would catch the worst cases of in-use files, but
13 # to be 100% safe the administrator needs to ensure this is safe.
14
15 RSYNC=${RSYNC:-rsync}
16 LFS_MIGRATE_RSYNC_MODE=${LFS_MIGRATE_RSYNC_MODE:-false}
17 ECHO=echo
18 LFS=${LFS:-lfs}
19 LFS_MIGRATE_RSYNC=${LFS_MIGRATE_RSYNC:-false}
20 RSYNC_WITH_HLINKS=false
21 LFS_MIGRATE_TMP=${TMPDIR:-/tmp}
22 MIGRATED_SET="$(mktemp ${LFS_MIGRATE_TMP}/lfs_migrate-$$.links.XXXXXX)"
23 NEWNAME=""
24 REMOVE_FID='s/^\[[0-9a-fx:]*\] //'
25
26 add_to_set() {
27         local old_fid="$1"
28         local path="$2"
29
30         echo -e "$old_fid $path" >> "$MIGRATED_SET"
31 }
32
33 path_in_set() {
34         local path="$1"
35
36         sed -e "$REMOVE_FID" $MIGRATED_SET | grep -q "^$path$"
37 }
38
39 old_fid_in_set() {
40         local old_fid="$1"
41
42         grep "^\\$old_fid" "$MIGRATED_SET" | head -n 1 |
43                 sed -e "$REMOVE_FID"
44 }
45
46 usage() {
47     cat -- <<USAGE 1>&2
48 usage: lfs_migrate [-c <stripe_count>] [-h] [-n] [-q] [-R] [-s]
49                    [-S <stripe_size>] [-y] [-0] [file|dir ...]
50     -c <stripe_count>
51        restripe file using the specified stripe count
52     -h show this usage message
53     -n only print the names of files to be migrated
54     -q run quietly (don't print filenames or status)
55     -R restripe file using default directory striping
56     -s skip file data comparison after migrate
57     -S <stripe_size>
58        restripe file using the specified stripe size
59     -y answer 'y' to usage question
60     -0 input file names on stdin are separated by a null character
61
62 The -c <stripe_count> and -S <stripe_size> options may not be specified at
63 the same time as the -R option.
64
65 If a directory is an argument, all files in the directory are migrated.
66 If no file/directory is given, the file list is read from standard input.
67
68 e.g.: lfs_migrate /mnt/lustre/dir
69       lfs find /test -O test-OST0004 -size +4G | lfs_migrate -y
70 USAGE
71     exit 1
72 }
73
74 cleanup() {
75         rm -f "$MIGRATED_SET"
76         [ -n "$NEWNAME" ] && rm -f "$NEWNAME"
77 }
78
79 trap cleanup EXIT
80
81 OPT_CHECK=y
82 OPT_STRIPE_COUNT=""
83 OPT_STRIPE_SIZE=""
84
85 while getopts "c:hlnqRsS:y0" opt $*; do
86     case $opt in
87         c) OPT_STRIPE_COUNT=$OPTARG;;
88         l) ;; # maintained for backward compatibility
89         n) OPT_DRYRUN=n; OPT_YES=y;;
90         q) ECHO=:;;
91         R) OPT_RESTRIPE=y;;
92         s) OPT_CHECK="";;
93         S) OPT_STRIPE_SIZE=$OPTARG;;
94         y) OPT_YES=y;;
95         0) OPT_NULL=y;;
96         h|\?) usage;;
97     esac
98 done
99 shift $((OPTIND - 1))
100
101 if [ -n "$OPT_STRIPE_COUNT""$OPT_STRIPE_SIZE" -a "$OPT_RESTRIPE" ]; then
102         echo ""
103         echo "$(basename $0) error: The -c <stripe_count> option and" 1>&2
104         echo "-S <stripe_size> option may not" 1>&2
105         echo "be specified at the same time as the -R option." 1>&2
106         exit 1
107 fi
108
109 if [ -z "$OPT_YES" ]; then
110         echo ""
111         echo "lfs_migrate is currently NOT SAFE for moving in-use files." 1>&2
112         echo "Use it only when you are sure migrated files are unused." 1>&2
113         echo "" 1>&2
114         echo "If emptying an OST that is active on the MDS, new files may" 1>&2
115         echo "use it.  To stop allocating any new objects on OSTNNNN run:" 1>&2
116         echo "  lctl set_param osp.<fsname>-OSTNNNN*.max_create_count=0'" 1>&2
117         echo "on each MDS using the OST(s) being emptied." 1>&2
118         echo -n "Continue? (y/n) "
119         read CHECK
120         [ "$CHECK" != "y" -a "$CHECK" != "yes" ] && exit 1
121 fi
122
123 # if rsync has --xattr support, then try to copy the xattrs.
124 $RSYNC --help 2>&1 | grep -q xattr && RSYNC_OPTS="$RSYNC_OPTS -X"
125 $RSYNC --help 2>&1 | grep -q acls && RSYNC_OPTS="$RSYNC_OPTS -A"
126 # If rsync copies lustre xattrs in the future, then we can skip lfs (bug 22189)
127 strings $(which $RSYNC) 2>&1 | grep -q lustre && LFS=:
128
129 # rsync creates its temporary files with lenient permissions, even if
130 # permissions on the original files are more strict. Tighten umask here
131 # to avoid the brief window where unprivileged users might be able to
132 # access the temporary file.
133 umask 0077
134
135 lfs_migrate() {
136         while IFS='' read -d '' OLDNAME; do
137                 local hlinks=()
138
139                 # avoid duplicate stat if possible
140                 local nlink_type=($(LANG=C stat -c "%h %F" "$OLDNAME" || true))
141
142                 # skip non-regular files, since they don't have any objects
143                 # and there is no point in trying to migrate them.
144                 if [ "${nlink_type[1]}" != "regular" ]; then
145                         echo -e "$OLDNAME: not a regular file, skipped"
146                         continue
147                 fi
148
149                 # working out write perms is hard, let the shell do it
150                 if [ ! -w "$OLDNAME" ]; then
151                         echo -e "$OLDNAME: no write permission, skipped"
152                         continue
153                 fi
154
155                 if [ "$OPT_DRYRUN" ]; then
156                         echo -e "$OLDNAME: dry run, skipped"
157                         continue
158                 fi
159
160                 # xattrs use absolute file paths, so ensure provided path is
161                 # also absolute so that the names can be compared
162                 local oldname_absolute=$(readlink -f "$OLDNAME")
163                 if [ $? -ne 0 ]; then
164                         echo -e "$OLDNAME: cannot resolve full path"
165                         continue
166                 fi
167                 OLDNAME=$oldname_absolute
168
169                 # In the future, the path2fid and fid2path calls below
170                 # should be replaced with a single call to
171                 # "lfs path2links" once that command is available.  The logic
172                 # for detecting unlisted hard links could then be removed.
173                 local fid=$(lfs path2fid "$OLDNAME" 2> /dev/null)
174                 if [ $? -ne 0 ]; then
175                         echo -n "$OLDNAME: cannot determine FID; skipping; "
176                         echo "is this a Lustre file system?"
177                         continue
178                 fi
179
180                 if [[ ${nlink_type[0]} -gt 1 || $RSYNC_WITH_HLINKS == true ]]; then
181                         # don't migrate a hard link if it was already migrated
182                         if path_in_set "$OLDNAME"; then
183                                 $ECHO -e "$OLDNAME: already migrated via another hard link"
184                                 continue
185                         fi
186
187                         # There is limited space available in the xattrs
188                         # to store all of the hard links for a file, so it's
189                         # possible that $OLDNAME is part of a link set but is
190                         # not listed in xattrs and therefore not listed as
191                         # being migrated.
192                         local migrated=$(old_fid_in_set "$fid")
193                         if [ -n "$migrated" ]; then
194                                 $ECHO -e "$OLDNAME: already migrated via another hard link"
195                                 if [[ $LFS_MIGRATE_RSYNC == true ]]; then
196                                         # Only the rsync case has to relink.
197                                         # The lfs migrate case preserves the
198                                         # inode so the links are already
199                                         # correct.
200                                         [ "$migrated" != "$OLDNAME" ] &&
201                                                 ln -f "$migrated" "$OLDNAME"
202                                 fi
203                                 add_to_set "$fid" "$OLDNAME"
204                                 continue;
205                         fi
206                 fi
207
208                 if [ "$OPT_RESTRIPE" ]; then
209                         UNLINK=""
210                 else
211                 # if rsync copies Lustre xattrs properly in the future
212                 # (i.e. before the file data, so that it preserves striping)
213                 # then we don't need to do this getstripe/mktemp stuff.
214                         UNLINK="-u"
215
216                         [ "$OPT_STRIPE_COUNT" ] &&
217                                 stripe_count=$OPT_STRIPE_COUNT ||
218                                 stripe_count=$($LFS getstripe -c "$OLDNAME" \
219                                                2> /dev/null)
220                         [ "$OPT_STRIPE_SIZE" ] &&
221                                 stripe_size=$OPT_STRIPE_SIZE ||
222                                 stripe_size=$($LFS getstripe -S \
223                                               "$OLDNAME" 2> /dev/null)
224
225                         if [ -z "$stripe_count" -o -z "$stripe_size" ]; then
226                                 UNLINK=""
227                                 echo -e "$OLDNAME: cannot determine stripe info; skipping"
228                                 continue
229                         fi
230                         stripe_size="-S$stripe_size"
231                         stripe_count="-c$stripe_count"
232                 fi
233
234                 # detect other hard links and store them on a global
235                 # list so we don't re-migrate them
236                 local mntpoint=$(df -P "$OLDNAME" |
237                                 awk 'NR==2 { print $NF; exit }')
238                 if [ -z "$mntpoint" ]; then
239                         echo -e "$OLDNAME: cannot determine mount point; skipping"
240                         continue
241                 fi
242                 local hlinks=$(lfs fid2path "$mntpoint" "$fid" 2> /dev/null)
243                 if [ $? -ne 0 ]; then
244                         echo -n "$OLDNAME: cannot determine hard link paths"
245                         continue
246                 fi
247                 hlinks+=("$OLDNAME")
248
249                 # first try to migrate via Lustre tools, then fall back to rsync
250                 if [[ $LFS_MIGRATE_RSYNC == false ]]; then
251                         if $LFS migrate "$stripe_count" "$stripe_size" "$OLDNAME"; then
252                                 $ECHO -e "$OLDNAME: done migrate"
253                                 for link in ${hlinks[*]}; do
254                                         add_to_set "$fid" "$link"
255                                 done
256                                 continue
257                         else
258                                 echo -e "$OLDNAME: falling back to rsync-based migration"
259                                 LFS_MIGRATE_RSYNC=true
260                         fi
261                 fi
262
263                 NEWNAME=$(mktemp $UNLINK "$OLDNAME-lfs_migrate.tmp.XXXXXX")
264                 if [ $? -ne 0 -o -z "$NEWNAME" ]; then
265                         echo -e "$OLDNAME: can't make temp file, skipped" 1>&2
266                         continue
267                 fi
268
269                 [ "$UNLINK" ] && $LFS setstripe ${stripe_count} \
270                         ${stripe_size} "$NEWNAME"
271
272                 # we use --inplace, since we created our own temp file already
273                 if ! $RSYNC -a --inplace $RSYNC_OPTS "$OLDNAME" "$NEWNAME";then
274                         echo -e "$OLDNAME: copy error, exiting" 1>&2
275                         exit 4
276                 fi
277
278                 if [ "$OPT_CHECK" ] && ! cmp -s "$OLDNAME" "$NEWNAME"; then
279                         echo -e "$NEWNAME: compare failed, exiting" 1>&2
280                         exit 8
281                 fi
282
283                 if ! mv "$NEWNAME" "$OLDNAME"; then
284                         echo -e "$OLDNAME: rename error, exiting" 1>&2
285                         exit 12
286                 fi
287
288                 $ECHO -e "$OLDNAME: done migrate via rsync"
289                 for link in ${hlinks[*]}; do
290                         if [ "$link" != "$OLDNAME" ]; then
291                                 ln -f "$OLDNAME" "$link"
292                         fi
293                         add_to_set "$fid" "$link"
294                 done
295
296                 # If the number of hlinks exceeds the space in the xattrs,
297                 # when the final path is statted it will have a link count
298                 # of 1 (all other links will point to the new inode).
299                 # This flag indicates that even paths with a link count of
300                 # 1 are potentially part of a link set.
301                 [ ${#hlinks[*]} -gt 1 ] && RSYNC_WITH_HLINKS=true
302         done
303 }
304
305 if [ "$#" -eq 0 ]; then
306         if [ "$OPT_NULL" ]; then
307                 lfs_migrate
308         else
309                 tr '\n' '\0' | lfs_migrate
310         fi
311 else
312         while [ "$1" ]; do
313                 if [ -d "$1" ]; then
314                         $LFS find "$1" -type f -print0 | lfs_migrate
315                 else
316                         echo -en "$1\0" | lfs_migrate
317                 fi
318                 shift
319         done
320 fi
321