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