3 # lfs_migrate: a simple tool to copy and check files.
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.
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.
16 LFS_MIGRATE_RSYNC_MODE=${LFS_MIGRATE_RSYNC_MODE:-false}
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)"
24 REMOVE_FID='s/^\[[0-9a-fx:]*\] //'
30 echo -e "$old_fid $path" >> "$MIGRATED_SET"
36 sed -e "$REMOVE_FID" $MIGRATED_SET | grep -q "^$path$"
42 grep "^\\$old_fid" "$MIGRATED_SET" | head -n 1 |
48 usage: lfs_migrate [-c <stripe_count>] [-h] [-n] [-q] [-R] [-s]
49 [-S <stripe_size>] [-y] [-0] [file|dir ...]
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
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
62 The -c <stripe_count> and -S <stripe_size> options may not be specified at
63 the same time as the -R option.
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.
68 e.g.: lfs_migrate /mnt/lustre/dir
69 lfs find /test -O test-OST0004 -size +4G | lfs_migrate -y
76 [ -n "$NEWNAME" ] && rm -f "$NEWNAME"
85 while getopts "c:hlnqRsS:y0" opt $*; do
87 c) OPT_STRIPE_COUNT=$OPTARG;;
88 l) ;; # maintained for backward compatibility
89 n) OPT_DRYRUN=n; OPT_YES=y;;
93 S) OPT_STRIPE_SIZE=$OPTARG;;
101 if [ -n "$OPT_STRIPE_COUNT""$OPT_STRIPE_SIZE" -a "$OPT_RESTRIPE" ]; then
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
109 if [ -z "$OPT_YES" ]; then
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
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) "
120 [ "$CHECK" != "y" -a "$CHECK" != "yes" ] && exit 1
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=:
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.
136 while IFS='' read -d '' OLDNAME; do
139 # avoid duplicate stat if possible
140 local nlink_type=($(LANG=C stat -c "%h %F" "$OLDNAME" || true))
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"
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"
155 if [ "$OPT_DRYRUN" ]; then
156 echo -e "$OLDNAME: dry run, skipped"
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"
167 OLDNAME=$oldname_absolute
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?"
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"
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
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
200 [ "$migrated" != "$OLDNAME" ] &&
201 ln -f "$migrated" "$OLDNAME"
203 add_to_set "$fid" "$OLDNAME"
208 if [ "$OPT_RESTRIPE" ]; then
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.
216 [ "$OPT_STRIPE_COUNT" ] &&
217 stripe_count=$OPT_STRIPE_COUNT ||
218 stripe_count=$($LFS getstripe -c "$OLDNAME" \
220 [ "$OPT_STRIPE_SIZE" ] &&
221 stripe_size=$OPT_STRIPE_SIZE ||
222 stripe_size=$($LFS getstripe -S \
223 "$OLDNAME" 2> /dev/null)
225 if [ -z "$stripe_count" -o -z "$stripe_size" ]; then
227 echo -e "$OLDNAME: cannot determine stripe info; skipping"
230 stripe_size="-S$stripe_size"
231 stripe_count="-c$stripe_count"
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"
242 local hlinks=$(lfs fid2path "$mntpoint" "$fid" 2> /dev/null)
243 if [ $? -ne 0 ]; then
244 echo -n "$OLDNAME: cannot determine hard link paths"
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"
258 echo -e "$OLDNAME: falling back to rsync-based migration"
259 LFS_MIGRATE_RSYNC=true
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
269 [ "$UNLINK" ] && $LFS setstripe ${stripe_count} \
270 ${stripe_size} "$NEWNAME"
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
278 if [ "$OPT_CHECK" ] && ! cmp -s "$OLDNAME" "$NEWNAME"; then
279 echo -e "$NEWNAME: compare failed, exiting" 1>&2
283 if ! mv "$NEWNAME" "$OLDNAME"; then
284 echo -e "$OLDNAME: rename error, exiting" 1>&2
288 $ECHO -e "$OLDNAME: done migrate via rsync"
289 for link in ${hlinks[*]}; do
290 if [ "$link" != "$OLDNAME" ]; then
291 ln -f "$OLDNAME" "$link"
293 add_to_set "$fid" "$link"
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
305 if [ "$#" -eq 0 ]; then
306 if [ "$OPT_NULL" ]; then
309 tr '\n' '\0' | lfs_migrate
314 $LFS find "$1" -type f -print0 | lfs_migrate
316 echo -en "$1\0" | lfs_migrate