]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added installation-helper
authorArvin Schnell <aschnell@suse.de>
Mon, 16 Feb 2015 11:00:20 +0000 (12:00 +0100)
committerArvin Schnell <aschnell@suse.de>
Mon, 16 Feb 2015 11:00:20 +0000 (12:00 +0100)
client/.gitignore
client/Makefile.am
client/installation-helper.cc [new file with mode: 0644]
snapper.spec.in
snapper/FileUtils.cc
snapper/FileUtils.h

index e250d9b7e2e86f7ff81ab9fd5075e802398d6ecd..896fc1837857c2bbcd4be3bb44a46bd37aac4ed4 100644 (file)
@@ -1,3 +1,4 @@
 *.o
 snapper
 systemd-helper
+installation-helper
index af0c99169f3877ca4e1d34583844c1844a8f6990..951ee6db8be1f2295f5b956a50be2789e84fc75c 100644 (file)
@@ -18,6 +18,15 @@ snapper_SOURCES =                    \
 
 snapper_LDADD = ../snapper/libsnapper.la utils/libutils.la ../dbus/libdbus.la
 
+libexecdir = /usr/lib/snapper
+
+libexec_PROGRAMS = installation-helper
+
+installation_helper_SOURCES =          \
+       installation-helper.cc
+
+installation_helper_LDADD = ../snapper/libsnapper.la utils/libutils.la
+
 noinst_PROGRAMS = systemd-helper
 
 systemd_helper_SOURCES =               \
diff --git a/client/installation-helper.cc b/client/installation-helper.cc
new file mode 100644 (file)
index 0000000..fff26a4
--- /dev/null
@@ -0,0 +1,220 @@
+/*
+ * Copyright (c) 2015 Novell, Inc.
+ *
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may
+ * find current contact information at www.novell.com.
+ */
+
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <iostream>
+#include <boost/algorithm/string.hpp>
+
+#include <snapper/Snapper.h>
+#include <snapper/AppUtil.h>
+#include <snapper/SnapperDefines.h>
+#include <snapper/Btrfs.h>
+#include <snapper/FileUtils.h>
+#include <snapper/Hooks.h>
+
+#include "utils/GetOpts.h"
+
+
+using namespace snapper;
+using namespace std;
+
+
+void
+step1(const string& device)
+{
+    // step runs in inst-sys
+
+    cout << "step 1 device:" << device << endl;
+
+    cout << "temporarily mounting device" << endl;
+
+    SDir s_dir("/");
+
+    TmpMount tmp_mount(s_dir, device, "tmp-mnt-XXXXXX", "btrfs", 0, "");
+
+    cout << "copying config-file" << endl;
+
+    mkdir((tmp_mount.getFullname() + "/etc").c_str(), 0777);
+    mkdir((tmp_mount.getFullname() + "/etc/snapper").c_str(), 0777);
+    mkdir((tmp_mount.getFullname() + "/etc/snapper/configs").c_str(), 0777);
+
+    try
+    {
+       SysconfigFile config(CONFIGTEMPLATEDIR "/" "default");
+
+       config.setName(tmp_mount.getFullname() + CONFIGSDIR "/" "root");
+
+       config.setValue(KEY_SUBVOLUME, "/");
+       config.setValue(KEY_FSTYPE, "btrfs");
+    }
+    catch (const FileNotFoundException& e)
+    {
+       cerr << "copying config-file failed" << endl;
+    }
+
+    cout << "creating filesystem config" << endl;
+
+    Btrfs btrfs("/", tmp_mount.getFullname());
+
+    btrfs.createConfig();
+
+    cout << "creating snapshot" << endl;
+
+    Snapper snapper("root", tmp_mount.getFullname());
+
+    SCD scd;
+    scd.read_only = false;
+
+    Snapshots::iterator snapshot = snapper.createSingleSnapshot(scd);
+
+    cout << "setting default subvolume" << endl;
+
+    snapper.getFilesystem()->setDefault(snapshot->getNum());
+
+    cout << "done" << endl;
+}
+
+
+void
+step2(const string& device, const string& root_prefix, const string& default_subvolume_name)
+{
+    // step runs in inst-sys
+
+    cout << "step 2 device:" << device << " root-prefix:" << root_prefix
+        << " default-subvolume-name:" << default_subvolume_name << endl;
+
+    cout << "mounting device" << endl;
+
+    string subvol_option = default_subvolume_name;
+    if (!subvol_option.empty())
+       subvol_option += "/";
+    subvol_option += ".snapshots";
+
+    SDir s_dir(root_prefix + "/.snapshots");
+    if (!s_dir.mount(device, "btrfs", 0, "subvol=" + subvol_option))
+    {
+       cerr << "mounting .snapshots failed" << endl;
+    }
+
+    cout << "done" << endl;
+}
+
+
+void
+step3(const string& root_prefix, const string& default_subvolume_name)
+{
+    // step runs in inst-sys
+
+    cout << "step 3 root-prefix:" << root_prefix << " default_subvolume_name:"
+        << default_subvolume_name << endl;
+
+    cout << "adding .snapshots to fstab" << endl;
+
+    Btrfs btrfs("/", root_prefix);
+
+    btrfs.addToFstab(default_subvolume_name);
+
+    cout << "done" << endl;
+}
+
+
+void
+step4()
+{
+    // step runs in chroot
+
+    cout << "step 4" << endl;
+
+    cout << "modifying sysconfig-file" << endl;
+
+    try
+    {
+       SysconfigFile sysconfig(SYSCONFIGFILE);
+       sysconfig.setValue("SNAPPER_CONFIGS", { "root" });
+    }
+    catch (const FileNotFoundException& e)
+    {
+       cerr << "sysconfig-file not found" << endl;
+    }
+
+    Btrfs btrfs("/", "");
+
+    cout << "running external programs" << endl;
+
+    Hooks::create_config("/", &btrfs);
+
+    cout << "done" << endl;
+}
+
+
+int
+main(int argc, char** argv)
+{
+    setlocale(LC_ALL, "");
+
+    const struct option options[] = {
+       { "step",                       required_argument,      0,      0 },
+       { "device",                     required_argument,      0,      0 },
+       { "root-prefix",                required_argument,      0,      0 },
+       { "default-subvolume-name",     required_argument,      0,      0 },
+       { 0, 0, 0, 0 }
+    };
+
+    string step;
+    string device;
+    string root_prefix = "/";
+    string default_subvolume_name;
+
+    GetOpts getopts;
+
+    getopts.init(argc, argv);
+
+    GetOpts::parsed_opts opts = getopts.parse(options);
+
+    GetOpts::parsed_opts::const_iterator opt;
+
+    if ((opt = opts.find("step")) != opts.end())
+       step = opt->second;
+
+    if ((opt = opts.find("device")) != opts.end())
+       device = opt->second;
+
+    if ((opt = opts.find("root-prefix")) != opts.end())
+       root_prefix = opt->second;
+
+    if ((opt = opts.find("default-subvolume-name")) != opts.end())
+       default_subvolume_name = opt->second;
+
+    if (step == "1")
+       step1(device);
+    else if (step == "2")
+       step2(device, root_prefix, default_subvolume_name);
+    else if (step == "3")
+       step3(root_prefix, default_subvolume_name);
+    else if (step == "4")
+       step4();
+}
index d09478cb0ae1e3f1ffa780e70ab8a0c7162fe190..ffcee450077114f1e3d12b03e6277630117fc87f 100644 (file)
@@ -1,7 +1,7 @@
 #
 # spec file for package snapper
 #
-# Copyright (c) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
+# Copyright (c) 2015 SUSE LINUX GmbH, Nuernberg, Germany.
 #
 # All modifications and additions to the file contributed by third parties
 # remain the property of their copyright owners, unless otherwise agreed
@@ -109,6 +109,7 @@ rm -rf "$RPM_BUILD_ROOT"
 %defattr(-,root,root)
 %{prefix}/bin/snapper
 %{prefix}/sbin/snapperd
+%{prefix}/lib/snapper
 %doc %{_mandir}/*/snapper.8*
 %doc %{_mandir}/*/snapperd.8*
 %doc %{_mandir}/*/snapper-configs.5*
index c92b3f9a09538fea2aae6bce136eed30b420cd37..3017451bf0ecfbad702e62d667649e76f3e96d60 100644 (file)
@@ -661,6 +661,13 @@ namespace snapper
     }
 
 
+    string
+    TmpDir::getFullname() const
+    {
+       return base_dir.fullname() + "/" + name;
+    }
+
+
     TmpMount::TmpMount(SDir& base_dir, const string& device, const string& name_template,
                       const string& mount_type, unsigned long mount_flags,
                       const string& mount_data)
index f4cb8e07d0c6c05055459415e417dbfc170251ca..f35a0ac60b537434e63a26d6f4a9ecd56249a319 100644 (file)
@@ -152,6 +152,8 @@ namespace snapper
 
        const string& getName() const { return name; }
 
+       string getFullname() const;
+
     protected:
 
        SDir& base_dir;