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