You are not logged in.

#1 2015-04-12 00:02:03

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

[SOLVED] Hook for Arch to optionally run "LIVE"

I currently patch init with the code shown below and it works as expected by loading Arch to RAM then unmounting the underlying real root. It's optional and there's also support for a compressed archive of the root filesystem which can speed the loading to RAM by a factor of more than 10 if booting from a USB memory stick.

What I'd like to do now however is convert the code to a hook since I have always found patching init a tad bit intrusive.

My code is run after the init  section that starts with the comment # Successfully mounted /new_root, but ${init} is missing as shown below.

What do I need to add to my code to make it function as a hook instead of as a patch to init?


.
.
.
elif [ ! -x "/new_root${init}" ]; then
    # Successfully mounted /new_root, but ${init} is missing
    # The same logic as above applies
    err "Root device mounted successfully, but ${init} does not exist."
    echo "Bailing out, you are on your own. Good luck."
    echo
    launch_interactive_shell --exec
fi

# "LIVE" code starts here >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

echo "]press a key *NOW* if you *DO NOT* want the root filesystem loaded to RAM...█"
if read -t 5 -n 1 k ; then
   echo "]running from real root...█"
else
   mkdir                                                             /tmp/real_root/
   mount --move                                                      /new_root/ /tmp/real_root/
   mount -t tmpfs -o size=60% none                                   /new_root/
   if [[ -f /tmp/real_root/rootfs.tar.gz ]] ; then
      echo "]copying root filesystem ARCHIVE $(date -r /tmp/real_root/rootfs.tar.gz +%F-%T) to RAM...█" ; tar -xf /tmp/real_root/rootfs.tar.gz -C /new_root/
   else
      echo "]copying root filesystem to RAM...█" ; cp -a -R          /tmp/real_root/* /new_root/
   fi
   umount                                                            /tmp/real_root/
   touch                                                             /new_root/RAMroot
fi # read -t 3 -n 1 k

# "LIVE" code ends here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

if [ "${break}" = "postmount" ]; then
    echo ":: Post-mount break requested, type 'exit' to resume operation"
    launch_interactive_shell
fi
.
.
.

Last edited by KairiTech (2015-04-16 21:53:07)

Offline

#2 2015-04-12 01:57:24

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,362

Re: [SOLVED] Hook for Arch to optionally run "LIVE"

That's all a hook is.


I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.

Offline

#3 2015-04-16 21:52:22

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [SOLVED] Hook for Arch to optionally run "LIVE"

[Edit] added the --preserve=all option to the cp command to retain the file capabilities of ping as cap_net_raw+ep if it's to be run by a non-root user. Still looking for a tar version that supports file capabilities.

[Edit] bsdtar supports file capabilities by default. You just need to add the -p option during the extract to restore them. I redirect bsdtar's output to the same file that I use to indicate that Arch is running from RAM because there will be instances when the file's and/or directories' flags cannot be restored. So far that hasn't caused any issues. 

After looking at the liveroot package in AUR and elsewhere on the internet I settled on the code below. I was mostly just missing the mount_handler part.

run_hook() {
   echo "]press a key *NOW* if you *DO NOT* want the root filesystem loaded to RAM...█"
   if read -t 5 -n 1 k ; then
      clear
   else
      poll_device ${root} 20
      mkdir /real_root/
      mount ${root} /real_root/
      mount -t tmpfs -o size=60% none /new_root/
      if [[ -f /real_root/rootfs.tar.gz ]] ; then
         echo "]copying root filesystem ARCHIVE $(date -r /real_root/rootfs.tar.gz +%F_%T) to RAM...█"
         bsdtar -pxf /real_root/rootfs.tar.gz -C /new_root/  > /new_root/LIVE 2>&1
      else
         echo "]copying root filesystem to RAM...█"
         cp --preserve=all -a -R /real_root/* /new_root/
      fi # [[ -f /real_root/rootfs.tar.gz ]]
      touch /new_root/LIVE
      umount /real_root/
      LIVE_mount() {
         #
         # since init doesn't have to mount anything anymore give it something usefull to do instead
         #
         clear
         echo "





                  ]you are L I V E ! ! ! █





              "
      }
      mount_handler=LIVE_mount
   fi # read -t 3 -n 1 k
}

I created the root filesystem archive with:

bsdtar --exclude={/rootfs.tar.gz,/var/cache/pacman/pkg/*,/boot/*,/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/usr/share/man/*,/usr/share/doc/*,/usr/share/gtk-doc/*} -vzcf /rootfs.tar.gz /

And for completeness here's the install:

#!/bin/ash

build() {
  add_runscript
}

help() {
  cat <<HELPEOF
]Run Arch from RAM█
HELPEOF
}

Last edited by KairiTech (2015-05-08 00:33:59)

Offline

Board footer

Powered by FluxBB