SELinux relabel API
by Richard W.M. Jones
[
I realized that we were discussing adding this feature, in various
private email, IRC, and this long bugzilla thread:
https://bugzilla.redhat.com/show_bug.cgi?id=1060423
That's not how we should do things. Let's discuss it on the
mailing list.
]
One thing that virt-customize/virt-sysprep/virt-builder have to do is
relabel SELinux guests.
What we do at the moment is run:
if load_policy && fixfiles restore; then
rm -f /.autorelabel
else
touch /.autorelabel
echo '%s: SELinux relabelling failed, will relabel at boot instead.'
fi
while chrooted into the guest (using the 'guestfs_sh' API).
This has a number of problems:
- It has to load the policy using 'load_policy', but this doesn't
work sometimes:
* RHEL 5 load_policy takes a parameter.
* Doesn't work if appliance kernel is significantly different from
guest kernel version, because the binary policy format changes
irregularly and is not backwards compatible.
* Requires the appliance [host] kernel to be compiled with
LSM/SELinux support.
- Touching /.autorelabel is often broken, eg. it's broken in Fedora 20
because of systemd (RHBZ#1049656).
- /etc/resolv.conf will not be relabelled if guestfs network is on,
because of resolv.conf shenanigans in libguestfs.git/daemon/command.c
- It requires running guest code, which we'd like to avoid.
What would be nice would be to have an API to just do this
relabelling. Libguestfs could change this API as required to handle
different guests.
Dan Walsh helpfully pointed out to us that we've been doing it wrong
all along :-) A much better way to relabel is to run:
setfiles /etc/selinux/targeted/contexts/files/file_contexts DIR
where 'file_contexts' is a file which contains the default labels for
files (a set of regexps), and 'DIR' is the directory at which
relabelling starts. Note that 'setfiles' would be the libguestfs
appliance binary, so no guest binary needs to be run.
A simple API could just look like this:
guestfs_selinux_relabel (g);
which would always use the 'targeted' policy from the guest, and
always start relabelling at the root. This would work fine for
virt-builder.
For Colin's requirements for Project Atomic, I suspect he will want to
be able to set the file_contexts file and the root directory, but I'll
leave him to describe what would be useful.
A couple of notes:
- I'd like to avoid baking in assumptions from the 'setfiles' command
as far as possible. libguestfs APIs last for many years and some
have caused us many years of regret (but that's our job) :-/
- Is it a good idea to tie this into inspection in some way -- for
example, inspection could provide us with the path to the current or
default SELinux policy.
Rich.
--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines. Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
8 years, 11 months
ANNOUNCE: libguestfs & virt-v2v & virt-p2v RHEL 7.2 preview packages available
by Richard W.M. Jones
[The subject line isn't strictly correct, as virt-p2v packages aren't
available right now, but will be shortly]
If you are running RHEL 7.1 then you may be interested in trying out
the newer RHEL 7.2 preview versions of:
- libguestfs
- virt-v2v (Technical Preview in RHEL 7.1, Supported in RHEL 7.2)
- virt-p2v (Technical Preview in RHEL 7.2)
To install the preview repository, copy the attached file into
/etc/yum.repos.d/
Then use this command to update virt-v2v:
yum install virt-v2v libguestfs-tools-c libguestfs-xfs
To convert Windows guests you'll also need libguestfs-winsupport which
is finally available in RHN (V2VWIN channel) or you can get it from
http://people.redhat.com/~rjones/libguestfs-winsupport/
Please note these packages are *not* supported by Red Hat. If you
find bugs, please file them using the link below:
https://bugzilla.redhat.com/enter_bug.cgi?product=Red%20Hat%20Enterprise%...
To downgrade back to the RHEL 7.1 supported packages, delete
/etc/yum.repos.d/libguestfs-RHEL-7.2-preview.repo , uninstall
libguestfs, then reinstall libguestfs from RHN.
I have not tested these packages on CentOS 7.1, but they will probably
work there too, and if they don't then let me know.
Rich.
--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines. Supports shell scripting,
bindings from many languages. http://libguestfs.org
9 years, 1 month
missing btrfs subvol support
by Olaf Hering
Is btrfs subvol support failing just for me? Looks like nothing adds the
required '@/' string. virt-ls uses the first variant of the command:
><rescue> mount -vo subvol=var/spool,ro /dev/sda2 /sysroot/
[ 113.852047] BTRFS info (device sda2): disk space caching is enabled
[ 113.852869] BTRFS: has skinny extents
mount: mount(2) failed: No such file or directory
><rescue> mount -vo subvol=@var/spool,ro /dev/sda2 /sysroot/
[ 116.082642] BTRFS info (device sda2): disk space caching is enabled
[ 116.083476] BTRFS: has skinny extents
mount: mount(2) failed: No such file or directory
><rescue> mount -vo subvol=@/var/spool,ro /dev/sda2 /sysroot/
[ 123.995343] BTRFS info (device sda2): disk space caching is enabled
[ 123.996238] BTRFS: has skinny extents
mount: /dev/sda2 mounted on /sysroot.
><rescue> exit
fstab is:
UUID=d538a81f-9778-424d-96cc-e48dd2d4a323 swap swap defaults 0 0
UUID=65b72f12-eccb-4cf2-a4f4-9c4bb462456f / btrfs defaults 0 0
UUID=65b72f12-eccb-4cf2-a4f4-9c4bb462456f /var/spool btrfs subvol=@/var/spool 0 0
Olaf
9 years, 2 months
[PATCH 2/3] inspection: Add support for CoreOS
by Nikos Skalkotos
* Implement coreos distro
* Detect CoreOS images
Signed-off-by: Nikos Skalkotos <skalkoto(a)grnet.gr>
---
generator/actions.ml | 4 +++
src/guestfs-internal.h | 3 +++
src/inspect-fs-unix.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++---
src/inspect-fs.c | 21 +++++++++++++++
src/inspect-icon.c | 1 +
src/inspect.c | 62 +++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 157 insertions(+), 3 deletions(-)
diff --git a/generator/actions.ml b/generator/actions.ml
index e9374a3..e1b14ca 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -1073,6 +1073,10 @@ Cirros.
=item \"debian\"
+CoreOS.
+
+=item \"coreos\"
+
Debian.
=item \"fedora\"
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index 01cbca7..1462673 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -551,6 +551,7 @@ enum inspect_os_distro {
OS_DISTRO_ORACLE_LINUX,
OS_DISTRO_FREEBSD,
OS_DISTRO_NETBSD,
+ OS_DISTRO_COREOS,
};
enum inspect_os_package_format {
@@ -797,6 +798,8 @@ extern int guestfs_int_check_netbsd_root (guestfs_h *g, struct inspect_fs *fs);
extern int guestfs_int_check_openbsd_root (guestfs_h *g, struct inspect_fs *fs);
extern int guestfs_int_check_hurd_root (guestfs_h *g, struct inspect_fs *fs);
extern int guestfs_int_check_minix_root (guestfs_h *g, struct inspect_fs *fs);
+extern int guestfs_int_check_coreos_root (guestfs_h *g, struct inspect_fs *fs);
+extern int guestfs_int_check_coreos_usr (guestfs_h *g, struct inspect_fs *fs);
/* inspect-fs-windows.c */
extern char *guestfs_int_case_sensitive_path_silently (guestfs_h *g, const char *);
diff --git a/src/inspect-fs-unix.c b/src/inspect-fs-unix.c
index 2abbf24..ff50b2a 100644
--- a/src/inspect-fs-unix.c
+++ b/src/inspect-fs-unix.c
@@ -160,11 +160,16 @@ parse_release_file (guestfs_h *g, struct inspect_fs *fs,
* DISTRIB_CODENAME=Henry_Farman
* DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
* Mandriva also has a normal release file called /etc/mandriva-release.
+ *
+ * CoreOS has a /etc/lsb-release link to /usr/share/coreos/lsb-release containing:
+ * DISTRIB_ID=CoreOS
+ * DISTRIB_RELEASE=647.0.0
+ * DISTRIB_CODENAME="Red Dog"
+ * DISTRIB_DESCRIPTION="CoreOS 647.0.0"
*/
static int
-parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
+parse_lsb_release (guestfs_h *g, struct inspect_fs *fs, const char *filename)
{
- const char *filename = "/etc/lsb-release";
int64_t size;
CLEANUP_FREE_STRING_LIST char **lines = NULL;
size_t i;
@@ -208,6 +213,11 @@ parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
fs->distro = OS_DISTRO_MAGEIA;
r = 1;
}
+ else if (fs->distro == 0 &&
+ STREQ (lines[i], "DISTRIB_ID=CoreOS")) {
+ fs->distro = OS_DISTRO_COREOS;
+ r = 1;
+ }
else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
char *major, *minor;
if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
@@ -338,7 +348,7 @@ guestfs_int_check_linux_root (guestfs_h *g, struct inspect_fs *fs)
if (guestfs_is_file_opts (g, "/etc/lsb-release",
GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
- r = parse_lsb_release (g, fs);
+ r = parse_lsb_release (g, fs, "/etc/lsb-release");
if (r == -1) /* error */
return -1;
if (r == 1) /* ok - detected the release from this file */
@@ -795,6 +805,59 @@ guestfs_int_check_minix_root (guestfs_h *g, struct inspect_fs *fs)
return 0;
}
+/* The currently mounted device is a CoreOS root. From this partition we can
+ * only determine the hostname. All immutable OS files are under a separate
+ * read-only /usr partition.
+ */
+int
+guestfs_int_check_coreos_root (guestfs_h *g, struct inspect_fs *fs)
+{
+ fs->type = OS_TYPE_LINUX;
+ fs->distro = OS_DISTRO_COREOS;
+
+ /* Determine hostname. */
+ if (check_hostname_unix (g, fs) == -1)
+ return -1;
+
+ /* CoreOS does not contain /etc/fstab to determine the mount points.
+ * Associate this filesystem with the "/" mount point.
+ */
+ if (add_fstab_entry (g, fs, fs->mountable, "/") == -1)
+ return -1;
+
+ return 0;
+}
+
+/* The currently mounted device looks like a CoreOS /usr. In CoreOS
+ * the read-only /usr contains the OS version. The /etc/os-release is a
+ * link to /usr/share/coreos/os-release.
+ */
+int
+guestfs_int_check_coreos_usr (guestfs_h *g, struct inspect_fs *fs)
+{
+ int r;
+
+ fs->type = OS_TYPE_LINUX;
+ fs->distro = OS_DISTRO_COREOS;
+ if (guestfs_is_file_opts (g, "/share/coreos/lsb-release",
+ GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
+ r = parse_lsb_release (g, fs, "/share/coreos/lsb-release");
+ if (r == -1) /* error */
+ return -1;
+ }
+
+ /* Determine the architecture. */
+ check_architecture (g, fs);
+
+ /* CoreOS does not contain /etc/fstab to determine the mount points.
+ * Associate this filesystem with the "/usr" mount point.
+ */
+ if (add_fstab_entry (g, fs, fs->mountable, "/usr") == -1)
+ return -1;
+
+ return 0;
+}
+
static void
check_architecture (guestfs_h *g, struct inspect_fs *fs)
{
diff --git a/src/inspect-fs.c b/src/inspect-fs.c
index 5f55f1d..64f904f 100644
--- a/src/inspect-fs.c
+++ b/src/inspect-fs.c
@@ -232,6 +232,17 @@ check_filesystem (guestfs_h *g, const char *mountable,
if (guestfs_int_check_linux_root (g, fs) == -1)
return -1;
}
+ /* CoreOS root? */
+ else if (is_dir_etc &&
+ guestfs_is_dir (g, "/root") > 0 &&
+ guestfs_is_dir (g, "/home") > 0 &&
+ guestfs_is_dir (g, "/usr") > 0 &&
+ guestfs_is_file (g, "/etc/coreos/update.conf") > 0) {
+ fs->is_root = 1;
+ fs->format = OS_FORMAT_INSTALLED;
+ if (guestfs_int_check_coreos_root (g, fs) == -1)
+ return -1;
+ }
/* Linux /usr/local? */
else if (is_dir_etc &&
is_dir_bin &&
@@ -246,6 +257,14 @@ check_filesystem (guestfs_h *g, const char *mountable,
guestfs_is_dir (g, "/local") > 0 &&
guestfs_is_file (g, "/etc/fstab") == 0)
;
+ /* CoreOS /usr? */
+ else if (is_dir_bin &&
+ is_dir_share &&
+ guestfs_is_dir (g, "/local") > 0 &&
+ guestfs_is_dir (g, "/share/coreos") > 0) {
+ if (guestfs_int_check_coreos_usr (g, fs) == -1)
+ return -1;
+ }
/* Linux /var? */
else if (guestfs_is_dir (g, "/log") > 0 &&
guestfs_is_dir (g, "/run") > 0 &&
@@ -476,6 +495,7 @@ guestfs_int_check_package_format (guestfs_h *g, struct inspect_fs *fs)
case OS_DISTRO_SLACKWARE:
case OS_DISTRO_TTYLINUX:
+ case OS_DISTRO_COREOS:
case OS_DISTRO_WINDOWS:
case OS_DISTRO_BUILDROOT:
case OS_DISTRO_CIRROS:
@@ -546,6 +566,7 @@ guestfs_int_check_package_management (guestfs_h *g, struct inspect_fs *fs)
case OS_DISTRO_SLACKWARE:
case OS_DISTRO_TTYLINUX:
+ case OS_DISTRO_COREOS:
case OS_DISTRO_WINDOWS:
case OS_DISTRO_BUILDROOT:
case OS_DISTRO_CIRROS:
diff --git a/src/inspect-icon.c b/src/inspect-icon.c
index fb998c2..57b2ce3 100644
--- a/src/inspect-icon.c
+++ b/src/inspect-icon.c
@@ -169,6 +169,7 @@ guestfs_impl_inspect_get_icon (guestfs_h *g, const char *root, size_t *size_r,
/* These are just to keep gcc warnings happy. */
case OS_DISTRO_ARCHLINUX:
case OS_DISTRO_BUILDROOT:
+ case OS_DISTRO_COREOS:
case OS_DISTRO_FREEDOS:
case OS_DISTRO_GENTOO:
case OS_DISTRO_LINUX_MINT:
diff --git a/src/inspect.c b/src/inspect.c
index f528bf2..dd6a06f 100644
--- a/src/inspect.c
+++ b/src/inspect.c
@@ -42,6 +42,7 @@
COMPILE_REGEXP (re_primary_partition, "^/dev/(?:h|s|v)d.[1234]$", 0)
static void check_for_duplicated_bsd_root (guestfs_h *g);
+static int collect_coreos_inspection_info (guestfs_h *g);
/* The main inspection code. */
char **
@@ -70,6 +71,15 @@ guestfs_impl_inspect_os (guestfs_h *g)
}
}
+ /* The OS inspection information for CoreOS are gathered by inspecting
+ * multiple filesystems. Gather all the inspected information in the
+ * inspect_fs struct of the root filesystem.
+ */
+ if (collect_coreos_inspection_info (g)) {
+ guestfs_int_free_inspect_info (g);
+ return NULL;
+ }
+
/* Check if the same filesystem was listed twice as root in g->fses.
* This may happen for the *BSD root partition where an MBR partition
* is a shadow of the real root partition probably /dev/sda5
@@ -87,6 +97,57 @@ guestfs_impl_inspect_os (guestfs_h *g)
return ret;
}
+/* Traverse through the filesystem list and find out if it contains the
+ * "/" and "/usr" filesystems of a CoreOS image. If this is the case,
+ * sum up all the collected information on the root fs.
+ */
+static int
+collect_coreos_inspection_info (guestfs_h *g)
+{
+ size_t i;
+ struct inspect_fs *root = NULL, *usr = NULL;
+
+ for (i = 0; i < g->nr_fses; ++i) {
+ struct inspect_fs *fs = &g->fses[i];
+
+ if (fs->distro == OS_DISTRO_COREOS && fs->is_root)
+ root = fs;
+ }
+
+ if (root == NULL)
+ return 0;
+
+ for (i = 0; i < g->nr_fses; ++i) {
+ struct inspect_fs *fs = &g->fses[i];
+
+ if (fs->distro != OS_DISTRO_COREOS || fs->is_root != 0)
+ continue;
+
+ /* CoreOS is designed to contain 2 /usr partitions (USR-A, USR-B):
+ * https://coreos.com/docs/sdk-distributors/sdk/disk-partitions/
+ * One is active and one passive. During the initial boot, the passive
+ * partition is empty and it gets filled up when an update is performed.
+ * Then, when the system reboots, the boot loader is instructed to boot
+ * from the passive partition. If both partitions are valid, we cannot
+ * determine which the active and which the passive is, unless we peep into
+ * the boot loader. As a workaround, we check the OS versions and pick the
+ * one with the higher version as active.
+ */
+ if (usr &&
+ (usr->major_version > fs->major_version ||
+ (usr->major_version == fs->major_version &&
+ usr->minor_version > fs->minor_version)))
+ continue;
+
+ usr = fs;
+ }
+
+ if (usr == NULL)
+ return 0;
+
+ return guestfs_int_merge_fs_inspections (g, root, usr);
+}
+
/* On *BSD systems, sometimes /dev/sda[1234] is a shadow of the real root
* filesystem that is probably /dev/sda5
* (see: http://www.freebsd.org/doc/handbook/disk-organization.html)
@@ -201,6 +262,7 @@ guestfs_impl_inspect_get_distro (guestfs_h *g, const char *root)
case OS_DISTRO_BUILDROOT: ret = safe_strdup (g, "buildroot"); break;
case OS_DISTRO_CENTOS: ret = safe_strdup (g, "centos"); break;
case OS_DISTRO_CIRROS: ret = safe_strdup (g, "cirros"); break;
+ case OS_DISTRO_COREOS: ret = safe_strdup (g, "coreos"); break;
case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
case OS_DISTRO_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
--
2.1.0
9 years, 5 months
[PATCH 1/3] inspection: Add func for merging fs inspections
by Nikos Skalkotos
Add a new guestfs_int_merge_fs_inspections() function that merges the OS
inspection information of two inspect_fs instances into one. This
function is useful if the inspection information for an OS are gathered
by inspecting multiple filesystems.
Signed-off-by: Nikos Skalkotos <skalkoto(a)grnet.gr>
---
src/guestfs-internal.h | 1 +
src/inspect-fs.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 116 insertions(+)
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index 4f06c37..01cbca7 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -788,6 +788,7 @@ extern char *guestfs_int_first_line_of_file (guestfs_h *g, const char *filename)
extern int guestfs_int_first_egrep_of_file (guestfs_h *g, const char *filename, const char *eregex, int iflag, char **ret);
extern void guestfs_int_check_package_format (guestfs_h *g, struct inspect_fs *fs);
extern void guestfs_int_check_package_management (guestfs_h *g, struct inspect_fs *fs);
+extern int guestfs_int_merge_fs_inspections (guestfs_h *g, struct inspect_fs *dst, struct inspect_fs *src);
/* inspect-fs-unix.c */
extern int guestfs_int_check_linux_root (guestfs_h *g, struct inspect_fs *fs);
diff --git a/src/inspect-fs.c b/src/inspect-fs.c
index 932e5e7..5f55f1d 100644
--- a/src/inspect-fs.c
+++ b/src/inspect-fs.c
@@ -655,3 +655,118 @@ guestfs_int_first_egrep_of_file (guestfs_h *g, const char *filename,
return 1;
}
+
+/* Merge the missing OS inspection information found on the src inspect_fs into
+ * the ones of the dst inspect_fs. This function is useful if the inspection
+ * information for an OS are gathered by inspecting multiple file systems.
+ *
+ * Returns: 0 = success
+ * -1 = error
+ */
+int
+guestfs_int_merge_fs_inspections (guestfs_h *g, struct inspect_fs *dst, struct inspect_fs *src)
+{
+ size_t n, i, old;
+ struct inspect_fstab_entry *fstab = NULL;
+ char ** mappings = NULL;
+
+ if (dst->type == 0)
+ dst->type = src->type;
+
+ if (dst->distro == 0)
+ dst->distro = src->distro;
+
+ if (dst->package_format == 0)
+ dst->package_format = src->package_format;
+
+ if (dst->package_management == 0)
+ dst->package_management = src->package_management;
+
+ if (dst->product_name == NULL) {
+ dst->product_name = src->product_name;
+ src->product_name = NULL;
+ }
+
+ if (dst->product_variant == NULL) {
+ dst->product_variant= src->product_variant;
+ src->product_variant = NULL;
+ }
+
+ if (dst->major_version == 0 && dst->minor_version == 0) {
+ dst->major_version = src->major_version;
+ dst->minor_version = src->minor_version;
+ }
+
+ if (dst->arch == NULL) {
+ dst->arch = src->arch;
+ src->arch = NULL;
+ }
+
+ if (dst->hostname == NULL) {
+ dst->hostname = src->hostname;
+ src->hostname = NULL;
+ }
+
+ if (dst->windows_systemroot == NULL) {
+ dst->windows_systemroot = src->windows_systemroot;
+ src->windows_systemroot = NULL;
+ }
+
+ if (dst->windows_current_control_set == NULL) {
+ dst->windows_current_control_set = src->windows_current_control_set;
+ src->windows_current_control_set = NULL;
+ }
+
+ if (src->drive_mappings != NULL) {
+ if (dst->drive_mappings == NULL) {
+ /* Adopt the drive mappings of src */
+ dst->drive_mappings = src->drive_mappings;
+ src->drive_mappings = NULL;
+ } else {
+ n = 0;
+ for (; dst->drive_mappings[n] != NULL; n++)
+ ;
+ old = n;
+ for (; src->drive_mappings[n] != NULL; n++)
+ ;
+
+ /* Merge the src mappings to dst */
+ mappings = realloc (dst->drive_mappings, (n + 1) * sizeof (char *));
+ if (mappings == NULL) {
+ perrorf (g, "realloc");
+ return -1;
+ }
+
+ for (i = old; i < n; i++)
+ mappings[i] = src->drive_mappings[i - old];
+
+ mappings[n] = NULL;
+ dst->drive_mappings = mappings;
+
+ free(src->drive_mappings);
+ src->drive_mappings = NULL;
+ }
+ }
+
+ if (src->nr_fstab > 0) {
+ n = dst->nr_fstab + src->nr_fstab;
+ fstab = realloc (dst->fstab, n * sizeof (struct inspect_fstab_entry));
+ if (fstab == NULL) {
+ perrorf (g, "realloc");
+ return -1;
+ }
+
+ for (i = 0; i < src->nr_fstab; i++) {
+ fstab[dst->nr_fstab + i].mountable = src->fstab[i].mountable;
+ fstab[dst->nr_fstab + i].mountpoint = src->fstab[i].mountpoint;
+ }
+ free(src->fstab);
+ src->fstab = NULL;
+ src->nr_fstab = 0;
+
+ dst->fstab = fstab;
+ dst->nr_fstab = n;
+ }
+
+ return 0;
+}
--
2.1.0
9 years, 5 months
[PATCH] inspection: fix CentOS 7 detection
by Pino Toscano
In newer CentOS 7 versions /etc/redhat-release says that the distro is
derived from RHEL, so we need to look at /etc/centos-release for
actually identifying it as CentOS.
The old code is needed as sub-case of /etc/redhat-release, as on
CentOS < 7 that file is a symlink to /etc/centos-release.
---
src/inspect-fs-unix.c | 32 ++++++++++++++++++++++++++++++--
1 file changed, 30 insertions(+), 2 deletions(-)
diff --git a/src/inspect-fs-unix.c b/src/inspect-fs-unix.c
index 2abbf24..f0fe141 100644
--- a/src/inspect-fs-unix.c
+++ b/src/inspect-fs-unix.c
@@ -345,8 +345,8 @@ guestfs_int_check_linux_root (guestfs_h *g, struct inspect_fs *fs)
goto skip_release_checks;
}
- /* Oracle Linux includes a "/etc/redhat-release" file, hence the Oracle check
- * needs to be performed before the Red-Hat one.
+ /* RHEL-based distros include a "/etc/redhat-release" file, hence their
+ * checks need to be performed before the Red-Hat one.
*/
if (guestfs_is_file_opts (g, "/etc/oracle-release",
GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
@@ -376,6 +376,34 @@ guestfs_int_check_linux_root (guestfs_h *g, struct inspect_fs *fs)
fs->minor_version = 0;
}
}
+ else if (guestfs_is_file_opts (g, "/etc/centos-release",
+ GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
+ fs->distro = OS_DISTRO_CENTOS;
+
+ if (parse_release_file (g, fs, "/etc/centos-release") == -1)
+ return -1;
+
+ if (match2 (g, fs->product_name, re_centos_old, &major, &minor) ||
+ match2 (g, fs->product_name, re_centos, &major, &minor)) {
+ fs->major_version = guestfs_int_parse_unsigned_int (g, major);
+ free (major);
+ if (fs->major_version == -1) {
+ free (minor);
+ return -1;
+ }
+ fs->minor_version = guestfs_int_parse_unsigned_int (g, minor);
+ free (minor);
+ if (fs->minor_version == -1)
+ return -1;
+ }
+ else if ((major = match1 (g, fs->product_name, re_centos_no_minor)) != NULL) {
+ fs->major_version = guestfs_int_parse_unsigned_int (g, major);
+ free (major);
+ if (fs->major_version == -1)
+ return -1;
+ fs->minor_version = 0;
+ }
+ }
else if (guestfs_is_file_opts (g, "/etc/redhat-release",
GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
--
2.1.0
9 years, 5 months
[PATCH] inspection: lift size limit for RHEL icons
by Pino Toscano
In RHEL-based distros, the provided icons have (obviously) different
sizes than the RHEL ones, used in icon_rhel as reference.
Since 100K should be a reasonable threshold for avoid keeping a
per-distro list of limits, just use it as only size limit.
---
src/inspect-icon.c | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/src/inspect-icon.c b/src/inspect-icon.c
index fb998c2..c83ba13 100644
--- a/src/inspect-icon.c
+++ b/src/inspect-icon.c
@@ -318,24 +318,21 @@ icon_fedora (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
*
* Conveniently the RHEL clones also have the same file with the
* same name, but containing their own logos. Sense prevails!
+ *
+ * Use a generic 100K limit for all the images, as logos in the
+ * RHEL clones have different sizes.
*/
static char *
icon_rhel (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
{
- size_t max_size = 0;
const char *shadowman;
- if (fs->major_version >= 5 && fs->major_version <= 6)
- max_size = 17000;
- else
- max_size = 66000;
-
if (fs->major_version <= 6)
shadowman = "/usr/share/pixmaps/redhat/shadowman-transparent.png";
else
shadowman = "/usr/share/pixmaps/fedora-logo-sprite.png";
- return get_png (g, fs, shadowman, size_r, max_size);
+ return get_png (g, fs, shadowman, size_r, 102400);
}
#define DEBIAN_ICON "/usr/share/pixmaps/debian-logo.png"
--
2.1.0
9 years, 5 months
[PATCH 3/3] Add tests for CoreOS
by Nikos Skalkotos
Signed-off-by: Nikos Skalkotos <skalkoto(a)grnet.gr>
---
.gitignore | 1 +
inspector/Makefile.am | 1 +
inspector/expected-coreos.img.xml | 30 +++++++++++
tests/guests/Makefile.am | 6 +++
tests/guests/guest-aux/make-coreos-img.sh | 83 +++++++++++++++++++++++++++++++
tests/guests/guests.xml.in | 16 ++++++
6 files changed, 137 insertions(+)
create mode 100644 inspector/expected-coreos.img.xml
create mode 100755 tests/guests/guest-aux/make-coreos-img.sh
diff --git a/.gitignore b/.gitignore
index 29f9d6d..096cdfc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -536,6 +536,7 @@ Makefile.in
/tests/guests/stamp-fedora-md.img
/tests/guests/ubuntu.img
/tests/guests/archlinux.img
+/tests/guests/coreos.img
/tests/guests/windows.img
/tests/mount-local/test-parallel-mount-local
/tests/mountable/test-internal-parse-mountable
diff --git a/inspector/Makefile.am b/inspector/Makefile.am
index 9d8303c..9c79bed 100644
--- a/inspector/Makefile.am
+++ b/inspector/Makefile.am
@@ -38,6 +38,7 @@ EXTRA_DIST = \
expected-fedora.img.xml \
expected-ubuntu.img.xml \
expected-archlinux.img.xml \
+ expected-coreos.img.xml \
expected-windows.img.xml \
test-virt-inspector.sh \
test-xmllint.sh.in \
diff --git a/inspector/expected-coreos.img.xml b/inspector/expected-coreos.img.xml
new file mode 100644
index 0000000..723a853
--- /dev/null
+++ b/inspector/expected-coreos.img.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<operatingsystems>
+ <operatingsystem>
+ <root>/dev/sda5</root>
+ <name>linux</name>
+ <distro>coreos</distro>
+ <product_name>CoreOS 647.0.0</product_name>
+ <major_version>647</major_version>
+ <minor_version>0</minor_version>
+ <hostname>coreos.invalid</hostname>
+ <format>installed</format>
+ <mountpoints>
+ <mountpoint dev="/dev/sda5">/</mountpoint>
+ <mountpoint dev="/dev/sda3">/usr</mountpoint>
+ </mountpoints>
+ <filesystems>
+ <filesystem dev="/dev/sda3">
+ <type>ext4</type>
+ <label>USR-A</label>
+ <uuid>01234567-0123-0123-0123-012345678901</uuid>
+ </filesystem>
+ <filesystem dev="/dev/sda5">
+ <type>ext4</type>
+ <label>ROOT</label>
+ <uuid>01234567-0123-0123-0123-012345678902</uuid>
+ </filesystem>
+ </filesystems>
+ <applications/>
+ </operatingsystem>
+</operatingsystems>
diff --git a/tests/guests/Makefile.am b/tests/guests/Makefile.am
index b752bb1..6ada4ec 100644
--- a/tests/guests/Makefile.am
+++ b/tests/guests/Makefile.am
@@ -30,6 +30,7 @@ EXTRA_DIST = \
guest-aux/make-ubuntu-img.sh \
guest-aux/make-archlinux-img.sh \
guest-aux/archlinux-package \
+ guest-aux/make-coreos-img.sh \
guest-aux/make-windows-img.sh \
guest-aux/windows-software \
guest-aux/windows-software.reg \
@@ -52,6 +53,7 @@ disk_images = \
fedora-btrfs.img \
ubuntu.img \
archlinux.img \
+ coreos.img \
windows.img
# This is 'check_DATA' because we don't need it until 'make check'
@@ -109,6 +111,10 @@ ubuntu.img: guest-aux/make-ubuntu-img.sh
archlinux.img: guest-aux/make-archlinux-img.sh
SRCDIR=$(srcdir) $(top_builddir)/run --test $<
+# Make a (dummy) CoreOS image.
+coreos.img: guest-aux/make-coreos-img.sh
+ SRCDIR=$(srcdir) $(top_builddir)/run --test $<
+
# Make a (dummy) Windows image.
windows.img: guest-aux/make-windows-img.sh \
guest-aux/windows-software guest-aux/windows-system
diff --git a/tests/guests/guest-aux/make-coreos-img.sh b/tests/guests/guest-aux/make-coreos-img.sh
new file mode 100755
index 0000000..b8e0816
--- /dev/null
+++ b/tests/guests/guest-aux/make-coreos-img.sh
@@ -0,0 +1,83 @@
+#!/bin/bash -
+# libguestfs
+# Copyright (C) 2015 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# 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, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Make a CoreOS image which is enough to fool the inspection heuristics.
+
+export LANG=C
+set -e
+
+# lsb-release file.
+cat > release.tmp.$$ <<'EOF'
+DISTRIB_ID=CoreOS
+DISTRIB_RELEASE=647.0.0
+DISTRIB_CODENAME="Red Dog"
+DISTRIB_DESCRIPTION="CoreOS 647.0.0"
+EOF
+
+# Create a disk image.
+guestfish <<EOF
+sparse coreos.img.tmp.$$ 512M
+run
+
+part-init /dev/sda gpt
+part-add /dev/sda p 4096 266239
+part-add /dev/sda p 266240 270335
+part-add /dev/sda p 270336 532479
+part-add /dev/sda p 532480 794623
+part-add /dev/sda p 794624 -4096
+
+part-set-name /dev/sda 1 EFI_SYSTEM
+part-set-bootable /dev/sda 1 true
+part-set-name /dev/sda 2 BIOS-BOOT
+part-set-name /dev/sda 3 USR-A
+part-set-name /dev/sda 4 USR-B
+part-set-name /dev/sda 5 ROOT
+
+mkfs fat /dev/sda1
+mkfs ext4 /dev/sda3
+set-label /dev/sda3 USR-A
+set-uuid /dev/sda3 01234567-0123-0123-0123-012345678901
+mkfs ext4 /dev/sda5
+set-label /dev/sda5 ROOT
+set-uuid /dev/sda5 01234567-0123-0123-0123-012345678902
+
+# Enough to fool inspection API.
+mount /dev/sda5 /
+mkdir-p /etc/coreos
+mkdir /usr
+mount /dev/sda3 /usr
+mkdir /usr/bin
+mkdir /usr/lib64
+mkdir /usr/local
+mkdir-p /usr/share/coreos/
+
+ln-s usr/bin /bin
+ln-s usr/lib64 /lib64
+ln-s lib64 /lib
+mkdir /root
+mkdir /home
+
+write /etc/coreos/update.conf "GROUP=stable"
+upload release.tmp.$$ /usr/share/coreos/lsb-release
+ln-s ../usr/share/coreos/lsb-release /etc/lsb-release
+write /etc/hostname "coreos.invalid"
+
+EOF
+
+rm release.tmp.$$
+mv coreos.img.tmp.$$ coreos.img
diff --git a/tests/guests/guests.xml.in b/tests/guests/guests.xml.in
index 451d3fa..8f7ac81 100644
--- a/tests/guests/guests.xml.in
+++ b/tests/guests/guests.xml.in
@@ -248,6 +248,22 @@
</domain>
<domain type='test'>
+ <name>coreos</name>
+ <memory>1048576</memory>
+ <os>
+ <type>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <devices>
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='raw'/>
+ <source file='@abs_builddir(a)/coreos.img'/>
+ <target dev='vda' bus='virtio'/>
+ </disk>
+ </devices>
+ </domain>
+
+ <domain type='test'>
<name>windows</name>
<memory>1048576</memory>
<os>
--
2.1.0
9 years, 5 months
libguestfs-tools missing implicit dependency in ubuntu
by Andrew Pennebaker
When I install libguestfs-tools in ubuntu (latest docker base image, 14.04
trusty tahr) and run virt-builder --list, it complains of a missing curl
dependency.
We should probably make curl an explicit dependency in the deb package to
fix this.
--
Cheers,
Andrew
9 years, 5 months
[PATCH v3] RFC: New virt-dib tool
by Pino Toscano
virt-dib is a new tool to run the elements of diskimage-builder using
libguestfs.
---
More or less stable now, although with debug stuff here and there;
needs initial review, for being included. I would like to have it
reviewed at this point, so it can be broadly used.
TODO items open:
- improve the documentation
- review
- getting more testing (although it has been successfully tested for
a while, not only by myself)
- polish
.gitignore | 5 +
Makefile.am | 1 +
appliance/packagelist.in | 5 +
configure.ac | 1 +
dib/Makefile.am | 144 ++++++++
dib/cmdline.ml | 239 ++++++++++++
dib/dib.ml | 920 +++++++++++++++++++++++++++++++++++++++++++++++
dib/elements.ml | 191 ++++++++++
dib/utils.ml | 137 +++++++
dib/virt-dib.pod | 591 ++++++++++++++++++++++++++++++
po-docs/podfiles | 1 +
run.in | 1 +
12 files changed, 2236 insertions(+)
create mode 100644 dib/Makefile.am
create mode 100644 dib/cmdline.ml
create mode 100644 dib/dib.ml
create mode 100644 dib/elements.ml
create mode 100644 dib/utils.ml
create mode 100644 dib/virt-dib.pod
diff --git a/.gitignore b/.gitignore
index 29f9d6d..054657a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -118,6 +118,10 @@ Makefile.in
/df/stamp-virt-df.pod
/df/virt-df
/df/virt-df.1
+/dib/.depend
+/dib/stamp-virt-dib.pod
+/dib/virt-dib
+/dib/virt-dib.1
/diff/stamp-virt-diff.pod
/diff/virt-diff
/diff/virt-diff.1
@@ -241,6 +245,7 @@ Makefile.in
/html/virt-copy-out.1.html
/html/virt-customize.1.html
/html/virt-df.1.html
+/html/virt-dib.1.html
/html/virt-diff.1.html
/html/virt-edit.1.html
/html/virt-filesystems.1.html
diff --git a/Makefile.am b/Makefile.am
index 331a34e..194ee19 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -133,6 +133,7 @@ SUBDIRS += \
mllib \
customize \
builder builder/website \
+ dib \
resize \
sparsify \
sysprep \
diff --git a/appliance/packagelist.in b/appliance/packagelist.in
index 76c7293..160d9e8 100644
--- a/appliance/packagelist.in
+++ b/appliance/packagelist.in
@@ -257,3 +257,8 @@ ifelse(VALGRIND_DAEMON,1,valgrind)
dnl Define this by doing: ./configure --with-extra-packages="..."
EXTRA_PACKAGES
+curl
+qemu-img
+debootstrap
+apt
+which
diff --git a/configure.ac b/configure.ac
index 1552bff..8194bb6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1740,6 +1740,7 @@ AC_CONFIG_FILES([Makefile
customize/Makefile
daemon/Makefile
df/Makefile
+ dib/Makefile
diff/Makefile
edit/Makefile
erlang/Makefile
diff --git a/dib/Makefile.am b/dib/Makefile.am
new file mode 100644
index 0000000..8932e64
--- /dev/null
+++ b/dib/Makefile.am
@@ -0,0 +1,144 @@
+# libguestfs virt-dib tool
+# Copyright (C) 2015 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# 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, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+include $(top_srcdir)/subdir-rules.mk
+
+EXTRA_DIST = \
+ $(SOURCES_ML) $(SOURCES_C) \
+ virt-dib.pod
+
+CLEANFILES = *~ *.annot *.cmi *.cmo *.cmx *.cmxa *.o virt-dib
+
+SOURCES_ML = \
+ utils.ml \
+ cmdline.ml \
+ elements.ml \
+ dib.ml
+
+SOURCES_C = \
+ $(top_srcdir)/mllib/mkdtemp-c.c
+
+bin_PROGRAMS =
+
+if HAVE_OCAML
+
+bin_PROGRAMS += virt-dib
+
+virt_dib_SOURCES = $(SOURCES_C)
+virt_dib_CPPFLAGS = \
+ -I. \
+ -I$(top_builddir) \
+ -I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib \
+ -I$(shell $(OCAMLC) -where) \
+ -I$(top_srcdir)/gnulib/lib \
+ -I$(top_srcdir)/src
+virt_dib_CFLAGS = \
+ -pthread \
+ $(WARN_CFLAGS) $(WERROR_CFLAGS)
+
+BOBJECTS = \
+ $(top_builddir)/mllib/libdir.cmo \
+ $(top_builddir)/mllib/config.cmo \
+ $(top_builddir)/mllib/common_gettext.cmo \
+ $(top_builddir)/mllib/common_utils.cmo \
+ $(top_builddir)/mllib/mkdtemp.cmo \
+ $(SOURCES_ML:.ml=.cmo)
+XOBJECTS = $(BOBJECTS:.cmo=.cmx)
+
+# -I $(top_builddir)/src/.libs is a hack which forces corresponding -L
+# option to be passed to gcc, so we don't try linking against an
+# installed copy of libguestfs.
+OCAMLPACKAGES = \
+ -package str,unix \
+ -I $(top_builddir)/src/.libs \
+ -I $(top_builddir)/gnulib/lib/.libs \
+ -I $(top_builddir)/ocaml \
+ -I $(top_builddir)/mllib
+if HAVE_OCAML_PKG_GETTEXT
+OCAMLPACKAGES += -package gettext-stub
+endif
+
+OCAMLCLIBS = \
+ -pthread -lpthread \
+ -lutils \
+ $(LIBINTL) \
+ -lgnu
+
+OCAMLFLAGS = $(OCAML_FLAGS) $(OCAML_WARN_ERROR)
+
+if !HAVE_OCAMLOPT
+OBJECTS = $(BOBJECTS)
+BEST = c
+OCAMLLINKFLAGS = mlguestfs.cma -custom
+else
+OBJECTS = $(XOBJECTS)
+BEST = opt
+OCAMLLINKFLAGS = mlguestfs.cmxa
+endif
+
+virt_dib_DEPENDENCIES = $(OBJECTS) $(top_srcdir)/ocaml-link.sh
+virt_dib_LINK = \
+ $(top_srcdir)/ocaml-link.sh -cclib '$(OCAMLCLIBS)' -- \
+ $(OCAMLFIND) $(BEST) $(OCAMLFLAGS) $(OCAMLPACKAGES) $(OCAMLLINKFLAGS) \
+ $(OBJECTS) -o $@
+
+.mli.cmi:
+ $(OCAMLFIND) ocamlc $(OCAMLFLAGS) $(OCAMLPACKAGES) -c $< -o $@
+.ml.cmo:
+ $(OCAMLFIND) ocamlc $(OCAMLFLAGS) $(OCAMLPACKAGES) -c $< -o $@
+if HAVE_OCAMLOPT
+.ml.cmx:
+ $(OCAMLFIND) ocamlopt $(OCAMLFLAGS) $(OCAMLPACKAGES) -c $< -o $@
+endif
+
+# Manual pages and HTML files for the website.
+
+man_MANS = virt-dib.1
+
+noinst_DATA = $(top_builddir)/html/virt-dib.1.html
+
+virt-dib.1 $(top_builddir)/html/virt-dib.1.html: stamp-virt-dib.pod
+
+stamp-virt-dib.pod: virt-dib.pod
+ $(PODWRAPPER) \
+ --man virt-dib.1 \
+ --html $(top_builddir)/html/virt-dib.1.html \
+ --license GPLv2+ \
+ $<
+ touch $@
+
+CLEANFILES += stamp-virt-dib.pod
+
+# Dependencies.
+depend: .depend
+
+.depend: $(wildcard $(abs_srcdir)/*.mli) $(wildcard $(abs_srcdir)/*.ml)
+ rm -f $@ $@-t
+ $(OCAMLFIND) ocamldep -I ../ocaml -I $(abs_srcdir) -I $(abs_top_builddir)/mllib $^ | \
+ $(SED) 's/ *$$//' | \
+ $(SED) -e :a -e '/ *\\$$/N; s/ *\\\n */ /; ta' | \
+ $(SED) -e 's,$(abs_srcdir)/,$(builddir)/,g' | \
+ sort > $@-t
+ mv $@-t $@
+
+-include .depend
+
+endif
+
+DISTCLEANFILES = .depend
+
+.PHONY: depend docs
diff --git a/dib/cmdline.ml b/dib/cmdline.ml
new file mode 100644
index 0000000..252465b
--- /dev/null
+++ b/dib/cmdline.ml
@@ -0,0 +1,239 @@
+(* virt-dib
+ * Copyright (C) 2015 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Command line argument parsing. *)
+
+open Common_gettext.Gettext
+open Common_utils
+
+open Utils
+
+open Printf
+
+let parse_args () =
+ let usage_msg =
+ sprintf (f_"\
+%s: run diskimage-builder elements to generate images
+
+ virt-dib -B DIB-LIB -p ELEMENTS-PATH elements...
+
+A short summary of the options is given below. For detailed help please
+read the man page virt-dib(1).
+")
+ prog in
+
+ let elements = ref [] in
+ let append_element element =
+ elements := element :: !elements in
+
+ let excluded_elements = ref [] in
+ let append_excluded_element element =
+ excluded_elements := element :: !excluded_elements in
+
+ let element_paths = ref [] in
+ let append_element_path arg =
+ element_paths := arg :: !element_paths in
+
+ let excluded_scripts = ref [] in
+ let append_excluded_script arg =
+ excluded_scripts := arg :: !excluded_scripts in
+
+ let debug = ref 0 in
+
+ let basepath = ref "" in
+
+ let image_name = ref "image" in
+
+ let fs_type = ref "ext4" in
+
+ let size = ref (unit_GB 5) in
+ let set_size arg = size := parse_size arg in
+
+ let memsize = ref None in
+ let set_memsize arg = memsize := Some arg in
+
+ let network = ref true in
+
+ let smp = ref None in
+ let set_smp arg = smp := Some arg in
+
+ let formats = ref ["qcow2"] in
+ let set_format arg =
+ let fmts = string_nsplit "," arg in
+ let fmts = remove_dups fmts in
+ List.iter (
+ function
+ | "qcow2" | "tar" | "raw" | "vhd" -> ()
+ | fmt ->
+ error (f_"invalid format '%s' in --formats") fmt
+ ) fmts;
+ formats := fmts in
+
+ let envvars = ref [] in
+ let append_envvar arg =
+ envvars := arg :: !envvars in
+
+ let use_base = ref true in
+
+ let arch = ref "" in
+
+ let drive = ref None in
+ let set_drive arg = drive := Some arg in
+
+ let root_label = ref None in
+ let set_root_label arg = root_label := Some arg in
+
+ let install_type = ref "source" in
+
+ let image_cache = ref None in
+ let set_image_cache arg = image_cache := Some arg in
+
+ let compressed = ref true in
+
+ let delete_on_failure = ref true in
+
+ let is_ramdisk = ref false in
+ let ramdisk_element = ref "ramdisk" in
+
+ let qemu_img_options = ref None in
+ let set_qemu_img_options arg = qemu_img_options := Some arg in
+
+ let mkfs_options = ref None in
+ let set_mkfs_options arg = mkfs_options := Some arg in
+
+ let machine_readable = ref false in
+
+ let extra_packages = ref [] in
+ let append_extra_packages arg =
+ extra_packages := List.rev (string_nsplit "," arg) @ !extra_packages in
+
+ let argspec = [
+ "--short-options", Arg.Unit display_short_options, " " ^ s_"List short options";
+ "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options";
+
+ "-p", Arg.String append_element_path, "path" ^ " " ^ s_"Add new a elements location";
+ "--element-path", Arg.String append_element_path, "path" ^ " " ^ s_"Add new a elements location";
+ "--exclude-element", Arg.String append_excluded_element,
+ "element" ^ " " ^ s_"Exclude the specified element";
+ "--exclude-script", Arg.String append_excluded_script,
+ "script" ^ " " ^ s_"Exclude the specified script";
+ "--envvar", Arg.String append_envvar, "envvar[=value]" ^ " " ^ s_"Carry/set this environment variable";
+ "-n", Arg.Clear use_base, " " ^ s_"Skip the inclusion of the 'base' element";
+ "--root-label", Arg.String set_root_label, "label" ^ " " ^ s_"Label for the root fs";
+ "--install-type", Arg.Set_string install_type, "type" ^ " " ^ s_"Installation type";
+ "--image-cache", Arg.String set_image_cache, "directory" ^ " " ^ s_"Location for cached images";
+ "-u", Arg.Clear compressed, " " ^ "Do not compress the qcow2 image";
+ "--qemu-img-options", Arg.String set_qemu_img_options,
+ "option" ^ " " ^ s_"Add qemu-img options";
+ "--mkfs-options", Arg.String set_mkfs_options,
+ "option" ^ " " ^ s_"Add mkfs options";
+ "--extra-packages", Arg.String append_extra_packages,
+ "pkg,..." ^ " " ^ s_"Add extra packages to install";
+
+ "--ramdisk", Arg.Set is_ramdisk, " " ^ "Switch to a ramdisk build";
+ "--ramdisk-element", Arg.Set_string ramdisk_element, "name" ^ " " ^ s_"Main element for building ramdisks";
+
+ "--name", Arg.Set_string image_name, "name" ^ " " ^ s_"Name of the image";
+ "--fs-type", Arg.Set_string fs_type, "fs" ^ " " ^ s_"Filesystem for the image";
+ "--size", Arg.String set_size, "size" ^ " " ^ s_"Set output disk size";
+ "--formats", Arg.String set_format, "qcow2,tgz,..." ^ " " ^ s_"Output formats";
+ "--arch", Arg.Set_string arch, "arch" ^ " " ^ s_"Output architecture";
+ "--drive", Arg.String set_drive, "path" ^ " " ^ s_"Optional drive for caches";
+
+ "-m", Arg.Int set_memsize, "mb" ^ " " ^ s_"Set memory size";
+ "--memsize", Arg.Int set_memsize, "mb" ^ " " ^ s_"Set memory size";
+ "--network", Arg.Set network, " " ^ s_"Enable appliance network (default)";
+ "--no-network", Arg.Clear network, " " ^ s_"Disable appliance network";
+ "--smp", Arg.Int set_smp, "vcpus" ^ " " ^ s_"Set number of vCPUs";
+ "--no-delete-on-failure", Arg.Clear delete_on_failure,
+ " " ^ s_"Don't delete output file on failure";
+ "--machine-readable", Arg.Set machine_readable, " " ^ s_"Make output machine readable";
+
+ "-V", Arg.Unit print_version_and_exit, " " ^ s_"Display version and exit";
+ "--version", Arg.Unit print_version_and_exit, " " ^ s_"Display version and exit";
+ "-v", Arg.Unit set_verbose, " " ^ s_"Enable libguestfs debugging messages";
+ "--verbose", Arg.Unit set_verbose, " " ^ s_"Enable libguestfs debugging messages";
+ "-x", Arg.Unit set_trace, " " ^ s_"Enable tracing of libguestfs calls";
+ "--debug", Arg.Set_int debug, "level" ^ " " ^ s_"Set debug level";
+ "-B", Arg.Set_string basepath, "path" ^ " " ^ s_"Base path of diskimage-builder library";
+ ] in
+
+ let argspec =
+ let cmp (arg1, _, _) (arg2, _, _) =
+ let arg1 = skip_dashes arg1 and arg2 = skip_dashes arg2 in
+ compare (String.lowercase arg1) (String.lowercase arg2)
+ in
+ List.sort cmp argspec in
+ let argspec = Arg.align argspec in
+ long_options := argspec;
+
+ Arg.parse argspec append_element usage_msg;
+
+ let debug = !debug in
+ let basepath = !basepath in
+ let elements = List.rev !elements in
+ let excluded_elements = List.rev !excluded_elements in
+ let element_paths = List.rev !element_paths in
+ let excluded_scripts = List.rev !excluded_scripts in
+ let image_name = !image_name in
+ let fs_type = !fs_type in
+ let size = !size in
+ let memsize = !memsize in
+ let network = !network in
+ let smp = !smp in
+ let formats = !formats in
+ let envvars = !envvars in
+ let use_base = !use_base in
+ let arch = !arch in
+ let drive = !drive in
+ let root_label = !root_label in
+ let install_type = !install_type in
+ let image_cache = !image_cache in
+ let compressed = !compressed in
+ let delete_on_failure = !delete_on_failure in
+ let is_ramdisk = !is_ramdisk in
+ let ramdisk_element = !ramdisk_element in
+ let qemu_img_options = !qemu_img_options in
+ let mkfs_options = !mkfs_options in
+ let machine_readable = !machine_readable in
+ let extra_packages = List.rev !extra_packages in
+
+ (* No elements and machine-readable mode? Print some facts. *)
+ if elements = [] && machine_readable then (
+ printf "virt-dib\n";
+ printf "output:qcow2\n";
+ printf "output:tar\n";
+ printf "output:raw\n";
+ printf "output:vhd\n";
+ exit 0
+ );
+
+ if basepath = "" then
+ error (f_"-B must be specified");
+
+ if formats = [] then
+ error (f_"the list of output formats cannot be empty");
+
+ if elements = [] then
+ error (f_"at least one distribution root element must be specified");
+
+ debug, basepath, elements, excluded_elements, element_paths,
+ excluded_scripts, use_base, drive,
+ image_name, fs_type, size, root_label, install_type, image_cache, compressed,
+ qemu_img_options, mkfs_options, is_ramdisk, ramdisk_element, extra_packages,
+ memsize, network, smp, delete_on_failure, formats, arch, envvars
diff --git a/dib/dib.ml b/dib/dib.ml
new file mode 100644
index 0000000..3dba88f
--- /dev/null
+++ b/dib/dib.ml
@@ -0,0 +1,920 @@
+(* virt-dib
+ * Copyright (C) 2015 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Common_gettext.Gettext
+open Common_utils
+
+open Cmdline
+open Utils
+open Elements
+
+open Printf
+
+module G = Guestfs
+
+let exclude_elements elements excluded_elements =
+ match excluded_elements with
+ | [] ->
+ (* No elements to filter out, so just don't bother iterating through
+ * the elements. *)
+ elements
+ | excl -> StringSet.filter (not_in_list excl) elements
+
+let read_envvars envvars =
+ filter_map (
+ fun var ->
+ let i = string_find var "=" in
+ if i = -1 then (
+ try Some (var, Sys.getenv var)
+ with Not_found -> None
+ ) else (
+ let len = String.length var in
+ Some (String.sub var 0 i, String.sub var (i + 1) (len - i - 1))
+ )
+ ) envvars
+
+let read_dib_envvars () =
+ let vars = Array.to_list (Unix.environment ()) in
+ let vars = List.filter (fun x -> string_prefix x "DIB_") vars in
+ let vars = List.map (fun x -> x ^ "\n") vars in
+ String.concat "" vars
+
+let make_dib_args args =
+ let args = Array.to_list args in
+ let rec quote_args = function
+ | [] -> ""
+ | x :: xs -> " " ^ (quote x) ^ quote_args xs
+ in
+ match args with
+ | [] -> ""
+ | app :: xs -> app ^ quote_args xs
+
+let write_script fn text =
+ let oc = open_out fn in
+ output_string oc text;
+ flush oc;
+ close_out oc;
+ Unix.chmod fn 0o755
+
+let prepare_external ~dib_args ~dib_vars ~out_name ~root_label ~rootfs_uuid
+ ~image_cache ~arch ~network ~debug
+ destdir libdir hooksdir tmpdir fakebindir all_elements element_paths =
+ let network_string = if network then "" else "1" in
+
+ let run_extra = sprintf "\
+#!/bin/bash
+%s
+target_dir=$1
+shift
+script=$1
+shift
+
+export PATH=%s:$PATH
+
+# d-i-b variables
+export TMP_MOUNT_PATH=%s
+export DIB_OFFLINE=%s
+export IMAGE_NAME=\"%s\"
+export DIB_ROOT_LABEL=\"%s\"
+export DIB_IMAGE_ROOT_FS_UUID=%s
+export DIB_IMAGE_CACHE=\"%s\"
+export _LIB=%s
+export ARCH=%s
+export TMP_HOOKS_PATH=%s
+export DIB_ARGS=\"%s\"
+export IMAGE_ELEMENT=\"%s\"
+export ELEMENTS_PATH=\"%s\"
+export DIB_ENV=%s
+export TMPDIR=\"${TMP_MOUNT_PATH}/tmp\"
+export TMP_DIR=\"${TMPDIR}\"
+export DIB_DEBUG_TRACE=%d
+
+ENVIRONMENT_D_DIR=$target_dir/../environment.d
+
+if [ -d $ENVIRONMENT_D_DIR ] ; then
+ env_files=$(find $ENVIRONMENT_D_DIR -maxdepth 1 -xtype f | \
+ grep -E \"/[0-9A-Za-z_\\.-]+$\" | \
+ LANG=C sort -n)
+ for env_file in $env_files ; do
+ source $env_file
+ done
+fi
+
+$target_dir/$script
+"
+ (if debug >= 1 then "set -x\n" else "")
+ fakebindir
+ (quote tmpdir)
+ network_string
+ out_name
+ root_label
+ rootfs_uuid
+ image_cache
+ (quote libdir)
+ arch
+ (quote hooksdir)
+ dib_args
+ (String.concat " " (StringSet.elements all_elements))
+ (String.concat ":" element_paths)
+ (quote dib_vars)
+ debug in
+ write_script (destdir // "run-part-extra.sh") run_extra;
+
+ (* Needed as TMPDIR for the extra-data hooks *)
+ do_mkdir (tmpdir // "tmp")
+
+let prepare_aux ~envvars ~dib_args ~dib_vars ~log_file ~out_name ~rootfs_uuid
+ ~arch ~network ~root_label ~install_type ~debug ~extra_packages
+ destdir all_elements =
+ let envvars_string = List.map (
+ fun (var, value) ->
+ sprintf "export %s=%s" var (quote value)
+ ) envvars in
+ let network_string = if network then "" else "1" in
+
+ let script_run_part = sprintf "\
+#!/bin/bash
+%s
+sysroot=$1
+shift
+mysysroot=$1
+shift
+blockdev=$1
+shift
+target_dir=$1
+shift
+new_wd=$1
+shift
+script=$1
+shift
+
+# user variables
+%s
+
+# system variables
+export HOME=$mysysroot/tmp/aux/perm/home
+export PATH=$mysysroot/tmp/aux/hooks/bin:$PATH
+export TMP=$mysysroot/tmp
+export TMPDIR=$TMP
+export TMP_DIR=$TMP
+
+# d-i-b variables
+export TMP_MOUNT_PATH=$sysroot
+export TARGET_ROOT=$sysroot
+export DIB_OFFLINE=%s
+export IMAGE_NAME=\"%s\"
+export DIB_IMAGE_ROOT_FS_UUID=%s
+export DIB_IMAGE_CACHE=$HOME/.cache/image-create
+export DIB_ROOT_LABEL=\"%s\"
+export _LIB=$mysysroot/tmp/aux/lib
+export _PREFIX=$mysysroot/tmp/aux/elements
+export ARCH=%s
+export TMP_HOOKS_PATH=$mysysroot/tmp/aux/hooks
+export DIB_ARGS=\"%s\"
+export DIB_MANIFEST_SAVE_DIR=\"$mysysroot/tmp/aux/out/${IMAGE_NAME}.d\"
+export IMAGE_BLOCK_DEVICE=$blockdev
+export IMAGE_ELEMENT=\"%s\"
+export DIB_ENV=%s
+export DIB_DEBUG_TRACE=%d
+export DIB_NO_TMPFS=1
+
+export TMP_BUILD_DIR=$mysysroot/tmp/aux
+export TMP_IMAGE_DIR=$mysysroot/tmp/aux
+
+if [ -n \"$mysysroot\" ]; then
+ export PATH=$mysysroot/tmp/aux/fake-bin:$PATH
+else
+ export PATH=\"$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\"
+fi
+
+ENVIRONMENT_D_DIR=$target_dir/../environment.d
+
+if [ -d $ENVIRONMENT_D_DIR ] ; then
+ env_files=$(find $ENVIRONMENT_D_DIR -maxdepth 1 -xtype f | \
+ grep -E \"/[0-9A-Za-z_\\.-]+$\" | \
+ LANG=C sort -n)
+ for env_file in $env_files ; do
+ source $env_file
+ done
+fi
+
+if [ -n \"$new_wd\" ]; then
+ cd \"$mysysroot/$new_wd\"
+fi
+
+$target_dir/$script
+"
+ (if debug >= 1 then "set -x\n" else "")
+ (String.concat "\n" envvars_string)
+ network_string
+ out_name
+ rootfs_uuid
+ root_label
+ arch
+ dib_args
+ (String.concat " " (StringSet.elements all_elements))
+ (quote dib_vars)
+ debug in
+ write_script (destdir // "run-part.sh") script_run_part;
+ let script_run_and_log = "\
+#!/bin/bash
+logfile=$1
+shift
+exec 3>&1
+exit `( ( ( $(dirname $0)/run-part.sh \"$@\" ) 2>&1 3>&-; echo $? >&4) | tee -a $logfile >&3 >&2) 4>&1`
+" in
+ write_script (destdir // "run-and-log.sh") script_run_and_log;
+
+ (* Create the fake sudo support. *)
+ do_mkdir (destdir // "fake-bin");
+ let fake_sudo = "\
+#!/bin/bash
+
+SCRIPTNAME=fake-sudo
+
+ARGS_SHORT=\"EHiu:\"
+ARGS_LONG=\"\"
+TEMP=`POSIXLY_CORRECT=1 getopt ${ARGS_SHORT:+-o $ARGS_SHORT} ${ARGS_LONG:+--long $ARGS_LONG} \
+ -n \"$SCRIPTNAME\" -- \"$@\"`
+if [ $? != 0 ]; then echo \"$SCRIPTNAME: terminating...\" >&2 ; exit 1 ; fi
+eval set -- \"$TEMP\"
+
+preserve_env=
+set_home=
+login_shell=
+user=
+
+while true; do
+ case \"$1\" in
+ -E) preserve_env=1; shift;;
+ -H) set_home=1; shift;;
+ -i) login_shell=1; shift;;
+ -u) user=$2; shift 2;;
+ --) shift; break;;
+ *) echo \"$SCRIPTNAME: internal arguments error\"; exit 1;;
+ esac
+done
+
+if [ -n \"$user\" ]; then
+ if [ $user != root -a $user != `whoami` ]; then
+ echo \"$SCRIPTNAME: cannot use the sudo user $user, only root and $(whoami) handled\" >&2
+ exit 1
+ fi
+fi
+
+if [ -z \"$preserve_env\" ]; then
+ for envvar in `env | grep '^\\w' | cut -d= -f1`; do
+ case \"$envvar\" in
+ PATH | USER | USERNAME | HOSTNAME | TERM | LANG | HOME | SHELL | LOGNAME ) ;;
+ *) unset $envvar ;;
+ esac
+ done
+fi
+
+cmd=$1
+shift
+$cmd \"$@\"
+" in
+ write_script (destdir // "fake-bin" // "sudo") fake_sudo;
+ (* Pick dib-run-parts from the host, if available, otherwise put
+ * a fake executable which will error out if used.
+ *)
+ (try
+ let loc = which "dib-run-parts" in
+ do_cp loc (destdir // "fake-bin")
+ with Tool_not_found _ ->
+ let fake_dib_run_parts = "\
+#!/bin/sh
+echo \"Please install dib-run-parts on the host\"
+exit 1
+" in
+ write_script (destdir // "fake-bin" // "dib-run-parts") fake_dib_run_parts;
+ );
+
+ (* Write the custom hooks. *)
+ let script_install_type_env = sprintf "\
+export DIB_DEFAULT_INSTALLTYPE=${DIB_DEFAULT_INSTALLTYPE:-\"%s\"}
+"
+ install_type in
+ write_script (destdir // "hooks" // "environment.d" // "11-dib-install-type.bash") script_install_type_env;
+
+ (* Write install-packages.sh if needed. *)
+ if extra_packages <> [] then (
+ let script_install_packages = sprintf "\
+#!/bin/bash
+install-packages %s
+"
+ (String.concat " " extra_packages) in
+ write_script (destdir // "install-packages.sh") script_install_packages;
+ );
+
+ do_mkdir (destdir // "perm")
+
+let timing_output ~target_name entries timings =
+ let buf = Buffer.create 4096 in
+ Buffer.add_string buf "----------------------- PROFILING -----------------------\n";
+ Buffer.add_char buf '\n';
+ bprintf buf "Target: %s\n" target_name;
+ Buffer.add_char buf '\n';
+ bprintf buf "%-40s %9s\n" "Script" "Seconds";
+ bprintf buf "%-40s %9s\n" "---------------------------------------" "----------";
+ Buffer.add_char buf '\n';
+ List.iter (
+ fun x ->
+ bprintf buf "%-40s %10.3f\n" x (Hashtbl.find timings x);
+ ) entries;
+ Buffer.add_char buf '\n';
+ Buffer.add_string buf "--------------------- END PROFILING ---------------------\n";
+ Buffer.contents buf
+
+type sysroot_type =
+ | In
+ | Out
+ | Subroot
+
+let timed_run fn =
+ let time_before = Unix.gettimeofday () in
+ fn ();
+ let time_after = Unix.gettimeofday () in
+ time_after -. time_before
+
+let run_parts ~debug ~sysroot ~blockdev ~log_file ?(new_wd = "")
+ (g : Guestfs.guestfs) hook_name scripts =
+ let hook_dir = "/tmp/aux/hooks/" ^ hook_name in
+ let scripts = List.sort digit_prefix_compare scripts in
+ let outbuf = Buffer.create 16384 in
+ let timings = Hashtbl.create 13 in
+ let new_wd =
+ match sysroot, new_wd with
+ | (Out|Subroot), "" -> "''"
+ | _, dir -> dir in
+ List.iter (
+ fun x ->
+ message (f_"Running: %s/%s") hook_name x;
+ g#write_append log_file (sprintf "Running %s/%s...\n" hook_name x);
+ let out = ref "" in
+ let run () =
+ let outstr =
+ match sysroot with
+ | In ->
+ g#sh (sprintf "/tmp/aux/run-and-log.sh '%s' '' '' '%s' '%s' '%s' '%s'" log_file blockdev hook_dir new_wd x)
+ | Out ->
+ g#debug "sh" [| "/sysroot/tmp/aux/run-and-log.sh"; "/sysroot" ^ log_file; "/sysroot"; "/sysroot"; blockdev; "/sysroot" ^ hook_dir; new_wd; x |]
+ | Subroot ->
+ g#debug "sh" [| "/sysroot/tmp/aux/run-and-log.sh"; "/sysroot" ^ log_file; "/sysroot/subroot"; "/sysroot"; blockdev; "/sysroot" ^ hook_dir; new_wd; x |] in
+ out := outstr;
+ Buffer.add_string outbuf outstr in
+ let delta_t = timed_run run in
+ Buffer.add_char outbuf '\n';
+ out := ensure_trailing_newline !out;
+ printf "%s%!" !out;
+ if debug >= 1 then (
+ printf "%s completed after %.3f s\n" x delta_t
+ );
+ Hashtbl.add timings x delta_t;
+ ) scripts;
+ g#write_append log_file (timing_output ~target_name:hook_name scripts timings);
+ flush_all ();
+ Buffer.contents outbuf
+
+let run_parts_host ~debug hooks_dir hook_name scripts run_script =
+ let hook_dir = hooks_dir // hook_name in
+ let scripts = List.sort digit_prefix_compare scripts in
+ let timings = Hashtbl.create 13 in
+ List.iter (
+ fun x ->
+ message (f_"Running: %s/%s") hook_name x;
+ let cmd = sprintf "%s %s %s" (quote run_script) (quote hook_dir) (quote x) in
+ let run () =
+ run_command cmd in
+ let delta_t = timed_run run in
+ if debug >= 1 then (
+ printf "\n";
+ printf "%s completed after %.3f s\n" x delta_t
+ );
+ Hashtbl.add timings x delta_t;
+ ) scripts;
+ if debug >= 1 then (
+ print_string (timing_output ~target_name:hook_name scripts timings)
+ );
+ flush_all ()
+
+let run_install_packages ~debug ~blockdev ~log_file
+ (g : Guestfs.guestfs) packages =
+ let pkgs_string = String.concat " " packages in
+ message (f_"Installing: %s") pkgs_string;
+ g#write_append log_file (sprintf "Installing %s...\n" pkgs_string);
+ let out = g#sh (sprintf "/tmp/aux/run-and-log.sh '%s' '' '' '%s' '/tmp/aux' '' 'install-packages.sh'" log_file blockdev) in
+ let out = ensure_trailing_newline out in
+ if debug >= 1 then (
+ printf "%s%!" out;
+ printf "package installation completed\n";
+ );
+ flush_all ();
+ out
+
+let main () =
+ let debug, basepath, elements, excluded_elements, element_paths,
+ excluded_scripts, use_base, drive,
+ image_name, fs_type, size, root_label, install_type, image_cache, compressed,
+ qemu_img_options, mkfs_options, is_ramdisk, ramdisk_element, extra_packages,
+ memsize, network, smp, delete_on_failure, formats, arch, envvars =
+ parse_args () in
+
+ (* Check that the specified base directory of diskimage-builder
+ * has the "die" script in it, so we know the directory is the
+ * right one (hopefully so, at least).
+ *)
+ if not (Sys.file_exists (basepath // "die")) then
+ error (f_"the specified base path is not the diskimage-builder library");
+
+ (* Check for required tools. *)
+ require_tool "uuidgen";
+ if List.mem "qcow2" formats then
+ require_tool "qemu-img";
+ if List.mem "vhd" formats then
+ require_tool "vhd-util";
+
+ let image_name_d = image_name ^ ".d" in
+
+ let tmpdir = Mkdtemp.temp_dir "dib." "" in
+ rmdir_on_exit tmpdir;
+ let auxtmpdir = tmpdir // "aux" in
+ do_mkdir auxtmpdir;
+ let hookstmpdir = auxtmpdir // "hooks" in
+ do_mkdir (hookstmpdir // "environment.d"); (* Just like d-i-b does. *)
+ let extradatatmpdir = tmpdir // "extra-data" in
+ do_mkdir extradatatmpdir;
+ do_mkdir (auxtmpdir // "out" // image_name_d);
+ let elements = if use_base then ["base"] @ elements else elements in
+ let elements = if is_ramdisk then [ramdisk_element] @ elements else elements in
+ message (f_"Elements: %s") (String.concat " " elements);
+ if debug >= 1 then (
+ printf "tmpdir: %s\n" tmpdir;
+ printf "element paths: %s\n" (String.concat ":" element_paths);
+ );
+
+ let loaded_elements = load_elements ~debug element_paths in
+ if debug >= 1 then (
+ printf "loaded elements:\n";
+ Hashtbl.iter (
+ fun k v ->
+ printf " %s => %s\n" k v.directory;
+ Hashtbl.iter (
+ fun k v ->
+ printf "\t%-20s %s\n" k (String.concat " " (List.sort compare v))
+ ) v.hooks;
+ ) loaded_elements;
+ printf "\n";
+ );
+ let all_elements = load_dependencies elements loaded_elements in
+ let all_elements = exclude_elements all_elements
+ (excluded_elements @ builtin_elements_blacklist) in
+
+ message (f_"Expanded elements: %s") (String.concat " " (StringSet.elements all_elements));
+
+ let envvars = read_envvars envvars in
+ message (f_"Carried environment variables: %s") (String.concat " " (List.map fst envvars));
+ if debug >= 1 then (
+ printf "carried over envvars:\n";
+ if envvars <> [] then
+ List.iter (
+ fun (var, value) ->
+ printf " %s=%s\n" var value
+ ) envvars
+ else
+ printf " (none)\n";
+ printf "\n";
+ );
+ let dib_args = make_dib_args Sys.argv in
+ let dib_vars = read_dib_envvars () in
+ if debug >= 1 then (
+ printf "DIB args:\n%s\n" dib_args;
+ printf "DIB envvars:\n%s\n" dib_vars
+ );
+
+ message (f_"Preparing auxiliary data");
+
+ copy_elements all_elements loaded_elements
+ (excluded_scripts @ builtin_scripts_blacklist) hookstmpdir;
+
+ (* Re-read the hook scripts from the hooks dir, as d-i-b (and we too)
+ * has basically copied over anything found in elements.
+ *)
+ let final_hooks = load_hooks ~debug hookstmpdir in
+
+ let log_file = "/tmp/aux/perm/" ^ (log_filename ()) in
+
+ let arch =
+ match arch with
+ | "" -> current_arch ()
+ | arch -> arch in
+
+ let root_label =
+ match root_label with
+ | None ->
+ (* XFS has a limit of 12 characters for filesystem labels.
+ * Not changing the default for other filesystems to maintain
+ * backwards compatibility.
+ *)
+ (match fs_type with
+ | "xfs" -> "img-rootfs"
+ | _ -> "cloudimg-rootfs")
+ | Some label -> label in
+
+ let image_cache =
+ match image_cache with
+ | None -> Sys.getenv "HOME" // ".cache" // "image-create"
+ | Some dir -> dir in
+ do_mkdir image_cache;
+
+ let rootfs_uuid = uuidgen () in
+
+ let formats_img, formats_archive = List.partition (
+ function
+ | "qcow2" | "raw" | "vhd" -> true
+ | _ -> false
+ ) formats in
+ let formats_img_nonraw = List.filter ((<>) "raw") formats_img in
+
+ prepare_aux ~envvars ~dib_args ~dib_vars ~log_file ~out_name:image_name ~rootfs_uuid
+ ~arch ~network ~root_label ~install_type ~debug ~extra_packages
+ auxtmpdir all_elements;
+
+ let delete_output_file = ref delete_on_failure in
+ let delete_file () =
+ if !delete_output_file then (
+ List.iter (
+ fun fmt ->
+ try Unix.unlink (output_filename image_name fmt) with _ -> ()
+ ) formats
+ )
+ in
+ at_exit delete_file;
+
+ prepare_external ~dib_args ~dib_vars ~out_name:image_name ~root_label ~rootfs_uuid
+ ~image_cache ~arch ~network ~debug
+ tmpdir basepath hookstmpdir extradatatmpdir (auxtmpdir // "fake-bin")
+ all_elements element_paths;
+
+ let run_hook_host hook =
+ try
+ let scripts = Hashtbl.find final_hooks hook in
+ if debug >= 1 then (
+ printf "Running hooks for %s...\n%!" hook;
+ );
+ run_parts_host ~debug hookstmpdir hook scripts
+ (tmpdir // "run-part-extra.sh")
+ with Not_found -> ()
+ and run_hook ~blockdev ~sysroot ?(new_wd = "") (g : Guestfs.guestfs) hook =
+ try
+ let scripts = Hashtbl.find final_hooks hook in
+ if debug >= 1 then (
+ printf "Running hooks for %s...\n%!" hook;
+ );
+ run_parts ~debug ~sysroot ~blockdev ~log_file ~new_wd g hook scripts
+ with Not_found -> "" in
+
+ run_hook_host "extra-data.d";
+
+ let copy_in (g : Guestfs.guestfs) srcdir destdir =
+ let desttar = Filename.temp_file ~temp_dir:tmpdir "virt-dib." ".tar.gz" in
+ let cmd = sprintf "tar czf %s -C %s --owner=root --group=root ."
+ (quote desttar) (quote srcdir) in
+ run_command cmd;
+ g#mkdir_p destdir;
+ g#tar_in ~compress:"gzip" desttar destdir;
+ Sys.remove desttar in
+
+ let copy_preserve_in (g : Guestfs.guestfs) srcdir destdir =
+ let desttar = Filename.temp_file ~temp_dir:tmpdir "virt-dib." ".tar.gz" in
+ let remotetar = "/tmp/aux/" ^ (Filename.basename desttar) in
+ let cmd = sprintf "tar czf %s -C %s --owner=root --group=root ."
+ (quote desttar) (quote srcdir) in
+ run_command cmd;
+ g#upload desttar remotetar;
+ let verbose_flag = if debug > 0 then "v" else "" in
+ ignore (g#debug "sh" [| "tar"; "-C"; "/sysroot" ^ destdir; "--no-overwrite-dir"; "-x" ^ verbose_flag ^ "zf"; "/sysroot" ^ remotetar |]);
+ Sys.remove desttar;
+ g#rm remotetar in
+
+ if debug >= 1 then
+ ignore (Sys.command (sprintf "tree -ps %s" (quote tmpdir)));
+
+ message (f_"Opening the disks");
+
+ let is_ramdisk_build = is_ramdisk || StringSet.mem "ironic-agent" all_elements in
+
+ let g, tmpdisk, tmpdiskfmt, drive_partition =
+ let g = new G.guestfs () in
+ if verbose () then g#set_verbose true;
+ if trace () then g#set_trace true;
+
+ (match memsize with None -> () | Some memsize -> g#set_memsize memsize);
+ (match smp with None -> () | Some smp -> g#set_smp smp);
+ g#set_network network;
+
+ (* Make sure to turn SELinux off to avoid awkward interactions
+ * between the appliance kernel and applications/libraries interacting
+ * with SELinux xattrs.
+ *)
+ g#set_selinux false;
+
+ (* Main disk with the built image. *)
+ let fmt = "raw" in
+ let fn =
+ (* If "raw" is among the selected outputs, use it as main backing
+ * disk, otherwise create a temporary disk.
+ *)
+ if not is_ramdisk_build && List.mem "raw" formats_img then image_name
+ else Filename.temp_file ~temp_dir:tmpdir "image." "" in
+ let fn = output_filename fn fmt in
+ (* Produce the output image. *)
+ g#disk_create fn fmt size;
+ g#add_drive ~readonly:false ~format:fmt fn;
+
+ (* Helper drive for elements and binaries. *)
+ g#add_drive_scratch (unit_GB 5);
+
+ (match drive with
+ | None ->
+ g#add_drive_scratch (unit_GB 5)
+ | Some drive ->
+ g#add_drive drive;
+ );
+
+ g#launch ();
+
+ (* Prepare the /aux partition. *)
+ g#mkfs "ext2" "/dev/sdb";
+ g#mount "/dev/sdb" "/";
+
+ copy_in g auxtmpdir "/";
+ copy_in g basepath "/lib";
+ g#umount "/";
+
+ (* Prepare the /aux/perm partition. *)
+ let drive_partition =
+ match drive with
+ | None ->
+ g#mkfs "ext2" "/dev/sdc";
+ "/dev/sdc"
+ | Some _ ->
+ let partitions = Array.to_list (g#list_partitions ()) in
+ (match partitions with
+ | [] -> "/dev/sdc"
+ | p ->
+ let p = List.filter (fun x -> string_prefix x "/dev/sdc") p in
+ if p = [] then
+ error (f_"no partitions found in the helper drive");
+ List.hd p
+ ) in
+ g#mount drive_partition "/";
+ g#mkdir_p "/home/.cache/image-create";
+ g#umount "/";
+
+ g, fn, fmt, drive_partition in
+
+ let mount_aux () =
+ g#mkmountpoint "/tmp/aux";
+ g#mount "/dev/sdb" "/tmp/aux";
+ g#mount drive_partition "/tmp/aux/perm" in
+
+ (* Small kludge: try to umount all first: if that fails, use lsof and fuser
+ * to find out what might have caused the failure, run udevadm to try
+ * to settle things down (udev, you never know), and try umount all again.
+ *)
+ let checked_umount_all () =
+ try g#umount_all ()
+ with G.Error _ ->
+ if debug >= 1 then (
+ (try printf "lsof:\n%s\nEND\n" (g#debug "sh" [| "lsof"; "/sysroot"; |]) with _ -> ());
+ (try printf "fuser:\n%s\nEND\n" (g#debug "sh" [| "fuser"; "-v"; "-m"; "/sysroot"; |]) with _ -> ());
+ (try printf "losetup:\n%s\nEND\n" (g#debug "sh" [| "losetup"; "--list"; "--all" |]) with _ -> ());
+ );
+ ignore (g#debug "sh" [| "udevadm"; "--debug"; "settle" |]);
+ g#umount_all ()
+ in
+
+ g#mkmountpoint "/tmp";
+ mount_aux ();
+
+ let blockdev =
+ (* Setup a loopback device, just like d-i-b would tie an image in the host
+ * environment.
+ *)
+ let run_losetup device =
+ let lines = g#debug "sh" [| "losetup"; "--show"; "-f"; device |] in
+ let lines = string_nsplit "\n" lines in
+ let lines = List.filter ((<>) "") lines in
+ (match lines with
+ | [] -> device
+ | x :: _ -> x
+ ) in
+ let blockdev = run_losetup "/dev/sda" in
+
+ let run_hook_out_eval hook envvar =
+ let lines = run_hook ~sysroot:Out ~blockdev g hook in
+ let lines = string_nsplit "\n" lines in
+ let lines = List.filter ((<>) "") lines in
+ if lines = [] then None
+ else (try Some (var_from_lines envvar lines) with _ -> None) in
+
+ (match run_hook_out_eval "block-device.d" "IMAGE_BLOCK_DEVICE" with
+ | None -> blockdev
+ | Some x -> x
+ ) in
+
+ let rec run_hook_out ?(new_wd = "") hook =
+ do_run_hooks_noout ~sysroot:Out ~new_wd hook
+ and run_hook_in hook =
+ do_run_hooks_noout ~sysroot:In hook
+ and run_hook_subroot hook =
+ do_run_hooks_noout ~sysroot:Subroot hook
+ and do_run_hooks_noout ~sysroot ?(new_wd = "") hook =
+ ignore (run_hook ~sysroot ~blockdev ~new_wd g hook) in
+
+ g#sync ();
+ checked_umount_all ();
+ flush_all ();
+
+ message (f_"Setting up the destination root");
+
+ (* Create and mount the target filesystem. *)
+ let mkfs_options =
+ match mkfs_options with
+ | None -> []
+ | Some o -> [ o ] in
+ let mkfs_options =
+ (match fs_type with
+ | "ext4" ->
+ (* Very conservative to handle images being resized a lot
+ * Without -J option specified, default journal size will be set to 32M
+ * and online resize will be failed with error of needs too many credits.
+ *)
+ [ "-i"; "4096"; "-J"; "size=64" ]
+ | _ -> []
+ ) @ mkfs_options @ [ "-t"; fs_type; blockdev ] in
+ ignore (g#debug "sh" (Array.of_list ([ "mkfs" ] @ mkfs_options)));
+ g#set_label blockdev root_label;
+ (match fs_type with
+ | x when string_prefix x "ext" -> g#set_uuid blockdev rootfs_uuid
+ | _ -> ());
+ g#mount blockdev "/";
+ g#mkmountpoint "/tmp";
+ mount_aux ();
+ g#mkdir "/subroot";
+
+ run_hook_subroot "root.d";
+
+ g#sync ();
+ g#umount "/tmp/aux/perm";
+ g#umount "/tmp/aux";
+ g#rm_rf "/tmp";
+ let subroot_items =
+ let l = Array.to_list (g#ls "/subroot") in
+ let l_lost_plus_found, l = List.partition ((=) "lost+found") l in
+ if l_lost_plus_found <> [] then (
+ g#rm_rf "/subroot/lost+found";
+ );
+ l in
+ List.iter (fun x -> g#mv ("/subroot/" ^ x) ("/" ^ x)) subroot_items;
+ g#rmdir "/subroot";
+ (* Check /tmp exists already. *)
+ ignore (g#is_dir "/tmp");
+ mount_aux ();
+ g#ln_s "aux/hooks" "/tmp/in_target.d";
+
+ copy_preserve_in g extradatatmpdir "/";
+
+ run_hook_in "pre-install.d";
+
+ if extra_packages <> [] then
+ ignore (run_install_packages ~debug ~blockdev ~log_file g extra_packages);
+
+ run_hook_in "install.d";
+
+ run_hook_in "post-install.d";
+
+ (* Unmount and remount the image, as d-i-b does at this point too. *)
+ g#sync ();
+ checked_umount_all ();
+ flush_all ();
+ g#mount blockdev "/";
+ (* Check /tmp/aux still exists. *)
+ ignore (g#is_dir "/tmp/aux");
+ g#mount "/dev/sdb" "/tmp/aux";
+ g#mount drive_partition "/tmp/aux/perm";
+
+ run_hook_in "finalise.d";
+
+ let out_dir = "/tmp/aux/out/" ^ image_name_d in
+
+ run_hook_out ~new_wd:out_dir "cleanup.d";
+
+ g#sync ();
+
+ if g#ls out_dir <> [||] then (
+ message (f_"Extracting data out of the image");
+ do_mkdir image_name_d;
+ g#copy_out out_dir ".";
+ );
+
+ (* Unmount everything, and remount only the root to cleanup
+ * its /tmp; this way we should be pretty sure that there is
+ * nothing left mounted over /tmp, so it is safe to empty it.
+ *)
+ checked_umount_all ();
+ flush_all ();
+ g#mount blockdev "/";
+ Array.iter (fun x -> g#rm_rf ("/tmp/" ^ x)) (g#ls "/tmp");
+
+ flush_all ();
+
+ List.iter (
+ fun fmt ->
+ let fn = output_filename image_name fmt in
+ match fmt with
+ | "tar" ->
+ message (f_"Compressing the image as tar");
+ g#tar_out ~excludes:[| "./sys/*"; "./proc/*" |] "/" fn
+ | _ as fmt -> error "unhandled format: %s" fmt
+ ) formats_archive;
+
+ message (f_"Umounting the disks");
+
+ (* Now that we've finished the build, don't delete the output file on
+ * exit.
+ *)
+ delete_output_file := false;
+
+ g#sync ();
+ checked_umount_all ();
+ g#shutdown ();
+ g#close ();
+
+ flush_all ();
+
+ (* Don't produce images as output when doing a ramdisk build,
+ * or ironic-agent is one of the elements (d-i-b does this too).
+ *)
+ if not is_ramdisk_build then (
+ List.iter (
+ fun fmt ->
+ let fn = output_filename image_name fmt in
+ message (f_"Converting to %s") fmt;
+ match fmt with
+ | "qcow2" ->
+ let cmd =
+ sprintf "qemu-img convert%s -f %s %s -O %s%s %s"
+ (if compressed then " -c" else "")
+ tmpdiskfmt
+ (quote tmpdisk)
+ fmt
+ (match qemu_img_options with
+ | None -> ""
+ | Some opt -> " -o " ^ quote opt)
+ (quote (qemu_input_filename fn)) in
+ if debug >= 1 then
+ printf "%s\n%!" cmd;
+ run_command cmd
+ | "vhd" ->
+ let fn_intermediate = Filename.temp_file ~temp_dir:tmpdir "vhd-intermediate." "" in
+ let cmd =
+ sprintf "vhd-util convert -s 0 -t 1 -i %s -o %s"
+ (quote tmpdisk)
+ (quote fn_intermediate) in
+ if debug >= 1 then
+ printf "%s\n%!" cmd;
+ run_command cmd;
+ let cmd =
+ sprintf "vhd-util convert -s 1 -t 2 -i %s -o %s"
+ (quote fn_intermediate)
+ (quote fn) in
+ if debug >= 1 then
+ printf "%s\n%!" cmd;
+ run_command cmd;
+ if not (Sys.file_exists fn) then
+ error (f_"VHD output not produced, most probably vhd-util is old or not patched for 'convert'")
+ | _ as fmt -> error "unhandled format: %s" fmt
+ ) formats_img_nonraw;
+ );
+
+ message (f_"Done")
+
+let () = run_main_and_handle_errors main
diff --git a/dib/elements.ml b/dib/elements.ml
new file mode 100644
index 0000000..db7c302
--- /dev/null
+++ b/dib/elements.ml
@@ -0,0 +1,191 @@
+(* virt-dib
+ * Copyright (C) 2015 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Parsing and handling of elements. *)
+
+open Common_gettext.Gettext
+open Common_utils
+
+open Utils
+
+open Printf
+
+module StringSet = Set.Make (String)
+
+type element = {
+ directory : string;
+ hooks : hooks_map;
+}
+and hooks_map = (string, string list) Hashtbl.t (* hook name, scripts *)
+
+exception Duplicate_script of string * string (* hook, script *)
+
+(* These are the elements which we don't ever try to use. *)
+let builtin_elements_blacklist = [
+]
+
+(* These are the scripts which we don't ever try to run.
+ * Usual reason could be that they are not compatible the way virt-dib works:
+ * e.g. they expect the tree of elements outside the chroot, which is not
+ * available in the appliance. *)
+let builtin_scripts_blacklist = [
+ "01-sahara-version"; (* Gets the Git commit ID of the d-i-b and
+ * sahara-image-elements repositories. *)
+]
+
+let valid_script_name n =
+ let is_char_valid = function
+ | 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' | '-' -> true
+ | _ -> false in
+ try ignore (string_index_fn (fun c -> not (is_char_valid c)) n); false
+ with Not_found -> true
+
+let stringset_of_list l =
+ List.fold_left (fun acc x -> StringSet.add x acc) StringSet.empty l
+
+let load_hooks ~debug path =
+ let hooks = Hashtbl.create 13 in
+ let entries = Array.to_list (Sys.readdir path) in
+ let entries = List.filter (fun x -> Filename.check_suffix x ".d") entries in
+ let entries = List.map (fun x -> (x, path // x)) entries in
+ let entries = List.filter (fun (_, x) -> is_directory x) entries in
+ List.iter (
+ fun (hook, p) ->
+ let listing = Array.to_list (Sys.readdir p) in
+ let scripts = List.filter valid_script_name listing in
+ let scripts = List.filter (
+ fun x ->
+ try
+ let s = Unix.stat (p // x) in
+ s.Unix.st_kind = Unix.S_REG && s.Unix.st_perm land 0o111 > 0
+ with Unix.Unix_error _ -> false
+ ) scripts in
+ if scripts <> [] then
+ Hashtbl.add hooks hook scripts
+ ) entries;
+ hooks
+
+let load_elements ~debug paths =
+ let loaded_elements = Hashtbl.create 13 in
+ let paths = List.filter is_directory paths in
+ List.iter (
+ fun path ->
+ let listing = Array.to_list (Sys.readdir path) in
+ let listing = List.map (fun x -> (x, path // x)) listing in
+ let listing = List.filter (fun (_, x) -> is_directory x) listing in
+ List.iter (
+ fun (p, dir) ->
+ if not (Hashtbl.mem loaded_elements p) then (
+ let elem = { directory = dir; hooks = load_hooks ~debug dir } in
+ Hashtbl.add loaded_elements p elem
+ ) else if debug >= 1 then (
+ printf "element %s (in %s) already present" p path;
+ )
+ ) listing
+ ) paths;
+ loaded_elements
+
+let load_dependencies elements loaded_elements =
+ let get filename element =
+ try
+ let path = (Hashtbl.find loaded_elements element).directory in
+ let path = path // filename in
+ if Sys.file_exists path then (
+ let lines = read_whole_file path in
+ let lines = string_nsplit "\n" lines in
+ let lines = List.filter ((<>) "") lines in
+ stringset_of_list lines
+ ) else
+ StringSet.empty
+ with Not_found ->
+ error (f_"element %s not found") element in
+ let get_deps = get "element-deps" in
+ let get_provides = get "element-provides" in
+
+ let queue = Queue.create () in
+ let final = ref StringSet.empty in
+ let provided = ref StringSet.empty in
+ List.iter (fun x -> Queue.push x queue) elements;
+ final := stringset_of_list elements;
+ while not (Queue.is_empty queue) do
+ let elem = Queue.pop queue in
+ if StringSet.mem elem !provided <> true then (
+ let deps = get_deps elem in
+ provided := StringSet.union !provided (get_provides elem);
+ StringSet.iter (fun x -> Queue.push x queue)
+ (StringSet.diff deps (StringSet.union !final !provided));
+ final := StringSet.union !final deps
+ )
+ done;
+ let conflicts = StringSet.inter (stringset_of_list elements) !provided in
+ if not (StringSet.is_empty conflicts) then
+ error (f_"following elements were explicitly required but are provided by other included elements: %s")
+ (String.concat "," (StringSet.elements conflicts));
+ if not (StringSet.mem "operating-system" !provided) then
+ error (f_"please include an operating system element");
+ StringSet.diff !final !provided
+
+let elements_with_script elements loaded_elements hook scriptname =
+ StringSet.filter (
+ fun e ->
+ try
+ let s = Hashtbl.find (Hashtbl.find loaded_elements e).hooks hook in
+ List.exists ((=) scriptname) s
+ with Not_found -> false
+ ) elements
+
+let copy_element element destdir blacklist =
+ let entries = Array.to_list (Sys.readdir element.directory) in
+ let entries = List.filter ((<>) "tests") entries in
+ let entries = List.filter ((<>) "test-elements") entries in
+ let dirs, nondirs = List.partition is_directory entries in
+ let dirs = List.map (fun x -> (x, element.directory // x, destdir // x)) dirs in
+ let nondirs = List.map (fun x -> element.directory // x) nondirs in
+ let is_regular_file file =
+ try (Unix.stat file).Unix.st_kind = Unix.S_REG
+ with Unix.Unix_error _ -> false in
+ List.iter (
+ fun (e, path, destpath) ->
+ do_mkdir destpath;
+ let subentries = Array.to_list (Sys.readdir path) in
+ let subentries = List.filter (not_in_list blacklist) subentries in
+ List.iter (
+ fun sube ->
+ if is_regular_file (destpath // sube) then (
+ raise (Duplicate_script (e, sube))
+ ) else
+ do_cp (path // sube) destpath
+ ) subentries;
+ ) dirs;
+ List.iter (
+ fun path ->
+ do_cp path destdir
+ ) nondirs
+
+let copy_elements elements loaded_elements blacklist destdir =
+ do_mkdir destdir;
+ StringSet.iter (
+ fun element ->
+ try
+ copy_element (Hashtbl.find loaded_elements element) destdir blacklist
+ with
+ | Duplicate_script (hook, script) ->
+ let dups = elements_with_script elements loaded_elements hook script in
+ error (f_"There is a duplicated script in your elements:\n%s/%s in: %s")
+ hook script (String.concat " " (StringSet.elements dups))
+ ) elements
diff --git a/dib/utils.ml b/dib/utils.ml
new file mode 100644
index 0000000..def2eee
--- /dev/null
+++ b/dib/utils.ml
@@ -0,0 +1,137 @@
+(* virt-dib
+ * Copyright (C) 2015 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Common_gettext.Gettext
+open Common_utils
+
+open Printf
+
+exception Tool_not_found of string (* tool *)
+
+let quote = Filename.quote
+
+let unit_GB howmany =
+ (Int64.of_int howmany) *^ 1024_L *^ 1024_L *^ 1024_L
+
+let current_arch () =
+ (* Turn a CPU into the dpkg architecture naming. *)
+ let normalize_arch = function
+ | "amd64" | "x86_64" -> "amd64"
+ | "i386"| "i486" | "i586" | "i686" -> "i386"
+ | arch when string_prefix arch "armv" -> "armhf"
+ | arch -> arch in
+ normalize_arch Config.host_cpu
+
+let output_filename image_name = function
+ | fmt -> image_name ^ "." ^ fmt
+
+let log_filename () =
+ let tm = Unix.gmtime (Unix.time ()) in
+ sprintf "%s-%d%02d%02d-%02d%02d%02d.log"
+ prog (tm.Unix.tm_year + 1900) (tm.Unix.tm_mon + 1) tm.Unix.tm_mday
+ tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec
+
+let var_from_lines var lines =
+ let var_with_equal = var ^ "=" in
+ let var_lines = List.filter (fun x -> string_prefix x var_with_equal) lines in
+ (match var_lines with
+ | [] ->
+ error (f_"variable '%s' not found in lines:\n%s")
+ var (String.concat "\n" lines)
+ | [x] -> snd (string_split "=" x)
+ | _ ->
+ error (f_"variable '%s' has more than one occurrency in lines:\n%s")
+ var (String.concat "\n" lines)
+ )
+
+let string_index_fn fn str =
+ let len = String.length str in
+ let rec loop i =
+ if i = len then raise Not_found
+ else if fn str.[i] then i
+ else loop (i + 1) in
+ loop 0
+
+let digit_prefix_compare a b =
+ let myint str =
+ try int_of_string str
+ with _ -> 0 in
+ let mylength str =
+ match String.length str with
+ | 0 -> max_int
+ | x -> x in
+ let split_prefix str =
+ let len = String.length str in
+ let digits =
+ let isdigit = function
+ | '0'..'9' -> true
+ | _ -> false in
+ try string_index_fn (fun x -> not (isdigit x)) str
+ with Not_found -> len in
+ match digits with
+ | 0 -> "", str
+ | x when x = len -> str, ""
+ | _ -> String.sub str 0 digits, String.sub str digits (len - digits) in
+
+ let pref_a, rest_a = split_prefix a in
+ let pref_b, rest_b = split_prefix b in
+ match mylength pref_a, mylength pref_b, compare (myint pref_a) (myint pref_b) with
+ | x, y, 0 when x = y -> compare rest_a rest_b
+ | x, y, 0 -> x - y
+ | _, _, x -> x
+
+let do_mkdir dir =
+ mkdir_p dir 0o755
+
+let rec remove_dups = function
+ | [] -> []
+ | x :: xs -> x :: (remove_dups (List.filter ((<>) x) xs))
+
+let which tool =
+ (* XXX ugly but effective *)
+ let lines = external_command (sprintf "sh -c \"which %s 2>/dev/null || true\"" tool) in
+ let lines = List.filter ((<>) "") lines in
+ (match lines with
+ | [] -> raise (Tool_not_found tool)
+ | [x] -> x
+ | x :: _ ->
+ error (f_"output of `which %s` contains more than one line:\n%s")
+ tool (String.concat "\n" lines))
+
+let run_command cmd =
+ ignore (external_command cmd)
+
+let check_tool tool =
+ try ignore (which tool); true
+ with Tool_not_found _ -> false
+
+let require_tool tool =
+ try ignore (which tool)
+ with Tool_not_found tool ->
+ error (f_"%s needed but not found") tool
+
+let do_cp src dest =
+ let cmd = sprintf "cp -t %s -a %s" (Filename.quote dest) (Filename.quote src) in
+ run_command cmd
+
+let ensure_trailing_newline str =
+ if String.length str > 0 && str.[String.length str - 1] <> '\n' then str ^ "\n"
+ else str
+
+let not_in_list l e =
+ not (List.mem e l)
diff --git a/dib/virt-dib.pod b/dib/virt-dib.pod
new file mode 100644
index 0000000..aa5a1ae
--- /dev/null
+++ b/dib/virt-dib.pod
@@ -0,0 +1,591 @@
+=head1 NAME
+
+virt-dib - Run diskimage-builder elements
+
+=head1 SYNOPSIS
+
+ virt-dib -B DIB-LIB [options] elements...
+
+=head1 DESCRIPTION
+
+Virt-dib is a tool for using the elements of C<diskimage-builder>
+to build a new disk image, generate new ramdisks, etc.
+
+Virt-dib is intended as safe replacement for C<diskimage-builder>
+and its C<ramdisk-image-create> mode, see
+L</COMPARISON WITH DISKIMAGE-BUILDER> for a quick comparison with
+usage of C<diskimage-builder>.
+
+C<diskimage-builder> is part of the TripleO OpenStack project:
+L<https://wiki.openstack.org/wiki/TripleO>.
+
+=head1 EXAMPLES
+
+=head2 Build simple images of distributions
+
+ virt-dib \
+ -B /path/to/diskimage-builder/lib \
+ -p /path/to/diskimage-builder/elements \
+ --envvar DIB_RELEASE=jessie \
+ --name debian-jessie \
+ debian vm
+
+This builds a Debian Jessie (8.x) disk image, suitable for running
+as virtual machine, saved as F<debian-jessie.qcow2>.
+
+=head2 Build ramdisks
+
+ virt-dib \
+ -B /path/to/diskimage-builder/lib \
+ -p /path/to/diskimage-builder/elements \
+ --ramdisk \
+ --name ramdisk \
+ ubuntu deploy-ironic
+
+This builds a ramdisk for the Ironic OpenStack component based
+on the Ubuntu distribution.
+
+=head1 OPTIONS
+
+=over 4
+
+=item B<--help>
+
+Display help.
+
+=item B<--arch> ARCHITECURE
+
+Use the specified architecture for the output image. The default
+value is the same as the host running virt-dib.
+
+Notes: right now it does nothing more than setting the C<ARCH>
+environment variable for the elements.
+
+=item B<-B> PATH
+
+Set the path to the library directory of C<diskimage-builder>. This is
+usually the C<lib> subdirectory.
+
+This parameter is B<mandatory>, as virt-dib needs to provide it for the
+elements (as some of them might use scripts in it). Virt-dib itself
+does not make use of the library directory.
+
+=item B<--debug> LEVEL
+
+Set the debug level to C<LEVEL>, which is a non-negative integer
+number. The default is C<0>.
+
+This debug level is different than what C<-x> and C<-v> set,
+and it increases the debugging information printed out.
+Specifically, this sets the C<DIB_DEBUG_TRACE>, and any value
+E<gt> C<0> enables tracing in the scripts executed.
+
+=item B<--drive> DISK
+
+Add the specified disk to be used as helper drive where to cache
+files of the elements, like disk images, distribution packages, etc.
+
+See L</HELPER DRIVE>.
+
+=item B<-p> PATH
+
+=item B<--element-path> PATH
+
+Add a new path with elements. Paths are used in the same order as the
+C<-p> parameters appear, so a path specified first is looked first,
+and so on.
+
+Obviously, it is recommended to add the path to the own elements of
+C<diskimage-builder>, as most of the other elements will rely on them.
+
+=item B<--extra-packages> PACKAGE,...
+
+Install additional packages in the image being built.
+
+This relies on the C<install-packages> binary provided by the
+package management elements.
+
+This option can be specified multiple times, each time with multiple
+packages separated by comma.
+
+=item B<--envvar> VARIABLE
+
+=item B<--envvar> VARIABLE=VALUE
+
+Carry or set an environment variable for the elements.
+
+See L</ENVIRONMENT VARIABLES> below for more information on the
+interaction and usage of environment variables.
+
+This option can be used in two ways:
+
+=over 4
+
+=item B<--envvar> VARIABLE
+
+Carry the environment variable C<VARIABLE>. If it is not set, nothing
+is exported to the elements.
+
+=item B<--envvar> VARIABLE=VALUE
+
+Set the environment variable C<VARIABLE> with value C<VALUE> for the
+elements, regardless whether an environment variable with the same
+name exists.
+
+This can be useful to pass environment variable without exporting
+them in the environment where virt-dib runs.
+
+=back
+
+=item B<--exclude-element> ELEMENT
+
+Ignore the specified element.
+
+=item B<--exclude-script> SCRIPT
+
+Ignore any element script named C<SCRIPT>, whichever element it is in.
+
+This can be useful in case some script does not run well with virt-dib,
+for example when they really need C<diskimage-builder>'s environment.
+
+=item B<--formats> FORMAT,...
+
+Set the list of output formats, separating them with comma.
+
+Supported formats are:
+
+=over 4
+
+=item C<qcow2> (enabled by default)
+
+QEMU's qcow2.
+
+=item C<raw>
+
+Raw disk format.
+
+=item C<tar>
+
+An uncompressed tarball.
+
+=item C<vhd>
+
+C<Virtual Hard Disk> disk image. This output format requires
+the C<vhd-util> tool.
+
+Please note that the version of C<vhd-util> tool needs to be patched
+to support the C<convert> subcommand, and to be bootable.
+The patch is available here:
+L<https://github.com/emonty/vhd-util/blob/master/debian/patches/citrix>.
+
+=back
+
+=item B<--fs-type> FILESYSTEM
+
+Set the filesystem type to use for the root filesystem. The default
+is C<ext4>.
+
+See also L<guestfs(3)/guestfs_filesystem_available>.
+
+=item B<--machine-readable>
+
+This option is used to make the output more machine friendly
+when being parsed by other programs. See
+L</MACHINE READABLE OUTPUT> below.
+
+=item B<-m> MB
+
+=item B<--memsize> MB
+
+Change the amount of memory allocated to the appliance. Increase
+this if you find that the virt-dib execution runs out of memory.
+
+The default can be found with this command:
+
+ guestfish get-memsize
+
+=item B<--network>
+
+=item B<--no-network>
+
+Enable or disable network access from the guest during the installation.
+
+Enabled is the default. Use I<--no-network> to disable access.
+
+The network only allows outgoing connections and has other minor
+limitations. See L<virt-rescue(1)/NETWORK>.
+
+This does not affect whether the guest can access the network once it
+has been booted, because that is controlled by your hypervisor or
+cloud environment and has nothing to do with virt-dib.
+
+If you use I<--no-network>, then the environment variable
+C<DIB_OFFLINE> is set to C<1>, signaling the elements that they
+should use only cached resources when available. Note also that,
+unlike with C<diskimage-builder> where elements may still be able
+to access to the network even with C<DIB_OFFLINE=>, under virt-dib
+network will be fully unaccessible.
+
+=item B<--image-cache> DIRECTORY
+
+Set the path in the host where cache the resources used by the elements
+of the C<extra-data.d> phase. The default is F<~/.cache/image-create>.
+
+Please note that most of the resources fetched after C<extra-data>
+will be cached in the helper drive specified with I<--drive>;
+see also L</HELPER DRIVE>.
+
+=item B<--install-type> TYPE
+
+Specify the default installation type. Defaults to C<source>.
+
+Set to C<package> to use package based installations by default.
+
+=item B<--mkfs-options> C<OPTION STRING>
+
+Add the specified options to L<mkfs(1)>, to be able to fine-tune
+the root filesystem creation. Note that this is not possible
+to override the filesystem type.
+
+You should use I<--mkfs-options> at most once. To pass multiple
+options, separate them with space, eg:
+
+ virt-dib ... --mkfs-options '-O someopt -I foo'
+
+=item B<-n>
+
+Skip the inclusion of the C<base> element.
+
+=item B<--name> NAME
+
+Set the name of the output image file. The default is C<image>.
+
+According to the chosen name, there will be the following in the
+current directory:
+
+=over 4
+
+=item F<$NAME.ext>
+
+For each output format, a disk image named after the outout image
+with the extension depending on the format; for example:
+F<$NAME.qcow2>, F<$NAME.raw>, F<$NAME.tar>.
+
+Not applicable in ramdisk mode, see L</RAMDISK BUILDING>.
+
+=item F<$NAME.d>
+
+A directory containing any files created by the elements, for example
+F<dib-manifests> directory (created by the C<manifests> element),
+ramdisks and kernels in ramdisk mode, and so on.
+
+=back
+
+=item B<--no-delete-on-failure>
+
+Don't delete the output files on failure to build. You can use this to
+debug failures to run scripts.
+
+The default is to delete the output file if virt-dib fails (or,
+for example, some script that it runs fails).
+
+=item B<--qemu-img-options> option[,option,...]
+
+Pass I<--qemu-img-options> option(s) to the L<qemu-img(1)> command
+to fine-tune the output format. Options available depend on
+the output format (see I<--formats>) and the installed version
+of the qemu-img program.
+
+You should use I<--qemu-img-options> at most once. To pass multiple
+options, separate them with commas, eg:
+
+ virt-dib ... --qemu-img-options cluster_size=512,preallocation=metadata ...
+
+=item B<--ramdisk>
+
+Set the ramdisk building mode.
+
+See L</RAMDISK BUILDING>.
+
+=item B<--ramdisk-element> NAME
+
+Set the name for the additional element added in ramdisk building mode.
+The default is C<ramdisk>.
+
+See L</RAMDISK BUILDING>.
+
+=item B<--root-label> LABEL
+
+Set the label for the root filesystem in the created image.
+
+Please note that some filesystems have different restrictions on
+the length of their labels; for example, on C<ext2/3/4> filesystems
+labels cannot be longer than 16 characters, while on C<xfs> they have
+at most 12 characters.
+
+The default depends on the actual filesystem for the root partition
+(see I<--fs-type>): on C<xfs> is C<img-rootfs>, while
+C<cloudimg-rootfs> on any other filesystem.
+
+=item B<--size> SIZE
+
+Select the size of the output disk, where the size can be specified
+using common names such as C<32G> (32 gigabytes) etc.
+The default size is C<5G>.
+
+To specify size in bytes, the number must be followed by the lowercase
+letter I<b>, eg: S<C<--size 10737418240b>>.
+
+See also L<virt-resize(1)> for resizing partitions of an existing
+disk image.
+
+=item B<--smp> N
+
+Enable N E<ge> 2 virtual CPUs for scripts to use.
+
+=item B<-u>
+
+Do not compress resulting qcow2 images. The default is
+to compressed them.
+
+=item B<-v>
+
+=item B<--verbose>
+
+Enable debugging messages.
+
+=item B<-V>
+
+=item B<--version>
+
+Display version number and exit.
+
+=item B<-x>
+
+Enable tracing of libguestfs API calls.
+
+=back
+
+=head1 ENVIRONMENT VARIABLES
+
+Unlike with C<diskimage-builder>, the environment of the host is
+B<not> inherited in the appliance when running most of the elements
+(i.e. all the ones different than C<extra-data.d>).
+
+To set environment for the elements being run, it is necessary to tell
+virt-dib to use them, with the option I<--envvar>. Such option
+allows to selectively export environment variables when running the
+elements, and it is the preferred way to pass environment variables
+to the elements.
+
+To recap: if you want the environment variable C<MYVAR>
+(and its content) to be available to the elements, you can do either
+
+ export MYVAR # whichever is its value
+ virt-dib ... --envvar MYVAR ...
+
+or
+
+ virt-dib ... --envvar MYVAR=value_of_it ...
+
+=head1 HELPER DRIVE
+
+Virt-dib runs most of the element in its own appliance, and thus not
+on the host. Because of this, there is no possibility for elements
+to cache resources directly on the host.
+
+To solve this issue, virt-dib allows the usage of an helper drive
+where to store cached resources, like disk images,
+distribution packages, etc. While this means that there is a smaller
+space available for caching, at least it allows to limit the space
+on the host for caches, without assuming that elements will do that
+by themselves.
+
+Currently this disk is either required to have a single partition
+on it, or the first partition on it will be used. A disk with
+the latter configuration can be easily created with L<guestfish(1)>
+like the following:
+
+ guestfish -N filename.img=fs:ext4:10G
+
+The above will create a disk image called F<filename.img>, 10G big,
+with a single partition of type ext4;
+see L<guestfish(1)/PREPARED DISK IMAGES>.
+
+It is recommended for it to be E<ge> 5G or even more, as elements
+will cache disk images, distribution packages, etc.
+
+The disk is also used as C<$HOME> during the elements run.
+Virt-dib stores in it the logs of the scripts executed in the appliance.
+
+The drive can be accessed like any other disk image, for example using
+other tools of libguestfs such as L<guestfish(1)>:
+
+ guestfish -a filename.img -m /dev/sda1
+
+If no helper drive is specified with I<--drive>, all the resources
+cached during a virt-dib run will be discarded.
+
+=head1 RAMDISK BUILDING
+
+Virt-dib can emulate also C<ramdisk-image-create>, which is a secondary
+operation mode of C<diskimage-builder>. Instead of being a different
+tool name, virt-dib provides easy access to this mode using the
+I<--ramdisk> switch.
+
+In this mode:
+
+=over 4
+
+=item
+
+there is an additional ramdisk element added (see I<--ramdisk-element>)
+
+=item
+
+no image is produced (so I<--formats> is ignored)
+
+=item
+
+F<$NAME.d> (see I<--name>) will contain initrd, kernel, etc
+
+=back
+
+=head1 TEMPORARY DIRECTORY
+
+Virt-dib uses the standard temporary directory used by libguestfs,
+see L<guestfs(3)/ENVIRONMENT VARIABLES>.
+
+By default this location is F</tmp> (default value for C<$TMPDIR>),
+which on some systems may be on a tmpfs filesystem, and thus
+defaulting to a maximum size of I<half> of physical RAM.
+If virt-dib exceeds this, it may hang or exit early with an error.
+The solution is to point C<$TMPDIR> to a permanent location used
+as temporary location, for example:
+
+ $ mkdir local-tmp
+ $ env TMPDIR=$PWD/local-tmp virt-dib ...
+ $ rm -rf local-tmp
+
+=head1 MACHINE READABLE OUTPUT
+
+The I<--machine-readable> option can be used to make the output more
+machine friendly, which is useful when calling virt-dib from other
+programs, GUIs etc.
+
+Use the option on its own to query the capabilities of the
+virt-dib binary. Typical output looks like this:
+
+ $ virt-dib --machine-readable
+ virt-dib
+ output:qcow2
+ output:tar
+ output:raw
+ output:vhd
+
+A list of features is printed, one per line, and the program exits
+with status 0.
+
+=head1 COMPARISON WITH DISKIMAGE-BUILDER
+
+Virt-dib is intended as safe replacement for C<diskimage-builder>
+and its C<ramdisk-image-create> mode; the user-notable differences
+consist in:
+
+=over 4
+
+=item
+
+the command line arguments; some of the arguments are the same as
+available in C<diskimage-builder>, while some have different names
+
+=item
+
+the location of non-image output files (like ramdisks and kernels)
+
+=item
+
+the way cached resources are saved: using an helper drive, not directly
+on the disk where virt-dib is run
+
+=item
+
+the need to specify a target size for the output disk, as opposed
+to C<diskimage-builder> calculating an optimal one
+
+=item
+
+the handling of environment variables, see L</ENVIRONMENT VARIABLES>.
+
+Furthermore, other than the libguestfs own environment variables
+(see L<guestfs(3)/ENVIRONMENT VARIABLES>), virt-dib does not read
+any other environment variable: this means that all the options
+and behaviour changes are specified solely using command line
+arguments
+
+=item
+
+C<extra-data.d> scripts run before in the host environment, before
+all the other ones (even C<root.d>); this means that, depending
+on the configuration for the elements, some of them may fail due to
+missing content (usually directories) in C<$TMP_HOOKS_PATH>.
+
+Workarounds for this may be either:
+
+=over 4
+
+=item
+
+fix the C<extra-data.d> scripts to create the missing directories
+
+=item
+
+create (and use) a simple element with a C<extra-data.d> script
+named e.g. F<00-create-missing-dirs> to create the missing
+directories
+
+=back
+
+=back
+
+Elements themselves should notice no difference in they way
+they are run; behaviour differences may due to wrong assumptions in
+elements, or not correct virt-dib emulation.
+
+Known issues at the moment:
+
+=over 4
+
+=item
+
+(none)
+
+=back
+
+=head1 TESTING
+
+Virt-dib has been tested with C<diskimage-builder> (and its elements)
+E<ge> 0.1.43; from time to time also with C<tripleo-image-elements>
+and C<sahara-image-elements>.
+
+Previous versions may work, but it is not guaranteed.
+
+=head1 EXIT STATUS
+
+This program returns 0 if successful, or non-zero if there was an
+error.
+
+=head1 SEE ALSO
+
+L<guestfs(3)>,
+L<guestfish(1)>,
+L<virt-resize(1)>,
+L<http://libguestfs.org/>.
+
+=head1 AUTHOR
+
+Pino Toscano (C<ptoscano at redhat dot com>)
+
+=head1 COPYRIGHT
+
+Copyright (C) 2015 Red Hat Inc.
diff --git a/po-docs/podfiles b/po-docs/podfiles
index c76f1b1..26bd880 100644
--- a/po-docs/podfiles
+++ b/po-docs/podfiles
@@ -11,6 +11,7 @@
../customize/virt-customize.pod
../daemon/guestfsd.pod
../df/virt-df.pod
+../dib/virt-dib.pod
../diff/virt-diff.pod
../edit/virt-edit.pod
../erlang/examples/guestfs-erlang.pod
diff --git a/run.in b/run.in
index 8fdf454..5d30bf4 100755
--- a/run.in
+++ b/run.in
@@ -102,6 +102,7 @@ prepend PATH "$b/sysprep"
prepend PATH "$b/test-tool"
prepend PATH "$b/tools"
prepend PATH "$b/v2v"
+prepend PATH "$b/dib"
export PATH
# Set LD_LIBRARY_PATH and DYLD_LIBRARY_PATH to contain library.
--
2.1.0
9 years, 5 months