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 namespace storage
00009 {
00010
00011 class SystemCmd;
00012 class ProcMounts;
00013 class EtcFstab;
00014 class FstabEntry;
00015 class Container;
00016 class Storage;
00017
00018 class Volume
00019 {
00020 friend class Storage;
00021
00022 public:
00023 Volume( const Container& d, unsigned Pnr, unsigned long long SizeK );
00024 Volume( const Container& d, const string& PName, unsigned long long SizeK );
00025 Volume( const Container& d );
00026 Volume( const Volume& );
00027 Volume& operator=( const Volume& );
00028
00029 virtual ~Volume();
00030
00031 const string& device() const { return dev; }
00032 const string& mountDevice() const;
00033 const string& loopDevice() const { return( loop_dev ); }
00034 const string& dmcryptDevice() const { return( dmcrypt_dev ); }
00035 const Container* getContainer() const { return cont; }
00036 storage::CType cType() const;
00037 bool deleted() const { return del; }
00038 bool created() const { return create; }
00039 bool silent() const { return silnt; }
00040 virtual const std::list<string> udevId() const { return(empty_slist); }
00041 virtual const string& udevPath() const { return(empty_string); }
00042 virtual string sysfsPath() const;
00043 void setDeleted( bool val=true ) { del=val; }
00044 void setCreated( bool val=true ) { create=val; }
00045 void setReadonly( bool val=true ) { ronly=val; }
00046 void setSilent( bool val=true ) { silnt=val; }
00047 bool ignoreFstab() const { return( ignore_fstab ); }
00048 void setIgnoreFstab( bool val=true ) { ignore_fstab=val; }
00049 bool ignoreFs() const { return( ignore_fs ); }
00050 void setIgnoreFs( bool val=true ) { ignore_fs=val; }
00051 void setFstabAdded( bool val=true ) { fstab_added=val; }
00052 bool sameDevice( const string& device ) const;
00053 bool fstabAdded() const { return( fstab_added ); }
00054 const storage::usedBy& getUsedBy() const{ return( uby ); }
00055 storage::UsedByType getUsedByType() const { return( uby.type() ); }
00056 const string& usedByName() const { return( uby.name() ); }
00057 void setUsedBy( storage::UsedByType t, const string& name ) { uby.set( t, name );}
00058 void getFsInfo( const Volume* source );
00059
00060 virtual int setFormat( bool format=true, storage::FsType fs=storage::REISERFS );
00061 void formattingDone() { format=false; detected_fs=fs; }
00062 bool getFormat() const { return format; }
00063 void rename( const string& newName );
00064 int changeFstabOptions( const string& options );
00065 int changeMountBy( storage::MountByType mby );
00066 virtual int changeMount( const string& m );
00067 bool loop() const { return is_loop; }
00068 bool dmcrypt() const { return encryption != ENC_NONE && encryption != ENC_UNKNOWN; }
00069 bool loopActive() const { return( is_loop&&loop_active ); }
00070 bool dmcryptActive() const { return( dmcrypt()&&dmcrypt_active ); }
00071 bool needCrsetup() const;
00072 const string& getUuid() const { return uuid; }
00073 const string& getLabel() const { return label; }
00074 int setLabel( const string& val );
00075 int eraseLabel() { label.erase(); orig_label.erase(); return 0; }
00076 bool needLabel() const { return( label!=orig_label ); }
00077 storage::EncryptType getEncryption() const { return encryption; }
00078 void setEncryption( storage::EncryptType val=storage::ENC_LUKS )
00079 { encryption=orig_encryption=val; }
00080 virtual int setEncryption( bool val, storage::EncryptType typ=storage::ENC_LUKS );
00081 const string& getCryptPwd() const { return crypt_pwd; }
00082 int setCryptPwd( const string& val );
00083 void clearCryptPwd() { crypt_pwd.erase(); }
00084 const string& getMount() const { return mp; }
00085 bool hasOrigMount() const { return !orig_mp.empty(); }
00086 bool needRemount() const;
00087 bool needShrink() const { return(size_k<orig_size_k); }
00088 bool needExtend() const { return(size_k>orig_size_k); }
00089 long long extendSize() const { return(size_k-orig_size_k);}
00090 storage::FsType getFs() const { return fs; }
00091 void setFs( storage::FsType val ) { detected_fs=fs=val; }
00092 void setUuid( const string& id ) { uuid=id; }
00093 void initLabel( const string& lbl ) { label=orig_label=lbl; }
00094 storage::MountByType getMountBy() const { return mount_by; }
00095 const string& getFstabOption() const { return fstab_opt; }
00096 void setFstabOption( const string& val ) { orig_fstab_opt=fstab_opt=val; }
00097 void setMount( const string& val ) { orig_mp=mp=val; }
00098 void updateFstabOptions();
00099 bool needFstabUpdate() const;
00100 const string& getMkfsOption() const { return mkfs_opt; }
00101 int setMkfsOption( const string& val ) { mkfs_opt=val; return 0; }
00102 const string& getTunefsOption() const { return tunefs_opt; }
00103 int setTunefsOption( const string& val ) { tunefs_opt=val; return 0; }
00104 const string& getDescText() const { return dtxt; }
00105 int setDescText( const string& val ) { dtxt=val; return 0; }
00106 const std::list<string>& altNames() const { return( alt_names ); }
00107 unsigned nr() const { return num; }
00108 unsigned long long sizeK() const { return size_k; }
00109 unsigned long long origSizeK() const { return orig_size_k; }
00110 const string& name() const { return nm; }
00111 unsigned long minorNr() const { return mnr; }
00112 unsigned long majorNr() const { return mjr; }
00113 bool isNumeric() const { return numeric; }
00114 void setMajorMinor( unsigned long Major, unsigned long Minor )
00115 { mjr=Major; mnr=Minor; }
00116 void setSize( unsigned long long SizeK ) { size_k=orig_size_k=SizeK; }
00117 virtual void setResizedSize( unsigned long long SizeK ) { size_k=SizeK; }
00118 void setDmcryptDev( const string& dm, bool active );
00119 void setDmcryptDevEnc( const string& dm, storage::EncryptType typ, bool active );
00120 virtual void forgetResize() { size_k=orig_size_k; }
00121 virtual bool canUseDevice() const;
00122
00123 bool operator== ( const Volume& rhs ) const;
00124 bool operator!= ( const Volume& rhs ) const
00125 { return( !(*this==rhs) ); }
00126 bool operator< ( const Volume& rhs ) const;
00127 bool operator<= ( const Volume& rhs ) const
00128 { return( *this<rhs || *this==rhs ); }
00129 bool operator>= ( const Volume& rhs ) const
00130 { return( !(*this<rhs) ); }
00131 bool operator> ( const Volume& rhs ) const
00132 { return( !(*this<=rhs) ); }
00133 bool equalContent( const Volume& rhs ) const;
00134 string logDifference( const Volume& c ) const;
00135 friend std::ostream& operator<< (std::ostream& s, const Volume &v );
00136
00137 int prepareRemove();
00138 int umount( const string& mp="" );
00139 int crUnsetup( bool force=false );
00140 int mount( const string& mp="", bool ro=false );
00141 int canResize( unsigned long long newSizeK ) const;
00142 int doMount();
00143 int doFormat();
00144 int doCrsetup();
00145 int doSetLabel();
00146 int doFstabUpdate();
00147 int resizeFs();
00148 void fstabUpdateDone();
00149 bool isMounted() const { return( is_mounted ); }
00150 virtual string removeText(bool doing=true) const;
00151 virtual string createText(bool doing=true) const;
00152 virtual string resizeText(bool doing=true) const;
00153 virtual string formatText(bool doing=true) const;
00154 virtual void getCommitActions( std::list<storage::commitAction*>& l ) const;
00155 string mountText( bool doing=true ) const;
00156 string labelText( bool doing=true ) const;
00157 string losetupText( bool doing=true ) const;
00158 string crsetupText( bool doing=true ) const;
00159 string fstabUpdateText() const;
00160 string sizeString() const;
00161 string bootMount() const;
00162 bool optNoauto() const;
00163 bool inCryptotab() const { return( encryption!=ENC_NONE &&
00164 encryption!=ENC_LUKS &&
00165 !optNoauto() ); }
00166 bool inCrypttab() const { return( encryption==ENC_LUKS && !optNoauto() ); }
00167 virtual void print( std::ostream& s ) const { s << *this; }
00168 int getFreeLoop();
00169 int getFreeLoop( SystemCmd& loopData );
00170 int getFreeLoop( SystemCmd& loopData, const std::list<unsigned>& ids );
00171 void getInfo( storage::VolumeInfo& info ) const;
00172 void mergeFstabInfo( storage::VolumeInfo& tinfo, const FstabEntry& fste ) const;
00173 void updateFsData();
00174 void triggerUdevUpdate();
00175 static bool loopInUse( Storage* sto, const string& loopdev );
00176
00177 struct SkipDeleted
00178 {
00179 bool operator()(const Volume&d) const { return( !d.deleted());}
00180 };
00181 static SkipDeleted SkipDel;
00182 static bool notDeleted( const Volume&d ) { return( !d.deleted() ); }
00183 static bool isDeleted( const Volume&d ) { return( d.deleted() ); }
00184 static bool getMajorMinor( const string& device,
00185 unsigned long& Major, unsigned long& Minor );
00186 static bool loopStringNum( const string& name, unsigned& num );
00187 static storage::EncryptType toEncType( const string& val );
00188 static storage::FsType toFsType( const string& val );
00189 static storage::MountByType toMountByType( const string& val );
00190 const string& fsTypeString() const { return fs_names[fs]; }
00191 static const string& fsTypeString( const storage::FsType type )
00192 { return fs_names[type]; }
00193 static const string& encTypeString( const storage::EncryptType type )
00194 { return enc_names[type]; }
00195 static const string& mbyTypeString( const storage::MountByType type )
00196 { return mb_names[type]; }
00197 static bool isTmpCryptMp( const string& mp );
00198
00199
00200 protected:
00201 void init();
00202 void setNameDev();
00203 int checkDevice();
00204 int checkDevice( const string& device );
00205 storage::MountByType defaultMountBy( const string& mp="" );
00206 bool allowedMountBy( storage::MountByType mby, const string& mp="" );
00207 void getFsData( SystemCmd& blkidData );
00208 void getLoopData( SystemCmd& loopData );
00209 void getMountData( const ProcMounts& mountData, bool swap_only=false );
00210 void getFstabData( EtcFstab& fstabData );
00211 void getStartData();
00212 void getTestmodeData( const string& data );
00213 void replaceAltName( const string& prefix, const string& newn );
00214 string getMountByString( storage::MountByType mby, const string& dev,
00215 const string& uuid,
00216 const string& label ) const;
00217 string getFstabDevice();
00218 string getFstabDentry();
00219 void getFstabOpts( std::list<string>& ol );
00220 bool getLoopFile( string& fname ) const;
00221 void setExtError( const SystemCmd& cmd, bool serr=true );
00222 string getDmcryptName();
00223 bool needLosetup() const;
00224 bool needCryptsetup() const;
00225 int doLosetup();
00226 int doCryptsetup();
00227 int loUnsetup( bool force=false );
00228 int cryptUnsetup( bool force=false );
00229
00230 std::ostream& logVolume( std::ostream& file ) const;
00231 string getLosetupCmd( storage::EncryptType, const string& pwdfile ) const;
00232 string getCryptsetupCmd( storage::EncryptType e, const string& dmdev,
00233 const string& mp, const string& pwdfile, bool format,
00234 bool empty_pwd=false ) const;
00235 storage::EncryptType detectEncryption();
00236 string getFilesysSysfsPath() const;
00237
00238 const Container* const cont;
00239 bool numeric;
00240 bool create;
00241 bool del;
00242 bool format;
00243 bool silnt;
00244 bool fstab_added;
00245 storage::FsType fs;
00246 storage::FsType detected_fs;
00247 storage::MountByType mount_by;
00248 storage::MountByType orig_mount_by;
00249 string uuid;
00250 string label;
00251 string orig_label;
00252 string mp;
00253 string orig_mp;
00254 string fstab_opt;
00255 string orig_fstab_opt;
00256 string mkfs_opt;
00257 string tunefs_opt;
00258 bool is_loop;
00259 bool is_mounted;
00260 bool ignore_fstab;
00261 bool ignore_fs;
00262 bool loop_active;
00263 bool dmcrypt_active;
00264 bool ronly;
00265 storage::EncryptType encryption;
00266 storage::EncryptType orig_encryption;
00267 string loop_dev;
00268 string dmcrypt_dev;
00269 string fstab_loop_dev;
00270 string crypt_pwd;
00271 string nm;
00272 std::list<string> alt_names;
00273 unsigned num;
00274 unsigned long long size_k;
00275 unsigned long long orig_size_k;
00276 string dev;
00277 string dtxt;
00278 unsigned long mnr;
00279 unsigned long mjr;
00280 storage::usedBy uby;
00281
00282 static string fs_names[storage::FSNONE+1];
00283 static string mb_names[storage::MOUNTBY_PATH+1];
00284 static string enc_names[storage::ENC_UNKNOWN+1];
00285 static string tmp_mount[3];
00286 static string empty_string;
00287 static std::list<string> empty_slist;
00288
00289 mutable storage::VolumeInfo info;
00290 };
00291
00292 }
00293
00294 #endif