OSDN Git Service

Recreate Storagepool (#3)
[karesansui/karesansui.git] / bin / ready_mount.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 2010 HDE, Inc.
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13
14 import os
15 import sys
16 import logging
17 import fcntl
18 from optparse import OptionParser
19
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
21 import __cmd__
22
23 try:
24     import karesansui
25     from karesansui import __version__
26     from karesansui.lib.utils import load_locale, execute_command, pipe_execute_command, generate_phrase
27     from karesansui.lib.const import KARESANSUI_TMP_DIR, MOUNT_CMD, UMOUNT_CMD, FORMAT_CMD, YES_CMD
28
29 except ImportError:
30     print >>sys.stderr, "[Error] karesansui package was not found."
31     sys.exit(1)
32
33 _ = load_locale()
34
35 usage = '%prog [options]'
36
37 def getopts():
38     optp = OptionParser(usage=usage, version=__version__)
39     optp.add_option('-d', '--dev', dest='dev', help=_('Target device'), default=None)
40     optp.add_option('-t', '--type', dest='type', help=_('Format type'), default="ext3")
41     optp.add_option('-f', '--format', dest='format', action="store_true", help=_('Format on mount failed'), default=False)
42     return optp.parse_args()
43
44 def chkopts(opts):
45     if not opts.dev:
46         raise KssCommandOptException('ERROR: %s option is required.' % '-d or --dev')
47
48     #TODO
49     # check fs format type
50     # hint.
51     #  /proc/filesystems
52     #    or
53     #  modprobe -l -t kernel/fs
54
55 class ReadyMount(KssCommand):
56
57     def process(self):
58         (opts, args) = getopts()
59         chkopts(opts)
60         self.up_progress(10)
61
62         try:
63             tmp_dir_name = generate_phrase(12,'abcdefghijklmnopqrstuvwxyz')
64             tmp_dir_path = "%s/%s" % (KARESANSUI_TMP_DIR, tmp_dir_name)
65             os.mkdir(tmp_dir_path)
66         except Exception, e:
67             self.logger.error('Failed to make tmpdir. path=%s' % (tmp_dir_path))
68             print >>sys.stderr, _('Failed to make tmpdir. path=%s' % (tmp_dir_path))
69             raise
70
71         self.up_progress(10)
72         mount_command_args = (MOUNT_CMD,
73                               opts.dev,
74                               tmp_dir_path,
75                               )
76         umount_command_args = (UMOUNT_CMD,
77                                tmp_dir_path,
78                                )
79
80         is_mountable = False
81         try:
82             (mount_cmd_rc, mount_cmd_res) = execute_command(mount_command_args)
83             if mount_cmd_rc == 0:
84                 is_mountable = True
85             else:
86                 self.logger.debug('Failed to mount. dev=%s' % (opts.dev))
87         finally:
88             (umount_cmd_rc, umount_cmd_res) = execute_command(umount_command_args)
89
90         self.up_progress(30)
91         import pdb; pdb.set_trace()
92         if is_mountable is False and opts.format is True:
93             first_command_args = (YES_CMD,
94                                   )
95             second_command_args = (FORMAT_CMD,
96                                    "-t",
97                                    opts.type,
98                                    opts.dev,
99                                    )
100             format_command_args = (first_command_args,
101                                    second_command_args,
102                                    )
103
104             (format_cmd_rc, format_cmd_res) = pipe_execute_command(format_command_args)
105             if format_cmd_rc != 0:
106                 self.logger.error('Failed to format. dev=%s type=%s res=%s' % (opts.dev, opts.type, format_cmd_res))
107                 print >>sys.stderr, _('Failed to format. dev=%s type=%s res=%s' % (opts.dev, opts.type, format_cmd_res))
108
109             try:
110                 (mount_cmd_rc, mount_cmd_res) = execute_command(mount_command_args)
111                 if mount_cmd_rc == 0:
112                     is_mountable = True
113                 else:
114                     self.logger.debug('Failed to mount. dev=%s' % (opts.dev))
115             finally:
116                 (umount_cmd_rc, umount_cmd_res) = execute_command(umount_command_args)
117
118         self.up_progress(40)
119         try:
120             os.rmdir(tmp_dir_path)
121         except Exception, e:
122             self.logger.error('Failed to delete tmpdir. path=%s' % (tmp_dir_path))
123             print >>sys.stderr, _('Failed to delete tmpdir. path=%s' % (tmp_dir_path))
124             raise
125
126         return is_mountable
127
128 if __name__ == "__main__":
129     target = ReadyMount()
130     sys.exit(target.run())