how to edit and build rpm sources ?

Hi,

I installed the source package of an rpm via “zypper -si suspend”. Now I
need to know how to correctly apply all the patches, edit the sources, and
rebuild the rpm.

I think it is via “rpmbuild”. Would it be “-bp”? Then edit the sources,
then do a “-bc”?


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

Hi
The program called quilt will do all that…

Your better of to just use the Open Build Service and osc locally.


osc bco <project> <package>

Look at the spec file to see how the patches were applied, some may be
-p1 so you would unpack the source tarball and step through each one.


patch < patchname

Else, if it’s not conflicting with existing patches, unpack the source
then rename the folder to a, do it again and rename to b. Then make all
your changes in the files of folder b, then run;


diff -Naur a b > my-new-patch.patch

In the spec file add a new path source;


PatchX:   my-new-patch.patch

Then in the %prep section add;


%patchX -p1


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 3.0.31-0.9-default
up 13:04, 2 users, load average: 0.84, 0.81, 0.81
GPU GeForce 8600 GTS Silent - Driver Version: 302.11

I think you should do a “-bi” too if you want to create the package.

cd ~/rpmbuild/SPECS
rpmbuild -bp suspend.spec
...
# edit the sources
...
rpmbuild -bc suspend.spec
rpmbuild -bi suspend.spec

But that’s not how I do it. I do this part too:

cd ~/rpmbuild/SPECS
rpmbuild -bp suspend.spec
...
# edit the sources

But then

  • I create patches for the changes I made in the source
  • copy them to ~/rpmbuild/SOURCES
  • edit ~/rpmbuid/SPECS/suspend.spec to add these patches
  • do rpmbuild -ba suspend.spec
  • send the patches upstream if they fixed bugs.

On 2012-06-20 18:36, please try again wrote:
>
> robin_listas;2470289 Wrote:
>> Hi,
>>
>> I installed the source package of an rpm via “zypper -si suspend”. Now I
>> need to know how to correctly apply all the patches, edit the sources,
>> and rebuild the rpm.
>>
>> I think it is via “rpmbuild”. Would it be “-bp”? Then edit the
>> sources, then do a “-bc”?
>>
>
> I think you should do a “-bi” too if you want to create the package.
>
>
> Code:
> --------------------
> cd ~/rpmbuild/SPECS
> rpmbuild -bp suspend.spec
> …
> # edit the sources
> …
> rpmbuild -bc suspend.spec
> rpmbuild -bi suspend.spec
> --------------------

I tried this before seeing your post. But I had to do “-bb” to make the
rpm, with “-bi” I could not find it.

> But that’s not how I do it. I do this part too:
>
>
> Code:
> --------------------
> cd ~/rpmbuild/SPECS
> rpmbuild -bp suspend.spec
> …
> # edit the sources
>
> --------------------
>
>
> But then
>
> - I create patches for the changes I made in the source
> - copy them to ~/rpmbuild/SOURCES
> - edit ~/rpmbuid/SPECS/suspend.spec to add these patches
> - do rpmbuild -ba suspend.spec
> - send the patches upstream if they fixed bugs.

Ah, I see… Well, before that I have to try understand the sources and see
if I can do what I want and how. About creating the patches I don’t know
how to do them.

When I programmed for a living years ago I used Borland C, nothing like
this. It is quite different, even if it is C.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

On 2012-06-20 17:14, malcolmlewis wrote:
>

> Hi
> The program called quilt will do all that…

I had a look. Not quite what I want…

>
> Your better of to just use the Open Build Service and osc locally.
>


> osc bco <project> <package>
> 

Huh, no, osc is out of my league.

>
> Look at the spec file to see how the patches were applied, some may be
> -p1 so you would unpack the source tarball and step through each one.

I simply want to apply all the patches that openSUSE applies in the same
maner that they do, and this the command “rpmbuild -bp” does it correctly,
so that I end with the same sources they have. I hope.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

You create a patch with the output of a unified diff.

cp sample.c sample.c.orig
# modify sample.c
diff -u sample.c.orig sample.c > sample.c.patch

Then you add the patch in the spec file:

Patch1:         somefile.patch
Patch2:         another.patch
.... 
PatchN:         sample.c.patch

and you apply it in the %prep section

%prep
%setup -q 
%patch0 -p1 
%patch1 -p1
.... 
%patchN -p1 

You might have to use -p0 or -p2 or something else, depending where the source file is located with respect to the location of the patch.
See the explanation of the -p option in man patch.

Look at my spec file for os-prober. It includes many patches.

https://build.opensuse.org/package/view_file?file=os-prober.spec&package=os-prober&project=home%3Aplease_try_again&rev=0714102c7e75d8684b3db74efda8b550

On 2012-06-20 22:56, please try again wrote:
>
> robin_listas;2470349 Wrote:
>>
>>
>> About creating the patches I don’t know how to do them.
>>
>
> You create a patch with the output of a unified diff.
>
>
> Code:
> --------------------
> cp sample.c sample.c.orig
> # modify sample.c
> diff -u sample.c.orig sample.c > sample.c.patch
> --------------------

Ah. The thing is, there are already .orig files in the source tree, and I
did not do them. Some phase of the rpm handling did them, I guess. Or it is
because I applied the patches that came in the source rpm.

>
>
> Then you add the patch in the spec file:
>
>
> Code:
> --------------------
> Patch1: somefile.patch
> Patch2: another.patch
> …
> PatchN: sample.c.patch
>
> --------------------
>
>
> and you apply it in the %prep section
>
>
>
> Code:
> --------------------
> %prep
> %setup -q
> %patch0 -p1
> %patch1 -p1
> …
> %patchN -p1
>
> --------------------
>
>
> You might have to use -p0 or -p2 or something else, depending where the
> source file is located with respect to the location of the patch.
> See the explanation of the -p option in man patch.

I see, more or less. :slight_smile:

I already added a few print messages to screen to suspend.c file and
installed the modified rpm. Who knows what will happen: crash, nothing,
success…

>
> Look at my spec file for os-prober. It includes many patches.
>
> http://tinyurl.com/79zbzon

Later, if I get to that phase. Only if I manage to get some interesting
result I’ll add that to the bugzilla; till them, I’ll keep the changes to
myself. :slight_smile:

Till I try later to hibernate, my changes could be a complete failure and
make things worse.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

When patch is executed with the option -b, it saves the original file and gives it by default the extension ‘.orig’. Depending on implementation, it might also make a backup by default. You don’t have to use ‘.orig’ for your copy of the file. Give it the extension you like. However, if the file has already been patched, it makes more sense to make your modifications and replace the original patch rather than patching the same file twice.

On 2012-06-21 01:26, please try again wrote:
>
> robin_listas;2470374 Wrote:
>>
>> Ah. The thing is, there are already .orig files in the source tree, and
>> I did not do them. Some phase of the rpm handling did them, I guess. Or
>> it is because I applied the patches that came in the source rpm.
>>
>
> When patch is executed with the option -b, it saves the original file
> and gives it by default the extension ‘.orig’. Depending on
> implementation, it might also make a backup by default. You don’t have
> to use ‘.orig’ for your copy of the file. Give it the extension you
> like. However, if the file has already been patched, it makes more
> sense to make your modifications and replace the original patch rather
> than patching the same file twice.

Anyway, my edits were replaced with the original file and not compiled.
Complete failure. Something in the rpmbuild chain is wrong.


edit
rpmbuild -bc suspend.spec
edit

and my edits have disappeared!

The man page says:


-bc    Do  the "%build" stage from the spec file (after doing
the %prep stage).  This generally involves the equiva-
lent of a "make".

The problem is that bc does the %prep stage again… I need an option that
does only the %build. Maybe “–short-circuit”


--short-circuit
Skip straight to specified stage (i.e., skip all
stages leading up to the specified stage).  Only valid  with
-bc and -bi.

…]

Ok, “-bc --short-circuit” works. “-bi --short-circuit” also works. But “-bi
–short-circuit” doesn’t, it replaces my edits, and “-bi” is needed to
build the rpm. And the “-bi” does not install the program to the
appropriate paths in the system, so I’m stuck.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

On 2012-06-21 02:03, Carlos E. R. wrote:

> Ok, “-bc --short-circuit” works. “-bi --short-circuit” also works. But “-bi
> --short-circuit” doesn’t, it replaces my edits, and “-bi” is needed to
> build the rpm. And the “-bi” does not install the program to the
> appropriate paths in the system, so I’m stuck.

errata:

Ok, “-bc --short-circuit” works. “-bi --short-circuit” also works. But “-bb
–short-circuit” doesn’t, it replaces my edits, and “-bb” is needed to
build the rpm. And the “-bi” does not install the program to the
appropriate paths in the system, so I’m stuck.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

???

My method works. It only involves -bp, creating the patches, puting them in ~/rpmbuild/SOURCES, modifying the spec file and running rpmbuild -ba (or -bb if you don’t want to package the source). If a patch already exists for a given file - as it seemed to be the case - and you add your changes to the existing patch, you don’t even need to modify the spec file.

On 2012-06-21 02:36, please try again wrote:
>
> robin_listas;2470386 Wrote:
>>
>> “-bi --short-circuit” also works. But “-bi --short-circuit” doesn’t,
>>
>
> ???

The second one is an errata, it is -bb

> robin_listas;2470386 Wrote:
>>
>> it replaces my edits, and “-bi” is needed to build the rpm. And the
>> “-bi” does not install the program to the appropriate paths in the
>> system, so I’m stuck.
>>
>
> My method works. It only involves -bp, creating the patches, puting
> them in ~/rpmbuild/SOURCES, modifying the spec file and running
> rpmbuild -ba (or -bb if you don’t want to package the source). If a
> patch already exists for a given file - as it seemed to be the case -
> and you add your changes to the existing patch, you don’t even need to
> modify the spec file.

I’m not doing any patches. I have to apply the patches that come with the
rpm, but I’m not creating any patches myself.

I first “-bp” to expand the sources with the patches that come in the rpm.

Then, in another xterm, I edit
“/usr/src/packages/BUILD/suspend-0.8.20100129/suspend.c”.

Then I compile and build the rpm, with for example, “-bb”.

I then check again the suspend.c file, and my edits have disappeared, the
original source rpm has been expanded (again!), the original patches
applied, I I lost everything. My changes are not in the code.

And this is as documented in the man page! :frowning:

I can do:


rpmbuild -bc --short-circuit suspend.spec
rpmbuild -bi --short-circuit suspend.spec

This does work, but it only creates the binaries in the source tree, it
does not install them to the system. As soon as I try to build the rpm, my
changes are destroyed.

I can not build an rpm with any modification.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

But you should. Then it will work. I’m no saying it is the only way, but this one works.

You’re missing a step. After modifying suspend.c, create a patch using diff to reflect your changes, modify the spec file so that it applies your patch and build the package with -bb. It should be possible however to recompile without extracting the source again - and loosing your changes - but I don’t know how nor why it doesn’t work.

Hmmm … Wait a minute! Try to comment out the %clean section in the spec file or any instruction which does remove the build subdirectory. It doesn’t need to be in the %clean section. Mine usually look like that:


%clean
rm -rf %{buildroot}

Hi
Probably why rpmbuild isn’t supported anymore? Post the changes your
wanting to suspend.c I’m assuming it’s 11.4 your building for?


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 3.0.31-0.9-default
up 23:31, 2 users, load average: 0.77, 0.77, 0.81
GPU GeForce 8600 GTS Silent - Driver Version: 302.11

On 2012-06-21 03:36, malcolmlewis wrote:

> Hi
> Probably why rpmbuild isn’t supported anymore? Post the changes your
> wanting to suspend.c I’m assuming it’s 11.4 your building for?

Yep.

I’m modifying this function only, some printf statement (search for the
word “step”). What I have done is copying the binary “s2disk” to place,
without building the rpm. It works, I see the strings printed to the
screen. I hibernated and restored five times, no crash - which is bad, I
wanted it to crash to find where it does. If it does beyond the step 1.5
then I don’t know where else to print.

It is ugly code, it has GOTOS! One of them and I would have failed my
exams… times most have “achanged”.


> int suspend_system(int snapshot_fd, int resume_fd, int test_fd)
> {
>         loff_t avail_swap;
>         unsigned long image_size;
>         int attempts, in_suspend, error = 0;
>         char message[SPLASH_GENERIC_MESSAGE_SIZE];
>
>         avail_swap = check_free_swap(snapshot_fd);
>         if (avail_swap > pref_image_size)
>                 image_size = pref_image_size;
>         else
>                 image_size = avail_swap;
>         if (!avail_swap) {
>                 suspend_error("Not enough swap space for suspend");
>                 return ENOSPC;
>         }
>
>         error = freeze(snapshot_fd);
>
>         /* This a hack for a bug in bootsplash. Apparently it will
>          * drop to 'verbose mode' after the freeze() call.
>          */
>         splash.switch_to();
>         splash.progress(15);
>
>         if (error) {
>                 suspend_error("Freeze failed.");
>                 goto Unfreeze;
>         }
>
>         if (test_fd >= 0) {
>                 printf("%s: Running in test mode
", my_name);
>                 error = write_image(snapshot_fd, resume_fd, test_fd);
>                 if (error)
>                         error = -error;
>                 reset_signature(resume_fd);
>                 free_swap_pages(snapshot_fd);
>                 goto Unfreeze;
>         }
>
>         if (shutdown_method == SHUTDOWN_METHOD_PLATFORM) {
>                 if (platform_prepare(snapshot_fd)) {
>                         suspend_error("Unable to use platform hibernation "
>                                         "support, using shutdown mode.");
>                         shutdown_method = SHUTDOWN_METHOD_SHUTDOWN;
>                 }
>         }
>
>         sprintf(message, "Snapshotting system - step 1.0");
>         printf("%s: %s
", my_name, message);
>         splash.set_caption(message);
>         attempts = 2;
>         do {
>                 sprintf(message, "Snapshotting system - step 1.1");
>                 printf("%s: %s
", my_name, message);
>                 if (set_image_size(snapshot_fd, image_size)) {
>                         error = errno;
>                         break;
>                 }
>                 sprintf(message, "Snapshotting system - step 1.2");
>                 printf("%s: %s
", my_name, message);
>                 if (atomic_snapshot(snapshot_fd, &in_suspend)) {
>                         error = errno;
>                         break;
>                 }
>                 sprintf(message, "Snapshotting system - step 1.3");
>                 printf("%s: %s
", my_name, message);
>                 if (!in_suspend) {
>                         /* first unblank the console, see console_codes(4) */
>                         printf("\e[13]");
>                         printf("%s: returned to userspace
", my_name);
>                         free_snapshot(snapshot_fd);
>                         break;
>                 }
>
>                 sprintf(message, "Snapshotting system - step 1.4");
>                 printf("%s: %s
", my_name, message);
>                 error = write_image(snapshot_fd, resume_fd, -1);
>                 if (error) {
>                         free_swap_pages(snapshot_fd);
>                         free_snapshot(snapshot_fd);
>                         image_size = 0;
>                         error = -error;
>                         if (error != ENOSPC)
>                                 break;
>                 } else {
>                         splash.progress(100);
> #ifdef CONFIG_BOTH
>                         if (s2ram) {
>                                 /* If we die (and allow system to continue)
>                                  * between now and reset_signature(), very bad
>                                  * things will happen. */
>                                 error = suspend_to_ram(snapshot_fd);
>                                 if (error)
>                                         goto Shutdown;
>                                 reset_signature(resume_fd);
>                                 free_swap_pages(snapshot_fd);
>                                 free_snapshot(snapshot_fd);
>                                 goto Unfreeze;
>                         }
> Shutdown:
> #endif
>                         close(resume_fd);
>                         suspend_shutdown(snapshot_fd);
>                 }
>                 sprintf(message, "Snapshotting system - step 1.5");
>                 printf("%s: %s
", my_name, message);
>
>         } while (--attempts);
>
>         /* We get here during the resume or when we failed to suspend.
>          * Remember, suspend_shutdown() never returns!
>          */
>         if (shutdown_method == SHUTDOWN_METHOD_PLATFORM)
>                 platform_finish(snapshot_fd);
>
> Unfreeze:
>         unfreeze(snapshot_fd);
>         /* ugly hack, because "user aborted suspend" is not really
>          * an error.
>          */
>         if (error == EINTR)
>                 return 0;
>         return error;
> }


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

On 2012-06-21 03:36, please try again wrote:
>
> robin_listas;2470391 Wrote:
>>
>>
>> I’m not doing any patches. I have to apply the patches that come with the
>> rpm, but I’m not creating any patches myself.
>>
>
> But you should. Then it will work. I’m no saying it is the only way,
> but this one works.

Ugh. :frowning:

>
> robin_listas;2470391 Wrote:
>>
>> I first “-bp” to expand the sources with the patches that come in the
>> rpm.
>>
>> Then, in another xterm, I edit
>> “/usr/src/packages/BUILD/suspend-0.8.20100129/suspend.c”.
>>
>> Then I compile and build the rpm, with for example, “-bb”.
>
> You’re missing a step. After modifying suspend.c, create a patch using
> diff to reflect your changes, modify the spec file so that it applies
> your patch and build the package with -bb. It should be possible however
> to recompile without extracting the source again - and loosing your
> changes - but I don’t know how nor why it doesn’t work.

This works because the sources are extracted, they replace your code, then
you patch is applied, and your code is restored. This is ugly.

I’ll have to script it. :frowning:

> Hmmm … Wait a minute! Try to comment out the %clean section in the
> spec file or any instruction which does remove the build subdirectory.
> It doesn’t need to be in the %clean section. Mine usually look like
> that:
>
>
> Code:
> --------------------
>
> %clean
> rm -rf %{buildroot}
> --------------------

No, it is the %prep stage. The manual says so :frowning:

I tried what you say, my code is replaced again. But this time I made a copy…


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

Hi
This look ok?
http://paste.opensuse.org/22736385


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 3.0.31-0.9-default
up 1 day 0:06, 2 users, load average: 1.02, 1.02, 0.96
GPU GeForce 8600 GTS Silent - Driver Version: 302.11

Hi
This look ok?
http://paste.opensuse.org/22736385

[/QUOTE]
FYI, all patched and built using osc… do you want me to put the
package up on OBS?


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 3.0.31-0.9-default
up 1 day 0:16, 2 users, load average: 0.99, 0.99, 0.96
GPU GeForce 8600 GTS Silent - Driver Version: 302.11

On 2012-06-21 04:10, malcolmlewis wrote:

> Hi
> This look ok?
> http://paste.opensuse.org/22736385

Ah, a patch? Thanks, I’ll look at it tomorrow. :slight_smile:
I’ll have to learn how to automate that. It is past bedtime here (4:19) and
tomorrow I get up early.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

On 2012-06-21 04:21, malcolmlewis wrote:
> FYI, all patched and built using osc… do you want me to put the
> package up on OBS?

Thanks, but no need :slight_smile:

I need controlling the changes to the binary, add other printf statements
in another section once I find where it crashes. Traditional debugging
where no debugger is possible. And so far, it is not crashing! It appears
to be a case of the observation altering the experiment :frowning:

I managed to do it without building the rpm. I can do a brutal “make
install” if I need :wink:

Meanwhile, I’ll try to learn to create the patches locally. I’m sure that
rpmbuild works, it must be that I’m not doing it right. What happens to me
matches the documentation, so surely there must be another method.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)