]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
installer: Remove mountsource.sh
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Jul 2014 16:13:51 +0000 (18:13 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Jul 2014 16:13:51 +0000 (18:13 +0200)
A small library for hardware-related things has been
created and uses libudev for listing all available devices.

config/rootfiles/installer/udev [new symlink]
lfs/installer
src/install+setup/install/Makefile
src/install+setup/install/downloadsource.sh
src/install+setup/install/hw.c [new file with mode: 0644]
src/install+setup/install/hw.h [new file with mode: 0644]
src/install+setup/install/main.c
src/install+setup/install/mountsource.sh [deleted file]

diff --git a/config/rootfiles/installer/udev b/config/rootfiles/installer/udev
new file mode 120000 (symlink)
index 0000000..d96e0b7
--- /dev/null
@@ -0,0 +1 @@
+../common/udev
\ No newline at end of file
index c02e0bac5c6f374212048e017b255f72e9eb3a1d..29095eb69e242565f094f16fe2bcc4b1b0784cbb 100644 (file)
@@ -61,12 +61,10 @@ $(TARGET) :
            -DNAME='\"$(NAME)\"' -DSNAME='\"$(SNAME)\"' -DVERSION='\"$(VERSION)\"' \
             -DSLOGAN='\"$(SLOGAN)\"' -DCONFIG_ROOT='\"$(CONFIG_ROOT)\"' -DKERNEL_VERSION='\"$(KVER)\"'"
        cd $(DIR_APP)/install && install -v -m 0755 install probenic.sh \
-               downloadsource.sh mountsource.sh mountdest.sh /install/initrd/bin
+               downloadsource.sh mountdest.sh /install/initrd/bin
        #Patch ISO Name for download ...
        sed -i -e "s|ipfire.iso|download.ipfire.org/releases/ipfire-2.x/$(VERSION)-core$(CORE)/$(SNAME)-$(VERSION).$(MACHINE)-full-core$(CORE).iso|g" \
                                /install/initrd/bin/downloadsource.sh
        #Patch version for mediacheck ...
-       sed -i -e "s|FullIPFireVersion|$(SNAME)-$(VERSION)-core$(CORE)|g" \
-                               /install/initrd/bin/mountsource.sh
        @rm -rf $(DIR_APP)
        @$(POSTBUILD)
index 89106de2280311a9e462bd2e592007963d6e8684..c8aa01e5c74feabd59682a34414c3cd1b32e20a0 100644 (file)
 
 
 CC      = gcc 
-CFLAGS  = -Os -Wall
+CFLAGS  = -O2 -Wall
 INCLUDE =
 
 LD      = gcc
 LDFLAGS =
-LIBS    = -lnewt -lslang -lpci
+LIBS    = -lnewt -lslang -lpci -ludev
 
 COMPILE = $(CC) -c $(INCLUDE) $(CFLAGS)
 
@@ -40,7 +40,7 @@ clean :
 
 ######
 
-OBJS=main.o config.o ../libsmooth/libsmooth.o unattended.o
+OBJS=main.o config.o ../libsmooth/libsmooth.o unattended.o hw.o
 
 install: $(OBJS)
        $(LINK) $(OBJS) -o $@ $(LIBS)
index 5c6360dca16bfc540354bc70dd1818bc96593968..1f6c432dd826b72764a5be9f39633a5736e58906 100644 (file)
@@ -47,13 +47,8 @@ echo "Checking download..."
 md5_file=`md5sum /tmp/download.iso | cut -d" " -f1`
 md5_down=`cat /tmp/download.iso.md5 | cut -d" " -f1`
 if [ "$md5_file" == "$md5_down" ]; then
-       mount /tmp/download.iso -o loop /cdrom 2> /dev/null
-       if [ -n "$(ls /cdrom/ipfire-*.tlz 2>/dev/null)" ]; then
-               echo -n "null" > /tmp/source_device
-               echo "Found tarball in /tmp/download.iso"
-               exit 0
-       fi
-       umount /cdrom 2> /dev/null
+       echo -n "/tmp/download.iso" > /tmp/source_device
+       exit 0
 fi
 echo "Error - SKIP"
 exit 10
diff --git a/src/install+setup/install/hw.c b/src/install+setup/install/hw.c
new file mode 100644 (file)
index 0000000..eb52f88
--- /dev/null
@@ -0,0 +1,117 @@
+/*#############################################################################
+#                                                                             #
+# IPFire - An Open Source Firewall Distribution                               #
+# Copyright (C) 2014 IPFire development team                                  #
+#                                                                             #
+# 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 3 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, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <assert.h>
+#include <libudev.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mount.h>
+#include <unistd.h>
+
+#include "hw.h"
+
+struct hw* hw_init() {
+       struct hw* hw = malloc(sizeof(*hw));
+       assert(hw);
+
+       // Initialize libudev
+       hw->udev = udev_new();
+       if (!hw->udev) {
+               fprintf(stderr, "Could not create udev instance\n");
+               exit(1);
+       }
+
+       return hw;
+}
+
+void hw_free(struct hw* hw) {
+       if (hw->udev)
+               udev_unref(hw->udev);
+
+       free(hw);
+}
+
+static int strstartswith(const char* a, const char* b) {
+       return (strncmp(a, b, strlen(b)) == 0);
+}
+
+int hw_mount(const char* source, const char* target, int flags) {
+       return mount(source, target, "iso9660", flags, NULL);
+}
+
+int hw_umount(const char* target) {
+       return umount2(target, MNT_DETACH);
+}
+
+static int hw_test_source_medium(const char* path) {
+       int ret = hw_mount(path, SOURCE_MOUNT_PATH, MS_RDONLY);
+
+       // If the source could not be mounted we
+       // cannot proceed.
+       if (ret)
+               return ret;
+
+       // Check if the test file exists.
+       ret = access(SOURCE_TEST_FILE, F_OK);
+
+       // Umount the test device.
+       hw_umount(SOURCE_MOUNT_PATH);
+
+       return ret;
+}
+
+char* hw_find_source_medium(struct hw* hw) {
+       char* ret = NULL;
+
+       struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev);
+
+       udev_enumerate_add_match_subsystem(enumerate, "block");
+       udev_enumerate_scan_devices(enumerate);
+
+       struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
+
+       struct udev_list_entry* dev_list_entry;
+       udev_list_entry_foreach(dev_list_entry, devices) {
+               const char* path = udev_list_entry_get_name(dev_list_entry);
+               struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path);
+
+               const char* dev_path = udev_device_get_devnode(dev);
+
+               // Skip everything what we cannot work with
+               if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") ||
+                               strstartswith(dev_path, "/dev/ram"))
+                       continue;
+
+               if (hw_test_source_medium(dev_path)) {
+                       ret = strdup(dev_path);
+               }
+
+               udev_device_unref(dev);
+
+               // If a suitable device was found the search will end.
+               if (ret)
+                       break;
+       }
+
+       udev_enumerate_unref(enumerate);
+
+       return ret;
+}
diff --git a/src/install+setup/install/hw.h b/src/install+setup/install/hw.h
new file mode 100644 (file)
index 0000000..3ef0d18
--- /dev/null
@@ -0,0 +1,41 @@
+/*#############################################################################
+#                                                                             #
+# IPFire - An Open Source Firewall Distribution                               #
+# Copyright (C) 2014 IPFire development team                                  #
+#                                                                             #
+# 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 3 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, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef HEADER_HW_H
+#define HEADER_HW_H
+
+#include <libudev.h>
+
+#define SOURCE_MOUNT_PATH "/cdrom"
+#define SOURCE_TEST_FILE  SOURCE_MOUNT_PATH "/" VERSION ".media"
+
+struct hw {
+       struct udev *udev;
+};
+
+struct hw* hw_init();
+void hw_free(struct hw* hw);
+
+int hw_mount(const char* source, const char* target, int flags);
+int hw_umount(const char* target);
+
+char* hw_find_source_medium(struct hw* hw);
+
+#endif /* HEADER_HW_H */
index 309e006c9d3927fd6ed0792ade6e911ae841159e..b053978f01a181ec6ef2ce9920670fcdcd7b51b3 100644 (file)
@@ -9,8 +9,16 @@
  * 
  */
 
-#include "install.h"
 #define _GNU_SOURCE
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mount.h>
+
+#include "hw.h"
+#include "install.h"
  
 #define INST_FILECOUNT 21000
 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
@@ -40,8 +48,8 @@ extern char *pl_tr[];
 extern char *ru_tr[];
 extern char *tr_tr[];
 
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
+       struct hw* hw = hw_init();
 
        char discl_msg[40000] = "Disclaimer\n";
 
@@ -49,7 +57,8 @@ int main(int argc, char *argv[])
        char *shortlangnames[] = { "de", "en", "fr", "es", "nl", "pl", "ru", "tr", NULL };
        char **langtrs[] = { de_tr, en_tr, fr_tr, es_tr, nl_tr, pl_tr, ru_tr, tr_tr, NULL };
        char hdletter;
-       char harddrive[30], sourcedrive[5];     /* Device holder. */
+       char harddrive[30];     /* Device holder. */
+       char* sourcedrive = NULL;
        char harddrive_info[STRING_SIZE];       /* Additional infos about target */
        struct devparams hdparams, cdromparams; /* Params for CDROM and HD */
        int rc = 0;
@@ -70,7 +79,7 @@ int main(int argc, char *argv[])
        FILE *handle, *cmdfile, *copying;
        char line[STRING_SIZE];
        char string[STRING_SIZE];
-       long memory = 0, disk = 0, free;
+       long memory = 0, disk = 0;
        long system_partition, boot_partition, root_partition, swap_file;
        int scsi_disk = 0;
        char *yesnoharddisk[3]; //      char *yesnoharddisk = { "NO", "YES", NULL };
@@ -150,20 +159,31 @@ int main(int argc, char *argv[])
                newtWinMessage(title, ctr[TR_OK], message);
        }
 
-       mysystem("/bin/mountsource.sh");
+       /* Search for a source drive that holds the right
+        * version of the image we are going to install. */
+       sourcedrive = hw_find_source_medium(hw);
 
-       if ((handle = fopen("/tmp/source_device", "r")) == NULL) {
+       fprintf(flog, "Source drive: %s\n", sourcedrive);
+       if (!sourcedrive) {
                newtWinMessage(title, ctr[TR_OK], ctr[TR_NO_LOCAL_SOURCE]);
-               runcommandwithstatus("/bin/downloadsource.sh",ctr[TR_DOWNLOADING_ISO]);
+               runcommandwithstatus("/bin/downloadsource.sh", ctr[TR_DOWNLOADING_ISO]);
                if ((handle = fopen("/tmp/source_device", "r")) == NULL) {
                        errorbox(ctr[TR_DOWNLOAD_ERROR]);
                        goto EXIT;
                }
+
+               fgets(sourcedrive, 5, handle);
+               fclose(handle);
        }
 
-       fgets(sourcedrive, 5, handle);
-       fprintf(flog, "Source drive: %s\n", sourcedrive);
-       fclose(handle);
+       assert(sourcedrive);
+
+       int r = hw_mount(sourcedrive, SOURCE_MOUNT_PATH, MS_RDONLY);
+       if (r) {
+               fprintf(flog, "Could not mount %s to %s\n", sourcedrive, SOURCE_MOUNT_PATH);
+               fprintf(flog, strerror(errno));
+               exit(1);
+       }
 
        if (!unattended) {
                // Read the license file.
@@ -629,5 +649,8 @@ EXIT:
        if (!(allok))
                system("/etc/halt");
 
+       // Free resources
+       free(sourcedrive);
+
        return 0;
 }
diff --git a/src/install+setup/install/mountsource.sh b/src/install+setup/install/mountsource.sh
deleted file mode 100644 (file)
index c335020..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-#!/bin/sh
-###############################################################################
-#                                                                             #
-# IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2007-2014  IPFire Team  <info@ipfire.org>                     #
-#                                                                             #
-# 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 3 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, see <http://www.gnu.org/licenses/>.       #
-#                                                                             #
-###############################################################################
-
-#lfs patch source here...
-version=FullIPFireVersion
-#
-
-echo "Scanning source media"
-
-# scan all Block devices
-for DEVICE in `find /sys/block/* -maxdepth 0 ! -name fd* ! -name loop* ! -name ram* -exec basename {} \;`
-do
-               mount /dev/${DEVICE} /cdrom 2> /dev/null
-               if [ -n "$(ls /cdrom/${version}.media 2>/dev/null)" ]; then
-                       echo -n ${DEVICE} > /tmp/source_device
-                       echo "Found ${version} on ${DEVICE}"
-                       exit 0
-               else
-                       echo "not found on ${DEVICE} - SKIP"
-               fi
-               umount /cdrom 2> /dev/null
-done
-
-# scan all Partitions on block devices
-for DEVICE in `find /sys/block/* -maxdepth 0 ! -name fd* ! -name loop* ! -name ram* -exec basename {} \;`
-do
-       for DEVICEP in $(ls /dev/${DEVICE}? 2>/dev/null | sed "s/\/dev\///");do
-               mount /dev/${DEVICEP} /cdrom 2> /dev/null
-               if [ -n "$(ls /cdrom/${version}.media 2>/dev/null)" ]; then
-                       echo -n ${DEVICEP} > /tmp/source_device
-                       echo "Found ${version} on ${DEVICEP}"
-                       exit 0
-               else
-                       echo "not found on ${DEVICEP} - SKIP"
-               fi
-               umount /cdrom 2> /dev/null
-       done
-done
-
-# scan all Partitions on raid/mmc devices
-for DEVICE in `find /sys/block/* -maxdepth 0 ! -name fd* ! -name loop* ! -name ram* -exec basename {} \;`
-do
-       for DEVICEP in $(ls /dev/${DEVICE}p? 2>/dev/null | sed "s/\/dev\///");do
-               mount /dev/${DEVICEP} /cdrom 2> /dev/null
-               if [ -n "$(ls /cdrom/${version}.media 2>/dev/null)" ]; then
-                       echo -n ${DEVICEP} > /tmp/source_device
-                       echo "Found ${version} on ${DEVICEP}"
-                       exit 0
-               else
-                       echo "not found on ${DEVICEP} - SKIP"
-               fi
-               umount /cdrom 2> /dev/null
-       done
-done
-
-exit 10