diff options
author | Scott Moser <smoser@ubuntu.com> | 2013-10-03 08:36:33 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2013-10-03 08:36:33 -0400 |
commit | 5bce3c2160e3b1b367e8bf3e33254847f31363b0 (patch) | |
tree | 5d56bf1b98b4280e61cfc1448a311e237006e054 | |
parent | 9f8822edfa5c1737933b83db54a3aa3143aaa280 (diff) | |
download | vyos-cloud-init-5bce3c2160e3b1b367e8bf3e33254847f31363b0.tar.gz vyos-cloud-init-5bce3c2160e3b1b367e8bf3e33254847f31363b0.zip |
use native python code for wiping partition table
I'm pretty sure the previous code wasn't seeking correctly
and probably writing near the end, but not to the end.
This is simpler and probably faster.
-rw-r--r-- | cloudinit/config/cc_disk_setup.py | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/cloudinit/config/cc_disk_setup.py b/cloudinit/config/cc_disk_setup.py index c9885d09..4abaf13d 100644 --- a/cloudinit/config/cc_disk_setup.py +++ b/cloudinit/config/cc_disk_setup.py @@ -19,6 +19,7 @@ from cloudinit.settings import PER_INSTANCE from cloudinit import util import logging +import os import shlex frequency = PER_INSTANCE @@ -471,6 +472,20 @@ def get_partition_mbr_layout(size, layout): return sfdisk_definition +def purge_disk_ptable(device): + # wipe the first and last megabyte of a disk (or file) + # gpt stores partition table both at front and at end. + start_len = 1024 * 1024 + end_len = 1024 * 1024 + with open(device, "rb+") as fp: + fp.write('\0' * (start_len)) + fp.seek(-end_len, os.SEEK_END) + fp.write('\0' * end_len) + fp.flush() + + read_parttbl(device) + + def purge_disk(device): """ Remove parition table entries @@ -488,24 +503,7 @@ def purge_disk(device): except Exception as e: raise Exception("Failed FS purge of /dev/%s" % d['name']) - dd_cmd = util.which("dd") - last_seek = int(get_hdd_size(device) / 1024) - 2 - first_mb = [dd_cmd, "if=/dev/zero", "of=%s" % device, "bs=1M", "count=1"] - last_mb = [dd_cmd, "if=/dev/zero", "of=%s" % device, "bs=1M", "seek=%s" % last_seek] - try: - util.subp(first_mb) - LOG.info("Purged MBR/Partition table from %s" % device) - util.subp(last_mb, rcs=[0, 1]) - LOG.info("Purged any chance of GPT table from %s" % device) - - # Wipe it for good measure - wipefs_cmd = [WIPEFS_CMD, "--all", device] - util.subp(wipefs_cmd) - except Exception as e: - LOG.critical(e) - raise Exception("Failed to remove MBR/Part from %s" % device) - - read_parttbl(device) + purge_disk_ptable(device) def get_partition_layout(table_type, size, layout): |