00001 #ifndef DISK_H
00002 #define DISK_H
00003
00004 #include <list>
00005
00006 #include "y2storage/Container.h"
00007 #include "y2storage/Partition.h"
00008
00009 namespace storage
00010 {
00011
00012 class Storage;
00013 class SystemCmd;
00014 class ProcPart;
00015 class Region;
00016
00017 class Disk : public Container
00018 {
00019 friend class Storage;
00020 friend class DmPartCo;
00021
00022 struct label_info
00023 {
00024 string name;
00025 bool extended;
00026 unsigned primary;
00027 unsigned logical;
00028 unsigned long long max_size_k;
00029 };
00030
00031 public:
00032 Disk( Storage * const s, const string& Name, unsigned long long Size );
00033 Disk( Storage * const s, const string& Name, unsigned num,
00034 unsigned long long Size, ProcPart& ppart );
00035 Disk( const Disk& rhs );
00036 virtual ~Disk();
00037
00038 unsigned long cylinders() const { return cyl; }
00039 unsigned heads() const { return head; }
00040 unsigned sectors() const { return sector; }
00041 unsigned long numMinor() const { return range; }
00042 unsigned long cylSizeB() const { return byte_cyl; }
00043 unsigned maxPrimary() const { return max_primary; }
00044 bool extendedPossible() const { return ext_possible; }
00045 unsigned maxLogical() const { return max_logical; }
00046 const string& labelName() const { return label; }
00047 const string& udevPath() const { return udev_path; }
00048 const std::list<string>& udevId() const { return udev_id; }
00049 void setSlave( bool val=true ) { dmp_slave=val; }
00050 void setNumMinor( unsigned long val ) { range=val; }
00051 const string& sysfsDir() const { return sysfs_dir; }
00052 unsigned numPartitions() const;
00053 bool isDasd() const { return( nm.find("dasd")==0 ); }
00054 bool isLogical( unsigned nr ) const;
00055 bool detect( ProcPart& ppart );
00056 static storage::CType staticType() { return storage::DISK; }
00057 friend std::ostream& operator<< (std::ostream&, const Disk& );
00058 void triggerUdevUpdate() const;
00059
00060 static bool needP( const string& dev );
00061 static string partNaming(const string& disk);
00062 void setUdevData(const string& path, const list<string>& id);
00063 virtual int createPartition( storage::PartitionType type, long unsigned start,
00064 long unsigned len, string& device,
00065 bool checkRelaxed=false );
00066 int createPartition( long unsigned len, string& device,
00067 bool checkRelaxed=false );
00068 int createPartition( storage::PartitionType type, string& device );
00069 virtual int removePartition( unsigned nr );
00070 virtual int changePartitionId( unsigned nr, unsigned id );
00071 virtual int initializeDisk( bool ) { return storage::DISK_INIT_NOT_POSSIBLE; }
00072 bool initializeDisk() const { return init_disk; }
00073 void resetInitDisk() { init_disk=false; }
00074 int forgetChangePartitionId( unsigned nr );
00075 int changePartitionArea( unsigned nr, unsigned long start,
00076 unsigned long size, bool checkRelaxed=false );
00077 int nextFreePartition(storage::PartitionType type, unsigned& nr,
00078 string& device) const;
00079 int destroyPartitionTable( const string& new_label );
00080 unsigned availablePartNumber(storage::PartitionType type = storage::PRIMARY) const;
00081 virtual void getCommitActions( std::list<storage::commitAction*>& l ) const;
00082 virtual int getToCommit( storage::CommitStage stage,
00083 std::list<Container*>& col,
00084 std::list<Volume*>& vol );
00085 virtual int commitChanges( storage::CommitStage stage );
00086 int commitChanges( storage::CommitStage stage, Volume* vol );
00087 int freeCylindersAfterPartition(const Partition* p, unsigned long& freeCyls) const;
00088 virtual int resizePartition( Partition* p, unsigned long newCyl );
00089 int resizeVolume( Volume* v, unsigned long long newSize );
00090 int removeVolume( Volume* v );
00091 void getUnusedSpace(std::list<Region>& free, bool all = true,
00092 bool logical = false) const;
00093 unsigned int numPrimary() const;
00094 bool hasExtended() const;
00095 unsigned int numLogical() const;
00096 string setDiskLabelText( bool doing=true ) const;
00097 unsigned long long cylinderToKb( unsigned long ) const;
00098 unsigned long kbToCylinder( unsigned long long ) const;
00099 string getPartName( unsigned nr ) const;
00100 void getInfo( storage::DiskInfo& info ) const;
00101 bool equalContent( const Container& rhs ) const;
00102 void logDifference( const Container& d ) const;
00103 Disk& operator= ( const Disk& rhs );
00104 bool FakeDisk() const { return(range==1); }
00105
00106 static string getPartName( const string& disk, unsigned nr );
00107 static string getPartName( const string& disk, const string& nr );
00108 static std::pair<string,unsigned> getDiskPartition( const string& dev );
00109 static unsigned long long maxSizeLabelK( const string& label );
00110
00111 protected:
00112
00113
00114
00115 typedef CastIterator<VIter, Partition *> PartInter;
00116 typedef CastIterator<CVIter, const Partition *> PartCInter;
00117 template< class Pred >
00118 struct PartitionPI { typedef ContainerIter<Pred, PartInter> type; };
00119 template< class Pred >
00120 struct PartitionCPI { typedef ContainerIter<Pred, PartCInter> type; };
00121 typedef CheckFnc<const Partition> CheckFncPartition;
00122 typedef CheckerIterator< CheckFncPartition, PartitionPI<CheckFncPartition>::type,
00123 PartInter, Partition > PartPIterator;
00124 typedef CheckerIterator< CheckFncPartition, PartitionCPI<CheckFncPartition>::type,
00125 PartCInter, const Partition > PartCPIterator;
00126 typedef DerefIterator<PartPIterator,Partition> PartIter;
00127 typedef IterPair<PartIter> PartPair;
00128
00129 PartPair partPair( bool (* CheckPart)( const Partition& )=NULL)
00130 {
00131 return( PartPair( partBegin( CheckPart ), partEnd( CheckPart ) ));
00132 }
00133 PartIter partBegin( bool (* CheckPart)( const Partition& )=NULL)
00134 {
00135 IterPair<PartInter> p( (PartInter(begin())), (PartInter(end())) );
00136 return( PartIter( PartPIterator( p, CheckPart )) );
00137 }
00138 PartIter partEnd( bool (* CheckPart)( const Partition& )=NULL)
00139 {
00140 IterPair<PartInter> p( (PartInter(begin())), (PartInter(end())) );
00141 return( PartIter( PartPIterator( p, CheckPart, true )) );
00142 }
00143
00144 public:
00145 typedef DerefIterator<PartCPIterator,const Partition> ConstPartIter;
00146 typedef IterPair<ConstPartIter> ConstPartPair;
00147 ConstPartPair partPair( bool (* CheckPart)( const Partition& )=NULL) const
00148 {
00149 return( ConstPartPair( partBegin( CheckPart ), partEnd( CheckPart ) ));
00150 }
00151 ConstPartIter partBegin( bool (* CheckPart)( const Partition& )=NULL) const
00152 {
00153 IterPair<PartCInter> p( (PartCInter(begin())), (PartCInter(end())) );
00154 return( ConstPartIter( PartCPIterator( p, CheckPart )) );
00155 }
00156 ConstPartIter partEnd( bool (* CheckPart)( const Partition& )=NULL) const
00157 {
00158 IterPair<PartCInter> p( (PartCInter(begin())), (PartCInter(end())) );
00159 return( ConstPartIter( PartCPIterator( p, CheckPart, true )) );
00160 }
00161
00162 protected:
00163 Disk( Storage * const s, const string& File );
00164 virtual bool detectGeometry();
00165 virtual bool detectPartitions( ProcPart& ppart );
00166 bool getSysfsInfo( const string& SysFsDir );
00167 int checkSystemError( const string& cmd_line, const SystemCmd& cmd );
00168 int execCheckFailed( const string& cmd_line );
00169 int execCheckFailed( SystemCmd& cmd, const string& cmd_line );
00170 bool checkPartedOutput( const SystemCmd& cmd, ProcPart& ppart );
00171 bool scanPartedLine( const string& Line, unsigned& nr,
00172 unsigned long& start, unsigned long& csize,
00173 storage::PartitionType& type,
00174 unsigned& id, bool& boot );
00175 bool checkPartedValid( const ProcPart& pp, const string& diskname,
00176 std::list<Partition*>& pl, unsigned long& rng );
00177 bool getPartedValues( Partition *p );
00178 bool getPartedSectors( const Partition *p, unsigned long long& start,
00179 unsigned long long& end );
00180 const Partition * getPartitionAfter( const Partition * p );
00181 virtual void print( std::ostream& s ) const { s << *this; }
00182 virtual Container* getCopy() const { return( new Disk( *this ) ); }
00183 void getGeometry( const string& line, unsigned long& c,
00184 unsigned& h, unsigned& s );
00185 virtual void redetectGeometry();
00186 void changeNumbers( const PartIter& b, const PartIter& e,
00187 unsigned start, int incr );
00188 int createChecks( storage::PartitionType& type, unsigned long start,
00189 unsigned long len, bool checkRelaxed );
00190 void removePresentPartitions();
00191 void removeFromMemory();
00192 void enlargeGpt();
00193
00194 static bool notDeleted( const Partition&d ) { return( !d.deleted() ); }
00195
00196 virtual int doCreate( Volume* v );
00197 virtual int doRemove( Volume* v );
00198 virtual int doResize( Volume* v );
00199 virtual int doSetType( Volume* v );
00200 virtual int doCreateLabel();
00201
00202 void logData( const string& Dir );
00203 void setLabelData( const string& );
00204
00205 static string defaultLabel( const Storage&, unsigned long long size_k );
00206 static label_info labels[];
00207 static string p_disks[];
00208
00209 unsigned long cyl;
00210 unsigned head;
00211 unsigned sector;
00212 unsigned long new_cyl;
00213 unsigned new_head;
00214 unsigned new_sector;
00215 string label;
00216 string udev_path;
00217 std::list<string> udev_id;
00218 string detected_label;
00219 string system_stderr;
00220 string logfile_name;
00221 string sysfs_dir;
00222 unsigned max_primary;
00223 bool ext_possible;
00224 unsigned max_logical;
00225 bool init_disk;
00226 bool iscsi;
00227 bool dmp_slave;
00228 bool gpt_enlarge;
00229 unsigned long byte_cyl;
00230 unsigned long range;
00231 mutable storage::DiskInfo info;
00232 };
00233
00234 }
00235
00236 #endif