~rvb/maas/dj-migrations

« back to all changes in this revision

Viewing changes to src/maasserver/models/partition.py

  • Committer: Raphael Badin
  • Date: 2015-03-13 15:22:22 UTC
  • mfrom: (3631.1.15 maas)
  • Revision ID: raphael.badin@canonical.com-20150313152222-nhq1vs6hop1ooq7e
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2015 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Model for a partition in a partition table."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
str = None
 
13
 
 
14
__metaclass__ = type
 
15
__all__ = [
 
16
    'Partition',
 
17
    ]
 
18
 
 
19
from uuid import uuid4
 
20
 
 
21
from django.db.models import (
 
22
    BigIntegerField,
 
23
    BooleanField,
 
24
    CharField,
 
25
    ForeignKey,
 
26
    )
 
27
from maasserver import DefaultMeta
 
28
from maasserver.enum import PARTITION_TABLE_TYPE
 
29
from maasserver.models.cleansave import CleanSave
 
30
from maasserver.models.partitiontable import PartitionTable
 
31
from maasserver.models.timestampedmodel import TimestampedModel
 
32
 
 
33
 
 
34
class Partition(CleanSave, TimestampedModel):
 
35
    """A partition in a partition table.
 
36
 
 
37
    :ivar partition_table: `PartitionTable` this partition belongs to.
 
38
    :ivar uuid: UUID of the partition if it's part of a GPT partition.
 
39
    :ivar start_offset: Offset in bytes the partition is from the start of
 
40
        the disk.
 
41
    :ivar size: Size of the partition in bytes.
 
42
    :ivar bootable: Whether the partition is set as bootable.
 
43
    """
 
44
 
 
45
    class Meta(DefaultMeta):
 
46
        """Needed for South to recognize this model."""
 
47
 
 
48
    partition_table = ForeignKey(
 
49
        PartitionTable, null=False, blank=False, related_name="partitions")
 
50
 
 
51
    uuid = CharField(
 
52
        max_length=36, unique=True, null=True, blank=True)
 
53
 
 
54
    start_offset = BigIntegerField(null=False)
 
55
 
 
56
    size = BigIntegerField(null=False)
 
57
 
 
58
    bootable = BooleanField(default=False)
 
59
 
 
60
    def get_node(self):
 
61
        """`Node` this partition belongs to."""
 
62
        return self.partition_table.get_node()
 
63
 
 
64
    def get_block_size(self):
 
65
        """Block size of partition."""
 
66
        return self.partition_table.get_block_size()
 
67
 
 
68
    def save(self, *args, **kwargs):
 
69
        """Save partition."""
 
70
        if (self.partition_table.table_type == PARTITION_TABLE_TYPE.GPT and
 
71
                not self.uuid):
 
72
            # Partition is part of a GPT partition table and doesn't have
 
73
            # a UUID set. Set the UUID so MAAS will know the UUID of the
 
74
            # partition on the created machine.
 
75
            self.uuid = uuid4()
 
76
        return super(Partition, self).save(*args, **kwargs)