00001 #ifndef VOLUME_H
00002 #define VOLUME_H
00003
00004 #include "y2storage/StorageInterface.h"
00005 #include "y2storage/StorageTypes.h"
00006 #include "y2storage/StorageTmpl.h"
00007
00008 class SystemCmd;
00009 class ProcMounts;
00010 class EtcFstab;
00011 class FstabEntry;
00012 class Container;
00013 class Storage;
00014
00015 class Volume
00016 {
00017 friend class Storage;
00018
00019 public:
00020 Volume( const Container& d, unsigned Pnr, unsigned long long SizeK );
00021 Volume( const Container& d, const string& PName, unsigned long long SizeK );
00022 Volume( const Container& d );
00023 Volume( const Volume& );
00024 Volume& operator=( const Volume& );
00025
00026 virtual ~Volume();
00027
00028 const string& device() const { return dev; }
00029 const string& mountDevice() const { return( is_loop?loop_dev:dev ); }
00030 const string& loopDevice() const { return( loop_dev ); }
00031 const Container* getContainer() const { return cont; }
00032 storage::CType cType() const;
00033 bool deleted() const { return del; }
00034 bool created() const { return create; }
00035 void setDeleted( bool val=true ) { del=val; }
00036 void setCreated( bool val=true ) { create=val; }
00037 void setReadonly( bool val=true ) { ronly=val; }
00038 bool ignoreFstab() const { return( ignore_fstab ); }
00039 void setIgnoreFstab( bool val=true ) { ignore_fstab=val; }
00040 void setFstabAdded( bool val=true ) { fstab_added=val; }
00041 bool fstabAdded() const { return( fstab_added ); }
00042 const storage::usedBy& getUsedBy() const{ return( uby ); }
00043 storage::UsedByType getUsedByType() const { return( uby.type() ); }
00044 const string& usedByName() const { return( uby.name() ); }
00045 void setUsedBy( storage::UsedByType t, const string& name ) { uby.set( t, name );}
00046
00047 virtual int setFormat( bool format=true, storage::FsType fs=storage::REISERFS );
00048 void formattingDone() { format=false; detected_fs=fs; }
00049 bool getFormat() const { return format; }
00050 int changeFstabOptions( const string& options );
00051 int changeMountBy( storage::MountByType mby );
00052 virtual int changeMount( const string& m );
00053 bool loop() const { return is_loop; }
00054 bool loopActive() const { return( is_loop&&loop_active ); }
00055 bool needLosetup() const { return is_loop!=loop_active; }
00056 const string& getUuid() const { return uuid; }
00057 const string& getLabel() const { return label; }
00058 int setLabel( const string& val );
00059 bool needLabel() const { return( label!=orig_label ); }
00060 storage::EncryptType getEncryption() const { return encryption; }
00061 void setEncryption( storage::EncryptType val=storage::ENC_TWOFISH )
00062 { encryption=orig_encryption=val; }
00063 int setEncryption( bool val );
00064 const string& getCryptPwd() const { return crypt_pwd; }
00065 int setCryptPwd( const string& val );
00066 void clearCryptPwd() { crypt_pwd.erase(); }
00067 const string& getMount() const { return mp; }
00068 bool needRemount() const;
00069 bool needShrink() const { return(size_k<orig_size_k); }
00070 bool needExtend() const { return(size_k>orig_size_k); }
00071 long long extendSize() const { return(orig_size_k-size_k);}
00072 storage::FsType getFs() const { return fs; }
00073 void setFs( storage::FsType val ) { detected_fs=fs=val; }
00074 storage::MountByType getMountBy() const { return mount_by; }
00075 const string& getFstabOption() const { return fstab_opt; }
00076 void setFstabOption( const string& val ) { fstab_opt=val; }
00077 bool needFstabUpdate() const
00078 { return( !ignore_fstab &&
00079 (fstab_opt!=orig_fstab_opt || mount_by!=orig_mount_by ||
00080 encryption!=orig_encryption) ); }
00081 const string& getMkfsOption() const { return mkfs_opt; }
00082 int setMkfsOption( const string& val ) { mkfs_opt=val; return 0; }
00083 const std::list<string>& altNames() const { return( alt_names ); }
00084 unsigned nr() const { return num; }
00085 unsigned long long sizeK() const { return size_k; }
00086 unsigned long long origSizeK() const { return orig_size_k; }
00087 const string& name() const { return nm; }
00088 unsigned long minorNr() const { return mnr; }
00089 unsigned long majorNr() const { return mjr; }
00090 void setMajorMinor( unsigned long Major, unsigned long Minor )
00091 { mjr=Major; mnr=Minor; }
00092 void setSize( unsigned long long SizeK ) { size_k=orig_size_k=SizeK; }
00093 virtual void setResizedSize( unsigned long long SizeK ) { size_k=SizeK; }
00094 virtual void forgetResize() { size_k=orig_size_k; }
00095 virtual bool canUseDevice() const;
00096
00097 bool operator== ( const Volume& rhs ) const;
00098 bool operator!= ( const Volume& rhs ) const
00099 { return( !(*this==rhs) ); }
00100 bool operator< ( const Volume& rhs ) const;
00101 bool operator<= ( const Volume& rhs ) const
00102 { return( *this<rhs || *this==rhs ); }
00103 bool operator>= ( const Volume& rhs ) const
00104 { return( !(*this<rhs) ); }
00105 bool operator> ( const Volume& rhs ) const
00106 { return( !(*this<=rhs) ); }
00107 bool equalContent( const Volume& rhs ) const;
00108 string logDifference( const Volume& c ) const;
00109 friend std::ostream& operator<< (std::ostream& s, const Volume &v );
00110
00111 int prepareRemove();
00112 int umount( const string& mp="" );
00113 int loUnsetup();
00114 int mount( const string& mp="" );
00115 int canResize( unsigned long long newSizeK ) const;
00116 int doMount();
00117 int doFormat();
00118 int doLosetup();
00119 int doSetLabel();
00120 int doFstabUpdate();
00121 int resizeFs();
00122 void fstabUpdateDone();
00123 bool isMounted() const { return( is_mounted ); }
00124 virtual string removeText(bool doing=true) const;
00125 virtual string createText(bool doing=true) const;
00126 virtual string resizeText(bool doing=true) const;
00127 virtual string formatText(bool doing=true) const;
00128 virtual void getCommitActions( std::list<storage::commitAction*>& l ) const;
00129 string mountText( bool doing=true ) const;
00130 string labelText( bool doing=true ) const;
00131 string losetupText( bool doing=true ) const;
00132 string fstabUpdateText() const;
00133 string sizeString() const;
00134 string bootMount() const;
00135 bool optNoauto() const;
00136 bool inCrypto() const { return( is_loop && !optNoauto() ); }
00137 virtual void print( std::ostream& s ) const { s << *this; }
00138 int getFreeLoop();
00139 void getInfo( storage::VolumeInfo& info ) const;
00140 void mergeFstabInfo( storage::VolumeInfo& tinfo, const FstabEntry& fste ) const;
00141 static bool loopInUse( Storage* sto, const string& loopdev );
00142
00143 struct SkipDeleted
00144 {
00145 bool operator()(const Volume&d) const { return( !d.deleted());}
00146 };
00147 static SkipDeleted SkipDel;
00148 static bool notDeleted( const Volume&d ) { return( !d.deleted() ); }
00149 static bool isDeleted( const Volume&d ) { return( d.deleted() ); }
00150 static bool getMajorMinor( const string& device,
00151 unsigned long& Major, unsigned long& Minor );
00152 static bool loopStringNum( const string& name, unsigned& num );
00153 static storage::EncryptType toEncType( const string& val );
00154 static storage::FsType toFsType( const string& val );
00155 static storage::MountByType toMountByType( const string& val );
00156 const string& fsTypeString() const { return fs_names[fs]; }
00157 static const string& fsTypeString( const storage::FsType type )
00158 { return fs_names[type]; }
00159 static const string& encTypeString( const storage::EncryptType type )
00160 { return enc_names[type]; }
00161 static const string& mbyTypeString( const storage::MountByType type )
00162 { return mb_names[type]; }
00163
00164
00165 protected:
00166 void init();
00167 void setNameDev();
00168 int checkDevice();
00169 int checkDevice( const string& device );
00170 void getFsData( SystemCmd& blkidData );
00171 void getLoopData( SystemCmd& loopData );
00172 void getMountData( const ProcMounts& mountData );
00173 void getFstabData( EtcFstab& fstabData );
00174 void getTestmodeData( const string& data );
00175 string getMountByString( storage::MountByType mby, const string& dev,
00176 const string& uuid, const string& label ) const;
00177 void setExtError( const SystemCmd& cmd, bool serr=true );
00178
00179 std::ostream& logVolume( std::ostream& file ) const;
00180 string getLosetupCmd( storage::EncryptType e, const string& pwdfile ) const;
00181 storage::EncryptType detectLoopEncryption();
00182
00183 const Container* const cont;
00184 bool numeric;
00185 bool create;
00186 bool del;
00187 bool format;
00188 bool silent;
00189 bool fstab_added;
00190 storage::FsType fs;
00191 storage::FsType detected_fs;
00192 storage::MountByType mount_by;
00193 storage::MountByType orig_mount_by;
00194 string uuid;
00195 string label;
00196 string orig_label;
00197 string mp;
00198 string orig_mp;
00199 string fstab_opt;
00200 string orig_fstab_opt;
00201 string mkfs_opt;
00202 bool is_loop;
00203 bool is_mounted;
00204 bool ignore_fstab;
00205 bool loop_active;
00206 bool ronly;
00207 storage::EncryptType encryption;
00208 storage::EncryptType orig_encryption;
00209 string loop_dev;
00210 string fstab_loop_dev;
00211 string crypt_pwd;
00212 string nm;
00213 std::list<string> alt_names;
00214 unsigned num;
00215 unsigned long long size_k;
00216 unsigned long long orig_size_k;
00217 string dev;
00218 unsigned long mnr;
00219 unsigned long mjr;
00220 storage::usedBy uby;
00221
00222 static string fs_names[storage::FSNONE+1];
00223 static string mb_names[storage::MOUNTBY_LABEL+1];
00224 static string enc_names[storage::ENC_UNKNOWN+1];
00225
00226 mutable storage::VolumeInfo info;
00227 };
00228
00229 #endif