Whamcloud - gitweb
LU-4825 utils: improve lfs_migrate usage message
[fs/lustre-release.git] / lustre / scripts / lfs_migrate
1 #!/bin/bash
2 # set -x
3 set -e
4
5 # lfs_migrate: a simple tool to copy and check files.
6 #
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.
10 #
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.
16
17 RSYNC=${RSYNC:-rsync}
18 ECHO=echo
19 LFS=${LFS:-lfs}
20 LFS_SIZE_OPT="-s"
21
22 usage() {
23     cat -- <<USAGE 1>&2
24 usage: lfs_migrate [-c <stripe_count>] [-h] [-l] [-n] [-q] [-R] [-s] [-y] [-0]
25                    [file|dir ...]
26     -c <stripe_count>
27        restripe file using the specified stripe count
28     -h show this usage message
29     -l migrate files with hard links (skip by default)
30     -n only print the names of files to be migrated
31     -q run quietly (don't print filenames or status)
32     -R restripe file using default directory striping
33     -s skip file data comparison after migrate
34     -y answer 'y' to usage question
35     -0 input file names on stdin are separated by a null character
36
37 The -c <stripe_count> option may not be specified at the same time as
38 the -R option.
39
40 If a directory is an argument, all files in the directory are migrated.
41 If no file/directory is given, the file list is read from standard input.
42
43 e.g.: lfs_migrate /mnt/lustre/dir
44       lfs find /test -O test-OST0004 -size +4G | lfs_migrate -y
45 USAGE
46     exit 1
47 }
48
49 OPT_CHECK=y
50 OPT_STRIPE_COUNT=""
51
52 while getopts "c:hlnqRsy0" opt $*; do
53     case $opt in
54         c) OPT_STRIPE_COUNT=$OPTARG;;
55         l) OPT_NLINK=y;;
56         n) OPT_DRYRUN=n; OPT_YES=y;;
57         q) ECHO=:;;
58         R) OPT_RESTRIPE=y;;
59         s) OPT_CHECK="";;
60         y) OPT_YES=y;;
61         0) OPT_NULL=y;;
62         h|\?) usage;;
63     esac
64 done
65 shift $((OPTIND - 1))
66
67 if [ "$OPT_STRIPE_COUNT" -a "$OPT_RESTRIPE" ]; then
68         echo ""
69         echo "$(basename $0) error: The -c <stripe_count> option may not" 1>&2
70         echo "be specified at the same time as the -R option." 1>&2
71         exit 1
72 fi
73
74 if [ -z "$OPT_YES" ]; then
75         echo ""
76         echo "lfs_migrate is currently NOT SAFE for moving in-use files." 1>&2
77         echo "Use it only when you are sure migrated files are unused." 1>&2
78         echo "" 1>&2
79         echo "If emptying an OST that is active on the MDS, new files may" 1>&2
80         echo "use it.  To stop allocating any new objects on OSTNNNN run:" 1>&2
81         echo "  lctl set_param osp.<fsname>-OSTNNNN*.max_create_count=0'" 1>&2
82         echo "on each MDS using the OST(s) being emptied." 1>&2
83         echo -n "Continue? (y/n) "
84         read CHECK
85         [ "$CHECK" != "y" -a "$CHECK" != "yes" ] && exit 1
86 fi
87
88 # if rsync has --xattr support, then try to copy the xattrs.
89 $RSYNC --help 2>&1 | grep -q xattr && RSYNC_OPTS="$RSYNC_OPTS -X"
90 $RSYNC --help 2>&1 | grep -q acls && RSYNC_OPTS="$RSYNC_OPTS -A"
91 # If rsync copies lustre xattrs in the future, then we can skip lfs (bug 22189)
92 strings $(which $RSYNC) 2>&1 | grep -q lustre && LFS=:
93
94 # rsync creates its temporary files with lenient permissions, even if
95 # permissions on the original files are more strict. Tighten umask here
96 # to avoid the brief window where unprivileged users might be able to
97 # access the temporary file.
98 umask 0077
99
100 # This is needed for 1.8 Interoperability and can be removed in the future
101 $LFS getstripe --help 2>&1 | grep -q stripe-size && LFS_SIZE_OPT="-S"
102
103 lfs_migrate() {
104         local RSYNC_MODE=false
105
106         while IFS='' read -d '' OLDNAME; do
107                 $ECHO -n "$OLDNAME: "
108
109                 # avoid duplicate stat if possible
110                 TYPE_LINK=($(LANG=C stat -c "%h %F" "$OLDNAME" || true))
111
112                 # skip non-regular files, since they don't have any objects
113                 # and there is no point in trying to migrate them.
114                 if [ "${TYPE_LINK[1]}" != "regular" ]; then
115                         echo -e "not a regular file, skipped"
116                         continue
117                 fi
118
119                 if [ -z "$OPT_NLINK" -a ${TYPE_LINK[0]} -gt 1 ]; then
120                         echo -e "multiple hard links, skipped"
121                         continue
122                 fi
123
124                 # working out write perms is hard, let the shell do it
125                 if [ ! -w "$OLDNAME" ]; then
126                         echo -e "no write permission, skipped"
127                         continue
128                 fi
129
130                 if [ "$OPT_DRYRUN" ]; then
131                         echo -e "dry run, skipped"
132                         continue
133                 fi
134
135                 if [ "$OPT_RESTRIPE" ]; then
136                         UNLINK=""
137                 else
138                 # if rsync copies Lustre xattrs properly in the future
139                 # (i.e. before the file data, so that it preserves striping)
140                 # then we don't need to do this getstripe/mktemp stuff.
141                         UNLINK="-u"
142
143                         [ "$OPT_STRIPE_COUNT" ] && COUNT=$OPT_STRIPE_COUNT ||
144                                 COUNT=$($LFS getstripe -c "$OLDNAME" \
145                                         2> /dev/null)
146                         SIZE=$($LFS getstripe $LFS_SIZE_OPT "$OLDNAME" \
147                                2> /dev/null)
148
149                         [ -z "$COUNT" -o -z "$SIZE" ] && UNLINK=""
150                         SIZE=${LFS_SIZE_OPT}${SIZE}
151                 fi
152
153                 # first try to migrate inside lustre
154                 # if failed go back to old rsync mode
155                 if [[ $RSYNC_MODE == false ]]; then
156                         if $LFS migrate -c${COUNT} ${SIZE} "$OLDNAME"; then
157                                 $ECHO "done"
158                                 continue
159                         else
160                                 echo "falling back to rsync-based migration"
161                                 RSYNC_MODE=true
162                         fi
163                 fi
164
165                 NEWNAME=$(mktemp $UNLINK "$OLDNAME.tmp.XXXXXX")
166                 if [ $? -ne 0 -o -z "$NEWNAME" ]; then
167                         echo -e "\r$OLDNAME: can't make temp file, skipped" 1>&2
168                         continue
169                 fi
170
171                 [ "$UNLINK" ] && $LFS setstripe -c${COUNT} ${SIZE} "$NEWNAME"
172
173                 # we use --inplace, since we created our own temp file already
174                 if ! $RSYNC -a --inplace $RSYNC_OPTS "$OLDNAME" "$NEWNAME";then
175                         echo -e "\r$OLDNAME: copy error, exiting" 1>&2
176                         rm -f "$NEWNAME"
177                         exit 4
178                 fi
179
180                 if [ "$OPT_CHECK" ] && ! cmp -s "$OLDNAME" "$NEWNAME"; then
181                         echo -e "\r$NEWNAME: compare failed, exiting" 1>&2
182                         exit 8
183                 fi
184
185                 if ! mv "$NEWNAME" "$OLDNAME"; then
186                         echo -e "\r$OLDNAME: rename error, exiting" 1>&2
187                         exit 12
188                 fi
189                 $ECHO "done"
190         done
191 }
192
193 if [ "$#" -eq 0 ]; then
194         if [ "$OPT_NULL" ]; then
195                 lfs_migrate
196         else
197                 tr '\n' '\0' | lfs_migrate
198         fi
199 else
200         while [ "$1" ]; do
201                 if [ -d "$1" ]; then
202                         lfs find "$1" -type f -print0 | lfs_migrate
203                 else
204                         echo -en "$1\0" | lfs_migrate
205                 fi
206                 shift
207         done
208 fi