Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Small source code modification
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
etnull
Apprentice
Apprentice


Joined: 26 Mar 2019
Posts: 154

PostPosted: Thu Apr 11, 2019 1:56 pm    Post subject: [SOLVED] Small source code modification Reply with quote

I want to change a couple of lines in grub's source code. It's a visual change: Deletion of the "GRUB Loading.. Welcome to GRUB!" message. Since I'm a n00b I want to do it in the most simplest way possible. All information that I've read on the topic is primarily focused on patches, how to apply a patch and how to work with ebuilds, but I'm not working with git, and don't really have a patch or metadata for it, or not writing my version/overlay of a package. I want to modify the original sources, and don't really care if it will be gone on the next package update (grub updates rarely anyway)
I did emerge --fetchonly grub, to get the sources, now I can extract and edit it, but how do I install it after that? Will portage still check for the new official versions after it?


Last edited by etnull on Fri Apr 12, 2019 12:19 pm; edited 1 time in total
Back to top
View user's profile Send private message
mike155
Veteran
Veteran


Joined: 17 Sep 2010
Posts: 1444
Location: Frankfurt, Germany

PostPosted: Thu Apr 11, 2019 4:44 pm    Post subject: Reply with quote

Quote:
I want to modify the original sources, and don't really care if it will be gone on the next package update

That's easy:
  1. Run 'emerge grub'

  2. Emerge will first unpack and prepare sources to '/var/tmp/portage/sys-boot/grub-2.02-r3/'

  3. As soon as the message '>>> Source prepared' appears: press Ctrl-Z, which will stop the emerge job

  4. Chdir to '/var/tmp/portage/sys-boot/grub-2.02-r3/work/grub-2.02' and modify the source code

  5. Chdir to root's home directory and enter '%1' to continue the emerge job

  6. That's all
Back to top
View user's profile Send private message
etnull
Apprentice
Apprentice


Joined: 26 Mar 2019
Posts: 154

PostPosted: Thu Apr 11, 2019 5:05 pm    Post subject: Reply with quote

mike155 wrote:

Chdir to root's home directory and enter '%1' to continue the emerge job

It gives me
Code:
-bash: fg %1 no such job

I tried multiple times, because stopping it in this way is not reliable, sometimes it reaches the patching and configuration stage.
emerge --resume Also didn't work, it starts emerging from the beginning


Last edited by etnull on Thu Apr 11, 2019 5:06 pm; edited 1 time in total
Back to top
View user's profile Send private message
Jaglover
Watchman
Watchman


Joined: 29 May 2005
Posts: 7191
Location: Saint Amant, Acadiana

PostPosted: Thu Apr 11, 2019 5:06 pm    Post subject: Reply with quote

Easiest would be creating a diff and using portage custom patching feature.
Second easiest would be to use ebuild command to unpack and merge. See 'man ebuild' for details.
_________________
Please learn how to denote units correctly!
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 43546
Location: 56N 3W

PostPosted: Thu Apr 11, 2019 7:41 pm    Post subject: Reply with quote

etnull,

I'll point you in the right direction but stop short of giving you all the commands

The grub-2.02-r3.ebuild is EAPI="6". This means it runs epatch_user for you to apply any patches that you put in the right place in /etc/portage/patches.

The ebuild command, (not emerge) can run the phase functions one at a time or sequentially to a given phase.
e.g.
Code:
ebuild /full/path/to/grub-2.02-r3.ebuild unpack
will run all the phases up to and including unpack.

Unpack the code, make copies oy the fles you want to change, I usually add _org to the existing filename.
Edit one copy to introduce your changes.
Use the diff command to make your patch.

Put the patch in the right place in /etc/portage/patches.
emerge grub. Portage will apply your patch.

A single patch file can apply 'hunks to lots of files. diff has a recursive mode, so you can generate a single diff to an entire directory tree.
If you need that, you need an _org ot the tree you want to patch but the idea is the same.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
mike155
Veteran
Veteran


Joined: 17 Sep 2010
Posts: 1444
Location: Frankfurt, Germany

PostPosted: Thu Apr 11, 2019 9:33 pm    Post subject: Reply with quote

mike155 wrote:
3) As soon as the message '>>> Source prepared' appears: press Ctrl-Z, which will stop the emerge job
...
5) Chdir to root's home directory and enter '%1' to continue the emerge job

etnull wrote:
It gives me"-bash: fg %1 no such job"

Really? That's basic job control, provided by bash. It should work. Look at 'man bash', section 'JOB CONTROL'.
man bash wrote:
JOB CONTROL

Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point.

...

I agree that the methods pointed out by NeddySeagoon and Jaglover are more reliable and preferable. I showed you what I do if I want a quick solution. Later, if I'm pleased with the result, I usually create a patch and store it to /etc/portage/patches to make sure that the change will be applied every time I emerge that package. But in order to develop or test small changes, I frequently use the Ctrl-Z method.
Back to top
View user's profile Send private message
Tony0945
Advocate
Advocate


Joined: 25 Jul 2006
Posts: 3148
Location: Illinois, USA

PostPosted: Thu Apr 11, 2019 9:49 pm    Post subject: Reply with quote

I'll give you one more hint because I had to search a lot for it. (I'm not good at google)
Creating a patch using diff
Yes, it works on directories too.
Back to top
View user's profile Send private message
etnull
Apprentice
Apprentice


Joined: 26 Mar 2019
Posts: 154

PostPosted: Fri Apr 12, 2019 12:58 am    Post subject: Reply with quote

Thank you everyone! I'm trying to go with the patch route.
But when I try to change even a single letter in the string I can't emerge it with the patch, the log:
Code:
cat /var/tmp/portage/sys-boot/grub-2.02-r3/temp/build.log
 * Package:    sys-boot/grub-2.02-r3
 * Repository: gentoo
 * Maintainer: floppym@gentoo.org base-system@gentoo.org
 * USE:        abi_x86_64 amd64 device-mapper elibc_glibc fonts grub_platforms_efi-64 grub_platforms_pc kernel_linux mount nls themes userland_GNU
 * FEATURES:   network-sandbox preserve-libs sandbox userpriv usersandbox
>>> Unpacking source...
>>> Unpacking grub-2.02.tar.xz to /var/tmp/portage/sys-boot/grub-2.02-r3/work
>>> Unpacking unifont-9.0.06.pcf.gz to /var/tmp/portage/sys-boot/grub-2.02-r3/work
>>> Unpacking dejavu-sans-ttf-2.37.zip to /var/tmp/portage/sys-boot/grub-2.02-r3/work
>>> Source unpacked in /var/tmp/portage/sys-boot/grub-2.02-r3/work
>>> Preparing source in /var/tmp/portage/sys-boot/grub-2.02-r3/work/grub-2.02 ...
 * Applying gfxpayload.patch ...                                         [ ok ]
 * Applying grub-2.02_beta2-KERNEL_GLOBS.patch ...                       [ ok ]
 * Applying 2.02-multiple-early-initrd.patch ...                         [ ok ]
 * Applying 2.02-freetype-capitalise-variables.patch ...                 [ ok ]
 * Applying 2.02-freetype-pkg-config.patch ...                           [ ok ]
 * Applying 2.02-xfs-sparse-inodes.patch ...                             [ ok ]
 * Applying 2.02-X86_64_PLT32.patch ...                                  [ ok ]
 * Applying 2.02-gcc8.patch ...                                          [ ok ]
 * Applying grub-silent.patch ...
The text leading up to this was:
--------------------------
|--- geli-orig.c   2019-04-12 02:36:57.394008596 +0300
|+++ geli.c   2019-04-12 02:37:07.760326102 +0300
--------------------------
No file to patch.  Skipping patch.
1 out of 1 hunk ignored                                                  [ !! ]
 * ERROR: sys-boot/grub-2.02-r3::gentoo failed (prepare phase):
 *   patch -p1  failed with /etc/portage/patches/sys-boot/grub/grub-silent.patch
 *
 * Call stack:
 *               ebuild.sh, line  124:  Called src_prepare
 *             environment, line 3989:  Called default
 *      phase-functions.sh, line  868:  Called default_src_prepare
 *      phase-functions.sh, line  933:  Called __eapi6_src_prepare
 *             environment, line  319:  Called eapply_user
 *             environment, line 1208:  Called eapply '/etc/portage/patches/sys-boot/grub/grub-silent.patch'
 *             environment, line 1178:  Called _eapply_patch '/etc/portage/patches/sys-boot/grub/grub-silent.patch'
 *             environment, line 1116:  Called __helpers_die 'patch -p1  failed with /etc/portage/patches/sys-boot/grub/grub-silent.patch'
 *   isolated-functions.sh, line  119:  Called die
 * The specific snippet of code:
 *         die "$@"
 *
 * If you need support, post the output of `emerge --info '=sys-boot/grub-2.02-r3::gentoo'`,
 * the complete build log and the output of `emerge -pqv '=sys-boot/grub-2.02-r3::gentoo'`.
 * The complete build log is located at '/var/tmp/portage/sys-boot/grub-2.02-r3/temp/build.log'.
 * The ebuild environment file is located at '/var/tmp/portage/sys-boot/grub-2.02-r3/temp/environment'.
 * Working directory: '/var/tmp/portage/sys-boot/grub-2.02-r3/work/grub-2.02'
 * S: '/var/tmp/portage/sys-boot/grub-2.02-r3/work/grub-2.02'

Patch itself:
Code:
cat /etc/portage/patches/sys-boot/grub/grub-silent.patch
--- geli-orig.c   2019-04-12 02:36:57.394008596 +0300
+++ geli.c   2019-04-12 02:37:07.760326102 +0300
@@ -438,7 +438,7 @@
   tmp = NULL;
   if (source->partition)
     tmp = grub_partition_get_name (source->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+  grub_printf_ (N_("1Enter passphrase for %s%s%s (%s): "), source->name,
       source->partition ? "," : "", tmp ? : "",
       dev->uuid);
   grub_free (tmp);

What I did:
Getting sources
cd /tmp
DISTDIR=$PWD emerge --fetchonly grub
Extract
tar xvf grub[TAB]
Copy, Edit, diff
cd grub[TAB]
file edits (geli.c)
diff -Naur geli.c geli-orig.c > grub-silent.patch
Making patches directory
mkdir -p /etc/portage/patches/sys-boot/grub/
Copying patch
cp grub-silent.patch /etc/portage/patches/sys-boot/grub/
emerge grub
Back to top
View user's profile Send private message
etnull
Apprentice
Apprentice


Joined: 26 Mar 2019
Posts: 154

PostPosted: Fri Apr 12, 2019 1:15 am    Post subject: Reply with quote

mike155 wrote:
Really? That's basic job control, provided by bash. It should work. Look at 'man bash', section 'JOB CONTROL'.

Sorry I didn't pay attention to the Z in the shortcut.. Skimmed over it and wrongly assumed it's Ctrl-C.
I think it's better for me to learn how to do it with the patches for now, but thanks for the tip, I will try it after I've done with the other way.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 14056

PostPosted: Fri Apr 12, 2019 1:51 am    Post subject: Reply with quote

etnull wrote:
But when I try to change even a single letter in the string I can't emerge it with the patch, the log:
Your patch is formatted incorrectly for how Portage expects to apply it.
etnull wrote:
Code:
The text leading up to this was:
--------------------------
|--- geli-orig.c   2019-04-12 02:36:57.394008596 +0300
|+++ geli.c   2019-04-12 02:37:07.760326102 +0300
--------------------------
No file to patch.  Skipping patch.
1 out of 1 hunk ignored                                                  [ !! ]
 * ERROR: sys-boot/grub-2.02-r3::gentoo failed (prepare phase):
 *   patch -p1  failed with /etc/portage/patches/sys-boot/grub/grub-silent.patch
According to the documentation, patch -p1 strips one level of directory structure from the filenames in the patch. You provided zero levels. This is unlikely to work. Prepare your patch with one level of leading structure. Git uses a/ and b/ for the before/after directories, and this has become popular in hand-made patches too. You may edit your patch file in place if you do not wish to remake it.

May I ask why you want to make grub produce less output? If I recall correctly, the messages you said you wish to delete can be useful to measure how far the bootloader has executed, which can be helpful if it is failing early on and you need to know where.


Last edited by Hu on Fri Apr 12, 2019 1:53 am; edited 1 time in total
Back to top
View user's profile Send private message
mike155
Veteran
Veteran


Joined: 17 Sep 2010
Posts: 1444
Location: Frankfurt, Germany

PostPosted: Fri Apr 12, 2019 1:52 am    Post subject: Reply with quote

Code:
--- geli-orig.c   2019-04-12 02:36:57.394008596 +0300
+++ geli.c   2019-04-12 02:37:07.760326102 +0300
@@ -438,7 +438,7 @@
   tmp = NULL;
   if (source->partition)
     tmp = grub_partition_get_name (source->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+  grub_printf_ (N_("1Enter passphrase for %s%s%s (%s): "), source->name,
       source->partition ? "," : "", tmp ? : "",
       dev->uuid);
   grub_free (tmp);

File names in your patch should be relative to WORKDIR (in this case: /var/tmp/portage/sys-boot/grub-2.02-r3/work). Change the first two lines to:
Code:
--- grub-2.02/grub-core/disk/geli-orig.c   2019-04-12 02:36:57.394008596 +0300
+++ grub-2.02/grub-core/disk/geli.c   2019-04-12 02:37:07.760326102 +0300

EDIT: I just saw Hu's post. If you like the a/, b/ notation, change the first two lines to
Code:
--- a/grub-core/disk/geli.c   2019-04-12 02:36:57.394008596 +0300
+++ b/grub-core/disk/geli.c   2019-04-12 02:37:07.760326102 +0300

This will work as well, because the leading directory will be ignored.
Back to top
View user's profile Send private message
etnull
Apprentice
Apprentice


Joined: 26 Mar 2019
Posts: 154

PostPosted: Fri Apr 12, 2019 12:19 pm    Post subject: Reply with quote

Thanks all! So I assume now I can backup or git my /etc/portage/patches/ and have it applied even when package is updated. But what if the upstream introduce a change which affects my patch, what would happen then? Will it just give me error during emerge?
Hu wrote:
May I ask why you want to make grub produce less output? If I recall correctly, the messages you said you wish to delete can be useful to measure how far the bootloader has executed, which can be helpful if it is failing early on and you need to know where.

I want my grub to be totally silent, or output something like Operating System not found.. while still accepting the passphrase, it's for plausible deniability against inexperienced users. Grub not really outputting that much prior of /boot unencryption, it can either work, or drop you to a rescue shell.
Though I'm not sure if I can patch it this way, poking around the source code tells me that some of the text is dynamic and can't be removed just by zeroing the string, but that's another story, the topic itself is now resolved.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 43546
Location: 56N 3W

PostPosted: Fri Apr 12, 2019 6:40 pm    Post subject: Reply with quote

etnull,

patch works by knowing where in the file the hunk should apply and what the context is around th. patch location.

Code:
@@ -438,7 +438,7 @@
Tells where and what the patch does to the line count.
Then you nave three lines of context before the change,
the change itself
finally, three lines of context after the change.

Provided the context and lines to be removed are located, patch will only emit a Hunk X applied at offset <lines>.
That makes patches resilient to changes elsewhere in the file.

When upstream change the code that your patch refers to, it won't apply any more.

This can make the order in which patches are applied important. e.g. when a later patch changes an earlier patch.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 14056

PostPosted: Sat Apr 13, 2019 12:55 am    Post subject: Reply with quote

etnull wrote:
But what if the upstream introduce a change which affects my patch, what would happen then? Will it just give me error during emerge?
Yes. Your first attempt failed because you specified the file in a way that patch failed to find it. If upstream introduces a text-wise conflicting change, patch will fail to modify the file, then exit with an error code. You will get output similar to what you posted above, but patch should instead tell you which file it failed to patch and which hunk was not applicable. If you want to demonstrate this, edit your patch by hand to transpose two characters in a removal line, so that the context no longer matches.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum