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