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