/system-config-lvm-1.1.17/src/CommandHandler.py

# · Python · 430 lines · 380 code · 42 blank · 8 comment · 65 complexity · 5f923a70d08caec84ca93ba2d04fd96c MD5 · raw file

  1. import os
  2. import string
  3. from CommandError import CommandError
  4. from execute import execWithCapture, execWithCaptureErrorStatus, execWithCaptureStatus, execWithCaptureProgress, execWithCaptureErrorStatusProgress, execWithCaptureStatusProgress
  5. from lvmui_constants import *
  6. import gettext
  7. _ = gettext.gettext
  8. class CommandHandler:
  9. def __init__(self):
  10. pass
  11. def new_lv(self, cmd_args_dict, pvlist=[]):
  12. #first set up lvcreate args
  13. arglist = list()
  14. arglist.append(LVCREATE_BIN_PATH)
  15. arglist.append("-n")
  16. lvname = cmd_args_dict[NEW_LV_NAME_ARG]
  17. arglist.append(lvname)
  18. if cmd_args_dict[NEW_LV_UNIT_ARG] == EXTENT_IDX:
  19. arglist.append("-l")
  20. arglist.append(str(cmd_args_dict[NEW_LV_SIZE_ARG]))
  21. else:
  22. arglist.append("-L")
  23. if cmd_args_dict[NEW_LV_UNIT_ARG] == KILOBYTE_IDX:
  24. arglist.append(str(cmd_args_dict[NEW_LV_SIZE_ARG]) + "k")
  25. elif cmd_args_dict[NEW_LV_UNIT_ARG] == MEGABYTE_IDX:
  26. arglist.append(str(cmd_args_dict[NEW_LV_SIZE_ARG]) + "m")
  27. elif cmd_args_dict[NEW_LV_UNIT_ARG] == GIGABYTE_IDX:
  28. arglist.append(str(cmd_args_dict[NEW_LV_SIZE_ARG]) + "g")
  29. if cmd_args_dict[NEW_LV_SNAPSHOT]:
  30. arglist.append("-s")
  31. arglist.append(cmd_args_dict[NEW_LV_SNAPSHOT_ORIGIN])
  32. else:
  33. if cmd_args_dict[NEW_LV_MIRRORING]:
  34. arglist.append("-m1")
  35. if cmd_args_dict[NEW_LV_IS_STRIPED_ARG] == True:
  36. arglist.append("-i")
  37. arglist.append(str(cmd_args_dict[NEW_LV_NUM_STRIPES_ARG]))
  38. arglist.append("-I")
  39. arglist.append(str(cmd_args_dict[NEW_LV_STRIPE_SIZE_ARG]))
  40. vgname = cmd_args_dict[NEW_LV_VGNAME_ARG]
  41. arglist.append(vgname)
  42. for pv in pvlist:
  43. arglist.append(pv.get_path())
  44. cmd_str = ' '.join(arglist)
  45. result_string,err,res = execWithCaptureErrorStatusProgress(LVCREATE_BIN_PATH, arglist,
  46. _("Creating Logical Volume"))
  47. if res != 0:
  48. raise CommandError('FATAL', COMMAND_FAILURE % ("lvcreate",cmd_str,err))
  49. def reduce_lv(self, lvpath, new_size_extents, test=False):
  50. cmd_args = list()
  51. cmd_args.append(LVREDUCE_BIN_PATH)
  52. cmd_args.append("--config")
  53. cmd_args.append("'log{command_names=0}'")
  54. if test:
  55. cmd_args.append('--test')
  56. cmd_args.append('-f')
  57. cmd_args.append('-l')
  58. cmd_args.append(str(new_size_extents))
  59. cmd_args.append(lvpath)
  60. cmdstr = ' '.join(cmd_args)
  61. if test:
  62. out,err,res = execWithCaptureErrorStatus(LVREDUCE_BIN_PATH, cmd_args)
  63. return (res == 0)
  64. out,err,res = execWithCaptureErrorStatusProgress(LVREDUCE_BIN_PATH, cmd_args,
  65. _("Resizing Logical Volume"))
  66. if res != 0:
  67. raise CommandError('FATAL', COMMAND_FAILURE % ("lvresize",cmdstr,err))
  68. def extend_lv(self, lvpath, new_size_extents, test=False):
  69. cmd_args = list()
  70. cmd_args.append(LVEXTEND_BIN_PATH)
  71. cmd_args.append("--config")
  72. cmd_args.append("'log{command_names=0}'")
  73. if test:
  74. cmd_args.append('--test')
  75. cmd_args.append('-l')
  76. cmd_args.append(str(new_size_extents))
  77. cmd_args.append(lvpath)
  78. cmdstr = ' '.join(cmd_args)
  79. if test:
  80. out,err,res = execWithCaptureErrorStatus(LVEXTEND_BIN_PATH, cmd_args)
  81. return (res == 0)
  82. out,err,res = execWithCaptureErrorStatusProgress(LVEXTEND_BIN_PATH, cmd_args,
  83. _("Resizing Logical Volume"))
  84. if res != 0:
  85. raise CommandError('FATAL', COMMAND_FAILURE % ("lvresize",cmdstr,err))
  86. def activate_lv(self, lvpath):
  87. cmd_args = list()
  88. cmd_args.append(LVCHANGE_BIN_PATH)
  89. cmd_args.append("--config")
  90. cmd_args.append("'log{command_names=0}'")
  91. cmd_args.append('-ay')
  92. cmd_args.append(lvpath)
  93. cmdstr = ' '.join(cmd_args)
  94. out,err,res = execWithCaptureErrorStatus(LVCHANGE_BIN_PATH, cmd_args)
  95. if res != 0:
  96. raise CommandError('FATAL', COMMAND_FAILURE % ("lvchange",cmdstr,err))
  97. def deactivate_lv(self, lvpath):
  98. cmd_args = list()
  99. cmd_args.append(LVCHANGE_BIN_PATH)
  100. cmd_args.append("--config")
  101. cmd_args.append("'log{command_names=0}'")
  102. cmd_args.append('-an')
  103. cmd_args.append(lvpath)
  104. cmdstr = ' '.join(cmd_args)
  105. out,err,res = execWithCaptureErrorStatus(LVCHANGE_BIN_PATH, cmd_args)
  106. if res != 0:
  107. raise CommandError('FATAL', COMMAND_FAILURE % ("lvchange",cmdstr,err))
  108. def add_mirroring(self, lvpath, pvlist=[]):
  109. cmd_args = list()
  110. cmd_args.append(LVCONVERT_BIN_PATH)
  111. cmd_args.append("--config")
  112. cmd_args.append("'log{command_names=0}'")
  113. cmd_args.append('-m1')
  114. cmd_args.append(lvpath)
  115. for pv in pvlist:
  116. cmd_args.append(pv.get_path())
  117. cmdstr = ' '.join(cmd_args)
  118. out,err,res = execWithCaptureErrorStatusProgress(LVCONVERT_BIN_PATH, cmd_args,
  119. _("Adding Mirror to Logical Volume"))
  120. if res != 0:
  121. raise CommandError('FATAL', COMMAND_FAILURE % ("lvconvert",cmdstr,err))
  122. def remove_mirroring(self, lvpath):
  123. cmd_args = list()
  124. cmd_args.append(LVCONVERT_BIN_PATH)
  125. cmd_args.append("--config")
  126. cmd_args.append("'log{command_names=0}'")
  127. cmd_args.append('-m0')
  128. cmd_args.append(lvpath)
  129. cmdstr = ' '.join(cmd_args)
  130. out,err,res = execWithCaptureErrorStatusProgress(LVCONVERT_BIN_PATH, cmd_args,
  131. _("Removing Mirror from Logical Volume"))
  132. if res != 0:
  133. raise CommandError('FATAL', COMMAND_FAILURE % ("lvconvert",cmdstr,err))
  134. def mount(self, dev_path, mnt_point, fstype):
  135. cmd_args = list()
  136. cmd_args.append("/bin/mount")
  137. cmd_args.append('-t')
  138. cmd_args.append(fstype)
  139. cmd_args.append(dev_path)
  140. cmd_args.append(mnt_point)
  141. cmdstr = ' '.join(cmd_args)
  142. out,err,res = execWithCaptureErrorStatus("/bin/mount",cmd_args)
  143. if res != 0:
  144. raise CommandError('FATAL', COMMAND_FAILURE % ("mount",cmdstr,err))
  145. def initialize_entity(self, ent):
  146. entity = ent.strip()
  147. command_args = list()
  148. command_args.append(PVCREATE_BIN_PATH)
  149. command_args.append("-M")
  150. command_args.append("2")
  151. command_args.append(entity)
  152. commandstring = ' '.join(command_args)
  153. out,err,res = execWithCaptureErrorStatusProgress(PVCREATE_BIN_PATH,command_args,
  154. _("Initializing Physical Volume"))
  155. if res != 0:
  156. raise CommandError('FATAL', COMMAND_FAILURE % ("pvcreate",commandstring,err))
  157. def add_unalloc_to_vg(self, pv, vg):
  158. args = list()
  159. args.append(VGEXTEND_BIN_PATH)
  160. args.append("--config")
  161. args.append("'log{command_names=0}'")
  162. args.append(vg.strip())
  163. args.append(pv.strip())
  164. cmdstr = ' '.join(args)
  165. out,err,res = execWithCaptureErrorStatusProgress(VGEXTEND_BIN_PATH, args,
  166. _("Adding Physical Volume to Volume Group"))
  167. if res != 0:
  168. raise CommandError('FATAL', COMMAND_FAILURE % ("vgextend",cmdstr,err))
  169. def create_new_vg(self, name, max_phys, max_log, extent_size, is_unit_megs,
  170. pv, clustered=False):
  171. if is_unit_megs:
  172. units_arg = 'm'
  173. else:
  174. units_arg = 'k'
  175. size_arg = extent_size + units_arg
  176. args = list()
  177. args.append(VGCREATE_BIN_PATH)
  178. args.append("-M2")
  179. args.append("-l")
  180. args.append(max_log)
  181. args.append("-p")
  182. args.append(max_phys)
  183. args.append("-s")
  184. args.append(size_arg)
  185. args.append('-c')
  186. if clustered:
  187. args.append('y')
  188. else:
  189. args.append('n')
  190. args.append(name.strip())
  191. args.append(pv.strip())
  192. cmdstr = ' '.join(args)
  193. out,err,res = execWithCaptureErrorStatusProgress(VGCREATE_BIN_PATH, args,
  194. _("Creating Volume Group"))
  195. if res != 0:
  196. raise CommandError('FATAL', COMMAND_FAILURE % ("vgcreate",cmdstr,err))
  197. def remove_vg(self, vgname):
  198. args = list()
  199. args.append(VGCHANGE_BIN_PATH)
  200. args.append("--config")
  201. args.append("'log{command_names=0}'")
  202. args.append("-a")
  203. args.append("n")
  204. args.append(vgname.strip())
  205. cmdstr = ' '.join(args)
  206. out,err,res = execWithCaptureErrorStatus(VGCHANGE_BIN_PATH,args)
  207. if res != 0:
  208. raise CommandError('FATAL', COMMAND_FAILURE % ("vgchange",cmdstr,err))
  209. return
  210. commandstring = VGREMOVE_BIN_PATH + " " + vgname
  211. args_list = list()
  212. args_list.append(VGREMOVE_BIN_PATH)
  213. args_list.append("--config")
  214. args_list.append("'log{command_names=0}'")
  215. args_list.append(vgname)
  216. cmdstring = ' '.join(args)
  217. outs,errs,result = execWithCaptureErrorStatusProgress(VGREMOVE_BIN_PATH,args_list,
  218. _("Removing Volume Group"))
  219. if result != 0:
  220. raise CommandError('FATAL', COMMAND_FAILURE % ("vgremove",cmdstring,errs))
  221. def remove_pv(self, pvname):
  222. args = list()
  223. args.append(PVREMOVE_BIN_PATH)
  224. args.append("--config")
  225. args.append("'log{command_names=0}'")
  226. args.append(pvname.strip())
  227. cmdstr = ' '.join(args)
  228. out,err,res = execWithCaptureErrorStatusProgress(PVREMOVE_BIN_PATH,args,
  229. _("Removing Physical Volume"))
  230. if res != 0:
  231. raise CommandError('FATAL', COMMAND_FAILURE % ("pvremove",cmdstr,err))
  232. def remove_lv(self, lvpath):
  233. if self.is_snapshot(lvpath) == False:
  234. self.deactivate_lv(lvpath)
  235. try:
  236. args = list()
  237. args.append(LVREMOVE_BIN_PATH)
  238. args.append("--config")
  239. args.append("'log{command_names=0}'")
  240. args.append("--force")
  241. args.append(lvpath.strip())
  242. cmdstr = ' '.join(args)
  243. out,err,res = execWithCaptureErrorStatusProgress(LVREMOVE_BIN_PATH,args,
  244. _("Removing Logical Volume"))
  245. if res != 0:
  246. raise CommandError('FATAL', COMMAND_FAILURE % ("lvremove",cmdstr,err))
  247. except CommandError, e:
  248. self.activate_lv(lvpath)
  249. raise e
  250. def rename_lv(self, vgname, lvname_old, lvname_new):
  251. args = list()
  252. args.append(LVRENAME_BIN_PATH)
  253. args.append("--config")
  254. args.append("'log{command_names=0}'")
  255. args.append(vgname)
  256. args.append(lvname_old)
  257. args.append(lvname_new)
  258. cmdstr = ' '.join(args)
  259. out,err,res = execWithCaptureErrorStatusProgress(LVRENAME_BIN_PATH,args,
  260. _("Renaming Logical Volume"))
  261. if res != 0:
  262. raise CommandError('FATAL', COMMAND_FAILURE % ("lvrename",cmdstr,err))
  263. def unmount(self, mountpoint):
  264. args = ['/bin/umount']
  265. args.append(mountpoint)
  266. cmdstr = ' '.join(args)
  267. out,err,res = execWithCaptureErrorStatus("/bin/umount", args)
  268. if res != 0:
  269. raise CommandError('FATAL', COMMAND_FAILURE % ("umount",cmdstr, err))
  270. # unmount all with mountpoint
  271. while res == 0:
  272. out,err,res = execWithCaptureErrorStatus("/bin/umount", args)
  273. def reduce_vg(self, vg, pv):
  274. args = list()
  275. args.append(VGREDUCE_BIN_PATH)
  276. args.append("--config")
  277. args.append("'log{command_names=0}'")
  278. args.append(vg.strip())
  279. args.append(pv.strip())
  280. cmdstr = ' '.join(args)
  281. out,err,res = execWithCaptureErrorStatusProgress(VGREDUCE_BIN_PATH,args,
  282. _("Removing Physical Volume from Volume Group"))
  283. if res != 0:
  284. raise CommandError('FATAL', COMMAND_FAILURE % ("vgreduce",cmdstr,err))
  285. # data = [pv to migrate to, policy (0 - inherit, 1 - normal, 2 - contiguous, 3 - anywhere), lv_path to migrate from]
  286. # extents_from = [(start, size), ...]
  287. def move_pv(self, pv, extents_from, data, background=False):
  288. args = list()
  289. args.append(PVMOVE_BIN_PATH)
  290. args.append("--config")
  291. args.append("'log{command_names=0}'")
  292. # policy
  293. if data[1] != None:
  294. if data[1] == 0:
  295. args.append('--alloc inherit')
  296. elif data[1] == 1:
  297. args.append('--alloc normal')
  298. elif data[1] == 2:
  299. args.append('--alloc contiguous')
  300. elif data[1] == 3:
  301. args.append('--alloc anywhere')
  302. # lv to migrate from
  303. if data[2] != None:
  304. args.append('--name ' + data[2])
  305. # pv to migrate from
  306. pv_from = pv.strip()
  307. for (start, size) in extents_from:
  308. pv_from = pv_from + ':' + str(start) + '-' + str(start + size - 1)
  309. args.append(pv_from)
  310. # pv to migrate to
  311. if data[0] != None:
  312. args.append(data[0])
  313. if background:
  314. args.append('--background')
  315. out, err, res = execWithCaptureErrorStatus(PVMOVE_BIN_PATH, args)
  316. else:
  317. out, err, res = execWithCaptureErrorStatusProgress(PVMOVE_BIN_PATH, args,
  318. _("Migrating Extents"))
  319. if res != 0:
  320. cmdstr = ' '.join(args)
  321. raise CommandError('FATAL', COMMAND_FAILURE % ("pvmove",cmdstr, err))
  322. def complete_pvmove(self, background=False):
  323. args = [PVMOVE_BIN_PATH]
  324. args.append("--config")
  325. args.append("'log{command_names=0}'")
  326. if background:
  327. args.append('--background')
  328. out, err, res = execWithCaptureErrorStatus(PVMOVE_BIN_PATH, args)
  329. else:
  330. out, err, res = execWithCaptureErrorStatusProgress(PVMOVE_BIN_PATH, args,
  331. _("Completing Extent Migration"))
  332. if res != 0:
  333. cmdstr = ' '.join(args)
  334. raise CommandError('FATAL', COMMAND_FAILURE % ("pvmove",cmdstr, err))
  335. def is_dm_mirror_loaded(self):
  336. arglist = list()
  337. arglist.append("/sbin/dmsetup")
  338. arglist.append("targets")
  339. result = execWithCapture("/sbin/dmsetup", arglist)
  340. textlines = result.splitlines()
  341. for textline in textlines:
  342. text_words = textline.split()
  343. possible_target = text_words[0].strip()
  344. if possible_target == "mirror":
  345. return True
  346. return False
  347. def is_dm_mirror_loaded(self):
  348. arglist = list()
  349. arglist.append("/sbin/dmsetup")
  350. arglist.append("targets")
  351. result = execWithCapture("/sbin/dmsetup", arglist)
  352. textlines = result.splitlines()
  353. for textline in textlines:
  354. text_words = textline.split()
  355. possible_target = text_words[0].strip()
  356. if possible_target == "mirror":
  357. return True
  358. return False
  359. def is_dm_snapshot_loaded(self):
  360. arglist = list()
  361. arglist.append("/sbin/dmsetup")
  362. arglist.append("targets")
  363. result = execWithCapture("/sbin/dmsetup", arglist)
  364. textlines = result.splitlines()
  365. for textline in textlines:
  366. text_words = textline.split()
  367. possible_target = text_words[0].strip()
  368. if possible_target == "snapshot":
  369. return True
  370. return False
  371. def reread_partition_table(self, devpath):
  372. BLOCKDEV_BIN = '/sbin/blockdev'
  373. args = [BLOCKDEV_BIN, '--rereadpt', devpath]
  374. out, err, status = execWithCaptureErrorStatus(BLOCKDEV_BIN, args)
  375. if status != 0:
  376. return False
  377. execWithCaptureProgress('sleep', ['sleep', '1'], _('Rereading partition table'))
  378. return True
  379. def is_snapshot(self, lvpath):
  380. arglist = list()
  381. arglist = [LVM_BIN_PATH, 'lvs', '--config', "'log{command_names=0}'", "-o", "attr", "--noheadings", lvpath]
  382. out, err, status = execWithCaptureErrorStatus(LVM_BIN_PATH, arglist)
  383. if status != 0:
  384. return False
  385. if out.lstrip().startswith("s") == True:
  386. return True
  387. else:
  388. return False