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