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