tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

anita diff to use dd when creating disk images



Hi!

I used anita to easily bootstrap a NetBSD installation to
do kernel debugging within qemu, but qemu-img does not
create suitable images for vnconfig.

Here's the diff to use dd in anita.

--- anita-1.18/anita    2010-11-23 14:33:42.000000000 +0200
+++ anita/anita 2011-04-09 16:51:07.000000000 +0300
@@ -21,6 +21,10 @@
     parser.add_option("--disk-size",
                      help="use a virtual disk of SIZE bytes (k/M/G/T suffix 
accecpted)",
                      metavar="SIZE")
+    parser.add_option("--dd",
+                     help="use dd(1) instead of qemu-img(1) to create a disk 
image",
+                     action="store_false",
+                     default=True)
     parser.add_option("--run",
                      help="run COMMAND on the virtual machine after boot", 
metavar="COMMAND")
     parser.add_option("--sets",
@@ -56,7 +60,7 @@
        raise Usage("expected distribution URL or directory, got " + distarg)
 
     a = anita.Anita(dist, workdir = options.workdir, qemu_args = qemu_args,
-        disk_size = options.disk_size)
+        disk_size = options.disk_size, use_dd = options.dd)
 
     def maybe_run_command(child):
         if options.run:
--- anita-1.18/anita.py 2010-12-03 22:21:41.000000000 +0200
+++ anita/anita.py      2011-04-09 17:00:40.000000000 +0300
@@ -418,13 +418,15 @@
 
 class Anita:
     def __init__(self, dist, workdir = None, qemu_args = None,
-        disk_size = None):
+        disk_size = None, use_dd = False):
         self.dist = dist
         if workdir:
             self.workdir = workdir
         else:
             self.workdir = dist.default_workdir()
 
+        self.use_dd = use_dd
+
        # Set the default disk size if none was given.
        # 384M is sufficient for i386 but not for amd64.
         if disk_size is None:
@@ -460,6 +462,20 @@
        self.child = child
         return child
 
+    @staticmethod
+    def _size_to_dd(size):
+        m = re.match(r'(\d+)([kMGT])?', size)
+        if not m:
+            raise RuntimeError("Disk size '%s' is invalid" % self.disk_size)
+        size, suffix = m.groups()
+        mult = dict(k=1024, M=1024**2, G=1024**3, T=1024**4).get(suffix, 1)
+        nbytes = int(size) * mult
+
+        # guess block size
+        bs = 512 if nbytes < 1024**2 else 1024**2
+
+        return ["bs=%d" % bs, "count=%d" % (nbytes / bs)]
+
     def _install(self):
         # Get the install ISO
         self.dist.set_workdir(self.workdir)
@@ -469,7 +485,12 @@
        boot_from_floppy = self.dist.boot_from_floppy()
 
        # Create a disk image file
-        spawn(qemu_img, ["qemu-img", "create", self.wd0_path(), 
self.disk_size])
+        if self.use_dd:
+            spawn("dd", [
+                "dd", "if=/dev/zero", "of=%s" % self.wd0_path()
+            ] + self._size_to_dd(self.disk_size))
+        else:
+            spawn(qemu_img, ["qemu-img", "create", self.wd0_path(), 
self.disk_size])
 
         qemu_args = ["-cdrom", self.dist.iso_path()]
 
--- anita-1.18/anita.1  2010-11-23 15:30:17.000000000 +0200
+++ anita/anita.1       2011-04-09 17:07:34.000000000 +0300
@@ -9,6 +9,7 @@
 .Op Fl -workdir Ar work_directory
 .Op Fl -qemu-args Ar qemu_arguments
 .Op Fl -disk-size Ar size
+.Op Fl -dd
 .Op Fl -run Ar command
 .Op Fl -sets Ar sets
 .Ar mode
@@ -206,6 +207,13 @@
 The exit status of the shell command will be returned as the exit status
 of
 .Nm .
+.Pp
+.It Fl -dd
+Use
+.Xr dd 1
+to create disk image do it will be usable with
+.Xr vnconfig 8
+.
 .It Fl -sets Ar sets
 The distribution sets to install, as a comma-separated list.
 For a minimal install, use something like


Home | Main Index | Thread Index | Old Index