summaryrefslogtreecommitdiff
path: root/install
blob: 9fea90a6fb7c626659c88044c1dd153eac028b58 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/python

"""
Written by Igor Pashev <pashev.igor@gmail.com>

The author has placed this work in the Public Domain,
thereby relinquishing all copyrights. Everyone is free
to use, modify, republish, sell or give away this work
without prior consent from anybody.
"""

from lib.hdd import HDD
from lib.snack import *
from snack import *
from subprocess import Popen, PIPE, call
from pprint import pprint
from tempfile import mkstemp
import os
import re
import sys

# Snack screen
screen = None

# List of disks installed on the system:
hdds = []

# Name of the root ZFS pool: either existing or newly created
rpool = None

# Name of the root slice, where ZFS root pool will be created (e. g. c0t0d0s0)
rslice = None

class Abort(Exception):
    ''' User clicked "Cancel" '''
    pass
class NoDisks(Exception):
    ''' No Hard Drives found '''
    pass

def welcome():
    welcome = ButtonChoiceWindow(screen, "Welcome to the Dyson installer",
            '''
Dyson is an operating system derived from Debian and based on Illumos core. \
It uses Illumos unix kernel and libc, ZFS file system and SMF to manage system startup. \
To learn more visit http://osdyson.org

This installer will guide you through disk paritioning, filesystem creation, \
installation of base system and minimal configuration.

Please note, this system IS VERY EXPERIMENTAL. \
You can LOOSE ALL YOUR DATA which this system can reach ;-)

Whould you like to continue?''',
            buttons=[('Install Dyson', True), ('Exit the installer', False)], width=70)
    if not welcome:
        raise Abort('Installation canceled')

def find_zpools():
    global zpools
    progress = ProgressBar(screen, title='Searching for zpools')
    progress.progress = 10
    zpools = []
    zpool_cmd = Popen(['zpool', 'list', '-H', '-o', 'name'], stdout=PIPE, stderr=PIPE)
    for line in zpool_cmd.stdout:
        zpools.append(line.rstrip())
    progress.progress = 50
    zpool_cmd = Popen("zpool import | awk '/pool:/ {print $2}'", shell=True, stdout=PIPE, stderr=PIPE)
    for line in zpool_cmd.stdout:
        zpools.append(line.rstrip())
    progress.progress = 100

def find_hdds():
    global hdds
    progress = ProgressMessage(screen, title='Please, wait',  text='Searching for disks...')
    hdds = []
    pat = re.compile('\d+\.\s+(\S+)\s+<(.+?)(\s+cyl.*)?>')
    format_cmd = Popen('format </dev/null', shell=True, stdout=PIPE)
    for line in format_cmd.stdout:
        m = pat.search(line)
        if m:
            name = m.group(1)
            desc = m.group(2)
            hdds.append(HDD(name, desc))
    if len(hdds) == 0:
        raise NoDisks('No disks found')


def choose_rpool():
    zpool_items = map(lambda x: (x, x), zpools)
    zpool_items.append(('Create new ZFS pool', None))

    choice = None

    buttons = [('Ok', 'ok'), ('Cancel', 'cancel')]

    while not choice in ['ok', 'cancel']:
        (choice, pool) = ListboxChoiceWindow(screen,
                title='Choose ZFS pool',
                text='Choose ZFS pool where Dyson will be installed.\n'
                'You can selected one of the existing pools or create a new one.',
                items=zpool_items,
                buttons=buttons,
                width=76,
                scroll=1,
                default=0,
                )
        # Enter is pressed:
        if choice == None:
            choice = 'ok'
    if choice == 'ok':
        return pool
    raise Abort('Pool choosing')

def create_root_slice(hdd_name):
    '''Create root slice (0) over entire Solaris partition'''
    fd, path = mkstemp()
    os.write(fd, 'partition\nmodify\n1\nyes\n0\n\n\n\n\n\n\nyes\n"{label}"\nquit\nquit\n'.format(label=hdd_name))
    os.close(fd)
    format_cmd = Popen(['format', '-f', path, '-d', hdd_name], stderr=PIPE, stdout=PIPE)
    out, err = format_cmd.communicate()
    os.unlink(path)
    if format_cmd.returncode != 0:
        ButtonChoiceWindow(screen, title='Creating of root slice failed',
                text=err, buttons = ['Ok'], width=70)
        return None
    return '{}s0'.format(hdd_name)

def create_rpool(hdd):
    global rslice
    rslice = create_root_slice(hdd.name)
    if not rslice:
        return None
    pool_name = None
    for pool_no in ['', 1, 2, 3, 4, 5, 6, 7]:
        pool_name = 'rpool{}'.format(rpool_no)
        if not pool_name in zpools:
            break

    zpool_cmd = Popen(['zpool', 'create', '-f', pool_name, rslice],
            stderr=PIPE, stdout=PIPE)
    out, err = zpool_cmd.communicate()
    if 0 != zpool_cmd.returncode:
        ButtonChoiceWindow(screen,
                title='Creating of ZFS root pool failed',
                text=err, buttons = ['Ok'], width=70)
        return None
    return pool

def pool_is_imported(pool):
    rpool_is_imported = call(['zpool', 'status', pool], stdout=PIPE, stderr=PIPE)
    return rpool_is_imported == 0

def configure_rpool():
    global rpool
    find_zpools()
    while not rpool:
        if len(zpools) > 0:
            rpool = choose_rpool()
        if not rpool:
            hdd = choose_hdd(len(zpools))
            if hdd:
                rpool = create_rpool(hdd)
        if rpool:
            if not pool_is_imported(rpool):
                progress = ProgressMessage(screen, title='Please, wait',  text='Importing ZFS pool "{}"...'.format(rpool))
                zpool_cmd = Popen(['zpool', 'import', '-f', rpool], stderr=PIPE, stdout=PIPE)
                out, err = zpool_cmd.communicate()
                if 0 !=  zpool_cmd.returncode:
                    ButtonChoiceWindow(screen,
                            title='Importing of ZFS pool "{}" failed'.format(rpool),
                            text=err, buttons=['Ok'], width=70)
                    rpool = None


def configure_zfs():
    pass


def choose_hdd(number_of_zpools = 0):
    hdd_items = map(lambda x: '{}: {} {}'.format(x.name, x.capacity, x.description), hdds)
    choice = None

    buttons = []
    buttons.append(('Use selected disk', 'ok'))
    if number_of_zpools > 0:
        buttons.append(('Back', 'zpool'))
    buttons.append(('Cancel', 'cancel'))


    while not choice in ['ok', 'zpool', 'cancel']:
        (choice, hdd_no) = ListboxChoiceWindow(screen,
                title='Choose hard disk drive',
                text='Choose a disk for the root ZFS pool',
                items=hdd_items,
                buttons=buttons,
                width=76,
                scroll=1,
                default=0,
                help=None
                )
        if choice in ['ok', None]:
            choice = configure_partitions(hdds[hdd_no], len(hdds))
            if choice == 'another':
                choice = None

    if choice == 'ok':
        return hdds[hdd_no]
    if choice == 'zpool':
        return None
    raise Abort('HDD choosing')
 
def configure_partitions(hdd, number_of_disks = 1):
    choice = None
    top_text = 'For the root ZFS pool you need a disk containing a Solaris partition (id 0xbf). \
If this disk includes such a partition and you are happy with it, use this disk. \
Otherwise you have to change partitioning. Note that only the first Solaris partition \
will be used.'

    while not choice in ['ok', 'another', 'cancel']:
        part_info = top_text
        part_info += '\n\n'

        have_partitions = len(hdd.partitions) > 0
        have_solaris_partition = False
        buttons = []

        if have_partitions:
            part_info += 'Partitions on {name} {desc} {size}:\n\n'.format(
                    name=hdd.name, desc=hdd.description, size=hdd.capacity)
            part_info += '    TYPE    ID    NAME                     SIZE\n'
            n = 0
            for p in hdd.partitions:
                n = n + 1
                if p.id == 0xBF:
                    have_solaris_partition = True
                part_info += '#{n:<2} {primary} {id:#04x} {system:25} {capacity:10}\n'.format(
                    n=n, capacity=p.capacity, system=p.system, id=p.id,
                    primary=['primary', 'logical'][p.logical])
        else:
            part_info += 'This disk is not partitioned.\n'

        if have_solaris_partition:
            buttons.append(('Use this disk', 'ok'))

        buttons.append(('Edit partitions', 'fdisk'))

        if number_of_disks > 1:
            buttons.append(('Back', 'another'))

        buttons.append(('Cancel', 'cancel'))

        choice = ButtonChoiceWindow(screen,
                title='Partitions of {}'.format(hdd.name),
                text=part_info,
                buttons=buttons,
                width=76,
                help=None
                )
        if choice == 'fdisk':
            screen.suspend()
            call(['cfdisk', hdd.raw_device])
            hdd.reread_partitions()
            screen.resume()
    # end while

    if choice == 'cancel':
        raise Abort('Partitioning')
    return choice



screen = SnackScreen()
screen.pushHelpLine(' ')
try:
    welcome()
    find_hdds()
    configure_rpool()
    configure_zfs()

except Abort as e:
    pass
except NoDisks as e:
    print (e)
finally:
    screen.finish()

sys.exit(0)