#coding=utf8
import subprocess

# 执行命令
def _run_cmd(cmd, wait=False):
    prc = subprocess.Popen(args=cmd, stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE, shell=True)
    if wait:
        prc.wait()
    return prc

# 获得执行命令后的标准输出
def _get_output(cmd):
    prc = _run_cmd(cmd)
    lines = prc.stdout.readlines()
    if not lines:
        lines = prc.stderr.readlines()
    return lines


def add_disk(disk=None,**kwargs):
    path=None
    if not disk:
        for i in range('a','z'):
            res = _get_output('export LANG=en_US;fdisk -l /dev/sd{}|grep "Disk identifier:"'.format(i))
            print res
            if 'Disk identifier: 0x00000000' in ''.join(res):
                path = '/dev/sd{}'.format(i)
                break
    print path
    if not path:
        path=disk
        
    if not path:
        print path + '磁盘扩展失败'
        return
    with open('/tmp/add_自动磁盘分区脚本disk.sh 

','w') as f:
        f.write('fdisk {} <<EOF\nn\np\n1\n\n\nw\nEOF'.format(path))
    _run_cmd('sh /tmp/add_自动磁盘分区脚本disk.sh 

', wait=True)
    res = _get_output('export LANG=en_US;fdisk -l {}|grep "Disk identifier:"'.format(path))[0]
    if res!='Disk identifier: 0x00000000':
        print path + '磁盘扩展成功!'
        return

if __name__=='__main__':
    print paths
    print ''.join(paths)
    path_list=''.join(paths).split(';')
    for path in path_list:
     add_disk(path)
    #asm_conf(user=user_v, group=group_v, disk=disk_v)

相关文章:

  • 2021-12-16
  • 2021-10-27
  • 2021-12-09
  • 2022-12-23
  • 2022-02-27
  • 2021-10-14
  • 2022-12-23
猜你喜欢
  • 2021-06-16
  • 2021-07-31
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
相关资源
相似解决方案