2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui.
6 # Copyright (C) 2010 HDE, Inc.
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.
18 from optparse import OptionParser
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
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
30 print >>sys.stderr, "[Error] karesansui package was not found."
35 usage = '%prog [options]'
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()
46 raise KssCommandOptException('ERROR: %s option is required.' % '-d or --dev')
49 # check fs format type
53 # modprobe -l -t kernel/fs
55 class ReadyMount(KssCommand):
58 (opts, args) = getopts()
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)
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))
72 mount_command_args = (MOUNT_CMD,
76 umount_command_args = (UMOUNT_CMD,
82 (mount_cmd_rc, mount_cmd_res) = execute_command(mount_command_args)
86 self.logger.debug('Failed to mount. dev=%s' % (opts.dev))
88 (umount_cmd_rc, umount_cmd_res) = execute_command(umount_command_args)
91 import pdb; pdb.set_trace()
92 if is_mountable is False and opts.format is True:
93 first_command_args = (YES_CMD,
95 second_command_args = (FORMAT_CMD,
100 format_command_args = (first_command_args,
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))
110 (mount_cmd_rc, mount_cmd_res) = execute_command(mount_command_args)
111 if mount_cmd_rc == 0:
114 self.logger.debug('Failed to mount. dev=%s' % (opts.dev))
116 (umount_cmd_rc, umount_cmd_res) = execute_command(umount_command_args)
120 os.rmdir(tmp_dir_path)
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))
128 if __name__ == "__main__":
129 target = ReadyMount()
130 sys.exit(target.run())