Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 01 Oct 2014, 20:55
All times are UTC - 4
 Forum index » Advanced Topics » Additional Software (PETs, n' stuff) » Utilities
Initrd.gz Editor - simple GUI tool
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 2 Posts_count   Goto page: 1, 2 Next
Author Message
sc0ttman


Joined: 16 Sep 2009
Posts: 2376
Location: UK

PostPosted: Fri 13 May 2011, 12:38    Post_subject:  Initrd.gz Editor - simple GUI tool
Sub_title: should work with all initrd.gz files, old & new
 

Initrd.gz Editor
a single shell script that can be used to edit contents of initrd.gz

Now supports all initrd.gz files, from all puppies

It will auto-install itself to /usr/bin if not found to be installed already.
Comment out line 150 (self_install) to disable this.

Requires X desktop, Gtkdialog, Xdialog
Save to a shell script. Make it executable. Run it.

If you pass the initrd.gz path, through the command line, no GUI is loaded, just your file manager.

You can also pass the desired save directory as the second argument.

Screenshots below, but here's the code:

Code:
#!/bin/sh

# sc0ttman
# Initrd-Editor: unpack, edit and rebuild your "initrd.gz" file
# a simple GUI.. requires X desktop, GtkDialog3 and Xdialog
# possible to easily modify, to translate (but I don't have gettext installed)
# do as you like with this, or not at all :)

# usage: initrd-editor.sh [ /full/path/to/initrd.gz ] [ /full/path/to/save/dir ]
# note: both paths must start with / and are optional

# export all functions
set -a

# setup vars to be used
SCRIPT_PATH="$0"
SCRIPT_NAME=`basename $0`
# apps to use, first listed is most preferable
[ ! "$FILEMAN" ] && FILEMAN="`which rox`"
[ ! "$FILEMAN" ] && FILEMAN="`which thunar`"
[ ! "$FILEMAN" ] && FILEMAN="`which nautilus`"
[ ! "$FILEMAN" ] && FILEMAN="`which pcmanfm`"
[ ! "$FILEMAN" ] && FILEMAN="`which mc`"
[ ! "$FILEMAN" ] && FILEMAN="`which asm`"
# setup desired options for the file manager in use
[ "$FILEMAN" = "`which asm`" ] && FILEMAN="rxvt -e ${FILEMAN}"
[ "$FILEMAN" = "`which mc`" ] && FILEMAN="rxvt -e ${FILEMAN}"
[ "$FILEMAN" = "`which rox`" ] && FILEMAN="${FILEMAN} -n"
# use latest gtkdialog
GTKDIALOG=`which gtkdialog`
GTKDIALOG=`which gtkdialog2`
GTKDIALOG=`which gtkdialog3`
# setup dirs
WORKDIR="/tmp/initrd-editor"
# use given save dir, or default if none given
[ -d "$2" ] && SAVEDIR="$2" || SAVEDIR="$HOME/Downloads"
echo "save dir is $SAVEDIR"
# no errors so far ;)
XPID=''
INSTALL_CHECK=''
ERROR=''

# GUI text
LOC_GUI_TITLE="Initrd.gz Editor - rebuild your init script"
LOC_GUI_FILE="Choose your initrd.gz file:"
LOC_GUI_EDIT_DIALOG="Edit the contents of initrd.gz\nClick OK only when you are finished."
LOC_GUI_SAVE_DIALOG="Success - your new initrd.gz is ready, in ${SAVEDIR}"
LOC_ERROR_FILE="Error: File not found."
LOC_ERROR_FILETYPE="Error: File must have a .gz extension"
LOC_ERROR_WORKDIR="Error: Work directory not available"
LOC_ERROR_SAVEDIR="Error: Save directory not available"

## functions
self_install () {
   # check if installed
   INSTALL_CHECK=`which $SCRIPT_NAME`
   # if it was not found, copy to /usr/bin
   [ "$INSTALL_CHECK" = "" ] && cp -f "$SCRIPT_PATH" "/usr/bin/"
}
# error checking
check_errors () {
   # file maybe changed since above, re-check
   FILENAME=`basename "$FILE"`
   FILEEXT=${FILENAME##*.} # file extension only
   if [ ! -f "$FILE" ];then
      ERROR="$LOC_ERROR_FILE"
   elif [ "$FILEEXT" != "gz" ];then
      ERROR="$LOC_ERROR_FILETYPE"
   elif [ ! -d "$WORKDIR" ];then
      mkdir "$WORKDIR" || ERROR="$LOC_ERROR_WORKDIR"
   elif [ ! -d "$SAVEDIR" ];then
      mkdir "$SAVEDIR" || ERROR="$LOC_ERROR_SAVEDIR"
   fi
   # set final message
   LOC_GUI_ERROR_DIALOG="$ERROR"
}
# check archive type
check_filetype () {
   CPIOTEST=''
   CPIOTEST=`file -z "$FILE" | grep cpio`
   GZIPTEST=''
   GZIPTEST=`file -z "$FILE" | grep "filesystem data"`
}
# extract function
extract_file () {
   cd "$WORKDIR"
   if [ "$CPIOTEST" != "" ];then
      zcat "$FILE" | cpio -i -d
      # make sure nothing mounted
      rm -r "${WORKDIR}/mntd/"*
      rmdir "${WORKDIR}/mntd"
   else
      # need to copy file to workdir
      cp "$FILE" "${WORKDIR}"
      # unpack
      rm -f "${WORKDIR}/initrd"
      gunzip "${WORKDIR}/initrd.gz"
      # mount
      mkdir "${WORKDIR}/mntd"
      mount -o loop initrd "${WORKDIR}/mntd"
   fi
}
# edit initrd function
edit_init () {
   if [ -d "${WORKDIR}/mntd" ];then # if initrd is mounted
      $FILEMAN "${WORKDIR}/mntd" &
   else
      $FILEMAN "$WORKDIR" &
   fi
   XPID=$!
   sleep 1
   Xdialog --title "Initrd Editor" --msgbox "$LOC_GUI_EDIT_DIALOG" 0 0
   kill $XPID > /dev/null 2>&1
   rox -D "$WORKDIR" > /dev/null 2>&1
}
# compress function
compress_file () {
   cd "${WORKDIR}"
   # remove any old ones
   rm "${SAVEDIR}/initrd.gz"
   # build the new file
   if [ "$CPIOTEST" != "" ];then
      find . | cpio -o -H newc | gzip -9 > "${SAVEDIR}/initrd.gz"
   else
      sync
      umount -f "${WORKDIR}/mntd"
      sync
      gzip "${WORKDIR}/initrd"
      mv -f "${WORKDIR}/initrd.gz" "${SAVEDIR}/initrd.gz"
   fi
   # show file in file manager, only if save dir not chosen by user
   if [ "$SAVEDIR" = "$HOME/Downloads" ];then
      $FILEMAN "${SAVEDIR}" &
   fi
   sleep 1
   Xdialog --title "Initrd Editor" --msgbox "$LOC_GUI_SAVE_DIALOG" 0 0
}
cleanup_files () {
   # clean up and exit program
   rm -r "${WORKDIR}/"*  > /dev/null 2>&1
   rmdir "${WORKDIR}/temp" > /dev/null 2>&1
   rmdir "${WORKDIR}/" > /dev/null 2>&1
   sync
   umount "${WORKDIR}/temp"
   sync
}
# run program function
run_program () {
   # install to /usr/bin, if not there already
   self_install
   # check arrors and exit with a message, if needed
   check_errors
   if [ "$ERROR" != "" ];then
      Xdialog --title "Initrd Editor" --msgbox "$LOC_GUI_ERROR_DIALOG" 0 0
      # exit with error
      exit 1
   else
      # do the thing
      cd "$WORKDIR"
      check_filetype
      extract_file
      edit_init
      compress_file
      cleanup_files
      # exit without error
      killall gtkdialog3 --p INITRD_EDITOR_GUI -c > /dev/null 2>&1
      exit 0
   fi
}

# check for file given through command line
if [ -f "$1" ];then
   # assign given file to $FILE
   FILE="$1"
   # run with no gui
   run_program
fi

# if no file given, user must use GUI to choose a file
[ "$FILE" = "" ] && FILE="/choose/your/path/to/initrd.gz"
# GUI
export INITRD_EDITOR_GUI='<window title="Initrd Editor">
<vbox>
<frame>
   <vbox homogeneous="true">
      <text><label>'${LOC_GUI_TITLE}'</label></text>
   </vbox>
</frame>
<frame>
   <vbox>
      <text><label>'${LOC_GUI_FILE}'</label></text>
      <hbox>
         <entry tooltip-text="The initrd.gz file.">
            <default>'$FILE'</default>
            <variable>FILE</variable>
         </entry>
         <button>
            <input file icon="gtk-open"></input>
            <action type="fileselect">FILE</action>
         </button>
      </hbox>
   </vbox>
</frame>
<frame>
   <vbox>
      <hbox>
         <button ok>
            <action>run_program</action>
         </button>
         <button cancel></button>
      </hbox>
   </vbox>
</frame>
</vbox>
</window>'

# run the GUI
$GTKDIALOG -p INITRD_EDITOR_GUI -c
unset INITRD_EDITOR_GUI

# exit without error
exit 0


I think it's all working as I want it now.
initrd-editor1.png
 Description   
 Filesize   131.24 KB
 Viewed   2270 Time(s)

initrd-editor1.png

initrd-editor.png
 Description   
 Filesize   188.56 KB
 Viewed   2307 Time(s)

initrd-editor.png


_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search

Edited_times_total
Back to top
View user's profile Send_private_message 
ICPUG

Joined: 24 Jul 2005
Posts: 1290
Location: UK

PostPosted: Fri 13 May 2011, 13:31    Post_subject:  

Confused by the screenshot.

Does this allow editing the init script or editing of any file that exists within initrd.gz?
Back to top
View user's profile Send_private_message 
nooby

Joined: 29 Jun 2008
Posts: 10557
Location: SwedenEurope

PostPosted: Fri 13 May 2011, 13:42    Post_subject:  

ICPUG I know too little but maybe this part indicate what it can and not can do?

# setup vars to be used
SCRIPT_PATH="$0"
SCRIPT_NAME=`basename $0`
# apps to use, first listed is least preferable
FILEMAN="`which asm`"
FILEMAN="`which mc`"
FILEMAN="`which nautilus`"
FILEMAN="`which pcmanfm`"
FILEMAN="`which thunar`"
FILEMAN="`which rox`"
# use latest gtkdialog
GTKDIALOG=`which gtkdialog`
GTKDIALOG=`which gtkdialog2`
GTKDIALOG=`which gtkdialog3`
# setup dirs
WORKDIR="/tmp/initrd-editor"
SAVEDIR="$HOME/Downloads"

It can use these then so if one look what they can do would that answer your question?

But my gut feeling is supporting your take on it that this "rebuilder" takes apart an initrd so you can rebuild it but maybe don't do anything inside the files that the initrd is made out of???

But maybe allow that one exchange one such file for another that one edit outside of this program? Sorry I only guess but I find these things interesting if they would help me get to boot a linux that insist to look for a file at the CD or USB when I have these files on the HDD instead.

So if this prog allow me to change such that would be cool

_________________
I use Google Search on Puppy Forum
not an ideal solution though
Back to top
View user's profile Send_private_message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2376
Location: UK

PostPosted: Fri 13 May 2011, 14:24    Post_subject:  

ICPUG wrote:
Confused by the screenshot.

Does this allow editing the init script or editing of any file that exists within initrd.gz?

it opens up all of the contents (not only init script) of the initrd.gz file in rox, or thunar if rox is not found, or pcmanfm, etc...

_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search
Back to top
View user's profile Send_private_message 
nooby

Joined: 29 Jun 2008
Posts: 10557
Location: SwedenEurope

PostPosted: Fri 13 May 2011, 14:43    Post_subject:  

So would this allow me to edit ArchBang and Kanotix and a lot of other distros that stubbornly only look for files on the CD/DVD and by changing what they look for make them look for these files on internal HDD instead?
_________________
I use Google Search on Puppy Forum
not an ideal solution though
Back to top
View user's profile Send_private_message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2376
Location: UK

PostPosted: Sat 14 May 2011, 06:08    Post_subject:  

I should say as a note, I built this with a plan to integrate into Woofy ...and I would still really appreciate someone talking a look at lines 77-80, and find out why the 'kill $XPID' command does not work....
_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search
Back to top
View user's profile Send_private_message 
nooby

Joined: 29 Jun 2008
Posts: 10557
Location: SwedenEurope

PostPosted: Sat 14 May 2011, 06:37    Post_subject:  

Does this help? The only similar codes I managed to find on the internet using google.

# Start new X
X :1 -layout SCLayout -ac &
XPID=$!
sleep 2

# Do Volume Key Control mapping if set up
if [ -f $HOME/.scbind ]; then
xbindkeys --display :1 -f $HOME/.scbind
fi

# Start Pulse Audio & Starcraft in new X
DISPLAY=:1 ck-launch-session wine $HOME/.wine/drive_c/Starcraft/StarCraft.exe -- /usr/bin/X :1 -layout SCLayout

# Cleanup
sleep 1
kill $XPID

As you see above he has first declared??? that

# Start new X
X :1 -layout SCLayout -ac &
XPID=$!
sleep 2

then in the end he does the kill thing

# Cleanup
sleep 1
kill $XPID

So my humble suggestion is that you need to look through if XPID ever was declared somewhere first or else it maybe don't know what you want it to do?

Sorry I know nothing but wanted to "help" your brain going on it.
If you are several layers higher or deeper already then I apology.

Never mind me using the term declare that could be misleading. Assign or set or establish or give or whatever word that is proper there.

_________________
I use Google Search on Puppy Forum
not an ideal solution though
Back to top
View user's profile Send_private_message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Sat 14 May 2011, 10:37    Post_subject:  

sc0ttman wrote:
I should say as a note, I built this with a plan to integrate into Woofy ...and I would still really appreciate someone talking a look at lines 77-80, and find out why the 'kill $XPID' command does not work....

sc0ttman,

It doesn't work because no additional process has been set.
"XPID" was never set to any value.

You could close the directory by using "rox -D [directoryname]

EDIT: I see you already discovered that - "rox -D "$WORKDIR" > /dev/null 2>&1". Unfortunately, you'll probably have to do something similar for other filemanagers Smile


Cheers,
s
Back to top
View user's profile Send_private_message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2376
Location: UK

PostPosted: Sat 14 May 2011, 15:10    Post_subject:  

But I have this above it:

Code:
# edit initrd function
edit_init () {
   $FILEMAN "$WORKDIR" > /dev/null 2>&1
   XPID=$!

that should work right? I've used it many times before in other scripts...

_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search
Back to top
View user's profile Send_private_message 
Dougal


Joined: 19 Oct 2005
Posts: 2505
Location: Hell more grotesque than any medieval woodcut

PostPosted: Sat 14 May 2011, 15:37    Post_subject:  

Your edit_init() is a complete mess:
- $! gives you the PID of the last backgrounded (by the shell) process -- the line above it does not end with "&"...
- With Rox it will be a problem anyway, as Rox defaults to running all instances as the same process... you could try using "rox -n", but killing is ugly and a shame to use when you have "rox -D" for closing windows
- You also include "rox -D", which breaks the whole FILEMAN abstraction...

What you could do is replace the Barry-style kludge of FILEMAN="`which bla`" repetition with a if-elif list thus:
Code:
if type rox >/dev/null 2>&1 ; then
# use rox
elif type thunar >/dev/null 2>&1 ; then
#use thunar
..
and use functions to abstract the opening and closing of directories, so the rox functions will look like:
Code:
open_dir(){
rox -d "$1"
}

close_dir(){
rox -D "$1"
}

(if the file manager doesn't support closing like rox, you can add the XPID parts into the functions)

Then edit_init will just be
Code:
edit_init () {
   open_dir "$WORKDIR" > /dev/null 2>&1
   Xdialog --title "Initrd Editor" --msgbox "$LOC_GUI_EDIT_DIALOG" 0 0
   close_dir "$WORKDIR" > /dev/null 2>&1
}


I'd also recommend not using all the redirection to stderr... you don't realize how many bugs in Puppy hide under Barry's liberal use of that.

_________________
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind
Back to top
View user's profile Send_private_message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2376
Location: UK

PostPosted: Sat 14 May 2011, 16:28    Post_subject:  

cheers for the info, much appreciated, its kind of adapted for my pc at the moment, i'll apply your suggestions, and I think my eyes are going - I thought I had that ampersand there already! But cheers for the info! Smile
_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search
Back to top
View user's profile Send_private_message 
Karl Godt


Joined: 20 Jun 2010
Posts: 3972
Location: Kiel,Germany

PostPosted: Sat 14 May 2011, 17:43    Post_subject:  

"!$" is used to use part of the last command, like
# echo "A"
A
# echo !$
echo "A"
A
#

The process-id could be determined with "$$" for the script itself or `pidof $PROGRAM`

but

# if [ -z "$ABC" ] ; then
geany -i &
XPID=$!
echo $XPID
ps axc | grep geany
pidof geany
fi

should work , too ... so there might be something like kill -9 $XPID , kill -14 ... kill -15 ... necessary ... There are over 30 kill -s SIGNAL , of which I only have "kill -s HUP `pidof pppd`" experience with .

On the other hand there might be the chance killing rox would kill the whole "rox -p $HOME/Choices/ROX-Filer/PuppyPin" ... and killing should be after "X"-ing the rox-window-frame-button , am I right ?

In that case perhaps
if [ "$FILEMAN" != "rox" ] ; then
$FILEMAN "$WORKDIR" 2>/tmp/fileman.errs
XPID=$!
Xdialog --title "Initrd Editor" --msgbox "$LOC_GUI_EDIT_DIALOG" 0 0
kill $XPID
else
rox -n "$WORKDIR" 2>/tmp/rox.errs
Xdialog --title "Initrd Editor" --msgbox "$LOC_GUI_EDIT_DIALOG" 0 0
rox -D "$WORKDIR"
fi

note : "rox -n" for new instance .... like dougal suggests [ rox -h ]

or

$FILEMAN $WORKDIR
XPID=$!
XPIDrox=`ps | grep -i 'rox' | grep -w "$WORKDIR"`
###[ -n "$XPIDrox" ] && XPID=$XPIDrox###
Xdialog --title "Initrd Editor" --msgbox "$LOC_GUI_EDIT_DIALOG" 0 0
[ -z "$XPIDrox" ] && kill -9 $XPID
[ -n "$XPIDrox" ] && rox -D "$WORKDIR"
Back to top
View user's profile Send_private_message Visit_website 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Sun 15 May 2011, 11:48    Post_subject:  

I'm a fan of placing tools for single files on right-click menus and usually looking to only edit the "init" file.
Code:
#!/bin/sh
# right-click to edit 'init' file in initrd.gz
# link file to /root/.config/rox.sourceforge.net/OpenWith

[ "$1" != "" ] && FILE="$1" || exit

mkdir -p /tmp/INITDIR
cd /tmp/INITDIR
zcat "$FILE" | cpio -i -d 
defaulttexteditor /tmp/INITDIR/init

Xdialog --title "Edit-init" --ok-label "Restore" --msgbox "You may now edit the 'init' file in $DEFAULTTEXTEDITOR.
When finished editing save the file
then Click 'Restore' when ready to construct
a new initrd file at '/tmp/initrd.gz'." 0 0

find . | cpio -o -H newc | gzip -9 > /tmp/initrd.gz

rm -r /tmp/INITDIR

exit


Since the tree is at "/tmp/INITDIR", you can always edit other things as well before doing a rebuild.

s
Back to top
View user's profile Send_private_message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2376
Location: UK

PostPosted: Mon 16 May 2011, 05:43    Post_subject:  

looks nice seaside... I do actually plan to release initrd-editor with the latest Woofy, which I will upgrade at some point. I will use the right click option though, it's a good idea... Although presumably, the right click option would appear for all .gz (or cpio) files, not only initrd.gz, right?

Also, thanks for the suggestions everyone, I will add them in and update the main post when I get time... I know parts of the script are weak, I posted it here after the first write and test, before I darted to work...

I may or may not include some of goingnuts work, to detect if X is running, and if not, to run only CLI tools for $FILEMAN and such... Although as this is merely a part of Woofy, that is out of the scope of the requirements.

Of course, another way around my lil problem, which is obviously quite easy to fix, would be to negate it entirely - by opening up the init script directly in geany..

I did think about this, although I do not know if there is anything else worth editing in there... If not I would be happy to load up only the init script... So....

Is there anything else in the initrd.gz file, apart from the init script, that is worth editing?

_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search
Back to top
View user's profile Send_private_message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Mon 16 May 2011, 11:19    Post_subject:  

sc0ttman wrote:
.. Although presumably, the right click option would appear for all .gz (or cpio) files, not only initrd.gz, right?

My script has no checking, but you could add extension checking and a mime type

. If not I would be happy to load up only the init script... So....
Is there anything else in the initrd.gz file, apart from the init script, that is worth editing?

It's mostly empty directories and setup for the initial ramdisk which perhaps a developer might modify but not usually the average user- not that the average user would normally be modifying the "init" file either Smile


Regards,
s
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 2 Posts_count   Goto page: 1, 2 Next
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Advanced Topics » Additional Software (PETs, n' stuff) » Utilities
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1258s ][ Queries: 13 (0.0051s) ][ GZIP on ]