582 lines
87 KiB
HTML
582 lines
87 KiB
HTML
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>pylorax.imgutils — Lorax 19.7.43 documentation</title>
|
|
|
|
<link rel="stylesheet" href="../../_static/default.css" type="text/css" />
|
|
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '../../',
|
|
VERSION: '19.7.43',
|
|
COLLAPSE_INDEX: false,
|
|
FILE_SUFFIX: '.html',
|
|
HAS_SOURCE: true
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="../../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../../_static/doctools.js"></script>
|
|
<link rel="top" title="Lorax 19.7.43 documentation" href="../../index.html" />
|
|
<link rel="up" title="pylorax" href="../pylorax.html" />
|
|
</head>
|
|
<body>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li><a href="../../index.html">Lorax 19.7.43 documentation</a> »</li>
|
|
<li><a href="../index.html" >Module code</a> »</li>
|
|
<li><a href="../pylorax.html" accesskey="U">pylorax</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body">
|
|
|
|
<h1>Source code for pylorax.imgutils</h1><div class="highlight"><pre>
|
|
<span class="c"># imgutils.py - utility functions/classes for building disk images</span>
|
|
<span class="c">#</span>
|
|
<span class="c"># Copyright (C) 2011 Red Hat, Inc.</span>
|
|
<span class="c">#</span>
|
|
<span class="c"># This program is free software; you can redistribute it and/or modify</span>
|
|
<span class="c"># it under the terms of the GNU General Public License as published by</span>
|
|
<span class="c"># the Free Software Foundation; either version 2 of the License, or</span>
|
|
<span class="c"># (at your option) any later version.</span>
|
|
<span class="c">#</span>
|
|
<span class="c"># This program is distributed in the hope that it will be useful,</span>
|
|
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
|
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
|
<span class="c"># GNU General Public License for more details.</span>
|
|
<span class="c">#</span>
|
|
<span class="c"># You should have received a copy of the GNU General Public License</span>
|
|
<span class="c"># along with this program. If not, see <http://www.gnu.org/licenses/>.</span>
|
|
<span class="c">#</span>
|
|
<span class="c"># Author(s): Will Woods <wwoods@redhat.com></span>
|
|
|
|
<span class="kn">import</span> <span class="nn">logging</span>
|
|
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">"pylorax.imgutils"</span><span class="p">)</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">tempfile</span>
|
|
<span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">join</span><span class="p">,</span> <span class="n">dirname</span>
|
|
<span class="kn">from</span> <span class="nn">subprocess</span> <span class="kn">import</span> <span class="n">Popen</span><span class="p">,</span> <span class="n">PIPE</span><span class="p">,</span> <span class="n">CalledProcessError</span>
|
|
<span class="kn">import</span> <span class="nn">sys</span>
|
|
<span class="kn">import</span> <span class="nn">time</span>
|
|
<span class="kn">import</span> <span class="nn">traceback</span>
|
|
<span class="kn">import</span> <span class="nn">multiprocessing</span>
|
|
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">sleep</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">pylorax.sysutils</span> <span class="kn">import</span> <span class="n">cpfile</span>
|
|
<span class="kn">from</span> <span class="nn">pylorax.executils</span> <span class="kn">import</span> <span class="n">execWithRedirect</span><span class="p">,</span> <span class="n">execWithCapture</span>
|
|
<span class="kn">from</span> <span class="nn">pylorax.executils</span> <span class="kn">import</span> <span class="n">runcmd</span><span class="p">,</span> <span class="n">runcmd_output</span>
|
|
|
|
<span class="c">######## Functions for making container images (cpio, tar, squashfs) ##########</span>
|
|
|
|
<div class="viewcode-block" id="compress"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.compress">[docs]</a><span class="k">def</span> <span class="nf">compress</span><span class="p">(</span><span class="n">command</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">compression</span><span class="o">=</span><span class="s">"xz"</span><span class="p">,</span> <span class="n">compressargs</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="sd">'''Make a compressed archive of the given rootdir.</span>
|
|
<span class="sd"> command is a list of the archiver commands to run</span>
|
|
<span class="sd"> compression should be "xz", "gzip", "lzma", "bzip2", or None.</span>
|
|
<span class="sd"> compressargs will be used on the compression commandline.'''</span>
|
|
<span class="k">if</span> <span class="n">compressargs</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">compressargs</span> <span class="o">=</span> <span class="p">[</span><span class="s">"-9"</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="n">compression</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="s">"xz"</span><span class="p">,</span> <span class="s">"gzip"</span><span class="p">,</span> <span class="s">"lzma"</span><span class="p">,</span> <span class="s">"bzip2"</span><span class="p">):</span>
|
|
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">,</span> <span class="s">"Unknown compression type </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">compression</span>
|
|
<span class="k">if</span> <span class="n">compression</span> <span class="o">==</span> <span class="s">"xz"</span><span class="p">:</span>
|
|
<span class="n">compressargs</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s">"--check=crc32"</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">compression</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">compression</span> <span class="o">=</span> <span class="s">"cat"</span> <span class="c"># this is a little silly</span>
|
|
<span class="n">compressargs</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="c"># make compression run with multiple threads if possible</span>
|
|
<span class="k">if</span> <span class="n">compression</span> <span class="ow">in</span> <span class="p">(</span><span class="s">"xz"</span><span class="p">,</span> <span class="s">"lzma"</span><span class="p">):</span>
|
|
<span class="n">compressargs</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s">"-T</span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">cpu_count</span><span class="p">())</span>
|
|
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"find </span><span class="si">%s</span><span class="s"> -print0 |</span><span class="si">%s</span><span class="s"> | </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> > </span><span class="si">%s</span><span class="s">"</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="s">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">command</span><span class="p">),</span>
|
|
<span class="n">compression</span><span class="p">,</span> <span class="s">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">compressargs</span><span class="p">),</span> <span class="n">outfile</span><span class="p">)</span>
|
|
<span class="n">find</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="n">comp</span> <span class="o">=</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">find</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s">"find"</span><span class="p">,</span> <span class="s">"."</span><span class="p">,</span> <span class="s">"-print0"</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">cwd</span><span class="o">=</span><span class="n">rootdir</span><span class="p">)</span>
|
|
<span class="n">archive</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">command</span><span class="p">,</span> <span class="n">stdin</span><span class="o">=</span><span class="n">find</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">cwd</span><span class="o">=</span><span class="n">rootdir</span><span class="p">)</span>
|
|
<span class="n">comp</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="n">compression</span><span class="p">]</span> <span class="o">+</span> <span class="n">compressargs</span><span class="p">,</span>
|
|
<span class="n">stdin</span><span class="o">=</span><span class="n">archive</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="s">"wb"</span><span class="p">))</span>
|
|
<span class="p">(</span><span class="n">_stdout</span><span class="p">,</span> <span class="n">_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="n">comp</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="n">comp</span><span class="o">.</span><span class="n">returncode</span>
|
|
<span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
|
|
<span class="c"># Kill off any hanging processes</span>
|
|
<span class="n">_</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">kill</span><span class="p">()</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="p">(</span><span class="n">find</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="n">comp</span><span class="p">)</span> <span class="k">if</span> <span class="n">p</span><span class="p">]</span>
|
|
<span class="k">return</span> <span class="mi">1</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkcpio"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkcpio">[docs]</a><span class="k">def</span> <span class="nf">mkcpio</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">compression</span><span class="o">=</span><span class="s">"xz"</span><span class="p">,</span> <span class="n">compressargs</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="k">return</span> <span class="n">compress</span><span class="p">([</span><span class="s">"cpio"</span><span class="p">,</span> <span class="s">"--null"</span><span class="p">,</span> <span class="s">"--quiet"</span><span class="p">,</span> <span class="s">"-H"</span><span class="p">,</span> <span class="s">"newc"</span><span class="p">,</span> <span class="s">"-o"</span><span class="p">],</span>
|
|
<span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">compression</span><span class="p">,</span> <span class="n">compressargs</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mktar"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mktar">[docs]</a><span class="k">def</span> <span class="nf">mktar</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">compression</span><span class="o">=</span><span class="s">"xz"</span><span class="p">,</span> <span class="n">compressargs</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="k">return</span> <span class="n">compress</span><span class="p">([</span><span class="s">"tar"</span><span class="p">,</span> <span class="s">"--no-recursion"</span><span class="p">,</span> <span class="s">"--selinux"</span><span class="p">,</span> <span class="s">"--acls"</span><span class="p">,</span> <span class="s">"--xattrs"</span><span class="p">,</span> <span class="s">"-cf-"</span><span class="p">,</span> <span class="s">"--null"</span><span class="p">,</span> <span class="s">"-T-"</span><span class="p">],</span>
|
|
<span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">compression</span><span class="p">,</span> <span class="n">compressargs</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mksquashfs"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mksquashfs">[docs]</a><span class="k">def</span> <span class="nf">mksquashfs</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">compression</span><span class="o">=</span><span class="s">"default"</span><span class="p">,</span> <span class="n">compressargs</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="sd">'''Make a squashfs image containing the given rootdir.'''</span>
|
|
<span class="k">if</span> <span class="n">compressargs</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">compressargs</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">if</span> <span class="n">compression</span> <span class="o">!=</span> <span class="s">"default"</span><span class="p">:</span>
|
|
<span class="n">compressargs</span> <span class="o">=</span> <span class="p">[</span><span class="s">"-comp"</span><span class="p">,</span> <span class="n">compression</span><span class="p">]</span> <span class="o">+</span> <span class="n">compressargs</span>
|
|
<span class="k">return</span> <span class="n">execWithRedirect</span><span class="p">(</span><span class="s">"mksquashfs"</span><span class="p">,</span> <span class="p">[</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">]</span> <span class="o">+</span> <span class="n">compressargs</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkrootfsimg"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkrootfsimg">[docs]</a><span class="k">def</span> <span class="nf">mkrootfsimg</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">sysroot</span><span class="o">=</span><span class="s">""</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Make rootfs image from a directory</span>
|
|
|
|
<span class="sd"> :param str rootdir: Root directory</span>
|
|
<span class="sd"> :param str outfile: Path of output image file</span>
|
|
<span class="sd"> :param str label: Filesystem label</span>
|
|
<span class="sd"> :param int size: Size of the image in GiB, if None computed automatically</span>
|
|
<span class="sd"> :param str sysroot: path to system (deployment) root relative to physical root</span>
|
|
<span class="sd"> """</span>
|
|
<span class="k">if</span> <span class="n">size</span><span class="p">:</span>
|
|
<span class="n">fssize</span> <span class="o">=</span> <span class="n">size</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">)</span> <span class="c"># 2GB sparse file compresses down to nothin'</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">fssize</span> <span class="o">=</span> <span class="bp">None</span> <span class="c"># Let mkext4img figure out the needed size</span>
|
|
|
|
<span class="n">mkext4img</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">fssize</span><span class="p">)</span>
|
|
<span class="c"># Reset selinux context on new rootfs</span>
|
|
<span class="k">with</span> <span class="n">LoopDev</span><span class="p">(</span><span class="n">outfile</span><span class="p">)</span> <span class="k">as</span> <span class="n">loopdev</span><span class="p">:</span>
|
|
<span class="k">with</span> <span class="n">Mount</span><span class="p">(</span><span class="n">loopdev</span><span class="p">)</span> <span class="k">as</span> <span class="n">mnt</span><span class="p">:</span>
|
|
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span> <span class="s">"setfiles"</span><span class="p">,</span> <span class="s">"-e"</span><span class="p">,</span> <span class="s">"/proc"</span><span class="p">,</span> <span class="s">"-e"</span><span class="p">,</span> <span class="s">"/sys"</span><span class="p">,</span> <span class="s">"-e"</span><span class="p">,</span> <span class="s">"/dev"</span><span class="p">,</span> <span class="s">"-e"</span><span class="p">,</span> <span class="s">"/install"</span><span class="p">,</span>
|
|
<span class="s">"/etc/selinux/targeted/contexts/files/file_contexts"</span><span class="p">,</span> <span class="s">"/"</span><span class="p">]</span>
|
|
<span class="n">root</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">mnt</span><span class="p">,</span> <span class="n">sysroot</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s">"/"</span><span class="p">))</span>
|
|
<span class="n">runcmd</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="n">root</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkdiskfsimage"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkdiskfsimage">[docs]</a><span class="k">def</span> <span class="nf">mkdiskfsimage</span><span class="p">(</span><span class="n">diskimage</span><span class="p">,</span> <span class="n">fsimage</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">"Anaconda"</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Copy the / partition of a partitioned disk image to an un-partitioned</span>
|
|
<span class="sd"> disk image.</span>
|
|
|
|
<span class="sd"> diskimage is the full path to partitioned disk image with a /</span>
|
|
<span class="sd"> fsimage is the full path of the output fs image file</span>
|
|
<span class="sd"> label is the label to apply to the image. Defaults to "Anaconda"</span>
|
|
<span class="sd"> """</span>
|
|
<span class="k">with</span> <span class="n">PartitionMount</span><span class="p">(</span><span class="n">diskimage</span><span class="p">)</span> <span class="k">as</span> <span class="n">img_mount</span><span class="p">:</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">img_mount</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">img_mount</span><span class="o">.</span><span class="n">mount_dir</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="bp">None</span>
|
|
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">"Creating fsimage </span><span class="si">%s</span><span class="s">"</span><span class="p">,</span> <span class="n">fsimage</span><span class="p">)</span>
|
|
<span class="n">mkext4img</span><span class="p">(</span><span class="n">img_mount</span><span class="o">.</span><span class="n">mount_dir</span><span class="p">,</span> <span class="n">fsimage</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">)</span>
|
|
|
|
<span class="c">######## Utility functions ###############################################</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mksparse"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mksparse">[docs]</a><span class="k">def</span> <span class="nf">mksparse</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
|
|
<span class="sd">'''use os.ftruncate to create a sparse file of the given size.'''</span>
|
|
<span class="n">fobj</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="s">"w"</span><span class="p">)</span>
|
|
<span class="n">os</span><span class="o">.</span><span class="n">ftruncate</span><span class="p">(</span><span class="n">fobj</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">size</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkqcow2"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkqcow2">[docs]</a><span class="k">def</span> <span class="nf">mkqcow2</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="sd">'''use qemu-img to create a file of the given size.</span>
|
|
<span class="sd"> options is a list of options passed to qemu-img</span>
|
|
|
|
<span class="sd"> Default format is qcow2, override by passing "-f", fmt</span>
|
|
<span class="sd"> in options.</span>
|
|
<span class="sd"> '''</span>
|
|
<span class="n">options</span> <span class="o">=</span> <span class="n">options</span> <span class="ow">or</span> <span class="p">[]</span>
|
|
<span class="k">if</span> <span class="s">"-f"</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
|
|
<span class="n">options</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"qcow2"</span><span class="p">])</span>
|
|
<span class="n">runcmd</span><span class="p">([</span><span class="s">"qemu-img"</span><span class="p">,</span> <span class="s">"create"</span><span class="p">]</span> <span class="o">+</span> <span class="n">options</span> <span class="o">+</span> <span class="p">[</span><span class="n">outfile</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">size</span><span class="p">)])</span>
|
|
</div>
|
|
<div class="viewcode-block" id="loop_waitfor"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.loop_waitfor">[docs]</a><span class="k">def</span> <span class="nf">loop_waitfor</span><span class="p">(</span><span class="n">loop_dev</span><span class="p">,</span> <span class="n">outfile</span><span class="p">):</span>
|
|
<span class="sd">"""Make sure the loop device is attached to the outfile.</span>
|
|
|
|
<span class="sd"> It seems that on rare occasions losetup can return before the /dev/loopX is</span>
|
|
<span class="sd"> ready for use, causing problems with mkfs. This tries to make sure that the</span>
|
|
<span class="sd"> loop device really is associated with the backing file before continuing.</span>
|
|
|
|
<span class="sd"> Raise RuntimeError if it isn't setup after 5 tries.</span>
|
|
<span class="sd"> """</span>
|
|
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">5</span><span class="p">):</span>
|
|
<span class="n">runcmd</span><span class="p">([</span><span class="s">"udevadm"</span><span class="p">,</span> <span class="s">"settle"</span><span class="p">,</span> <span class="s">"--timeout"</span><span class="p">,</span> <span class="s">"300"</span><span class="p">])</span>
|
|
<span class="c">## XXX Note that losetup --list output can be truncated to 64 bytes in some</span>
|
|
<span class="c">## situations. Don't use it to lookup backing file, go the other way</span>
|
|
<span class="c">## and lookup the loop for the backing file. See util-linux lib/loopdev.c</span>
|
|
<span class="c">## loopcxt_get_backing_file()</span>
|
|
<span class="k">if</span> <span class="n">get_loop_name</span><span class="p">(</span><span class="n">outfile</span><span class="p">)</span> <span class="o">==</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">loop_dev</span><span class="p">):</span>
|
|
<span class="k">return</span>
|
|
|
|
<span class="c"># If this really is a race, give it some time to settle down</span>
|
|
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
|
|
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">"Unable to setup </span><span class="si">%s</span><span class="s"> on </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">loop_dev</span><span class="p">,</span> <span class="n">outfile</span><span class="p">))</span>
|
|
</div>
|
|
<div class="viewcode-block" id="loop_attach"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.loop_attach">[docs]</a><span class="k">def</span> <span class="nf">loop_attach</span><span class="p">(</span><span class="n">outfile</span><span class="p">):</span>
|
|
<span class="sd">"""Attach a loop device to the given file. Return the loop device name.</span>
|
|
|
|
<span class="sd"> On rare occasions it appears that the device never shows up, some experiments</span>
|
|
<span class="sd"> seem to indicate that it may be a race with another process using /dev/loop* devices.</span>
|
|
|
|
<span class="sd"> So we now try 3 times before actually failing.</span>
|
|
|
|
<span class="sd"> Raises CalledProcessError if losetup fails.</span>
|
|
<span class="sd"> """</span>
|
|
<span class="n">retries</span> <span class="o">=</span> <span class="mi">0</span>
|
|
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">retries</span> <span class="o">+=</span> <span class="mi">1</span>
|
|
<span class="n">dev</span> <span class="o">=</span> <span class="n">runcmd_output</span><span class="p">([</span><span class="s">"losetup"</span><span class="p">,</span> <span class="s">"--find"</span><span class="p">,</span> <span class="s">"--show"</span><span class="p">,</span> <span class="n">outfile</span><span class="p">])</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
|
|
|
|
<span class="c"># Sometimes the loop device isn't ready yet, make extra sure before returning</span>
|
|
<span class="n">loop_waitfor</span><span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">outfile</span><span class="p">)</span>
|
|
<span class="k">except</span> <span class="n">CalledProcessError</span><span class="p">:</span>
|
|
<span class="c"># Problems running losetup are always errors, raise immediately</span>
|
|
<span class="k">raise</span>
|
|
<span class="k">except</span> <span class="ne">RuntimeError</span><span class="p">:</span>
|
|
<span class="c"># Try to setup the loop device 3 times</span>
|
|
<span class="k">if</span> <span class="n">retries</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">"loop_attach failed, retries exhausted."</span><span class="p">)</span>
|
|
<span class="k">raise</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"Try </span><span class="si">%d</span><span class="s"> failed, </span><span class="si">%s</span><span class="s"> did not appear."</span><span class="p">,</span> <span class="n">retries</span><span class="p">,</span> <span class="n">dev</span><span class="p">)</span>
|
|
<span class="k">break</span>
|
|
<span class="k">return</span> <span class="n">dev</span>
|
|
</div>
|
|
<div class="viewcode-block" id="loop_detach"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.loop_detach">[docs]</a><span class="k">def</span> <span class="nf">loop_detach</span><span class="p">(</span><span class="n">loopdev</span><span class="p">):</span>
|
|
<span class="sd">'''Detach the given loop device. Return False on failure.'''</span>
|
|
<span class="k">return</span> <span class="p">(</span><span class="n">execWithRedirect</span><span class="p">(</span><span class="s">"losetup"</span><span class="p">,</span> <span class="p">[</span><span class="s">"--detach"</span><span class="p">,</span> <span class="n">loopdev</span><span class="p">])</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="get_loop_name"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.get_loop_name">[docs]</a><span class="k">def</span> <span class="nf">get_loop_name</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
|
|
<span class="sd">'''Return the loop device associated with the path.</span>
|
|
<span class="sd"> Raises RuntimeError if more than one loop is associated'''</span>
|
|
<span class="n">buf</span> <span class="o">=</span> <span class="n">runcmd_output</span><span class="p">([</span><span class="s">"losetup"</span><span class="p">,</span> <span class="s">"-j"</span><span class="p">,</span> <span class="n">path</span><span class="p">])</span>
|
|
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">buf</span><span class="o">.</span><span class="n">splitlines</span><span class="p">())</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
|
|
<span class="c"># there should never be more than one loop device listed</span>
|
|
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">"multiple loops associated with </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">path</span><span class="p">)</span>
|
|
<span class="n">name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">buf</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">":"</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
|
|
<span class="k">return</span> <span class="n">name</span>
|
|
</div>
|
|
<div class="viewcode-block" id="dm_attach"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.dm_attach">[docs]</a><span class="k">def</span> <span class="nf">dm_attach</span><span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="sd">'''Attach a devicemapper device to the given device, with the given size.</span>
|
|
<span class="sd"> If name is None, a random name will be chosen. Returns the device name.</span>
|
|
<span class="sd"> raises CalledProcessError if dmsetup fails.'''</span>
|
|
<span class="k">if</span> <span class="n">name</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">name</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mktemp</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s">"lorax.imgutils."</span><span class="p">,</span> <span class="nb">dir</span><span class="o">=</span><span class="s">""</span><span class="p">)</span>
|
|
<span class="n">runcmd</span><span class="p">([</span><span class="s">"dmsetup"</span><span class="p">,</span> <span class="s">"create"</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="s">"--table"</span><span class="p">,</span>
|
|
<span class="s">"0 </span><span class="si">%i</span><span class="s"> linear </span><span class="si">%s</span><span class="s"> 0"</span> <span class="o">%</span> <span class="p">(</span><span class="n">size</span><span class="o">/</span><span class="mi">512</span><span class="p">,</span> <span class="n">dev</span><span class="p">)])</span>
|
|
<span class="k">return</span> <span class="n">name</span>
|
|
</div>
|
|
<div class="viewcode-block" id="dm_detach"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.dm_detach">[docs]</a><span class="k">def</span> <span class="nf">dm_detach</span><span class="p">(</span><span class="n">dev</span><span class="p">):</span>
|
|
<span class="sd">'''Detach the named devicemapper device. Returns False if dmsetup fails.'''</span>
|
|
<span class="n">dev</span> <span class="o">=</span> <span class="n">dev</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">"/dev/mapper/"</span><span class="p">,</span> <span class="s">""</span><span class="p">)</span> <span class="c"># strip prefix, if it's there</span>
|
|
<span class="k">return</span> <span class="n">execWithRedirect</span><span class="p">(</span><span class="s">"dmsetup"</span><span class="p">,</span> <span class="p">[</span><span class="s">"remove"</span><span class="p">,</span> <span class="n">dev</span><span class="p">])</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mount"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mount">[docs]</a><span class="k">def</span> <span class="nf">mount</span><span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">opts</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">mnt</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="sd">'''Mount the given device at the given mountpoint, using the given opts.</span>
|
|
<span class="sd"> opts should be a comma-separated string of mount options.</span>
|
|
<span class="sd"> if mnt is none, a temporary directory will be created and its path will be</span>
|
|
<span class="sd"> returned.</span>
|
|
<span class="sd"> raises CalledProcessError if mount fails.'''</span>
|
|
<span class="k">if</span> <span class="n">mnt</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">mnt</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s">"lorax.imgutils."</span><span class="p">)</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"make tmp mountdir </span><span class="si">%s</span><span class="s">"</span><span class="p">,</span> <span class="n">mnt</span><span class="p">)</span>
|
|
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s">"mount"</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="n">opts</span><span class="p">:</span>
|
|
<span class="n">cmd</span> <span class="o">+=</span> <span class="p">[</span><span class="s">"-o"</span><span class="p">,</span> <span class="n">opts</span><span class="p">]</span>
|
|
<span class="n">cmd</span> <span class="o">+=</span> <span class="p">[</span><span class="n">dev</span><span class="p">,</span> <span class="n">mnt</span><span class="p">]</span>
|
|
<span class="n">runcmd</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">mnt</span>
|
|
</div>
|
|
<div class="viewcode-block" id="umount"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.umount">[docs]</a><span class="k">def</span> <span class="nf">umount</span><span class="p">(</span><span class="n">mnt</span><span class="p">,</span> <span class="n">lazy</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">maxretry</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">retrysleep</span><span class="o">=</span><span class="mf">1.0</span><span class="p">):</span>
|
|
<span class="sd">'''Unmount the given mountpoint. If lazy is True, do a lazy umount (-l).</span>
|
|
<span class="sd"> If the mount was a temporary dir created by mount, it will be deleted.</span>
|
|
<span class="sd"> raises CalledProcessError if umount fails.'''</span>
|
|
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s">"umount"</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="n">lazy</span><span class="p">:</span> <span class="n">cmd</span> <span class="o">+=</span> <span class="p">[</span><span class="s">"-l"</span><span class="p">]</span>
|
|
<span class="n">cmd</span> <span class="o">+=</span> <span class="p">[</span><span class="n">mnt</span><span class="p">]</span>
|
|
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
|
<span class="k">while</span> <span class="n">maxretry</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">rv</span> <span class="o">=</span> <span class="n">runcmd</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
|
|
<span class="k">except</span> <span class="n">CalledProcessError</span><span class="p">:</span>
|
|
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
|
<span class="k">if</span> <span class="n">count</span> <span class="o">==</span> <span class="n">maxretry</span><span class="p">:</span>
|
|
<span class="k">raise</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">"failed to unmount </span><span class="si">%s</span><span class="s">. retrying (</span><span class="si">%d</span><span class="s">/</span><span class="si">%d</span><span class="s">)..."</span><span class="p">,</span>
|
|
<span class="n">mnt</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">maxretry</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">logger</span><span class="o">.</span><span class="n">getEffectiveLevel</span><span class="p">()</span> <span class="o"><=</span> <span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
|
|
<span class="n">fuser</span> <span class="o">=</span> <span class="n">execWithCapture</span><span class="p">(</span><span class="s">"fuser"</span><span class="p">,</span> <span class="p">[</span><span class="s">"-vm"</span><span class="p">,</span> <span class="n">mnt</span><span class="p">])</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"fuser -vm:</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">fuser</span><span class="p">)</span>
|
|
<span class="n">sleep</span><span class="p">(</span><span class="n">retrysleep</span><span class="p">)</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="k">break</span>
|
|
<span class="k">if</span> <span class="s">'lorax.imgutils'</span> <span class="ow">in</span> <span class="n">mnt</span><span class="p">:</span>
|
|
<span class="n">os</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="n">mnt</span><span class="p">)</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"remove tmp mountdir </span><span class="si">%s</span><span class="s">"</span><span class="p">,</span> <span class="n">mnt</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="p">(</span><span class="n">rv</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="copytree"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.copytree">[docs]</a><span class="k">def</span> <span class="nf">copytree</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">preserve</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
|
|
<span class="sd">'''Copy a tree of files using cp -a, thus preserving modes, timestamps,</span>
|
|
<span class="sd"> links, acls, sparse files, xattrs, selinux contexts, etc.</span>
|
|
<span class="sd"> If preserve is False, uses cp -R (useful for modeless filesystems)</span>
|
|
<span class="sd"> raises CalledProcessError if copy fails.'''</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"copytree </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">"</span><span class="p">,</span> <span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">)</span>
|
|
<span class="n">cp</span> <span class="o">=</span> <span class="p">[</span><span class="s">"cp"</span><span class="p">,</span> <span class="s">"-a"</span><span class="p">]</span> <span class="k">if</span> <span class="n">preserve</span> <span class="k">else</span> <span class="p">[</span><span class="s">"cp"</span><span class="p">,</span> <span class="s">"-R"</span><span class="p">,</span> <span class="s">"-L"</span><span class="p">]</span>
|
|
<span class="n">cp</span> <span class="o">+=</span> <span class="p">[</span><span class="s">"."</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">dest</span><span class="p">)]</span>
|
|
<span class="n">runcmd</span><span class="p">(</span><span class="n">cp</span><span class="p">,</span> <span class="n">cwd</span><span class="o">=</span><span class="n">src</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="do_grafts"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.do_grafts">[docs]</a><span class="k">def</span> <span class="nf">do_grafts</span><span class="p">(</span><span class="n">grafts</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">preserve</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
|
|
<span class="sd">'''Copy each of the items listed in grafts into dest.</span>
|
|
<span class="sd"> If the key ends with '/' it's assumed to be a directory which should be</span>
|
|
<span class="sd"> created, otherwise just the leading directories will be created.'''</span>
|
|
<span class="k">for</span> <span class="n">imgpath</span><span class="p">,</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">grafts</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
|
|
<span class="k">if</span> <span class="n">imgpath</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s">'/'</span><span class="p">:</span>
|
|
<span class="n">targetdir</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">imgpath</span><span class="p">)</span>
|
|
<span class="n">imgpath</span> <span class="o">=</span> <span class="n">imgpath</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">targetdir</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">dirname</span><span class="p">(</span><span class="n">imgpath</span><span class="p">))</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">targetdir</span><span class="p">):</span>
|
|
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">targetdir</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
|
|
<span class="n">copytree</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">join</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">imgpath</span><span class="p">),</span> <span class="n">preserve</span><span class="p">)</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">cpfile</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">join</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">imgpath</span><span class="p">))</span>
|
|
</div>
|
|
<div class="viewcode-block" id="round_to_blocks"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.round_to_blocks">[docs]</a><span class="k">def</span> <span class="nf">round_to_blocks</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">blocksize</span><span class="p">):</span>
|
|
<span class="sd">'''If size isn't a multiple of blocksize, round up to the next multiple'''</span>
|
|
<span class="n">diff</span> <span class="o">=</span> <span class="n">size</span> <span class="o">%</span> <span class="n">blocksize</span>
|
|
<span class="k">if</span> <span class="n">diff</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">size</span><span class="p">:</span>
|
|
<span class="n">size</span> <span class="o">+=</span> <span class="n">blocksize</span> <span class="o">-</span> <span class="n">diff</span>
|
|
<span class="k">return</span> <span class="n">size</span>
|
|
|
|
<span class="c"># TODO: move filesystem data outside this function</span></div>
|
|
<div class="viewcode-block" id="estimate_size"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.estimate_size">[docs]</a><span class="k">def</span> <span class="nf">estimate_size</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">graft</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">fstype</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">blocksize</span><span class="o">=</span><span class="mi">4096</span><span class="p">,</span> <span class="n">overhead</span><span class="o">=</span><span class="mi">128</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="n">graft</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">graft</span> <span class="o">=</span> <span class="p">{}</span>
|
|
<span class="n">getsize</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">f</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">lstat</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">st_size</span>
|
|
<span class="k">if</span> <span class="n">fstype</span> <span class="o">==</span> <span class="s">"btrfs"</span><span class="p">:</span>
|
|
<span class="n">overhead</span> <span class="o">=</span> <span class="mi">64</span><span class="o">*</span><span class="mi">1024</span> <span class="c"># don't worry, it's all sparse</span>
|
|
<span class="k">if</span> <span class="n">fstype</span> <span class="o">==</span> <span class="s">"hfsplus"</span><span class="p">:</span>
|
|
<span class="n">overhead</span> <span class="o">=</span> <span class="mi">200</span> <span class="c"># hack to deal with two bootloader copies</span>
|
|
<span class="k">if</span> <span class="n">fstype</span> <span class="ow">in</span> <span class="p">(</span><span class="s">"vfat"</span><span class="p">,</span> <span class="s">"msdos"</span><span class="p">):</span>
|
|
<span class="n">blocksize</span> <span class="o">=</span> <span class="mi">2048</span>
|
|
<span class="n">getsize</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">f</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">st_size</span> <span class="c"># no symlinks, count as copies</span>
|
|
<span class="n">total</span> <span class="o">=</span> <span class="n">overhead</span><span class="o">*</span><span class="n">blocksize</span>
|
|
<span class="n">dirlist</span> <span class="o">=</span> <span class="n">graft</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="n">rootdir</span><span class="p">:</span>
|
|
<span class="n">dirlist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rootdir</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">root</span> <span class="ow">in</span> <span class="n">dirlist</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">top</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
|
|
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">files</span> <span class="o">+</span> <span class="n">dirs</span><span class="p">:</span>
|
|
<span class="n">total</span> <span class="o">+=</span> <span class="n">round_to_blocks</span><span class="p">(</span><span class="n">getsize</span><span class="p">(</span><span class="n">join</span><span class="p">(</span><span class="n">top</span><span class="p">,</span><span class="n">f</span><span class="p">)),</span> <span class="n">blocksize</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">fstype</span> <span class="o">==</span> <span class="s">"btrfs"</span><span class="p">:</span>
|
|
<span class="n">total</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">256</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">,</span> <span class="n">total</span><span class="p">)</span> <span class="c"># btrfs minimum size: 256MB</span>
|
|
<span class="k">return</span> <span class="n">total</span>
|
|
</div>
|
|
<div class="viewcode-block" id="default_image_name"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.default_image_name">[docs]</a><span class="k">def</span> <span class="nf">default_image_name</span><span class="p">(</span><span class="n">compression</span><span class="p">,</span> <span class="n">basename</span><span class="p">):</span>
|
|
<span class="sd">""" Return a default image name with the correct suffix for the compression type.</span>
|
|
|
|
<span class="sd"> :param str compression: Compression type</span>
|
|
<span class="sd"> :param str basename: Base filename</span>
|
|
<span class="sd"> :returns: basename with compression suffix</span>
|
|
|
|
<span class="sd"> If the compression is unknown it defaults to xz</span>
|
|
<span class="sd"> """</span>
|
|
<span class="n">SUFFIXES</span> <span class="o">=</span> <span class="p">{</span><span class="s">"xz"</span><span class="p">:</span> <span class="s">".xz"</span><span class="p">,</span> <span class="s">"gzip"</span><span class="p">:</span> <span class="s">".gz"</span><span class="p">,</span> <span class="s">"bzip2"</span><span class="p">:</span> <span class="s">".bz2"</span><span class="p">,</span> <span class="s">"lzma"</span><span class="p">:</span> <span class="s">".lzma"</span><span class="p">}</span>
|
|
<span class="k">return</span> <span class="n">basename</span> <span class="o">+</span> <span class="n">SUFFIXES</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">compression</span><span class="p">,</span> <span class="s">".xz"</span><span class="p">)</span>
|
|
|
|
<span class="c">######## Execution contexts - use with the 'with' statement ##############</span>
|
|
</div>
|
|
<div class="viewcode-block" id="LoopDev"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.LoopDev">[docs]</a><span class="k">class</span> <span class="nc">LoopDev</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">loopdev</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
|
|
<span class="k">if</span> <span class="n">size</span><span class="p">:</span>
|
|
<span class="n">mksparse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
|
|
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">loopdev</span> <span class="o">=</span> <span class="n">loop_attach</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">loopdev</span>
|
|
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">):</span>
|
|
<span class="n">loop_detach</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">loopdev</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="DMDev"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.DMDev">[docs]</a><span class="k">class</span> <span class="nc">DMDev</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dev</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dev</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mapperdev</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mapperdev</span> <span class="o">=</span> <span class="n">dm_attach</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dev</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapperdev</span>
|
|
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">):</span>
|
|
<span class="n">dm_detach</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mapperdev</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="Mount"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.Mount">[docs]</a><span class="k">class</span> <span class="nc">Mount</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dev</span><span class="p">,</span> <span class="n">opts</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">mnt</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dev</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mnt</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">opts</span><span class="p">,</span> <span class="n">mnt</span><span class="p">)</span>
|
|
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mnt</span> <span class="o">=</span> <span class="n">mount</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dev</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mnt</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mnt</span>
|
|
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">):</span>
|
|
<span class="n">umount</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mnt</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="PartitionMount"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.PartitionMount">[docs]</a><span class="k">class</span> <span class="nc">PartitionMount</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="sd">""" Mount a partitioned image file using kpartx """</span>
|
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">disk_img</span><span class="p">,</span> <span class="n">mount_ok</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> disk_img is the full path to a partitioned disk image</span>
|
|
<span class="sd"> mount_ok is a function that is passed the mount point and</span>
|
|
<span class="sd"> returns True if it should be mounted.</span>
|
|
<span class="sd"> """</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_dev</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_size</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">disk_img</span> <span class="o">=</span> <span class="n">disk_img</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_ok</span> <span class="o">=</span> <span class="n">mount_ok</span>
|
|
|
|
<span class="c"># Default is to mount partition with /etc/passwd</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">mount_ok</span><span class="p">:</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_ok</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">mount_dir</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">mount_dir</span><span class="o">+</span><span class="s">"/etc/passwd"</span><span class="p">)</span>
|
|
|
|
<span class="c"># Example kpartx output</span>
|
|
<span class="c"># kpartx -p p -v -a /tmp/diskV2DiCW.im</span>
|
|
<span class="c"># add map loop2p1 (253:2): 0 3481600 linear /dev/loop2 2048</span>
|
|
<span class="c"># add map loop2p2 (253:3): 0 614400 linear /dev/loop2 3483648</span>
|
|
<span class="n">kpartx_output</span> <span class="o">=</span> <span class="n">runcmd_output</span><span class="p">([</span><span class="s">"kpartx"</span><span class="p">,</span> <span class="s">"-v"</span><span class="p">,</span> <span class="s">"-a"</span><span class="p">,</span> <span class="s">"-s"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">disk_img</span><span class="p">])</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">kpartx_output</span><span class="p">)</span>
|
|
|
|
<span class="c"># list of (deviceName, sizeInBytes)</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">loop_devices</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">kpartx_output</span><span class="o">.</span><span class="n">splitlines</span><span class="p">():</span>
|
|
<span class="c"># add map loop2p3 (253:4): 0 7139328 linear /dev/loop2 528384</span>
|
|
<span class="c"># 3rd element is size in 512 byte blocks</span>
|
|
<span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">"add map "</span><span class="p">):</span>
|
|
<span class="n">fields</span> <span class="o">=</span> <span class="n">line</span><span class="p">[</span><span class="mi">8</span><span class="p">:]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">loop_devices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span><span class="o">*</span><span class="mi">512</span><span class="p">)</span> <span class="p">)</span>
|
|
|
|
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="c"># Mount the device selected by mount_ok, if possible</span>
|
|
<span class="n">mount_dir</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">()</span>
|
|
<span class="k">for</span> <span class="n">dev</span><span class="p">,</span> <span class="n">size</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">loop_devices</span><span class="p">:</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">mount</span><span class="p">(</span> <span class="s">"/dev/mapper/"</span><span class="o">+</span><span class="n">dev</span><span class="p">,</span> <span class="n">mnt</span><span class="o">=</span><span class="n">mount_dir</span> <span class="p">)</span>
|
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mount_ok</span><span class="p">(</span><span class="n">mount_dir</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span> <span class="o">=</span> <span class="n">mount_dir</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_dev</span> <span class="o">=</span> <span class="n">dev</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_size</span> <span class="o">=</span> <span class="n">size</span>
|
|
<span class="k">break</span>
|
|
<span class="n">umount</span><span class="p">(</span> <span class="n">mount_dir</span> <span class="p">)</span>
|
|
<span class="k">except</span> <span class="n">CalledProcessError</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">())</span>
|
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">"Partition mounted on </span><span class="si">%s</span><span class="s"> size=</span><span class="si">%d</span><span class="s">"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mount_size</span><span class="p">)</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">"Unable to mount anything from </span><span class="si">%s</span><span class="s">"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">disk_img</span><span class="p">)</span>
|
|
<span class="n">os</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="n">mount_dir</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="bp">self</span>
|
|
|
|
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span><span class="p">:</span>
|
|
<span class="n">umount</span><span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span> <span class="p">)</span>
|
|
<span class="n">os</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span><span class="p">)</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mount_dir</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="n">execWithRedirect</span><span class="p">(</span><span class="s">"kpartx"</span><span class="p">,</span> <span class="p">[</span><span class="s">"-d"</span><span class="p">,</span> <span class="s">"-s"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">disk_img</span><span class="p">])</span>
|
|
|
|
|
|
<span class="c">######## Functions for making filesystem images ##########################</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkfsimage"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkfsimage">[docs]</a><span class="k">def</span> <span class="nf">mkfsimage</span><span class="p">(</span><span class="n">fstype</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">mkfsargs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">graft</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="sd">'''Generic filesystem image creation function.</span>
|
|
<span class="sd"> fstype should be a filesystem type - "mkfs.${fstype}" must exist.</span>
|
|
<span class="sd"> graft should be a dict: {"some/path/in/image": "local/file/or/dir"};</span>
|
|
<span class="sd"> if the path ends with a '/' it's assumed to be a directory.</span>
|
|
<span class="sd"> Will raise CalledProcessError if something goes wrong.'''</span>
|
|
<span class="k">if</span> <span class="n">mkfsargs</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">mkfsargs</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">if</span> <span class="n">graft</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">graft</span> <span class="o">=</span> <span class="p">{}</span>
|
|
<span class="n">preserve</span> <span class="o">=</span> <span class="p">(</span><span class="n">fstype</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">(</span><span class="s">"msdos"</span><span class="p">,</span> <span class="s">"vfat"</span><span class="p">))</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">size</span><span class="p">:</span>
|
|
<span class="n">size</span> <span class="o">=</span> <span class="n">estimate_size</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">graft</span><span class="p">,</span> <span class="n">fstype</span><span class="p">)</span>
|
|
<span class="k">with</span> <span class="n">LoopDev</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span> <span class="k">as</span> <span class="n">loopdev</span><span class="p">:</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">runcmd</span><span class="p">([</span><span class="s">"mkfs.</span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">fstype</span><span class="p">]</span> <span class="o">+</span> <span class="n">mkfsargs</span> <span class="o">+</span> <span class="p">[</span><span class="n">loopdev</span><span class="p">])</span>
|
|
<span class="k">except</span> <span class="n">CalledProcessError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">"mkfs exited with a non-zero return code: </span><span class="si">%d</span><span class="s">"</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">returncode</span><span class="p">)</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">output</span><span class="p">)</span>
|
|
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">returncode</span><span class="p">)</span>
|
|
|
|
<span class="k">with</span> <span class="n">Mount</span><span class="p">(</span><span class="n">loopdev</span><span class="p">,</span> <span class="n">mountargs</span><span class="p">)</span> <span class="k">as</span> <span class="n">mnt</span><span class="p">:</span>
|
|
<span class="k">if</span> <span class="n">rootdir</span><span class="p">:</span>
|
|
<span class="n">copytree</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">mnt</span><span class="p">,</span> <span class="n">preserve</span><span class="p">)</span>
|
|
<span class="n">do_grafts</span><span class="p">(</span><span class="n">graft</span><span class="p">,</span> <span class="n">mnt</span><span class="p">,</span> <span class="n">preserve</span><span class="p">)</span>
|
|
|
|
<span class="c"># Make absolutely sure that the data has been written</span>
|
|
<span class="n">runcmd</span><span class="p">([</span><span class="s">"sync"</span><span class="p">])</span>
|
|
|
|
<span class="c"># convenience functions with useful defaults</span></div>
|
|
<div class="viewcode-block" id="mkdosimg"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkdosimg">[docs]</a><span class="k">def</span> <span class="nf">mkdosimg</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="s">"shortname=winnt,umask=0077"</span><span class="p">,</span> <span class="n">graft</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="n">mkfsimage</span><span class="p">(</span><span class="s">"msdos"</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="n">mountargs</span><span class="p">,</span>
|
|
<span class="n">mkfsargs</span><span class="o">=</span><span class="p">[</span><span class="s">"-n"</span><span class="p">,</span> <span class="n">label</span><span class="p">],</span> <span class="n">graft</span><span class="o">=</span><span class="n">graft</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkext4img"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkext4img">[docs]</a><span class="k">def</span> <span class="nf">mkext4img</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">graft</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="n">mkfsimage</span><span class="p">(</span><span class="s">"ext4"</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="n">mountargs</span><span class="p">,</span>
|
|
<span class="n">mkfsargs</span><span class="o">=</span><span class="p">[</span><span class="s">"-L"</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="s">"-b"</span><span class="p">,</span> <span class="s">"1024"</span><span class="p">,</span> <span class="s">"-m"</span><span class="p">,</span> <span class="s">"0"</span><span class="p">],</span> <span class="n">graft</span><span class="o">=</span><span class="n">graft</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkbtrfsimg"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkbtrfsimg">[docs]</a><span class="k">def</span> <span class="nf">mkbtrfsimg</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">graft</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="n">mkfsimage</span><span class="p">(</span><span class="s">"btrfs"</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="n">mountargs</span><span class="p">,</span>
|
|
<span class="n">mkfsargs</span><span class="o">=</span><span class="p">[</span><span class="s">"-L"</span><span class="p">,</span> <span class="n">label</span><span class="p">],</span> <span class="n">graft</span><span class="o">=</span><span class="n">graft</span><span class="p">)</span>
|
|
</div>
|
|
<div class="viewcode-block" id="mkhfsimg"><a class="viewcode-back" href="../../pylorax.html#pylorax.imgutils.mkhfsimg">[docs]</a><span class="k">def</span> <span class="nf">mkhfsimg</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="s">""</span><span class="p">,</span> <span class="n">graft</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="n">mkfsimage</span><span class="p">(</span><span class="s">"hfsplus"</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">mountargs</span><span class="o">=</span><span class="n">mountargs</span><span class="p">,</span>
|
|
<span class="n">mkfsargs</span><span class="o">=</span><span class="p">[</span><span class="s">"-v"</span><span class="p">,</span> <span class="n">label</span><span class="p">],</span> <span class="n">graft</span><span class="o">=</span><span class="n">graft</span><span class="p">)</span></div>
|
|
</pre></div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar">
|
|
<div class="sphinxsidebarwrapper">
|
|
<div id="searchbox" style="display: none">
|
|
<h3>Quick search</h3>
|
|
<form class="search" action="../../search.html" method="get">
|
|
<input type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
<p class="searchtip" style="font-size: 90%">
|
|
Enter search terms or a module, class or function name.
|
|
</p>
|
|
</div>
|
|
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li><a href="../../index.html">Lorax 19.7.43 documentation</a> »</li>
|
|
<li><a href="../index.html" >Module code</a> »</li>
|
|
<li><a href="../pylorax.html" >pylorax</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2018, Red Hat, Inc..
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
|
|
</div>
|
|
</body>
|
|
</html> |