summaryrefslogtreecommitdiff
path: root/src/op_mode/format_disk.py
diff options
context:
space:
mode:
authorThomas Mangin <thomas.mangin@exa.net.uk>2020-04-05 17:59:39 +0100
committerThomas Mangin <thomas.mangin@exa.net.uk>2020-04-06 20:22:35 +0100
commitdeed5740137032f5512f21d167af9f362922c71d (patch)
treebf93da1cebaee67623b240d564c2b8c36cf57ca5 /src/op_mode/format_disk.py
parent846d0aebeac85632b3079ee1d8fd03732eba43d5 (diff)
downloadvyos-1x-deed5740137032f5512f21d167af9f362922c71d.tar.gz
vyos-1x-deed5740137032f5512f21d167af9f362922c71d.zip
util: T2226: rewrite format disk to use cmd
Diffstat (limited to 'src/op_mode/format_disk.py')
-rwxr-xr-xsrc/op_mode/format_disk.py17
1 files changed, 5 insertions, 12 deletions
diff --git a/src/op_mode/format_disk.py b/src/op_mode/format_disk.py
index 5a3b250ee..9d3797f17 100755
--- a/src/op_mode/format_disk.py
+++ b/src/op_mode/format_disk.py
@@ -17,13 +17,12 @@
import argparse
import os
import re
-import subprocess
import sys
from datetime import datetime
from time import sleep
from vyos.util import is_admin, ask_yes_no
-
+from vyos.util import run, cmd, DEVNULL
def list_disks():
disks = set()
@@ -37,10 +36,7 @@ def list_disks():
def is_busy(disk: str):
"""Check if given disk device is busy by re-reading it's partition table"""
-
- cmd = 'sudo blockdev --rereadpt /dev/{}'.format(disk)
- status = subprocess.call([cmd], shell=True, stderr=subprocess.DEVNULL)
- return status != 0
+ return run(f'sudo blockdev --rereadpt /dev/{disk}', stderr=DEVNULL) != 0
def backup_partitions(disk: str):
@@ -49,8 +45,7 @@ def backup_partitions(disk: str):
device_path = '/dev/' + disk
backup_ts = datetime.now().strftime('%Y-%m-%d-%H:%M')
backup_file = '/var/tmp/backup_{}.{}'.format(disk, backup_ts)
- cmd = 'sudo /sbin/sfdisk -d {} > {}'.format(device_path, backup_file)
- subprocess.check_call([cmd], shell=True)
+ cmd(f'sudo /sbin/sfdisk -d {device_path} > {backup_file}')
def list_partitions(disk: str):
@@ -68,13 +63,11 @@ def list_partitions(disk: str):
def delete_partition(disk: str, partition_idx: int):
- cmd = 'sudo /sbin/parted /dev/{} rm {}'.format(disk, partition_idx)
- subprocess.check_call([cmd], shell=True)
+ cmd(f'sudo /sbin/parted /dev/{disk} rm {partition_idx}')
def format_disk_like(target: str, proto: str):
- cmd = 'sudo /sbin/sfdisk -d /dev/{} | sudo /sbin/sfdisk --force /dev/{}'.format(proto, target)
- subprocess.check_call([cmd], shell=True)
+ cmd(f'sudo /sbin/sfdisk -d /dev/{proto} | sudo /sbin/sfdisk --force /dev/{target}')
if __name__ == '__main__':