From: Kevin Day Date: Sat, 23 May 2026 13:41:38 +0000 (-0500) Subject: Update: Import initrd_init sources. X-Git-Tag: 0.4.1 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=1861cf31d496cfb8b51919c2e3044e6667405e96;p=initrd_init Update: Import initrd_init sources. Imported as-is from the source code tar ball. --- diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..fc1dd77 --- /dev/null +++ b/ChangeLog @@ -0,0 +1,203 @@ +Version 0.4.1 +- Use syscall to call pivot_root() to avoid "warning: implicit declaration of function 'pivot_root'". +- Replace /sbin/ paths with /bin/ paths. +- Add additional mount flags to "floating_root" tmpfs options. +- Move "floating_root" tmpfs options into a define. +- Improve error logging in some cases and adding color highlight to the error. +- Remove dead code involing the init string and init program. +- Support ARCHITECTURE_BITS (and ARCHITECTURE_BITS_PATH) environment variables to designate architecture specific paths in the target system. +- Improve detection of pre_init and init paths and use accordingly (prioritize pre_init and if not found then attempt init). +- Fix the copyright license (should be lgpl-2.1-or-later, which is what the FLL library License is). +- Add extern "C" macros to allow for c++ compiling. +- Attempt arch-specific and simple on system umount call fallback. +- Do lazy unmounting. +- Perform system umount call failsafe if umount fails for "/tmp" (just like how it is done for "/proc"). +- Perform flush after printing while handling error and exiting. + +Version 0.4.0 +- use REG_OK instead of REG_NOERROR +- the kernel has an rdinit= for the ramdisk init program and it seems root=/dev/ram0 is no longer necesasry. + - Replace finalroot= and finalinit= with just root= and init=. + +Version 0.3.0 +- add -s to grep statements in shell scripts to silence error messages. +- add a 1 second sleep after the failed to mount warning for dynamic files. +- add additional warning to do_dynamic_files(). +- give cdrom searches more time and chances to spin up so that the cdrom can be read during the detection process. +- rewrite settings init to: + - perform mounts if not doing a live init. + - add support for sharedevice=, packagedevice=, perldevice=, and pythondevice=. + - process the following possible device mounts rootfs home etc share var tmp package perl python documentation. + - use (device).encrypted instead of (device).device for identifying encrypted partitions. + - use (device).device to identify the devices manual mount path. + - add support for settings.lock inside of the settings directory to force ignoring command line (device)device= options. + - check for settings directory at: ${device_directory}boot/settings/, ${device_directory}settings/, or ${device_directory}. +- add mount_init.sh as a quick and dirty way to mount devices defined in the settings directory for non-live inits. +- mount settings directory and /settings/ instead of /boot/ and leave it mounted so that it can be used later, such as during a live init or a mount init. +- remove stale cdrom and mmc search code. +- remove find_device.sh, blkid has long since added the necessary functionality that this script used to guess. +- remove cdromsearch= and mmcsearch= boot options. +- made the naming conventions of the floating root path and the root paths a little less confusing. +- add /.settings/ directory as a valid settings path directory. +- unmount the system directory in squash boot mode because it is not required. +- fix raid array support which had stopped working at some point due to code changes. +- use tag support from blkid instead of directly calling UUID and LABEL support so that more tags (such as UUID_SUB, which is useful for raid devices) can be supported. + +Version 0.2.1 +- add package directory support +- cleanup firmware and kernel modules on error during live_init.sh +- warn if the firmware or kernel modules are missing during live_init.sh +- always float the squish and squash rootfs and remove the floating_root parameter +- fix bug where I used squishpath= and should have used squashpath= because squish and squash should both use squashpath= +- ensure that filesystemlimit starts with a number to reduce chances of injecting custom mount options + +Version 0.2.0 +- now using the new group naming convention created in turtle kevux 0.9.3 (at this time, group names are not dynamically detected) +- ensure that the appropriate binaries exists before attempting a chroot +- ensure that when any of the following (homedevice=, etcdevice=, vardevice=, or tmpdevice=) are passed to the kernel command line that symbolic links are not made for these paths during live or subroot boots +- removed /var/tmp usage from the pre_init file +- correct simple mistake in raid boot device detection that prevented raid devices from being auto-created during boot + +Version 0.1.18 +- ensure that the boot directory has world execute bit set during live init +- add software raid array support +- kernel modules now use the suffix .modules instead of .sfs +- allow do_unsquash bash function to support dynamic file extensions so that .modules and .sfs can both be used +- check to see if $init_string (/sbin/pre_init) exists before doing a pivot_root and drop to bash if it does not +- make sure that the tmp directory exists within the pivot_root target +- make quoted information inside of errors stand out + +Version 0.1.17 +- fix group permission on rootfs for squashboot to be set to root instead of boot +- correct spelling mistake from fileystem to filesystem +- removal of toolchain and documentation directories on live extract failure are now properly removed in squishboot +- remove duplicate toolchain removal on live extract failure +- squishboot=memory was not blacklisting /share during the symlink process +- remove improper use of ${the_dir} +- ensure that subroot works when using squishboot modes +- if dev/console is missing after chroot, attempt to drop to a bash shell to prevent a kernel crash +- correct dev group permission to be dev and not udev + +Version 0.1.16 +- make sure to check both the boot/settings path and the settings path for the settings files +- add subroot support +- add initial floating root support (required by subroot when not using the squash or squash boot methods) +- correct a the parameters for the handle_error print command for the initrd_init program + +Version 0.1.15 +- make sure the squash and squish root directory is mounted with world executable bit +- add installation mode support +- add support for kiwi installation mode +- only set install_files path if actually in install mode + +Version 0.1.14 +- add support for encrypted booting with LABEL and UUID +- add documentation=yes boot parameter + +Version 0.1.13 +- change .squash file extensions to the now known squash standard of .sfs + +Version 0.1.12 +- added support for /modules and /firmware directory handling +- removed use_xorg_driver as it is no longer needed +- removed _without_xorg_ and _with_xorg_ as the processing code was moved to a qingy patch +- added _squish_into_memory_ support +- changed default squash_path and squish_paths from live/ to boot/live/ +- moved squish_init into squash_init script, removing the squish_init script +- subroot has been temporarily removed +- added tmpdevice= support +- switched /live/ to /this/ to make it more obvious that that directory contains the files of the hardware that contains the boot files +- replaced squash, squish, and cdrom boot options with: squashboot=, squishboot=, and cdromsearch= +- added mmcsearch= option for searching MMC devices +- pre_init now handles mounting: /var/run, /var/tmp, /var/log, and /mnt +- renamed squash_init to live_init +- synchronize boot delays between live_init script and the initrd_init program; apparently, they had different values +- added encryption support for specific partitions: home, etc, tmp, var, rootfs +- added find_device.sh for wrapping blkid device search calls for devices that are not auto-searched +- added settings_init.sh for managing loading of dynamic boot settings, like encrypted boot or network boot +- added settingscdromsearch= and settingsmmcsearch= options +- change cdrom_search= and mmc_search= options to default to ignore +- fix a bug that caused toolchain to improperly extract itself as /toolchain/squashfs-root instead of putting the contents inside of /toolchain +- perform the settings init process prior to dropping into maintenance mode +- Do not fail if toolchain.squash is missing, just print a warning + +Version 0.1.11 +- added _boot_to_squish_ boot option that is a hybrind between squashfs boot and a normal boot +- fixed an issue where the end-system /var was not being created just before the mount command is called + +Version 0.1.10 +- Fix the case where the options specified on the first line are not caught by the regex pattern +- Shrink binary size by 1 or 2 k +- Fix potential memory leak of commandline parameter when dropping to failsafe environment +- Add boot by UUID= support +- Removed squash_init's creation and use of /mnt devices now that udev handles that + +Version 0.1.9 +- Don't allow squashfs to display its progress bar as it has a tendency to generate a floating point exception +- Remove stale (commented out) code +- Increase root boot delays from 8 retries to 10 retries to cope for very slow/buggy devices + - in particular the olpc mmc device sometimes takes a very long time. + +Version 0.1.8 +- Increase final root number of tries from 6 to 8, to give really slow devices +an extra chance +- Make sure that the squash_init will umount the tmpfs with the lazy umount +flag set +- Apparently in squash_init, there was another location where the root mount +retry is attempted only 2 times, this was changed to 8 +- Added some extra critical failure catches when handling etc.squash and +home.squash +- Move the corrupt failure checks into a single function to reduce file size + +Version 0.1.7 +- memset the init_string to guarantee that there is a null +- have the init_string max path be allocated max path + 1 so that there is always a trailing null no matter what +- fix an issue with the finalroot= not always working + - Apparently, I was allocating the exact amount of space needed and no more. + - There must be space_used + 1 to guarantee that a null pointer exists to terminate the string + - Because there was no explicit null terminator, the finalroot= only terminated out of pure luck! + - This was a serious buffer overrun issue +- Apparently, I never allocated the subroot array, it should now be allocated! + +Version 0.1.6 +- add pre_init to handle the mounting of the /proc, /dev, and /sys directories as well as fixing the initng boot process problem + - This improves the boot process and simplifies initng's initial work +- added some verbosity and extra error handling to the initrd_init +- for squash_init, make sure the proc is not mounted before trying to unmount /tmp +- final fix initrd_init such that the initrd fs gets properly removed! + +Version 0.1.5 +- Fix regression in squash_init where run from live system always returns failure, even when things work. + +Version 0.1.4 +- Fixed a bug where the wrong ignore_length was used for the init regex + - This bug was not noticed because the values happened to be the same +- Add _with_toolchain_ option +- Add subroot= option +- The return 0; statement should never be reached, but if it does, exec the failsafe environment +- Fix a problem with squash_init does not copy the squashed data into memory +- Added a Critical Failure error handler + +Version 0.1.3 +- Make sure the rootfs does not have +t set. +- Make sure /var/run and /var/tmp have group writability + +Version 0.1.2 +- add a chdir command following the pivot_root +- Change livecd_init to squash_init +- Cleanup Color Codes +- Add the squash_init script to this project +- Add more boot options +- Change the _drop_to_init_bash_ to _maintenance_mode_ +- make sure the _into_memory_ options are processed and executed correctly + +Version 0.1.1 +- Create This file +- Modify regex to expect a space in front of the option (so that Bloblalbalasfasfinalroot= does not get processed as finalroot) +- Repeat the modification to prevent trailing data that would otherwise cause the statement to be invalid +- Add the programmers name: Kevin Day +- Add _boot_to_livecd_ option for booting the live cd via grub. The livecd itself will be a seperate program or script called: /sbin/livecd_init. +- Have bash perform a login + +Version 0.1.0 +- Initial Release diff --git a/initrd_init.c b/initrd_init.c new file mode 100644 index 0000000..ac12615 --- /dev/null +++ b/initrd_init.c @@ -0,0 +1,389 @@ +// Programmer: Kevin Day +// License: lgpl-2.1-or-later +// NOTE: This is just a very quick implementation to get things off the ground. I inted to rewrite this accordingly and move it into the FLL project. +// This provides the kernel with the ability to boot devices by LABEL or UUID. +// This provides the kernel with the ability to not lock up on reboot + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// To disable ARCHITECTURE_BITS entirely, set both ARCHITECTURE_BITS and ARCHITECTURE_BITS_PATH to an empty string. +// The ARCHITECTURE_BITS and ARCHITECTURE_BITS_PATH must be strings (as in: gcc -O2 -fPIE -DARCHITECTURE_BITS="\"${architecture_bits}\"" initrd_init.c -o initrd_init). +#ifdef ARCHITECTURE_BITS + #define arch_bits ARCHITECTURE_BITS +#else + #define arch_bits "x64" +#endif // ARCHITECTURE_BITS + +#ifdef ARCHITECTURE_BITS_PATH + #define arch_bits_path "" ARCHITECTURE_BITS_PATH "/" +#else + #define arch_bits_path "" arch_bits "/" +#endif // ARCHITECTURE_BITS_PATH + +#define the_init "\\" +#define the_squash "\\/dev/null && + mv --progress squashfs-root/* $2 && + rmdir -p squashfs-root + else + unsquashfs -n $d$2.$1 &>/dev/null && + mv --progress squashfs-root $2 + fi + + if [[ $? != 0 ]] ; then + if [[ -e squashfs-root ]] ; then + rm -Rf squashfs-root + fi + + if [[ $exists == "no" && -d $2 ]] ; then + rm -Rf $2 + fi + + echo -e "\\033[1;31mFailure\\033[0m" + return 1 + fi + + echo -e "\\033[1mDone\\033[0m" +} + +copy_squash_files(){ + local d=$float_path$boot_dir$system_dir$sq_path + local l=$float_path$boot_dir$live_dir + + cp --progress ${d}{bin,lib,sbin}.sfs $l || failure="yes" + + if [[ $failure == "no" && $without_share == "" && $share_device == "" ]] ; then + cp --progress ${d}share.sfs $l || failure="yes" + fi + + if [[ $failure == "no" ]] ; then + if [[ -f ${d}firmware.sfs ]] ; then + cp --progress ${d}firmware.sfs $l || failure="yes" + else + missing_file_warning copy ${d}firmware.sfs + fi + fi + + if [[ $failure == "no" && -f ${d}$current_kernel.modules ]] ; then + if [[ -f ${d}$current_kernel.modules ]] ; then + cp --progress ${d}$current_kernel.modules $l || failure="yes" + else + missing_file_warning copy ${d}$current_kernel.modules + fi + fi + + if [[ $failure == "no" && $with_toolchain != "" && $toolchain_device == "" ]] ; then + if [[ -f ${d}toolchain.sfs ]] ; then + cp --progress ${d}toolchain.sfs $l || failure="yes" + else + missing_file_warning copy ${d}toolchain.sfs + fi + fi + + if [[ $failure == "no" && $with_documentation != "" && $documentation_device == "" ]] ; then + if [[ -f ${d}documentation.sfs ]] ; then + cp --progress ${d}documentation.sfs $l || failure="yes" + else + missing_file_warning copy ${d}documentation.sfs + fi + fi + + if [[ $failure == "no" && $with_package != "" && $package_device == "" ]] ; then + if [[ -f ${d}package.sfs ]] ; then + cp --progress ${d}package.sfs $l || failure="yes" + else + missing_file_warning copy ${d}package.sfs + fi + fi + + if [[ $failure == "no" && $with_perl != "" && $perl_device == "" ]] ; then + if [[ -f ${d}perl.sfs ]] ; then + cp --progress ${d}perl.sfs $l || failure="yes" + else + missing_file_warning copy ${d}perl.sfs + fi + fi + + if [[ $failure == "no" && $with_python != "" && $python_device == "" ]] ; then + if [[ -f ${d}python.sfs ]] ; then + cp --progress ${d}python.sfs $l || failure="yes" + else + missing_file_warning copy ${d}python.sfs + fi + fi +} + +do_dynamic_files(){ + local d=$float_path$boot_dir$system_dir$sq_path + local l=$float_path$boot_dir$live_dir + + do_unsquash sfs $1 || failure="yes" + + if [[ $failure == "yes" ]] ; then + failure="no" + + if [[ -f $d$1.sfs ]] ; then + mkdir -p $1 + cp --progress $d$1.sfs $l && + mount -o loop $l$1.sfs $1 || failure="yes" + + corrupt_failure_check $failure + else + echo -e "\\033[0;33mWarning: Unable to find '\\033[0;01m$1.sfs\\033[0;33m' in '\\033[0;01m${d}\\033[0;33m'.\\033[0m" + sleep 1 + fi + fi +} + +mount_floating_root(){ + if [[ $filesystem_limit == "" || $(echo $filesystem_limit | grep -s -o "^\<[[:digit:]]") == "" ]] ; then + mount -t tmpfs -o gid=d_root,mode=2751 live_system $float_path || drop_to_init_bash "Unable to mount the floating root filesystem" + else + mount -t tmpfs -o size=$filesystem_limit,gid=d_root,mode=2751 live_system $float_path || drop_to_init_bash "Unable to mount the floating root filesystem" + fi +} + +create_basic_root_directories(){ + local d=$float_path + + mkdir -p ${d}{$boot_dir/{$live_dir,$system_dir},dev,mnt,proc,sys} + mkdir -p ${d}dev/pts + + chmod o-r ${d}{mnt,proc,dev,boot,sys} + chmod o+x ${d}$boot_dir{,/$live_dir,/$system_dir} + chgrp d_root ${d} + chgrp d_boot ${d}$boot_dir{,/$live_dir,/$system_dir} + chgrp d_device ${d}dev + chgrp k_system ${d}sys + chgrp k_process ${d}proc + chgrp d_mount ${d}mnt + chmod -t ${d} + chmod g+s ${d}mnt ${d}$boot_dir{,/$live_dir,/$system_dir} +} + +create_additional_root_directories(){ + local d=$float_path + local i= + + if [[ ! -e ${d}var ]] ; then + mkdir -p ${d}var + chgrp d_variable ${d}var + chmod u+rwx,g+rxs-w,o+x-rw ${d}var + fi + + if [[ ! -e ${d}var/log ]] ; then + mkdir -p ${d}var/log + chgrp d_log ${d}var/log + chmod u+rwx,g+rxs-w,o+x-rw ${d}var/log + fi + + if [[ ! -e ${d}var/run ]] ; then + mkdir -p ${d}var/run + chgrp d_run ${d}var/run + chmod u+rwx,g+rxs-w,o+x-rw ${d}var/run + fi + + if [[ ! -d ${d}tmp ]] ; then + mkdir -p ${d}tmp && + chgrp d_temporary ${d}tmp && + chmod u+rwx,g+rwx,o-rwx ${d}tmp && + chmod +t ${d}tmp || + failure="yes" + + if [[ $failure == "yes" ]] ; then + echo -e "\\033[1;31mERROR: Failed to create the '\\033[0;01m${d}tmp\\033[1;31m' directory\\033[0m" + fi + fi +} + +# All of the boot time parameters available are here +boot_string=$(/bin/grep -s -o "\" /proc/cmdline | sed -e 's|\" /proc/cmdline | sed -e 's|\" /proc/cmdline | sed -e 's|\" /proc/cmdline) +with_documentation=$(/bin/grep -s -o "\" /proc/cmdline) +with_python=$(/bin/grep -s -o "\" /proc/cmdline) +with_perl=$(/bin/grep -s -o "\" /proc/cmdline) +with_package=$(/bin/grep -s -o "\" /proc/cmdline) +without_share=$(/bin/grep -s -o "\" /proc/cmdline) +current_kernel=$(/bin/uname -r) +doing_squash=$(/bin/grep -s -o "\" /proc/cmdline | sed -e 's|\" /proc/cmdline | sed -e 's|\" /proc/cmdline | sed -e 's|\" /proc/cmdline | sed -e 's|\ /dev/null + if [[ $? -ne 0 ]] ; then + echo -e "\\033[0;33mWarning: Failed to mount drive '\\033[0;01m${boot_string}\\033[0;33m' to installation directory '\\033[0;01m${float_path}installation/device\\033[0;33m'\\033[0m" + sleep 0.5 + else + ln -s device$install_files ${float_path}installation/live + fi +fi + +# Extract the following files & directories (or perform mounts) +if [[ $failure == "no" && $doing_squash != "" ]] ; then + if [[ $etc_device == "" ]] ; then + do_dynamic_files etc || failure=yes + fi + + if [[ $failure == "no" && $home_device == "" ]] ; then + do_dynamic_files home || failure=yes + fi + + if [[ $failure == "yes" ]] ; then + drop_to_init_bash "Failed to extract squash files" + fi +fi + +# Copy the devices +if [[ ! -e ${float_path}dev/console ]] ; then + cp -pR /dev/console ${float_path}dev/ +fi + +# Mount the rest of the squashed files +if [[ $sq_command == "extract" && $failure == "no" ]] ; then + echo -e "\\033[0;32mExtracting System Into Memory\\033[0m" + + do_unsquash sfs bin && + do_unsquash sfs lib && + do_unsquash sfs sbin || + failure="yes" + + if [[ $failure == "no" && $without_share == "" && $share_device == "" ]] ; then + do_unsquash sfs share || failure="yes" + fi + + # extract firmware & modules for currently running kernel, which are not critical + if [[ $failure == "no" ]] ; then + do_unsquash sfs firmware silent_on_missing + + if [[ ! -d modules ]] ; then + mkdir -p modules + fi + + cd modules/ + do_unsquash modules $current_kernel silent_on_missing + cd .. + fi + + if [[ $failure == "no" && $with_toolchain != "" && $toolchain_device == "" ]] ; then + if [[ -f $float_path$boot_dir$system_dir${sq_path}toolchain.sfs ]] ; then + do_unsquash sfs toolchain || failure="yes" + else + missing_file_warning unsquash $float_path$boot_dir$system_dir${sq_path}toolchain.sfs + fi + fi + + if [[ $failure == "no" && $with_documentation != "" && $documentation_device == "" ]] ; then + if [[ -f $float_path$boot_dir${system_dir}documentation.sfs ]] ; then + do_unsquash sfs documentation || failure="yes" + else + missing_file_warning unsquash $float_path$boot_dir${system_dir}documentation.sfs + fi + fi + + if [[ $failure == "no" && $with_package != "" && $package_device == "" ]] ; then + if [[ -f $float_path$boot_dir$system_dir${sq_path}package.sfs ]] ; then + do_unsquash sfs package || failure="yes" + else + missing_file_warning unsquash $float_path$boot_dir$system_dir${sq_path}package.sfs + fi + fi + + if [[ $failure == "no" && $with_perl != "" && $perl_device == "" ]] ; then + if [[ -f $float_path$boot_dir$system_dir${sq_path}perl.sfs ]] ; then + do_unsquash sfs perl || failure="yes" + else + missing_file_warning unsquash $float_path$boot_dir$system_dir${sq_path}perl.sfs + fi + fi + + if [[ $failure == "no" && $with_python != "" && $python_device == "" ]] ; then + if [[ -f $float_path$boot_dir$system_dir${sq_path}python.sfs ]] ; then + do_unsquash sfs python || failure="yes" + else + missing_file_warning unsquash $float_path$boot_dir$system_dir${sq_path}python.sfs + fi + fi + + if [[ $failure == "yes" ]] ; then + echo -e "\\033[1;31mERROR: Failed to Extract the Filesystem into Memory\\033[0m" + echo -e "\\033[1mAttempting Compressed Memory Filesystem Load.\\033[0m" + + rm -Rf bin lib sbin firmware modules + + if [[ $share_device == "" ]] ; then + rm -Rf share + fi + + if [[ $toolchain_device == "" ]] ; then + rm -Rf toolchain + fi + + if [[ $documentation_device == "" ]] ; then + rm -Rf documentation + fi + + if [[ $package_device == "" ]] ; then + rm -Rf package + fi + + if [[ $perl_device == "" ]] ; then + rm -Rf perl + fi + + if [[ $python_device == "" ]] ; then + rm -Rf python + fi + + sq_command="memory" + sleep 3 + fi +fi + +if [[ $sq_command == "memory" ]] ; then + failure="no" + + echo -e "\\033[0;32mCopying Squash System Data Into Memory\\033[0m" + copy_squash_files + + if [[ $failure == "yes" ]] ; then + echo -e "\\033[1;31mERROR: Failed to Load the Compressed Filesystem into Memory\\033[0m" + echo -e "\\033[1mAttempting Device Filesystem Load.\\033[0m" + + rm -f $float_path$boot_dir${live_dir}{bin,lib,sbin,share,firmware,toolchain,documentation,package,perl,python}.sfs + rm -f $float_path$boot_dir$live_dir$current_kernel.modules + + sq_command="device" + + sleep 3 + fi +fi + +if [[ $sq_command == "device" ]] ; then + failure="no" + + cd $float_path$boot_dir$system_dir$sq_path || failure="yes" + + if [[ $failure == "no" ]] ; then + for i in * ; do + ln -sf ../$system_dir$sq_path$i $float_path$boot_dir$live_dir$i && + system_symlinks="$system_symlinks$i " || failure="yes" + done + + cd $float_path + fi +fi + +if [[ $failure == "no" && ( $sq_command == "memory" || $sq_command == "device" ) ]] ; then + mkdir -p bin lib sbin && + mount -o loop $float_path$boot_dir${live_dir}bin.sfs bin && + mount -o loop $float_path$boot_dir${live_dir}lib.sfs lib && + mount -o loop $float_path$boot_dir${live_dir}sbin.sfs sbin || + failure="yes" + + if [[ $failure == "no" && $without_share == "" && $share_device == "" ]] ; then + mkdir -p share && + mount -o loop $float_path$boot_dir${live_dir}share.sfs share || failure="yes" + fi + + if [[ $failure == "no" && -f $float_path$boot_dir${live_dir}firmware.sfs ]] ; then + mkdir -p firmware && + mount -o loop $float_path$boot_dir${live_dir}firmware.sfs firmware || failure="yes" + fi + + if [[ $failure == "no" && -f $float_path$boot_dir$live_dir$current_kernel.modules ]] ; then + mkdir -p modules/$current_kernel && + mount -o loop $float_path$boot_dir$live_dir$current_kernel.modules modules/$current_kernel || failure="yes" + fi + + if [[ $failure == "no" && $with_toolchain != "" && $toolchain_device == "" ]] ; then + if [[ -f $float_path$boot_dir${live_dir}toolchain.sfs ]] ; then + mkdir -p toolchain && + mount -o loop $float_path$boot_dir${live_dir}toolchain.sfs toolchain || failure="yes" + else + missing_file_warning mount $float_path$boot_dir${live_dir}toolchain.sfs + fi + fi + + if [[ $failure == "no" && $with_documentation != "" && $documentation_device == "" ]] ; then + if [[ -f $float_path$boot_dir${live_dir}documentation.sfs ]] ; then + mkdir -p documentation && + mount -o loop $float_path$boot_dir${live_dir}documentation.sfs documentation || failure="yes" + else + missing_file_warning mount $float_path$boot_dir${live_dir}documentation.sfs + fi + fi + + if [[ $failure == "no" && $with_package != "" && $package_device == "" ]] ; then + if [[ -f $float_path$boot_dir${live_dir}package.sfs ]] ; then + mkdir -p package && + mount -o loop $float_path$boot_dir${live_dir}package.sfs package || failure="yes" + else + missing_file_warning mount $float_path$boot_dir${live_dir}package.sfs + fi + fi + + if [[ $failure == "no" && $with_perl != "" && $perl_device == "" ]] ; then + if [[ -f $float_path$boot_dir${live_dir}perl.sfs ]] ; then + mkdir -p perl && + mount -o loop $float_path$boot_dir${live_dir}perl.sfs perl || failure="yes" + else + missing_file_warning mount $float_path$boot_dir${live_dir}perl.sfs + fi + fi + + if [[ $failure == "no" && $with_python != "" && $python_device == "" ]] ; then + if [[ -f $float_path$boot_dir${live_dir}python.sfs ]] ; then + mkdir -p documentation && + mount -o loop $float_path$boot_dir${live_dir}python.sfs python || failure="yes" + else + missing_file_warning mount $float_path$boot_dir${live_dir}python.sfs + fi + fi + + if [[ $failure == "yes" ]] ; then + echo -e "\\033[1;31mERROR: Failed to mount the squashed filesystems\\033[0m" + + for i in bin lib sbin share firmware toolchain documentation package perl python; do + if [[ $(mount -l | grep -s -o "^.* on $float_path$boot_dir$live_dir$i ") ]] ; then + umount -l $i + fi + done + + if [[ $(mount -l | grep -s -o "^.* on $float_path$boot_dir${live_dir}modules/$current_kernel ") ]] ; then + umount -l modules/$current_kernel + fi + + sleep 3 + fi +fi + +if [[ $failure == "no" && $doing_squish != "" ]] ; then + cd $float_path$boot_dir$system_dir$subroot_string || failure="yes" + + if [[ $failure == "no" ]] ; then + for i in * ; do + if [[ ! -e $float_path$i ]] ; then + ln -sf $boot_dir$system_dir$subroot_string$i $float_path$i && + system_symlinks="$system_symlinks$i " || failure="yes" + fi + done + + cd $float_path + else + echo -e "\\033[1;31mERROR: Failed to change to the path '\\033[0;01m$float_path$boot_dir$system_dir$subroot_string\\033[1;31m'\\033[0m" + fi +fi + +if [[ $failure == "no" ]] ; then + create_additional_root_directories + + if [[ $squash_boot != "" ]] ; then + umount -l $float_path$boot_dir$system_dir + fi +fi + +corrupt_failure_check $failure + +if [[ $system_symlinks == "" ]] ; then + umount -l $float_path$boot_dir$system_dir +fi + +# make sure /proc is not mounted prior to chroot & pivot_root +umount -n /proc + +# Now attempt to switch to the new system +echo -e -n "\\033[1;32mNow Switching to the " +if [[ $doing_squash != "" ]] ; then + echo -n "Squash" +else + echo -n "Squish" +fi +echo -e " System\\033[0m" + +cd $float_path + +pivot_root . tmp/ || drop_to_init_bash "Failed to pivot root to the live system" +cd / +umount -l -n /tmp + +if [[ ! -e dev/console ]] ; then + echo -e "\\033[1;31mERROR: Failed to find console device at dev/console\\033[0m" + echo -e "\\033[1mDropping to system shell.\\033[0m" + exec /bin/bash +fi + +if [[ ! -e $init_string ]] ; then + drop_to_init_bash "Could not find init binary: \\033[0;01m${init_string}\\033[1;31m" +fi + +if [[ ! -e bin/bash ]] ; then + drop_to_init_bash "Could not find bash binary: \\033[0;01mbin/bash\\033[1;31m" +fi + +exec chroot . /bin/bash -c "exec $init_string" < dev/console > dev/console 2>&1 diff --git a/mount_init.sh b/mount_init.sh new file mode 100644 index 0000000..e6fbffb --- /dev/null +++ b/mount_init.sh @@ -0,0 +1,162 @@ +#!/bin/bash + +find_drive(){ + local count= + local max=4 + local custom= + local target=$drive + + if [[ $target == "" ]] ; then + return 1 + fi + + if [[ $(echo $target | grep -o "^[[:alnum:]_-]*=") == "" ]] ; then + return 0 + fi + + drive= + let count=0 + while [[ $count -lt $max ]] ; do + custom=$(blkid -o device -l -t $target) + + if [[ $custom != "" ]] ; then + drive=$custom + break + fi + + let count=$count+1 + echo -e "\\033[0;33mWarning: Failed to find device by the id or name of: '\\033[0;01m${target}\\033[0;33m', trying again in 2 seconds.\\033[0m" + sleep 2 + done + + if [[ $drive == "" ]] ; then + return 1 + fi +} + +mount_drive(){ + local directory=$1 + local destination=$2 + local drive=$3 + local the_status= + local custom= + + if [[ $drive == "" && -f $path${directory}.device ]] ; then + drive=$(cat ${path}${directory}.device) + fi + + find_drive + + if [[ $drive == "" ]] ; then + if [[ $directory == "rootfs" ]] ; then + echo -e "\\033[1;31mError: The rootfs device was either not given or it was not found.\\033[0m" + sleep 2 + return 1 + fi + + return 2 + fi + + if [[ $directory != "rootfs" ]] ; then + mkdir -p $destination + fi + + mount $drive $destination + the_status=$? + + if [[ $the_status != 0 ]] ; then + sleep 2 + mount $drive $destination + the_status=$? + fi + + if [[ $the_status != 0 ]] ; then + if [[ $directory == "rootfs" ]] ; then + echo -e "\\033[1;31mError: Failed to mount the '\\033[0;01mrootfs\\033[1;31m' device.\\033[0m" + sleep 2 + return 1 + else + echo -e "\\033[0;33mWarning: Failed to mount '\\033[0;01m${directory}\\033[0;33m' device.\\033[0m" + sleep 2 + return 2 + fi + fi + + return 0 +} + +perform_mounts(){ + local path=${device_directory}boot/settings/ + local status= + local drive= + local i= + + if [[ ! -d $path ]] ; then + path=${device_directory}settings/ + + if [[ ! -d $path ]] ; then + path=${device_directory}/.settings/ + + if [[ ! -d $path ]] ; then + path=${device_directory} + fi + fi + fi + + if [[ $settings_name != "" ]] ; then + path=$path${settings_name}/ + fi + + if [[ $rootfs_device != "" && ! -f ${path}settings.lock ]] ; then + mount_drive rootfs $rootfs_path $rootfs_device + status=$? + elif [[ -f ${path}rootfs.device ]] ; then + mount_drive rootfs $rootfs_path + status=$? + else + echo -e "\\033[1;31mError: Neither root= was passed nor was there a rootfs.device found in the settings directory.\\033[0m" + return 1 + fi + + if [[ $status -eq 0 ]] ; then + mounted_rootfs="y" + else + return 1 + fi + + for i in home etc share var tmp package perl python documentation toolchain ; do + drive=$(/bin/grep -s -o "\<${i}device=[[:alnum:][:punct:]]*\>" /proc/cmdline | sed -e "s|\<${i}device=||") + + if [[ $drive != "" && ! -f ${path}settings.lock ]] ; then + mkdir -p $float_path$i && + mount_drive $i $float_path$i $drive + status=$? + elif [[ -f $path$i.device ]] ; then + mkdir -p $float_path$i && + mount_drive $i $float_path$i + status=$? + fi + done +} + +process_parameters(){ + local float_path=$1 + local rootfs_path=$2 + local rootfs_device=$(/bin/grep -s -o "\" /proc/cmdline | sed -e 's|\ /dev/null + + if [[ $float_path == "" || $mounted_rootfs == "" ]] ; then + return 1; + fi +} + + +process_parameters $1 $2 diff --git a/pre_init.c b/pre_init.c new file mode 100644 index 0000000..0a16a0e --- /dev/null +++ b/pre_init.c @@ -0,0 +1,230 @@ +// Programmer: Kevin Day +// License: lgpl-2.1-or-later +// NOTE: This is just a very quick implementation to get things off the ground. I inted to rewrite this accordingly and move it into the FLL project. +// What this does is mount the /dev/ directory and then creates the appropriate /dev/console and /dev/null devices and finally calls the init program +// To allow for a quick implementation, the settings are hardcoded +// Accepts 1 parameter and it is the init parameter + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef ARCHITECTURE_BITS + #define arch_bits ARCHITECTURE_BITS +#else + #define arch_bits "x64" +#endif // ARCHITECTURE_BITS + +#ifdef ARCHITECTURE_BITS_PATH + #define arch_bits_path "" ARCHITECTURE_BITS_PATH "/" +#else + #define arch_bits_path "" arch_bits "/" +#endif // ARCHITECTURE_BITS_PATH + +#define the_init "\\ /dev/null + + if [[ $? != 0 ]] ; then + echo -e "\\033[0;33mWarning: Unable to mount settings drive: '\\033[0;01m$settings_drive\\033[0;33m', trying again in 2 seconds.\\033[0m" + let current_count=${current_count}+1 + sleep 2 + else + current_count="success" + process_settings_data + return 0 + fi + done +} + + +process_parameters