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 "storage/Blkid.h"
00027 #include "storage/StorageInterface.h"
00028 #include "storage/StorageTypes.h"
00029 #include "storage/StorageTmpl.h"
00030 #include "storage/Device.h"
00031 #include "storage/Enum.h"
00032
00033
00034 namespace storage
00035 {
00036 using std::list;
00037
00038
00039 class SystemCmd;
00040 class ProcMounts;
00041 class EtcFstab;
00042 class FstabEntry;
00043 class Container;
00044 class Storage;
00045
00046
00047 class Volume : public Device
00048 {
00049 friend class Storage;
00050
00051 public:
00052
00053 Volume(const Container& c, unsigned Pnr, unsigned long long SizeK);
00054 Volume(const Container& c, const string& name, const string& device);
00055 Volume(const Container& c, const string& name, const string& device,
00056 SystemInfo& systeminfo);
00057 Volume(const Container& c, const xmlNode* node);
00058 Volume(const Container& c, const Volume& v);
00059 virtual ~Volume();
00060
00061 void saveData(xmlNode* node) const;
00062
00063 const string& mountDevice() const;
00064 const string& loopDevice() const { return( loop_dev ); }
00065 const string& dmcryptDevice() const { return( dmcrypt_dev ); }
00066
00067 const Container* getContainer() const { return cont; }
00068 Storage* getStorage() const;
00069
00070 storage::CType cType() const;
00071 void setReadonly( bool val=true ) { ronly=val; }
00072 bool ignoreFstab() const { return( ignore_fstab ); }
00073 void setIgnoreFstab( bool val=true ) { ignore_fstab=val; }
00074 bool ignoreFs() const { return( ignore_fs ); }
00075 void setIgnoreFs( bool val=true ) { ignore_fs=val; }
00076 void setFstabAdded( bool val=true ) { fstab_added=val; }
00077 bool fstabAdded() const { return( fstab_added ); }
00078
00079 void getFsInfo( const Volume* source );
00080
00081 virtual int setFormat( bool format=true, storage::FsType fs=storage::REISERFS );
00082 void formattingDone() { format=false; fs=detected_fs; }
00083 bool getFormat() const { return format; }
00084 int changeFstabOptions( const string& options );
00085 int changeMountBy( storage::MountByType mby );
00086 virtual int changeMount( const string& m );
00087 bool loop() const { return is_loop; }
00088 bool dmcrypt() const { return encryption != ENC_NONE && encryption != ENC_UNKNOWN; }
00089 bool loopActive() const { return( is_loop&&loop_active ); }
00090 bool dmcryptActive() const { return( dmcrypt()&&dmcrypt_active ); }
00091 bool needCrsetup( bool urgent=true ) const;
00092 const string& getUuid() const { return uuid; }
00093 const string& getLabel() const { return label; }
00094 int setLabel( const string& val );
00095 int eraseLabel() { label.erase(); orig_label.erase(); return 0; }
00096 int eraseUuid() { uuid.erase(); orig_uuid.erase(); return 0; }
00097 bool needLabel() const { return( label!=orig_label ); }
00098 storage::EncryptType getEncryption() const { return encryption; }
00099 void initEncryption( storage::EncryptType val=storage::ENC_LUKS )
00100 { encryption=orig_encryption=val; }
00101 virtual int setEncryption(bool val, storage::EncryptType typ = storage::ENC_LUKS );
00102 const string& getCryptPwd() const { return crypt_pwd; }
00103 int setCryptPwd( const string& val );
00104 void clearCryptPwd() { crypt_pwd.erase(); orig_crypt_pwd.erase(); }
00105 bool needCryptPwd() const;
00106 const string& getMount() const { return mp; }
00107 bool hasOrigMount() const { return !orig_mp.empty(); }
00108 bool needRemount() const;
00109 bool needShrink() const { return !del && size_k<orig_size_k; }
00110 bool needExtend() const { return !del && size_k>orig_size_k; }
00111 long long extendSize() const { return size_k-orig_size_k; }
00112 storage::FsType getFs() const { return fs; }
00113 storage::FsType detectedFs() const { return detected_fs; }
00114 void setFs( storage::FsType val ) { detected_fs=fs=val; }
00115 void initUuid( const string& id ) { uuid=orig_uuid=id; }
00116 void initLabel( const string& lbl ) { label=orig_label=lbl; }
00117 storage::MountByType getMountBy() const { return mount_by; }
00118 const string& getFstabOption() const { return fstab_opt; }
00119 void setFstabOption( const string& val ) { orig_fstab_opt=fstab_opt=val; }
00120 unsigned fstabFreq() const;
00121 unsigned fstabPassno() const;
00122 void setMount( const string& val ) { orig_mp=mp=val; }
00123 void updateFstabOptions();
00124 bool needFstabUpdate() const;
00125 const string& getMkfsOption() const { return mkfs_opt; }
00126 int setMkfsOption( const string& val ) { mkfs_opt=val; return 0; }
00127 const string& getTunefsOption() const { return tunefs_opt; }
00128 int setTunefsOption( const string& val ) { tunefs_opt=val; return 0; }
00129 const string& getDescText() const { return dtxt; }
00130 int setDescText( const string& val ) { dtxt=val; return 0; }
00131 unsigned nr() const { return num; }
00132 unsigned long long origSizeK() const { return orig_size_k; }
00133 bool isNumeric() const { return numeric; }
00134 void setSize( unsigned long long SizeK ) { size_k=orig_size_k=SizeK; }
00135 virtual void setResizedSize( unsigned long long SizeK ) { size_k=SizeK; }
00136 void setUsedByUuid( UsedByType type, const string& uuid );
00137 void setDmcryptDev( const string& dm, bool active );
00138 void setDmcryptDevEnc( const string& dm, storage::EncryptType typ, bool active );
00139 virtual void forgetResize() { size_k=orig_size_k; }
00140 virtual bool canUseDevice() const;
00141
00142 bool operator== ( const Volume& rhs ) const;
00143 bool operator!= ( const Volume& rhs ) const
00144 { return( !(*this==rhs) ); }
00145 bool operator< ( const Volume& rhs ) const;
00146 bool operator<= ( const Volume& rhs ) const
00147 { return( *this<rhs || *this==rhs ); }
00148 bool operator>= ( const Volume& rhs ) const
00149 { return( !(*this<rhs) ); }
00150 bool operator> ( const Volume& rhs ) const
00151 { return( !(*this<=rhs) ); }
00152
00153 bool equalContent(const Volume& rhs) const;
00154
00155 void logDifference(std::ostream& log, const Volume& rhs) const;
00156
00157 friend std::ostream& operator<< (std::ostream& s, const Volume &v );
00158
00159 int prepareRemove();
00160 int unaccessVol();
00161 int umount( const string& mp="" );
00162 int crUnsetup( bool force=false );
00163 int mount( const string& mp="", bool ro=false );
00164 int canResize( unsigned long long newSizeK ) const;
00165 int doMount();
00166 int doFormat();
00167 int doCrsetup();
00168 int doSetLabel();
00169 int doFstabUpdate(bool force=false);
00170 int resizeFs();
00171 void fstabUpdateDone();
00172 bool isMounted() const { return( is_mounted ); }
00173 virtual Text removeText(bool doing) const;
00174 virtual Text createText(bool doing) const;
00175 virtual Text resizeText(bool doing) const;
00176 virtual Text formatText(bool doing) const;
00177 virtual void getCommitActions(list<commitAction>& l) const;
00178 virtual Text mountText(bool doing) const;
00179 Text labelText(bool doing) const;
00180 Text losetupText(bool doing) const;
00181 Text crsetupText(bool doing) const;
00182 Text fstabUpdateText() const;
00183 bool optNoauto() const;
00184 bool inCryptotab() const { return( encryption!=ENC_NONE &&
00185 encryption!=ENC_LUKS &&
00186 !optNoauto() ); }
00187 bool inCrypttab() const { return( encryption==ENC_LUKS ); }
00188 bool pvEncryption() const;
00189 virtual void print( std::ostream& s ) const { s << *this; }
00190 int getFreeLoop();
00191 int getFreeLoop( SystemCmd& loopData );
00192 int getFreeLoop( SystemCmd& loopData, const std::list<unsigned>& ids );
00193 void getInfo( storage::VolumeInfo& info ) const;
00194 void mergeFstabInfo( storage::VolumeInfo& tinfo, const FstabEntry& fste ) const;
00195 void updateFsData( bool setUsedByLvm=false );
00196 void triggerUdevUpdate() const;
00197 static bool loopInUse(const Storage* sto, const string& loopdev);
00198
00199 static bool notDeleted( const Volume&d ) { return( !d.deleted() ); }
00200 static bool isDeleted( const Volume&d ) { return( d.deleted() ); }
00201
00202 static bool loopStringNum( const string& name, unsigned& num );
00203
00204 const string& fsTypeString() const { return toString(fs); }
00205
00206 static bool isTmpCryptMp( const string& mp );
00207
00208 protected:
00209 void init();
00210 void setNameDev();
00211 int checkDevice() const;
00212 int checkDevice(const string& device) const;
00213 MountByType defaultMountBy() const;
00214 bool allowedMountBy(MountByType mby) const;
00215 bool findBlkid( const Blkid& blkid, Blkid::Entry& entry );
00216 void getFsData(const Blkid& blkid, bool setUsedByLvm=false );
00217 void getLoopData( SystemCmd& loopData );
00218 void getMountData(const ProcMounts& mounts, bool swap_only = false);
00219 void getFstabData( EtcFstab& fstabData );
00220 void updateUuid( const string& new_uuid );
00221 void replaceAltName( const string& prefix, const string& newn );
00222 string getMountByString() const;
00223 string getFstabDevice() const;
00224 string getFstabDentry() const;
00225 list<string> getFstabOpts() const;
00226 bool getLoopFile( string& fname ) const;
00227 void setExtError( const SystemCmd& cmd, bool serr=true );
00228 string getDmcryptName() const;
00229 bool needLosetup( bool urgent ) const;
00230 bool needCryptsetup() const;
00231 int doLosetup();
00232 int doCryptsetup();
00233 int loUnsetup( bool force=false );
00234 int cryptUnsetup( bool force=false );
00235 bool pwdLengthOk( storage::EncryptType typ, const string& val,
00236 bool format ) const;
00237 bool noFreqPassno() const;
00238 int prepareTmpMount( string& m, bool& needUmount, bool useMounted=true,
00239 const string& options="" );
00240 int umountTmpMount( const string& m, int ret );
00241 int doFormatBtrfs();
00242
00243 string getLosetupCmd( storage::EncryptType, const string& pwdfile ) const;
00244 string getCryptsetupCmd( storage::EncryptType e, const string& dmdev,
00245 const string& mp, const string& pwdfile, bool format,
00246 bool empty_pwd=false ) const;
00247 storage::EncryptType detectEncryption();
00248 string getFilesysSysfsPath() const;
00249
00250 const Container* const cont;
00251 bool numeric;
00252 bool format;
00253 bool fstab_added;
00254 storage::FsType fs;
00255 storage::FsType detected_fs;
00256 storage::MountByType mount_by;
00257 storage::MountByType orig_mount_by;
00258 string uuid;
00259 string orig_uuid;
00260 string label;
00261 string orig_label;
00262 string mp;
00263 string orig_mp;
00264 string fstab_opt;
00265 string orig_fstab_opt;
00266 string mkfs_opt;
00267 string tunefs_opt;
00268 bool is_loop;
00269 bool is_mounted;
00270 bool ignore_fstab;
00271 bool ignore_fs;
00272 bool loop_active;
00273 bool dmcrypt_active;
00274 bool ronly;
00275 storage::EncryptType encryption;
00276 storage::EncryptType orig_encryption;
00277 string loop_dev;
00278 string dmcrypt_dev;
00279 string fstab_loop_dev;
00280 string crypt_pwd;
00281 string orig_crypt_pwd;
00282 unsigned num;
00283 unsigned long long orig_size_k;
00284 string dtxt;
00285
00286 static const string tmp_mount[3];
00287 static const string ignore_opt[1];
00288 static const string ignore_beg[4];
00289
00290 mutable storage::VolumeInfo info;
00291
00292 private:
00293
00294 Volume(const Volume&);
00295 Volume& operator=(const Volume&);
00296
00297 };
00298
00299 }
00300
00301 #endif