首页 > 解决方案 > 使用 python 创建 LVM 存储

问题描述

我正在尝试使用 python 制作一个脚本来复制虚拟机,但是我收到以下错误:

sudo lvcreate -L3072 -s -n TestPrestashop1-disk /dev/ls2018-vg/Prestashop-disk
Traceback (most recent call last):
  File "VMDuplicator.py", line 74, in <module>
    main()
  File "VMDuplicator.py", line 62, in main
    createLV(True, amount)
  File "VMDuplicator.py", line 37, in createLV
    output = call(s)
  File "/usr/lib/python2.7/subprocess.py", line 172, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

我的代码:

#create a new LVM storage and duplicate the prestashop disk into it and name it Prestashop[i]-disk / swap
def createLV(swap, amount):
    i = 1
    if(swap == False):
        while(i < int(amount)):
            try:
                output = subprocess.check_output(["sudo lvcreate -L3072 -s -n TestPrestashop" + str(i) + " -disk /dev/ls2018-vg/Prestashop-disk"])
            except subprocess.CalledProcessError, e:
                return False
            i += 1
        return true
    elif(swap == True):
        while(i < int(amount)):
            try:
                s = "sudo lvcreate -L3072 -s -n TestPrestashop" + str(i) + "-disk /dev/ls2018-vg/Prestashop-disk"
                print(s)
                output = call(s)
                #output = subprocess.check_output(["sudo lvcreate -L3072 -s -n TestPrestashop" + str(i) + "-disk /dev/ls2018-vg/Prestashop-disk"])
                #output = subprocess.check_output([s])
                #output = subprocess.check_output(["sudo lvcreate -L3072 -s -n TestPrestashop" + str(i) + "-swap /dev/ls2018-vg/Prestashop-swap"])
            except subprocess.CalledProcessError, e:
                return False
            i += 1
        return True

有谁知道这是否是在 python 中使用函数调用或子进程的正确方法?因为它适用于例如 ls -l 命令。

我还打印出执行的命令,即:sudo lvcreate -L3072 -s -n TestPrestashop1-disk /dev/ls2018-vg/Prestashop-disk(在变量 S 中)。如果我手动执行命令它工作得很好......任何建议将不胜感激!

标签: pythonxenlvm

解决方案


我用 os.system 调用修复了它。


推荐阅读