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