summaryrefslogtreecommitdiff
path: root/morphlib/fsutils.py
blob: dbe8b2610b1eb77a5a8be61327bbee0142aa0487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (C) 2012  Codethink Limited
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os


def create_image(ex, image_name, size):
    # FIXME a pure python implementation may be better
    ex.runv(['dd', 'if=/dev/zero', 'of=' + image_name, 'bs=1',
             'seek=%d' % size, 'count=0'])

def partition_image(ex, image_name):
    # FIXME make this more flexible with partitioning options
    ex.runv(['sfdisk', image_name], feed_stdin='1,,83,*\n')

def install_mbr(ex, image_name):
    for path in ['/usr/lib/extlinux/mbr.bin',
                 '/usr/share/syslinux/mbr.bin']:
        if os.path.exists(path):
            ex.runv(['dd', 'if=' + path, 'of=' + image_name,
                     'conv=notrunc'])
            break

def setup_device_mapping(ex, image_name):
    out = ex.runv(['sfdisk', '-d', image_name])
    for line in out.splitlines():
        words = line.split()
        if (len(words) >= 4 and 
            words[2] == 'start=' and 
            words[3] != '0,'):
            n = int(words[3][:-1]) # skip trailing comma
            start = n * 512
            break
    
    ex.runv(['losetup', '-o', str(start), '-f', image_name])
    
    out = ex.runv(['losetup', '-j', image_name])
    line = out.strip()
    i = line.find(':')
    return line[:i]

def create_fs(ex, partition):
    # FIXME: the hardcoded size of 4GB is icky but the default broke
    # when we used mkfs -t ext4
    ex.runv(['mkfs', '-t', 'btrfs', '-L', 'baserock',
             '-b', '4294967296', partition])

def mount(ex, partition, mount_point):
    os.mkdir(mount_point)
    ex.runv(['mount', partition, mount_point])

def unmount(ex, mount_point):
    ex.runv(['umount', mount_point])

def undo_device_mapping(ex, image_name):
    out = ex.runv(['losetup', '-j', image_name])
    for line in out.splitlines():
        i = line.find(':')
        device = line[:i]
        ex.runv(['losetup', '-d', device])