summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2013-10-03 08:36:33 -0400
committerScott Moser <smoser@ubuntu.com>2013-10-03 08:36:33 -0400
commitdb8e61e240a3a1a134078fe3cdf9f5c076064afa (patch)
tree5d56bf1b98b4280e61cfc1448a311e237006e054
parent472be9d63a6c1643acbb22b1ef556f6d3c33fc5b (diff)
downloadcloud-init-db8e61e240a3a1a134078fe3cdf9f5c076064afa.tar.gz
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.py34
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):