From f0fa1795c4fe1b3c2f0d9022f9ebf47a879c0970 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 9 Jul 2014 18:13:51 +0200 Subject: [PATCH] installer: Remove mountsource.sh A small library for hardware-related things has been created and uses libudev for listing all available devices. --- config/rootfiles/installer/udev | 1 + lfs/installer | 4 +- src/install+setup/install/Makefile | 6 +- src/install+setup/install/downloadsource.sh | 9 +- src/install+setup/install/hw.c | 117 ++++++++++++++++++++ src/install+setup/install/hw.h | 41 +++++++ src/install+setup/install/main.c | 45 ++++++-- src/install+setup/install/mountsource.sh | 74 ------------- 8 files changed, 199 insertions(+), 98 deletions(-) create mode 120000 config/rootfiles/installer/udev create mode 100644 src/install+setup/install/hw.c create mode 100644 src/install+setup/install/hw.h delete mode 100644 src/install+setup/install/mountsource.sh diff --git a/config/rootfiles/installer/udev b/config/rootfiles/installer/udev new file mode 120000 index 0000000000..d96e0b7081 --- /dev/null +++ b/config/rootfiles/installer/udev @@ -0,0 +1 @@ +../common/udev \ No newline at end of file diff --git a/lfs/installer b/lfs/installer index c02e0bac5c..29095eb69e 100644 --- a/lfs/installer +++ b/lfs/installer @@ -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) diff --git a/src/install+setup/install/Makefile b/src/install+setup/install/Makefile index 89106de228..c8aa01e5c7 100644 --- a/src/install+setup/install/Makefile +++ b/src/install+setup/install/Makefile @@ -20,12 +20,12 @@ 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) diff --git a/src/install+setup/install/downloadsource.sh b/src/install+setup/install/downloadsource.sh index 5c6360dca1..1f6c432dd8 100644 --- a/src/install+setup/install/downloadsource.sh +++ b/src/install+setup/install/downloadsource.sh @@ -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 index 0000000000..eb52f889d6 --- /dev/null +++ b/src/install+setup/install/hw.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include +#include +#include +#include +#include + +#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 index 0000000000..3ef0d18bfe --- /dev/null +++ b/src/install+setup/install/hw.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef HEADER_HW_H +#define HEADER_HW_H + +#include + +#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 */ diff --git a/src/install+setup/install/main.c b/src/install+setup/install/main.c index 309e006c9d..b053978f01 100644 --- a/src/install+setup/install/main.c +++ b/src/install+setup/install/main.c @@ -9,8 +9,16 @@ * */ -#include "install.h" #define _GNU_SOURCE + +#include +#include +#include +#include +#include + +#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 index c3350209c2..0000000000 --- a/src/install+setup/install/mountsource.sh +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/sh -############################################################################### -# # -# IPFire.org - A linux based firewall # -# Copyright (C) 2007-2014 IPFire 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 . # -# # -############################################################################### - -#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 -- 2.39.2