Skip to content

Commit

Permalink
Merge pull request #11 from AhmedKamal1432/amahi-8
Browse files Browse the repository at this point in the history
 Shift the start_sector if it on the partition_table size
  • Loading branch information
cpg committed Aug 20, 2015
2 parents b7680fe + 6d0a181 commit 7dc6ed4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/controllers/disk_wizard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ def process_disk
# send start sector as to be able to know which partition to mount
device = Device.find_with_unallocated(user_selections['disk'])
@selected_disk = (device.partitions.select{|part| part.identifier == user_selections['identifier'] }).first
start_sector = @selected_disk.start_sector
end_sector = @selected_disk.end_sector
end

para = {path: path, label: label, start_sector: start_sector}
para = {path: path, label: label, end_sector: end_sector}
job_name = :mount_job
jobs_queue.enqueue({job_name: job_name, job_para: para})
end
Expand Down
20 changes: 7 additions & 13 deletions app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
class Device #< ActiveRecord::Base
include Operation

MEGA_BYTE = 1024 *1024
TERA_BYTE = 1024 *1024 * 1024 *1024
# 2 Tera byte is the edge between MBR and GPT
GPT_EDGE = 2 * TERA_BYTE

# Device attributes:
# vendor : Device vendor i.e. Western Digital Technologies
# model : Device model i.e. WDBLWE0120JCH
Expand Down Expand Up @@ -85,8 +80,12 @@ def full_format fstype, label = nil
end

def create_partition(size = nil, type = Partition.PartitionType[:TYPE_PRIMARY])
# Shift start sector if it is on the patition table size
new_start_sector = [self.megabyte_to_sectors(PARTITION_TABLE_SIZE_MB), size[:start_sector].to_i].max
raise "cannot create a partition with negative size" if size[:end_sector].to_i < new_start_sector

old_partitions = Device.find(self.path).partitions
DiskUtils.create_partition self, size[:start_sector], size[:end_sector]
DiskUtils.create_partition self, new_start_sector, size[:end_sector]
new_partitions = Device.find(self.path).partitions
# Return the newest partitions that is just add to the device
return new_partitions.reject {|part| old_partitions.include? part}.first
Expand Down Expand Up @@ -124,10 +123,10 @@ def mount_job params_hash
Device.progress = 60
kname = @kname
DebugLogger.info "|#{self.class.name}|>|#{__method__}|:New partition Label #{params_hash[:label]}"
unless params_hash[:start_sector].blank?
unless params_hash[:end_sector].blank?
# mount new partition
device = Device.find_with_unallocated "/dev/#{self.kname}"
new_partition = device.partitions.select{|part| part.start_sector.to_i == params_hash[:start_sector].to_i}.first
new_partition = device.partitions.select{|part| part.end_sector.to_i == params_hash[:end_sector].to_i}.first
else
new_partition = self.partitions.last
end
Expand All @@ -141,11 +140,6 @@ def delete_all_partitions
end
end

def megabyte_to_sectors number_of_megas
sector_size = DiskUtils.get_sector_size self.kname
return (MEGA_BYTE * number_of_megas) / sector_size.to_i
end

class << self
def find_with_unallocated device_path
DebugLogger.info "|#{self.class.name}|>|#{__method__}|:find = #{device_path}"
Expand Down
22 changes: 21 additions & 1 deletion lib/modules/operation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
module Operation

MEGA_BYTE = 1024 *1024
TERA_BYTE = 1024 *1024 * 1024 *1024
# 2 Tera byte is the edge between MBR and GPT
GPT_EDGE = 2 * TERA_BYTE

# the space in MB that we let it to the partition table
PARTITION_TABLE_SIZE_MB = 2

DRIVE_MOUNT_ROOT = "/var/hda/files/drives"

# Remove the partition from device/disk
def delete
#TODO: remove fstab entry if disk is permanently mounted
Expand Down Expand Up @@ -43,6 +53,16 @@ def self.included(base)
base.extend(ClassMethods)
end

def megabyte_to_sectors number_of_megas
if self.instance_of? Partition
device = self.device
else
device = self
end
sector_size = DiskUtils.get_sector_size device.kname
return (MEGA_BYTE * number_of_megas) / sector_size.to_i
end

def pre_checks_job params_hash
DebugLogger.info "|#{self.class.name}|>|#{__method__}|:Params_hash #{params_hash}"
# TODO: Implement rollback mechanism, if something went wrong bring back the system to original state,where it was before stating DW
Expand Down Expand Up @@ -70,7 +90,7 @@ def pre_checks_job params_hash
def create_new_partition_job params_hash
device = Device.find params_hash[:path]
raise "We don't support GPT yet" if device.partition_table != "msdos"
raise "We don't support extended partitions yet, The number of partitions >= 3" if device.partition_count > 2
raise "You already reached partitions count limit, you have #{device.partition_count} partitions" if device.partition_count >= 4
DebugLogger.info "|#{self.class.name}|>|#{__method__}|:Params_hash #{params_hash}"

device = Device.find_with_unallocated params_hash[:path]
Expand Down

0 comments on commit 7dc6ed4

Please sign in to comment.