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