1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
|
/*
* cowbuilder / pbuilder with cowdancer
* Copyright (C) 2005-2009 Junichi Uekawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/*
sudo ./cowbuilder --create --distribution sid --basepath /mnt/120g-1/dancer/cow/cow
sudo ./cowbuilder --update --basepath /mnt/120g-1/dancer/cow/cow
sudo mkdir /mnt/120g-1/dancer/cow/cow
cd /mnt/120g-1/dancer/cow/cow
sudo tar xfzp /var/cache/pbuilder/base.tgz
sudo chroot . dpkg -i tmp/cowdancer_0.3_i386.deb
sudo ./cowbuilder.c --build --basepath /mnt/120g-1/dancer/cow/cow $FILENAME
i.e. requires cowdancer inside the chroot.
How to find out changed times of chroot:
$ sudo find /var/cache/pbuilder/build/cow -mtime -1 -ls
will show the list of files changed.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include "cowbuilder_util.h"
#include "ilist.h"
#include "parameter.h"
const char *ilist_PRGNAME = "cowbuilder";
/**
remove a directory.
be careful about bind-mounted directories.
@return 0 on success
*/
static int rmrf(const char *path) {
return forkexeclp("rm", "rm", "-rf", path, NULL);
}
/**
Remove a directory, but only if nothing is mounted under it.
@return 0 on success
*/
static int rmrf_check_mount(const char *path) {
/*
* A directory bind-mounted into path will be cleaned out by rmrf().
* To avoid that potential disaster we want to detect if there's
* something mounted under path before deleting it.
*/
char *canonical_path = strdupa(path);
canonicalize_doubleslash(path, canonical_path);
if (check_mountpoint(canonical_path)) {
return 1;
}
return rmrf(path);
}
/**
* @return .ilist path as malloc()ed string, or NULL on error.
*/
static char *get_ilistfile_path(const struct pbuilderconfig *pc) {
char *ilistfile;
if (0 > asprintf(&ilistfile, "%s/.ilist", pc->buildplace)) {
/* outofmemory */
log_printf(log_error, "cowdancer: out of memory.");
return NULL;
}
return ilistfile;
}
/**
break hardlink.
return 0 on success, 1 on fail.
*/
int break_cowlink(const char *s) {
char *backup_file;
struct stat buf;
int pid;
int status;
if (lstat(s, &buf)) {
/* if stat fails, the file probably
* doesn't exist; return, success */
return 1;
}
if (S_ISLNK(buf.st_mode)) {
return 1; /* ignore symbolic link */
}
if ((buf.st_nlink > 1) && /* it is hardlinked */
S_ISREG(buf.st_mode)) {
if (asprintf(&backup_file, "%sXXXXXX", s) == -1) {
ilist_outofmemory("out of memory in break_cowlink");
return 1;
}
switch (pid = fork()) {
case 0:
/* child process, run cp */
putenv("COWDANCER_IGNORE=yes");
execl("/bin/cp", "/bin/cp", "-a", s, backup_file, NULL);
log_perror("execl:cp:");
exit(EXIT_FAILURE);
case -1:
/* error condition in fork(); something is really wrong */
ilist_outofmemory("out of memory in check_inode_and_copy, 2");
break;
default:
/* parent process, waiting for cp -a to terminate */
if (-1 == waitpid(pid, &status, 0)) {
log_perror("waitpid:cp");
log_printf(
log_error,
"%s: unexpected waitpid error when waiting for process %i with status %x",
ilist_PRGNAME,
pid,
status);
goto error_spm;
}
if (!WIFEXITED(status)) {
/* something unexpected */
log_printf(log_error,
"%s: unexpected WIFEXITED status in waitpid: %x",
ilist_PRGNAME,
(int)status);
goto error_spm;
} else if (WEXITSTATUS(status)) {
/* cp -a failed */
log_printf(log_error,
"%s: cp -a failed for %s",
ilist_PRGNAME,
backup_file);
goto error_spm;
} else if (-1 == rename(backup_file, s)) {
/* when cp -a succeeded, overwrite the target file from the temporary file with rename */
log_perror("file overwrite with rename");
log_printf(log_error,
"while trying rename of %s to %s",
backup_file,
s);
goto error_spm;
}
}
}
return 0;
error_spm:
free(backup_file);
return 1;
}
/**
Convenience function to call break_cowlink with basepath prepended.
*/
static void break_cowlink_inside_chroot(const char *basepath,
const char *filepath) {
char *filename;
asprintf(&filename, "%s%s", basepath, filepath);
break_cowlink(filename);
free(filename);
}
/**
hardlink-copy directory for COW use. Un-hardlink some files which
do not work with hardlinking.
@return 0 on success, 1 on failure.
*/
static int cpbuilder_internal_cowcopy(const struct pbuilderconfig *pc) {
char *ilistfile;
log_printf(log_info, "Copying COW directory");
if (0 != rmrf_check_mount(pc->buildplace)) {
return 1;
}
if (0 !=
forkexeclp("cp", "cp", "-al", pc->basepath, pc->buildplace, NULL)) {
return 1;
}
/* ilist file handling.
Delete existing ilist file if it exists, because I use COWDANCER_REUSE */
if (!(ilistfile = get_ilistfile_path(pc))) {
/* outofmemory */
log_printf(log_error, "cowdancer: out of memory.");
return 1;
}
if (unlink(ilistfile)) {
/* if there was no ilist file in the beginning, that's not a
problem. */
log_printf(log_info,
"unlink for ilistfile %s failed, it didn't exist?",
ilistfile);
} else {
log_printf(log_info, "removed stale ilistfile %s", ilistfile);
}
free(ilistfile);
/* Non-hardlinkable file support */
break_cowlink_inside_chroot(pc->buildplace,
"/var/cache/debconf/config.dat");
break_cowlink_inside_chroot(pc->buildplace,
"/var/cache/debconf/templates.dat");
break_cowlink_inside_chroot(pc->buildplace,
"/var/cache/debconf/passwords.dat");
return 0;
}
/*
@return 0 on success, 1 on failure.
*/
static int cpbuilder_internal_cleancow(const struct pbuilderconfig *pc) {
log_printf(log_info, "Cleaning COW directory");
if (!pc->debug && 0 != rmrf_check_mount(pc->buildplace)) {
return 1;
}
return 0;
}
int cpbuilder_check_config(const struct pbuilderconfig *pc) {
return 0;
}
/*
* @return: return code of pbuilder, or <0 on failure
*/
int cpbuilder_build(const struct pbuilderconfig *pc, const char *dscfile_) {
const char *dscfile = canonicalize_file_name(dscfile_);
char *ilistfile;
char *buf_chroot = NULL;
int ret;
char *prevdir;
if (!dscfile) {
log_printf(log_error, "File not found: %s", dscfile_);
return 1;
}
if (cpbuilder_internal_cowcopy(pc)) {
log_printf(log_error, "Failed cowcopy.");
return 1;
}
if (0 > asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace)) {
/* outofmemory */
log_printf(log_error, "cowdancer: out of memory.");
return -1;
}
if (!(ilistfile = get_ilistfile_path(pc))) {
/* outofmemory */
log_printf(log_error, "cowdancer: out of memory.");
return 1;
}
prevdir = get_current_dir_name();
chdir(pc->buildplace);
if (forkexeclp(
"chroot",
"chroot",
pc->buildplace,
"cowdancer-ilistcreate",
"/.ilist",
"find . -xdev -path ./home -prune -o \\( \\( -type l -o -type f \\) -a -links +1 -print0 \\) | xargs -0 stat --format '%d %i '",
NULL)) {
/* if there was an error, back off to manual form */
/* but first, try etch-workaround */
if (pc->debian_etch_workaround) {
log_printf(
log_warn,
"Trying my backup method for working with Debian Etch chroots.");
if (forkexeclp("chroot",
"chroot",
pc->buildplace,
"cow-shell",
"/bin/true",
NULL)) {
/* I failed, what can I do? noooo */
log_printf(
log_error,
"failed running Debian Etch compatibility backup plan, I'm in panic, eek.");
return 1;
}
} else {
/*
* try the normal backup method
* It probably doesn't work but try it anyway.
*/
log_printf(
log_warn,
"cowdancer-ilistcreate failed to run within chroot, falling back to old method");
ilistcreate(
ilistfile,
"find . -xdev -path ./home -prune -o \\( \\( -type l -o -type f \\) -a -links +1 -print0 \\) | xargs -0 stat --format '%d %i '");
}
}
chdir(prevdir);
free(prevdir);
log_printf(log_info, "Invoking pbuilder");
pbuildercommandline[1] = "build";
PBUILDER_ADD_PARAM("--buildplace");
PBUILDER_ADD_PARAM(pc->buildplace);
if (pc->buildresult) {
PBUILDER_ADD_PARAM("--buildresult");
PBUILDER_ADD_PARAM(pc->buildresult);
}
if (pc->mirror) {
PBUILDER_ADD_PARAM("--mirror");
PBUILDER_ADD_PARAM(pc->mirror);
}
if (pc->arch) {
PBUILDER_ADD_PARAM("--architecture");
PBUILDER_ADD_PARAM(pc->arch);
}
PBUILDER_ADD_PARAM("--distribution");
PBUILDER_ADD_PARAM(pc->distribution);
if (pc->extrapackages) {
PBUILDER_ADD_PARAM("--extrapackages");
PBUILDER_ADD_PARAM(pc->extrapackages);
}
if (pc->binary_arch) {
PBUILDER_ADD_PARAM("--binary-arch");
} else if (pc->binary_indep) {
PBUILDER_ADD_PARAM("--binary-indep");
}
PBUILDER_ADD_PARAM("--no-targz");
PBUILDER_ADD_PARAM("--internal-chrootexec");
PBUILDER_ADD_PARAM(buf_chroot);
PBUILDER_ADD_PARAM(strdup(dscfile));
PBUILDER_ADD_PARAM(NULL);
ret = forkexecvp(pbuildercommandline);
free(buf_chroot);
if (ret < 128) {
cpbuilder_internal_cleancow(pc);
} else {
log_printf(log_warn, "pbuilder build aborted, not cleaning");
}
free(ilistfile);
return ret;
}
int cpbuilder_create(const struct pbuilderconfig *pc) {
int ret;
mkdir(pc->basepath, 0777);
log_printf(log_info, "Invoking pbuilder");
pbuildercommandline[1] = "create";
PBUILDER_ADD_PARAM("--buildplace");
PBUILDER_ADD_PARAM(pc->basepath);
if (pc->mirror) {
PBUILDER_ADD_PARAM("--mirror");
PBUILDER_ADD_PARAM(pc->mirror);
}
if (pc->arch) {
PBUILDER_ADD_PARAM("--architecture");
PBUILDER_ADD_PARAM(pc->arch);
}
PBUILDER_ADD_PARAM("--distribution");
PBUILDER_ADD_PARAM(pc->distribution);
PBUILDER_ADD_PARAM("--no-targz");
PBUILDER_ADD_PARAM("--extrapackages");
if (pc->extrapackages) {
char *extrapackages;
if (0 > asprintf(&extrapackages, "%s cowdancer", pc->extrapackages)) {
log_printf(log_error, "cowdancer: out of memory.");
ret = 1;
goto out;
}
PBUILDER_ADD_PARAM(extrapackages);
} else {
PBUILDER_ADD_PARAM("cowdancer");
}
PBUILDER_ADD_PARAM(NULL);
ret = forkexecvp(pbuildercommandline);
out:
if (ret) {
log_printf(log_error, "pbuilder create failed");
if (!pc->debug && 0 != rmrf_check_mount(pc->basepath)) {
log_printf(log_error, "Could not remove original tree");
}
}
return ret;
}
/*
mv basepath basepath-
mv buildplace basepath
rm -rf basepath-
return 0 on success, nonzero on error.
*/
int cpbuilder_internal_saveupdate(const struct pbuilderconfig *pc) {
int ret = 0;
char *buf_tmpfile = NULL;
if (0 >
asprintf(&buf_tmpfile, "%s-%i-tmp", pc->buildplace, (int)getpid())) {
/* outofmemory */
return -1;
}
log_printf(
log_info,
"Moving work directory [%s] to final location [%s] and cleaning up old copy",
pc->buildplace,
pc->basepath);
if (-1 == rename(pc->basepath, buf_tmpfile)) {
log_perror("rename");
ret = -1;
goto out;
}
if (-1 == rename(pc->buildplace, pc->basepath)) {
log_perror("rename");
ret = -1;
goto out;
}
if (0 != rmrf_check_mount(buf_tmpfile)) {
log_printf(log_error, "Could not remove original tree");
ret = -1;
goto out;
}
out:
free(buf_tmpfile);
return ret;
}
int cpbuilder_login(const struct pbuilderconfig *pc) {
char *buf_chroot;
int ret;
if (cpbuilder_internal_cowcopy(pc)) {
log_printf(log_error, "Failed cowcopy.");
return 1;
}
/* this is the option passed to internal-chrootexec */
if (0 > asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace)) {
/* outofmemory */
return -1;
}
log_printf(log_info, "Invoking pbuilder");
pbuildercommandline[1] = "login";
PBUILDER_ADD_PARAM("--buildplace");
PBUILDER_ADD_PARAM(pc->buildplace);
if (pc->mirror) {
PBUILDER_ADD_PARAM("--mirror");
PBUILDER_ADD_PARAM(pc->mirror);
}
PBUILDER_ADD_PARAM("--distribution");
PBUILDER_ADD_PARAM(pc->distribution);
PBUILDER_ADD_PARAM("--no-targz");
PBUILDER_ADD_PARAM("--internal-chrootexec");
PBUILDER_ADD_PARAM(buf_chroot);
PBUILDER_ADD_PARAM(NULL);
ret = forkexecvp(pbuildercommandline);
free(buf_chroot);
if (ret < 128) {
if (pc->save_after_login) {
if (0 != forkexeclp("chroot",
"chroot",
pc->buildplace,
"apt-get",
"clean",
NULL)) {
ret = -1;
}
if (cpbuilder_internal_saveupdate(pc)) {
ret = -1;
}
} else {
cpbuilder_internal_cleancow(pc);
}
} else {
log_printf(log_warn, "pbuilder login aborted, not cleaning");
}
return ret;
}
/*
Mostly a copy of pbuilder login, executes a script.
*/
int cpbuilder_execute(const struct pbuilderconfig *pc, char **av) {
char *buf_chroot;
int ret;
int i = 0;
if (cpbuilder_internal_cowcopy(pc)) {
log_printf(log_error, "Failed cowcopy.");
return 1;
}
if (0 > asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace)) {
/* outofmemory */
return -1;
}
log_printf(log_info, "Invoking pbuilder");
pbuildercommandline[1] = "execute";
PBUILDER_ADD_PARAM("--buildplace");
PBUILDER_ADD_PARAM(pc->buildplace);
if (pc->mirror) {
PBUILDER_ADD_PARAM("--mirror");
PBUILDER_ADD_PARAM(pc->mirror);
}
PBUILDER_ADD_PARAM("--distribution");
PBUILDER_ADD_PARAM(pc->distribution);
PBUILDER_ADD_PARAM("--no-targz");
PBUILDER_ADD_PARAM("--internal-chrootexec");
PBUILDER_ADD_PARAM(buf_chroot);
/* add all additional parameters */
while (av[i]) {
PBUILDER_ADD_PARAM(av[i]);
i++;
}
PBUILDER_ADD_PARAM(NULL);
ret = forkexecvp(pbuildercommandline);
free(buf_chroot);
if (ret < 128) {
if (pc->save_after_login) {
if (0 != forkexeclp("chroot",
"chroot",
pc->buildplace,
"apt-get",
"clean",
NULL)) {
ret = -1;
}
if (cpbuilder_internal_saveupdate(pc)) {
ret = -1;
}
} else {
cpbuilder_internal_cleancow(pc);
}
} else {
log_printf(log_warn, "pbuilder execute aborted, not cleaning");
}
return ret;
}
/*
find matching parameter, and check if it's set.
@returns found=1, not found=0.
*/
int find_matching_param(const char *option) {
int i;
int found = 0;
for (i = 0; i < offset; ++i) {
if (!strcmp(pbuildercommandline[i], option)) {
found = 1;
}
}
return found;
}
/**
implement pbuilder update
@return 0 on success, other values on failure.
*/
int cpbuilder_update(const struct pbuilderconfig *pc) {
char *buf_chroot;
int ret;
/*
* I want the following actions done:
* NEW=buildplace OLD=basepath
*
* cp -al OLD NEW
* update in NEW
* mv OLD OLD-
* mv NEW OLD
* rm -rf OLD-
* But if you ignore that part, it's really simple.
*/
/* This simple process will not disrupt already-running pbuilder-cow session,
but it will not work with new pbuilder-cow sessions until this process finishes,
and any failure to pbuilder update will leave crap. */
if (0 > asprintf(&buf_chroot,
(pc->no_cowdancer_update)
? "chroot %s"
: /* try to not use cow-shell, when
* no-cowdancer-update option is used */
"chroot %s cow-shell",
pc->buildplace)) {
/* if outofmemory, die. */
log_printf(log_error, "Out of memory.");
return -1;
}
if (cpbuilder_internal_cowcopy(pc)) {
log_printf(log_error, "Failed cowcopy.");
return 1;
}
log_printf(log_info, "Invoking pbuilder");
pbuildercommandline[1] = "update";
PBUILDER_ADD_PARAM("--buildplace");
PBUILDER_ADD_PARAM(pc->buildplace);
if (pc->mirror) {
PBUILDER_ADD_PARAM("--mirror");
PBUILDER_ADD_PARAM(pc->mirror);
}
PBUILDER_ADD_PARAM("--distribution");
PBUILDER_ADD_PARAM(pc->distribution);
if (pc->extrapackages) {
PBUILDER_ADD_PARAM("--extrapackages");
PBUILDER_ADD_PARAM(pc->extrapackages);
}
PBUILDER_ADD_PARAM("--no-targz");
PBUILDER_ADD_PARAM("--internal-chrootexec");
PBUILDER_ADD_PARAM(buf_chroot);
PBUILDER_ADD_PARAM(NULL);
ret = forkexecvp(pbuildercommandline);
if (ret) {
if (ret < 128) {
log_printf(log_error, "pbuilder update failed");
if (!pc->no_cowdancer_update) {
/* advise the user to try this option first */
log_printf(
log_error,
"could not update with cowdancer, try --no-cowdancer-update option");
}
if (!pc->debug && 0 != rmrf_check_mount(pc->buildplace)) {
log_printf(log_error, "Could not remove original tree");
}
} else {
log_printf(log_warn, "pbuilder update aborted, not cleaning");
}
/* either way, I don't want to touch the original tree */
goto out;
}
log_printf(log_info, "removing %s working copy", ilist_PRGNAME);
cpbuilder_internal_saveupdate(pc);
out:
free(buf_chroot);
return ret;
}
int cpbuilder_help(void) {
printf("%s [operation] [options]\n"
"operation:\n"
" --build\n"
" --create\n"
" --update\n"
" --login\n"
" --execute\n"
" --help\n"
" --dumpconfig\n"
"options:\n"
" --basepath:\n"
" --buildplace:\n"
" --distribution:\n"
" ... and other pbuilder options\n",
ilist_PRGNAME);
return 0;
}
int app_main(int ac, char **av) {
setenv("COWDANCER_REUSE", "yes", 1);
return parse_parameter(ac, av, "cow");
}
|