5 # lfs_migrate: a simple tool to copy and check files.
7 # To avoid allocating objects on one or more OSTs, they should be
8 # deactivated on the MDS via "lctl --device {device_number} deactivate",
9 # where {device_number} is from the output of "lctl dl" on the MDS.
11 # To guard against corruption, the file is compared after migration
12 # to verify the copy is correct and the file has not been modified.
13 # This is not a protection against the file being open by another
14 # process, but it would catch the worst cases of in-use files, but
15 # to be 100% safe the administrator needs to ensure this is safe.
23 usage: lfs_migrate [-c|-s] [-h] [-l] [-n] [-y] [file|dir ...]
24 -c compare file data after migrate (default)
25 -s skip file data comparison after migrate
26 -h show this usage message
27 -l migrate files with hard links (skip by default)
28 -n only print the names of files to be migrated
29 -q run quietly (don't print filenames or status)
30 -y answer 'y' to usage question
32 If a directory is an argument, all files in the directory are migrated.
33 If no file/directory is given, the file list is read from standard input.
35 e.g.: lfs_migrate /mnt/lustre/file
36 lfs find /test -O test-OST0004 -size +4G | lfs_migrate -y
43 while getopts "chlnqsy" opt $*; do
47 n) OPT_DRYRUN=n; OPT_YES=y;;
56 if [ -z "$OPT_YES" ]; then
58 echo "lfs_migrate is currently NOT SAFE for moving in-use files." 1>&2
59 echo "Use it only when you are sure migrated files are unused." 1>&2
61 echo "If emptying OST(s) that are not disabled on the MDS, new" 1>&2
62 echo "files may use them. To prevent MDS allocating any files on" 1>&2
63 echo "OSTNNNN run 'lctl --device %{fsname}-OSTNNNN-osc deactivate'" 1>&2
64 echo "on the MDS." 1>&2
65 echo -n "Continue? (y/n) "
67 [ "$CHECK" != "y" -a "$CHECK" != "yes" ] && exit 1
70 # if rsync has --xattr support, then try to copy the xattrs.
71 $RSYNC --help 2>&1 | grep -q xattr && RSYNC_OPTS="$RSYNC_OPTS -X"
72 $RSYNC --help 2>&1 | grep -q acls && RSYNC_OPTS="$RSYNC_OPTS -A"
73 # If rsync copies lustre xattrs in the future, then we can skip lfs (bug 22189)
74 strings $(which $RSYNC) 2>&1 | grep -q lustre && LFS=:
77 while read OLDNAME; do
80 # avoid duplicate stat if possible
81 TYPE_LINK=($(stat -c "%h %F" "$OLDNAME" || true))
83 # skip non-regular files, since they don't have any objects
84 # and there is no point in trying to migrate them.
85 if [ "${TYPE_LINK[1]}" != "regular" ]; then
86 echo -e "not a regular file, skipped"
90 if [ -z "$OPT_NLINK" -a ${TYPE_LINK[0]} -gt 1 ]; then
91 echo -e "multiple hard links, skipped"
95 # working out write perms is hard, let the shell do it
96 if [ ! -w "$OLDNAME" ]; then
97 echo -e "no write permission, skipped"
101 if [ "$OPT_DRYRUN" ]; then
102 echo -e "dry run, skipped"
107 # if rsync copies Lustre xattrs properly in the future
108 # (i.e. before the file data, so that it preserves striping)
109 # then we don't need to do this getstripe/mktemp stuff.
111 COUNT=$($LFS getstripe -c "$OLDNAME" 2> /dev/null)
112 SIZE=$($LFS getstripe -s "$OLDNAME" 2> /dev/null)
113 [ -z "$COUNT" -o -z "$SIZE" ] && UNLINK=""
114 NEWNAME=$(mktemp $UNLINK "$OLDNAME.tmp.XXXXXX")
115 if [ $? -ne 0 -o -z "$NEWNAME" ]; then
116 echo -e "\r$OLDNAME: can't make temp file, skipped" 1>&2
120 [ "$UNLINK" ] && $LFS setstripe -c${COUNT} -s${SIZE} "$NEWNAME"
122 # we use --inplace, since we created our own temp file already
123 if ! $RSYNC -a --inplace $RSYNC_OPTS "$OLDNAME" "$NEWNAME";then
124 echo -e "\r$OLDNAME: copy error, exiting" 1>&2
129 if [ "$OPT_CHECK" ] && ! cmp "$OLDNAME" "$NEWNAME"; then
130 echo -e "\r$NEWNAME: compare failed, exiting" 1>&2
134 if ! mv "$NEWNAME" "$OLDNAME"; then
135 echo -e "\r$OLDNAME: rename error, exiting" 1>&2
142 if [ "$#" -eq 0 ]; then
147 lfs find "$1" -type f | lfs_migrate
149 echo $1 | lfs_migrate