From mboxrd@z Thu Jan 1 00:00:00 1970 From: kupcevic@sourceware.org Date: 26 Sep 2006 03:02:59 -0000 Subject: [Cluster-devel] conga/ricci/modules/storage ContentFS.cpp Cont ... Message-ID: <20060926030259.25044.qmail@sourceware.org> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit CVSROOT: /cvs/cluster Module name: conga Changes by: kupcevic at sourceware.org 2006-09-26 03:02:57 Modified files: ricci/modules/storage: ContentFS.cpp ContentFS.h ExtendedFS.cpp ExtendedFS.h FSController.cpp Props.cpp Props.h SwapFS.cpp SwapFS.h defines.h Log message: storage module: - unify mount handling - add pretty names to filesystem types Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/ContentFS.cpp.diff?cvsroot=cluster&r1=1.3&r2=1.4 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/ContentFS.h.diff?cvsroot=cluster&r1=1.5&r2=1.6 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/ExtendedFS.cpp.diff?cvsroot=cluster&r1=1.4&r2=1.5 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/ExtendedFS.h.diff?cvsroot=cluster&r1=1.3&r2=1.4 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/FSController.cpp.diff?cvsroot=cluster&r1=1.5&r2=1.6 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/Props.cpp.diff?cvsroot=cluster&r1=1.3&r2=1.4 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/Props.h.diff?cvsroot=cluster&r1=1.3&r2=1.4 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/SwapFS.cpp.diff?cvsroot=cluster&r1=1.4&r2=1.5 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/SwapFS.h.diff?cvsroot=cluster&r1=1.3&r2=1.4 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/defines.h.diff?cvsroot=cluster&r1=1.5&r2=1.6 --- conga/ricci/modules/storage/ContentFS.cpp 2006/08/10 22:53:09 1.3 +++ conga/ricci/modules/storage/ContentFS.cpp 2006/09/26 03:02:57 1.4 @@ -27,17 +27,23 @@ #include "ContentNone.h" #include "BDFactory.h" #include "defines.h" +#include "utils.h" using namespace std; -ContentFS::ContentFS(const String& fs_name, const String& path) : +#define ILLEGAL_MOUNT_CHARS String(" <>?'\":;|}]{[)(*&^%$#@!~`+=") + + + +ContentFS::ContentFS(const String& path, + const String& name, + const String& module) : Content(CONTENT_FS_TYPE, path), - _name(fs_name) -{ - -} + _name(name), + _module(module) +{} ContentFS::~ContentFS() {} @@ -85,7 +91,6 @@ } - XMLObject ContentFS::xml() const { @@ -97,13 +102,185 @@ +void +ContentFS::mount_props_probe(const String& path, + const String& fsname, + Props& props) +{ + FstabLocker l; + MountHandler mh; + + // mountpoint + pair maj_min = mh.maj_min(path); + list mounts = mh.mounts(maj_min); + String mountpoint; + if (mounts.size()) + mountpoint = mounts.front().mountpoint; + list ill_mnts = mh.used_dirs(); + ill_mnts.remove(mountpoint); + // fstab + list fstabs = mh.fstabs(maj_min); + String fstabpoint; + if (fstabs.size()) + fstabpoint = fstabs.front().mountpoint; + ill_mnts.remove(fstabpoint); + + + + bool mountable = mount_fs_supported(fsname); + props.set(Variable("mountable", mountable)); + if (mountable) + props.set(Variable("mountpoint", + mountpoint, + 0, + 128, + ILLEGAL_MOUNT_CHARS, + ill_mnts)); + if (mountable || + !fstabpoint.empty()) + props.set(Variable("fstabpoint", + fstabpoint, + 0, + 128, + ILLEGAL_MOUNT_CHARS, + ill_mnts)); +} + +void +ContentFS::mount_props_apply(const String& path, + const String& fsname, + const Props& old_props, + const Props& new_props) +{ + FstabLocker l; + + // mountpoint + if (old_props.has("mountpoint")) { + String mnt_curr = old_props.get("mountpoint").get_string(); + String mnt_new = new_props.get("mountpoint").get_string(); + if (mnt_curr != mnt_new) { + MountHandler mh; + if (mnt_curr.size()) + if (!mh.umount(path, mnt_curr)) + throw UMountError(mnt_curr); + if (mnt_new.size()) + if (!mh.mount(path, mnt_new, fsname)) + throw String("mount failed"); + } + } + + // fstab + if (old_props.has("fstabpoint")) { + String mnt_curr = old_props.get("fstabpoint").get_string(); + String mnt_new = new_props.get("fstabpoint").get_string(); + if (mnt_curr != mnt_new) { + MountHandler mh; + if (mnt_curr.size()) { + list l = mh.fstabs(mh.maj_min(path)); + for (list::const_iterator iter = l.begin(); + iter != l.end(); + iter++) + if (iter->mountpoint == mnt_curr) + mh.fstab_remove(iter->devname, mnt_curr); + } + if (mnt_new.size()) + mh.fstab_add(path, mnt_new, fsname); + } + } +} + +bool +ContentFS::mount_props_mounted(const Props& props) +{ + if (props.has("mountpoint")) + return props.get("mountpoint").get_string().size(); + return false; +} + + + + + + + + ContentFSTemplate::ContentFSTemplate(const String& name) : ContentTemplate(CONTENT_FS_TYPE), - name(name) + _name(name) { - attrs["fs_type"] = name; + attrs["fs_type"] = _name; } ContentFSTemplate::~ContentFSTemplate() {} + + + +void +ContentFSTemplate::mount_props_template(const String& fsname, + Props& props) +{ + bool mountable = ContentFS::mount_fs_supported(fsname); + props.set(Variable("mountable", mountable)); + + if (mountable) { + list illegal_mps = MountHandler().used_dirs(); + props.set(Variable("mountpoint", + "", + 0, + 128, + ILLEGAL_MOUNT_CHARS, + illegal_mps)); + props.set(Variable("fstab", false, true)); + props.set(Variable("mount", false, true)); + } +} + +void +ContentFSTemplate::mount_props_create(const String& path, + const String& fsname, + const Props& props) +{ + if (props.has("mountpoint")) { + String mountpoint = props.get("mountpoint").get_string(); + String label; + try { + label = props.get("label").get_string(); + } catch ( ... ) {} + bool mount = props.get("mount").get_bool(); + bool fstab = props.get("fstab").get_bool(); + + if (mountpoint.size()) { + FstabLocker l; + if (mount) + MountHandler().mount(path, + mountpoint, + fsname); + if (fstab) + MountHandler().fstab_add(label.empty() ? path : String("LABEL=") + label, + mountpoint, + fsname); + } + } +} + + + + + +bool +ContentFS::mount_fs_supported(const String& fsname) +{ + list l = MountHandler().fstypes(); + if (find(l.begin(), l.end(), fsname) != l.end()) + return true; + + String out, err; + int status; + vector args; + args.push_back(fsname); + if (utils::execute("/sbin/modinfo", args, out, err, status)) + throw String("execute failed"); + return !status; +} --- conga/ricci/modules/storage/ContentFS.h 2006/08/10 22:53:09 1.5 +++ conga/ricci/modules/storage/ContentFS.h 2006/09/26 03:02:57 1.6 @@ -28,6 +28,9 @@ #include "String.h" +#define ILLEGAL_LABEL_CHARS String(" <>?'\":;|}]{[)(*&^%$#@!~`+=") + + class ContentFS : public Content { public: @@ -39,11 +42,23 @@ virtual void remove(); - protected: - ContentFS(const String& name, const String& path); - String _name; + + static bool mount_fs_supported(const String& fsname); // true if mountable + // these fcns extract/place info from/into props + static bool mount_props_mounted(const Props& props); // true if mounted + static void mount_props_probe(const String& path, + const String& module, + Props& props); + static void mount_props_apply(const String& path, + const String& module, + const Props& old_props, + const Props& new_props); + protected: + ContentFS(const String& path, const String& name, const String& module); + String _name; + String _module; }; @@ -53,9 +68,16 @@ public: virtual ~ContentFSTemplate(); + static void mount_props_template(const String& module, + Props &props); + static void mount_props_create(const String& path, + const String& module, + const Props &props); + + protected: ContentFSTemplate(const String& name); - String name; + String _name; }; --- conga/ricci/modules/storage/ExtendedFS.cpp 2006/09/14 17:52:04 1.4 +++ conga/ricci/modules/storage/ExtendedFS.cpp 2006/09/26 03:02:57 1.5 @@ -24,7 +24,6 @@ #include "ExtendedFS.h" #include "utils.h" #include "MountHandler.h" -#include "defines.h" #include "UMountError.h" #include "FileMagic.h" @@ -33,20 +32,21 @@ using namespace std; - - - +const static String MKE2FS_path("/sbin/mke2fs"); +const String ExtendedFS::PRETTY_NAME("Linux Extended FS"); ExtendedFS::ExtendedFS(const String& path) : - ContentFS("extended_fs", path), + ContentFS(path, + PRETTY_NAME, + "ext2"), _journaled(false) { String magic(FileMagic::get_description(path)); if (magic.find("ext3") == magic.npos && magic.find("ext2") == magic.npos) - throw String(path + " not extended_fs"); + throw path + ": not an " + _name; // dumpe2fs String out, err; @@ -95,36 +95,27 @@ String label = words[3]; if (label == "") label.clear(); - // _props.set(Variable("label", label, 0, 16, illegal_label_chars, list())); + // _props.set(Variable("label", label, 0, 16, ILLEGAL_LABEL_CHARS, list())); _props.set(Variable("label", label)); } } } - // mountpoint - MountHandler mh; - pair maj_min = mh.maj_min(path); - list mounts = mh.mounts(maj_min); - String mountpoint; - if (mounts.size()) - mountpoint = mounts.front().mountpoint; - list ill_mnts = mh.used_dirs(); - ill_mnts.remove(mountpoint); - // fstab - list fstabs = mh.fstabs(maj_min); - String fstabpoint; - if (fstabs.size()) - fstabpoint = fstabs.front().mountpoint; - ill_mnts.remove(fstabpoint); + long long block_count = _props.get("block_count").get_int(); - _props.set(Variable("mountpoint", mountpoint, 0, 128, ";? *!", ill_mnts)); - _props.set(Variable("fstabpoint", fstabpoint, 0, 128, ";? *!", ill_mnts)); + if (_journaled) + _module = "ext3"; + else + _module = "ext2"; // journaling - long long block_count = _props.get("block_count").get_int(); _props.set(Variable("has_journal", _journaled, (!_journaled && block_count > 1024))); + + mount_props_probe(path, + _module, + _props); } ExtendedFS::~ExtendedFS() @@ -134,7 +125,7 @@ bool ExtendedFS::expandable(long long& max_size) const { - bool mounted = _props.get("mountpoint").get_string().size(); + bool mounted = mount_props_mounted(_props); long long bs = _props.get("block_size").get_int(); long long bc = _props.get("block_count").get_int(); @@ -197,7 +188,7 @@ bool index_new = new_props.get("dir_index").get_bool(); if (index_old != index_new) { vector args; - String out, err, bin = "/sbin/tune2fs"; + String out, err, bin("/sbin/tune2fs"); int status; if (!index_old && index_new) { args.push_back("-O"); @@ -257,8 +248,8 @@ void ExtendedFS::enable_journal(const String& path) { - MountHandler mh; FstabLocker lock; + MountHandler mh; list mounts = mh.mounts(mh.maj_min(path)); list fstabs = mh.fstabs(mh.maj_min(path)); @@ -276,7 +267,7 @@ // add journal vector args; - String out, err, bin = "/sbin/tune2fs"; + String out, err, bin("/sbin/tune2fs"); int status; args.push_back("-j"); args.push_back(path); @@ -284,12 +275,14 @@ throw String("execute failed"); if (status) throw bin + " failed"; + _module = "ext3"; + } catch ( ... ) { for (list::const_iterator iter = rollback.begin(); iter != rollback.end(); iter++) try { - mh.mount(iter->devname, iter->mountpoint, "ext2"); + mh.mount(iter->devname, iter->mountpoint, _module); } catch ( ... ) {} throw; } @@ -299,14 +292,14 @@ iter != fstabs.end(); iter++) { mh.fstab_remove(iter->devname, iter->mountpoint); - mh.fstab_add(iter->devname, iter->mountpoint, "ext3"); + mh.fstab_add(iter->devname, iter->mountpoint, _module); } // mount for (list::const_iterator iter = mounts.begin(); iter != mounts.end(); iter++) - if (!mh.mount(iter->devname, iter->mountpoint, "ext3")) + if (!mh.mount(iter->devname, iter->mountpoint, _module)) throw String("mount failed"); } @@ -316,37 +309,10 @@ unsigned long long new_size, const Props& new_props) { - FstabLocker l; - - // mountpoint - String mnt_curr = _props.get("mountpoint").get_string(); - String mnt_new = new_props.get("mountpoint").get_string(); - if (mnt_curr != mnt_new) { - MountHandler mh; - if (mnt_curr.size()) - if (!mh.umount(path, mnt_curr)) - throw UMountError(mnt_curr); - if (mnt_new.size()) - if (!mh.mount(path, mnt_new, "ext3")) - throw String("mount failed"); - } - - // fstab mountpoint - mnt_curr = _props.get("fstabpoint").get_string(); - mnt_new = new_props.get("fstabpoint").get_string(); - if (mnt_curr != mnt_new) { - MountHandler mh; - if (mnt_curr.size()) { - list l = mh.fstabs(mh.maj_min(path)); - for (list::const_iterator iter = l.begin(); - iter != l.end(); - iter++) - if (iter->mountpoint == mnt_curr) - mh.fstab_remove(iter->devname, mnt_curr); - } - if (mnt_new.size()) - mh.fstab_add(path, mnt_new, "ext3"); - } + mount_props_apply(path, + _module, + _props, + new_props); } bool @@ -365,9 +331,6 @@ { String label = templ->_props.get("label").get_string(); String bs = utils::to_string(templ->_props.get("block_size").get_int()); - String mountpoint = templ->_props.get("mountpoint").get_string(); - bool mount = templ->_props.get("mount").get_bool(); - bool fstab = templ->_props.get("fstab").get_bool(); bool dir_index = templ->_props.get("dir_index").get_bool(); bool has_journal = templ->_props.get("has_journal").get_bool(); @@ -388,43 +351,43 @@ args.push_back("-j"); args.push_back(path); - String out, err, bin("/sbin/mke2fs"); + String out, err; int status; - if (utils::execute(bin, args, out, err, status)) + if (utils::execute(MKE2FS_path, args, out, err, status)) throw String("execute failed"); if (status != 0) - throw bin + " failed"; + throw MKE2FS_path + " failed"; - String fstype = (has_journal) ? "ext3" : "ext2"; - if (mountpoint.size()) { - if (mount) - MountHandler().mount(path, - mountpoint, - fstype); - if (fstab) - MountHandler().fstab_add(label.empty() ? path : String("LABEL=") + label, - mountpoint, - fstype); - } + // mountpoints + ContentFSTemplate::mount_props_create(path, + (has_journal) ? "ext3" : "ext2", + templ->_props); } ExtendedFSTemplate::ExtendedFSTemplate() : - ContentFSTemplate("extended_fs") + ContentFSTemplate(ExtendedFS::PRETTY_NAME) { - // label - _props.set(Variable("label", "", 0, 16, illegal_label_chars, list())); + if (access(MKE2FS_path.c_str(), X_OK|R_OK)) + throw String("no mke2fs exists"); + + bool ext3_supported = ContentFS::mount_fs_supported("ext3"); + + _props.set(Variable("label", + "", + 0, + 16, + ILLEGAL_LABEL_CHARS, + list())); - // dir_index _props.set(Variable("dir_index", true, true)); - // has_journal - _props.set(Variable("has_journal", true, true)); + _props.set(Variable("has_journal", + ext3_supported, + ext3_supported)); - // mountpoint - list illegal_mps = MountHandler().used_dirs(); - _props.set(Variable("mountpoint", "", 0, 128, ";? *!", illegal_mps)); - _props.set(Variable("fstab", false, true)); - _props.set(Variable("mount", false, true)); + mount_props_template(ext3_supported ? "ext3" : "ext2", + _props); + // block_size list b_sizes; --- conga/ricci/modules/storage/ExtendedFS.h 2006/08/10 22:53:09 1.3 +++ conga/ricci/modules/storage/ExtendedFS.h 2006/09/26 03:02:57 1.4 @@ -38,6 +38,7 @@ ExtendedFS(const String& path); virtual ~ExtendedFS(); + const static String PRETTY_NAME; virtual bool expandable(long long& max_size) const; virtual bool shrinkable(long long& min_size) const; --- conga/ricci/modules/storage/FSController.cpp 2006/08/10 22:53:09 1.5 +++ conga/ricci/modules/storage/FSController.cpp 2006/09/26 03:02:57 1.6 @@ -39,14 +39,9 @@ try { return counting_auto_ptr(new SwapFS(path)); } catch ( ... ) {} - try { - // return counting_auto_ptr(new gfs(path)); - } catch ( ... ) {} throw String("not FS"); - } - std::list > FSController::get_available_fss() { @@ -58,9 +53,6 @@ try { cnts.push_back(counting_auto_ptr(new SwapFSTemplate())); } catch ( ... ) {} - try { - // return counting_auto_ptr(new gfsTemplate(path)); - } catch ( ... ) {} return cnts; } @@ -73,9 +65,9 @@ throw String("content_template not of filesystem type"); String fs_type = cont_templ->attrs["fs_type"]; - if (fs_type == "extended_fs") + if (fs_type == ExtendedFS::PRETTY_NAME) create_extended_fs(bd->path(), cont_templ); - else if (fs_type == "swap") + else if (fs_type == SwapFS::PRETTY_NAME) create_swap_fs(bd->path(), cont_templ); else throw String("unknown fs type \"") + fs_type + "\""; --- conga/ricci/modules/storage/Props.cpp 2006/09/26 01:04:20 1.3 +++ conga/ricci/modules/storage/Props.cpp 2006/09/26 03:02:57 1.4 @@ -51,6 +51,12 @@ {} +bool +Props::has(const String& name) const +{ + return _vars.find(name) != _vars.end(); +} + const Variable Props::get(const String& name) const { --- conga/ricci/modules/storage/Props.h 2006/09/26 01:04:20 1.3 +++ conga/ricci/modules/storage/Props.h 2006/09/26 03:02:57 1.4 @@ -40,8 +40,9 @@ Props(const XMLObject&); virtual ~Props(); - const Variable get(const String& name) const; void set(const Variable& var); + bool has(const String& name) const; + const Variable get(const String& name) const; bool is_active(const Variable& var) const; --- conga/ricci/modules/storage/SwapFS.cpp 2006/09/14 17:52:04 1.4 +++ conga/ricci/modules/storage/SwapFS.cpp 2006/09/26 03:02:57 1.5 @@ -31,8 +31,11 @@ using namespace std; +const String SwapFS::PRETTY_NAME("Swap"); + + SwapFS::SwapFS(const String& path) : - ContentFS("swap", path) + ContentFS(path, PRETTY_NAME, "swap") { String magic(FileMagic::get_description(path)); if (magic.find("swap") == magic.npos) @@ -177,7 +180,12 @@ ContentFSTemplate("swap") { // label - _props.set(Variable("label", "", 0, 16, illegal_label_chars, list())); + _props.set(Variable("label", + "", + 0, + 16, + ILLEGAL_LABEL_CHARS, + list())); // swapon _props.set(Variable("swapon", true, true)); --- conga/ricci/modules/storage/SwapFS.h 2006/08/10 22:53:09 1.3 +++ conga/ricci/modules/storage/SwapFS.h 2006/09/26 03:02:57 1.4 @@ -39,6 +39,7 @@ SwapFS(const String& path); virtual ~SwapFS(); + const static String PRETTY_NAME; virtual bool expandable(long long& max_size) const; virtual bool shrinkable(long long& min_size) const; --- conga/ricci/modules/storage/defines.h 2006/08/10 22:53:09 1.5 +++ conga/ricci/modules/storage/defines.h 2006/09/26 03:02:57 1.6 @@ -27,9 +27,6 @@ #include "String.h" -static String illegal_label_chars("| \\ /?.,"); - - // XML tags for various objects