summaryrefslogtreecommitdiff
path: root/hscript/disk.hh
blob: 2a43192ee2e37f7d66e907823827c8f7859f5ed6 (plain) (blame)
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
/*
 * disk.hh - Definition of the Key classes for disk manipulation
 * libhscript, the HorizonScript library for
 * Project Horizon
 *
 * Copyright (c) 2019 Adélie Linux and contributors.  All rights reserved.
 * This code is licensed under the AGPL 3.0 license, as noted in the
 * LICENSE-code file in the root directory of this repository.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

#ifndef __HSCRIPT_DISK_HH_
#define __HSCRIPT_DISK_HH_

#include "key.hh"

namespace Horizon {
namespace Keys {

class DiskId : public Key {
private:
    const std::string _block;
    const std::string _ident;

    DiskId(int _line, const std::string &my_block, const std::string &my_i) :
        Key(_line), _block(my_block), _ident(my_i) {}
public:
    /*! Retrieve the block device that this key identifies. */
    const std::string device() const { return this->_block; }
    /*! Retrieve the identification for the block device. */
    const std::string ident() const { return this->_ident; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    bool execute(ScriptOptions) const override;
};

class DiskLabel : public Key {
public:
    /*! The type of disklabel. */
    enum LabelType {
        /*! Apple Partition Map (APM) */
        APM,
        /*! Master Boot Record (MBR) */
        MBR,
        /*! GUID Partition Table (GPT) */
        GPT
    };
private:
    const std::string _block;
    const LabelType _type;

    DiskLabel(int _line, const std::string &_b, const LabelType &_t) :
        Key(_line), _block(_b), _type(_t) {}
public:
    /*! Retrieve the block device that this key identifies. */
    const std::string device() const { return this->_block; }
    /*! Retrieve the type of disklabel for the block device. */
    LabelType type() const { return this->_type; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    bool execute(ScriptOptions) const override;
};

/*! The type of size a disk key has */
enum SizeType {
    /*! Sized in bytes */
    Bytes,
    /*! Sized as a percentage of the whole disk */
    Percent,
    /*! Fill the rest of the disk */
    Fill
};

class Partition : public Key {
public:
    /*! Valid partition type codes */
    enum PartitionType {
        /*! None (default) */
        None = 0,
        /*! Bootable */
        Boot,
        /*! EFI System Partition (GPT only) */
        ESP
    };
private:
    const std::string _block;
    const int _partno;
    const SizeType _size_type;
    const uint64_t _size;
    const PartitionType _type;

    Partition(int _line, const std::string &_b, const int _p,
              const SizeType _st, const uint64_t _s, const PartitionType _pt) :
        Key(_line), _block(_b), _partno(_p), _size_type(_st), _size(_s),
        _type(_pt) {}
public:
    /*! Retrieve the block device that this key identifies. */
    const std::string device() const { return this->_block; }
    /*! Retrieve the partition number that this key identifies. */
    int partno() const { return this->_partno; }
    /*! Retrieve the type of size that this partition uses. */
    SizeType size_type() const { return this->_size_type; }
    /*! Retrieve the size of this partition. */
    uint64_t size() const { return this->_size; }
    /*! Retrieve the Type Code of this partition, if any. */
    PartitionType type() const { return this->_type; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    bool execute(ScriptOptions) const override;
};

class Encrypt : public Key {
private:
    const std::string _block;
    const std::string _pw;

    Encrypt(int _line, const std::string &_b, const std::string &_p) :
        Key(_line), _block(_b), _pw(_p) {}
public:
    /*! Retrieve the block device that this key encrypts. */
    const std::string device() const { return this->_block; }
    /*! Retrieve the passphrase used to encrypt the block device. */
    const std::string passphrase() const { return this->_pw; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    bool execute(ScriptOptions) const override;
};

class LVMPhysical : public StringKey {
private:
    LVMPhysical(int _line, const std::string &_d) : StringKey(_line, _d) {}
public:
    static Key *parseFromData(const std::string &, int, int*, int*);
    bool execute(ScriptOptions) const override;
};

class LVMGroup : public Key {
private:
    const std::string _pv;
    const std::string _vgname;

    LVMGroup(int _line, const std::string &_p, const std::string &_v) :
        Key(_line), _pv(_p), _vgname(_v) {}
public:
    /*! Retrieve the physical volume where this volume group will reside. */
    const std::string pv() const { return this->_pv; }
    /*! Retrieve the name of this volume group. */
    const std::string name() const { return this->_vgname; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    /*! Determine if the PV passed is a real one. */
    bool test_pv(ScriptOptions) const;
    bool execute(ScriptOptions) const override;
};

class LVMVolume : public Key {
private:
    const std::string _vg;
    const std::string _lvname;
    const SizeType _size_type;
    const uint64_t _size;

    LVMVolume(int _line, const std::string &_v, const std::string &_n,
              SizeType _t, uint64_t _s) : Key(_line), _vg(_v), _lvname(_n),
        _size_type(_t), _size(_s) {}
public:
    /*! Retrieve the volume group to which this volume belongs. */
    const std::string vg() const { return this->_vg; }
    /*! Retrieve the name of this volume. */
    const std::string name() const { return this->_lvname; }
    /*! Retrieve the type of size that this volume uses. */
    SizeType size_type() const { return this->_size_type; }
    /*! Retrieve the size of this volume. */
    uint64_t size() const { return this->_size; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    bool execute(ScriptOptions) const override;
};

class Filesystem : public Key {
public:
    enum FilesystemType {
        Ext2,
        Ext3,
        Ext4,
        JFS,
        VFAT,
        XFS
    };
private:
    const std::string _block;
    FilesystemType _type;

    Filesystem(int _line, const std::string &_b, FilesystemType _t) :
        Key(_line), _block(_b), _type(_t) {}
public:
    /*! Retrieve the block device on which to create the filesystem. */
    const std::string device() const { return this->_block; }
    /*! Retreive the type of filesystem to create. */
    FilesystemType fstype() const { return this->_type; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    bool execute(ScriptOptions) const override;
};

class Mount : public Key {
private:
    const std::string _block;
    const std::string _mountpoint;
    const std::string _opts;

    Mount(int _line, const std::string &my_block,
          const std::string &my_mountpoint, const std::string &my_opts = "") :
        Key(_line), _block(my_block), _mountpoint(my_mountpoint),
        _opts(my_opts) {}
public:
    /*! Retrieve the block device to which this mount pertains. */
    const std::string device() const { return this->_block; }
    /*! Retrieve the mountpoint for this mount. */
    const std::string mountpoint() const { return this->_mountpoint; }
    /*! Retrieve the mount options for this mount, if any. */
    const std::string options() const { return this->_opts; }

    static Key *parseFromData(const std::string &, int, int*, int*);
    bool validate(ScriptOptions) const override;
    bool execute(ScriptOptions) const override;
};

}
}

#endif /* !__HSCRIPT_DISK_HH_ */