summaryrefslogtreecommitdiff
path: root/morphlib/exts/rawdisk.write
blob: 7d0ebaeed3fd5af31af509ece7e48d8a4f8ad558 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
# Copyright (C) 2012-2014  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.


'''A Morph deployment write extension for raw disk images.'''


import cliapp
import os
import sys
import time
import tempfile

import morphlib.writeexts


class RawDiskWriteExtension(morphlib.writeexts.WriteExtension):

    '''Create a raw disk image during Morph's deployment.
    
    If the image already exists, it is upgraded.

    The location command line argument is the pathname of the disk image
    to be created/upgraded.
    
    '''

    def process_args(self, args):
        if len(args) != 2:
            raise cliapp.AppException('Wrong number of command line args')
        
        temp_root, location = args
        if os.path.isfile(location):
            self.upgrade_local_system(location, temp_root)
        else:
            try:
                self.create_raw_disk_image(location)
                self.create_local_system(temp_root, location)
                self.status(msg='Disk image has been created at %s' % location)
            except Exception:
                self.status(msg='Failure to create disk image at %s' %
                                 location)
                if os.path.exists(location):
                    os.remove(location)
                raise

    def upgrade_local_system(self, raw_disk, temp_root):
        self.complete_fstab_for_btrfs_layout(temp_root)

        mp = self.mount(raw_disk)

        version_label = self.get_version_label(mp)
        self.status(msg='Updating image to a new version with label %s' %
                    version_label)

        version_root = os.path.join(mp, 'systems', version_label)
        os.mkdir(version_root)

        old_orig = os.path.join(mp, 'systems', 'default', 'orig')
        new_orig = os.path.join(version_root, 'orig')
        cliapp.runcmd(
            ['btrfs', 'subvolume', 'snapshot', old_orig, new_orig])

        cliapp.runcmd(
            ['rsync', '-a', '--checksum', '--numeric-ids', '--delete',
             temp_root + os.path.sep, new_orig])

        self.create_run(version_root)

        default_path = os.path.join(mp, 'systems', 'default')
        if os.path.exists(default_path):
            os.remove(default_path)
        else:
            # we are upgrading and old system that does
            # not have an updated extlinux config file
            if self.bootloader_config_is_wanted():
                self.generate_bootloader_config(mp)
            self.install_bootloader(mp)
        os.symlink(version_label, default_path)

        if self.bootloader_config_is_wanted():
            self.install_kernel(version_root, temp_root)

        self.unmount(mp)

    def get_version_label(self, mp):
        version_label = os.environ.get('VERSION_LABEL')

        if version_label is None:
            self.unmount(mp)
            raise cliapp.AppException('VERSION_LABEL was not given')

        if os.path.exists(os.path.join(mp, 'systems', version_label)):
            self.unmount(mp)
            raise cliapp.AppException('VERSION_LABEL %s already exists'
                                      % version_label)

        return version_label


RawDiskWriteExtension().run()