📄 mkswap.c
字号:
return (r & m) != 0;}static voidusage(void) { fprintf(stderr, _("Usage: %s [-c] [-v0|-v1] [-pPAGESZ] /dev/name [blocks]\n"), program_name); exit(1);}static voiddie(const char *str) { fprintf(stderr, "%s: %s\n", program_name, str); exit(1);}static voidpage_ok(int page) { if (version==0) bit_set(signature_page, page);}static voidpage_bad(int page) { if (version == 0) bit_test_and_clear(signature_page, page); else { if (badpages == MAX_BADPAGES) die(_("too many bad pages")); p->badpages[badpages] = page; } badpages++;}static voidcheck_blocks(void) { unsigned int current_page; int do_seek = 1; char *buffer; buffer = malloc(pagesize); if (!buffer) die(_("Out of memory")); current_page = 0; while (current_page < PAGES) { if (!check) { page_ok(current_page++); continue; } if (do_seek && lseek(DEV,current_page*pagesize,SEEK_SET) != current_page*pagesize) die(_("seek failed in check_blocks")); if ((do_seek = (pagesize != read(DEV, buffer, pagesize)))) { page_bad(current_page++); continue; } page_ok(current_page++); } if (badpages == 1) printf(_("one bad page\n")); else if (badpages > 1) printf(_("%lu bad pages\n"), badpages);}static longvalid_offset (int fd, off_t offset) { char ch; if (lseek (fd, offset, 0) < 0) return 0; if (read (fd, &ch, 1) < 1) return 0; return 1;}static off_tfind_size (int fd) { off_t high, low; low = 0; for (high = 1; high > 0 && valid_offset (fd, high); high *= 2) low = high; while (low < high - 1) { const off_t mid = (low + high) / 2; if (valid_offset (fd, mid)) low = mid; else high = mid; } return (low + 1);}/* return size in pages, to avoid integer overflow */static unsigned longget_size(const char *file) { int fd; unsigned long size; fd = open(file, O_RDONLY); if (fd < 0) { perror(file); exit(1); } if (ioctl(fd, BLKGETSIZE, &size) >= 0) { int sectors_per_page = pagesize/512; size /= sectors_per_page; } else { size = find_size(fd) / pagesize; } close(fd); return size;}static intisnzdigit(char c) { return (c >= '1' && c <= '9');}intmain(int argc, char ** argv) { struct stat statbuf; int i; unsigned long maxpages; unsigned long goodpages; unsigned long sz; off_t offset; int force = 0; char *block_count = 0; char *pp; program_name = (argc && *argv) ? argv[0] : "fsck.minix"; if ((pp = strrchr(program_name, '/')) != NULL) program_name = pp+1; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); if (argc == 2 && (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) { printf(_("%s from %s\n"), program_name, util_linux_version); exit(0); } for (i=1; i<argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'c': check=1; break; case 'f': force=1; break; case 'p': pp = argv[i]+2; if (!*pp && i+1 < argc) pp = argv[++i]; if (isnzdigit(*pp)) user_pagesize=atoi(pp); else usage(); break; case 'v': version = atoi(argv[i]+2); break; default: usage(); } } else if (!device_name) { device_name = argv[i]; } else if (!block_count) { block_count = argv[i]; } else usage(); } init_signature_page(); /* get pagesize */ if (!device_name) { fprintf(stderr, _("%s: error: Nowhere to set up swap on?\n"), program_name); usage(); } if (block_count) { /* this silly user specified the number of blocks explicitly */ char *tmp; int blocks_per_page = pagesize/1024; PAGES = strtoul(block_count,&tmp,0)/blocks_per_page; if (*tmp) usage(); } sz = get_size(device_name); if (!PAGES) { PAGES = sz; } else if (PAGES > sz && !force) { fprintf(stderr, _("%s: error: " "size %lu is larger than device size %lu\n"), program_name, PAGES*(pagesize/1024), sz*(pagesize/1024)); exit(1); } if (version == -1) { /* use version 1 as default, if possible */ if (PAGES <= V0_MAX_PAGES && PAGES > V1_MAX_PAGES) version = 0; else if (linux_version_code() < MAKE_VERSION(2,1,117)) version = 0; else if (pagesize < 2048) version = 0; else version = 1; } if (version != 0 && version != 1) { fprintf(stderr, _("%s: error: unknown version %d\n"), program_name, version); usage(); } if (PAGES < 10) { fprintf(stderr, _("%s: error: swap area needs to be at least %ldkB\n"), program_name, (long)(10 * pagesize / 1000)); usage(); } if (version == 0) maxpages = V0_MAX_PAGES; else if (linux_version_code() >= MAKE_VERSION(2,3,4)) maxpages = PAGES; else if (linux_version_code() >= MAKE_VERSION(2,2,1)) maxpages = V1_MAX_PAGES; else maxpages = V1_OLD_MAX_PAGES; if (PAGES > maxpages) { PAGES = maxpages; fprintf(stderr, _("%s: warning: truncating swap area to %ldkB\n"), program_name, PAGES * pagesize / 1000); } DEV = open(device_name,O_RDWR); if (DEV < 0 || fstat(DEV, &statbuf) < 0) { perror(device_name); exit(1); } /* Want a block device. Probably not /dev/hda or /dev/hdb. */ if (!S_ISBLK(statbuf.st_mode)) check=0; else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) die(_("Will not try to make swapdevice on '%s'"));#ifdef __sparc__ if (!force && version == 0) { /* Don't overwrite partition table unless forced */ unsigned char *buffer = (unsigned char *)signature_page; unsigned short *q, sum; if (read(DEV, buffer, 512) != 512) die(_("fatal: first page unreadable")); if (buffer[508] == 0xDA && buffer[509] == 0xBE) { q = (unsigned short *)(buffer + 510); for (sum = 0; q >= (unsigned short *) buffer;) sum ^= *q--; if (!sum) { fprintf(stderr, _("\%s: Device '%s' contains a valid Sun disklabel.\n\This probably means creating v0 swap would destroy your partition table\n\No swap created. If you really want to create swap v0 on that device, use\n\the -f option to force it.\n"), program_name, device_name); exit(1); } } }#endif if (version == 0 || check) check_blocks(); if (version == 0 && !bit_test_and_clear(signature_page,0)) die(_("fatal: first page unreadable")); if (version == 1) { p->version = version; p->last_page = PAGES-1; p->nr_badpages = badpages; } goodpages = PAGES - badpages - 1; if ((long) goodpages <= 0) die(_("Unable to set up swap-space: unreadable")); printf(_("Setting up swapspace version %d, size = %llu kB\n"), version, (unsigned long long)goodpages * pagesize / 1000); write_signature((version == 0) ? "SWAP-SPACE" : "SWAPSPACE2"); offset = ((version == 0) ? 0 : 1024); if (lseek(DEV, offset, SEEK_SET) != offset) die(_("unable to rewind swap-device")); if (write(DEV,(char*)signature_page+offset, pagesize-offset) != pagesize-offset) die(_("unable to write signature page")); /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) */#ifdef HAVE_fsync if (fsync(DEV)) die(_("fsync failed"));#endif return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -