mkswap.c

Go to the documentation of this file.
00001 /*      mkswap 1.0 - Initialize a swap partition or file
00002  *                                                      Author: Kees J. Bot
00003  *                                                              6 Jan 2001
00004  */
00005 #define nil ((void*)0)
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <errno.h>
00010 #include <unistd.h>
00011 #include <dirent.h>
00012 #include <fcntl.h>
00013 #include <sys/stat.h>
00014 #include <sys/types.h>
00015 #include <sys/ioctl.h>
00016 #include <minix/config.h>
00017 #include <minix/const.h>
00018 #include <minix/type.h>
00019 #include <minix/u64.h>
00020 #include <minix/partition.h>
00021 #include <minix/swap.h>
00022 #include <servers/fs/const.h>
00023 #include <servers/fs/type.h>
00024 #include <servers/fs/super.h>
00025 
00026 static void usage(void)
00027 {
00028     fprintf(stderr, "Usage: mkswap [-f] device-or-file [size[km]]\n");
00029     exit(1);
00030 }
00031 
00032 int main(int argc, char **argv)
00033 {
00034     int first;
00035     int i;
00036     char *file;
00037     unsigned long offset, size, devsize;
00038     int fd;
00039     struct stat st;
00040     ssize_t r;
00041     struct super_block super;
00042     swap_hdr_t swap_hdr;
00043     static u8_t block[_MAX_BLOCK_SIZE];
00044 
00045     first= 0;
00046     i= 1;
00047     while (i < argc && argv[i][0] == '-') {
00048         char *opt= argv[i++]+1;
00049 
00050         if (opt[0] == '-' && opt[1] == 0) break;        /* -- */
00051 
00052         while (*opt != 0) switch (*opt++) {
00053         case 'f':       first= 1;       break;
00054         default:        usage();
00055         }
00056     }
00057     if (i == argc) usage();
00058     file= argv[i++];
00059 
00060     size= 0;
00061     if (i < argc) {
00062         char *end;
00063         unsigned long m;
00064 
00065         size= strtoul(argv[i], &end, 10);
00066         if (end == argv[i]) usage();
00067         m= 1024;
00068         if (*end != 0) {
00069             switch (*end) {
00070             case 'm':   case 'M':       m *= 1024;      /*FALL THROUGH*/
00071             case 'k':   case 'K':       end++;          break;
00072             }
00073         }
00074         if (*end != 0 || size == -1
00075             || (size * m) / m != size || (size *= m) <= SWAP_OFFSET
00076         ) {
00077             fprintf(stderr, "mkswap: %s: Bad size\n", argv[i]);
00078             exit(1);
00079         }
00080         i++;
00081     }
00082     if (i != argc) usage();
00083 
00084     /* Open the device or file. */
00085     if ((fd= open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
00086         fprintf(stderr, "mkswap: Can't open %s: %s\n", file, strerror(errno));
00087         exit(1);
00088     }
00089 
00090     /* File or device? */
00091     (void) fstat(fd, &st);
00092     if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
00093         struct partition part;
00094 
00095         /* How big is the partition? */
00096         if (ioctl(fd, DIOCGETP, &part) < 0) {
00097             fprintf(stderr, "mkswap: Can't determine the size of %s: %s\n",
00098                 file, strerror(errno));
00099             exit(1);
00100         }
00101         devsize= cv64ul(part.size);
00102         offset= 0;
00103 
00104         if (!first) {
00105             /* Is there a file system? */
00106             r= -1;
00107             if (lseek(fd, SUPER_BLOCK_BYTES, SEEK_SET) == -1
00108                 || (r= read(fd, block, _STATIC_BLOCK_SIZE)) < _STATIC_BLOCK_SIZE
00109             ) {
00110                 fprintf(stderr, "mkswap: %s: %s\n",
00111                     file, r >= 0 ? "End of file" : strerror(errno));
00112                 exit(1);
00113             }
00114             memcpy(&super, block, sizeof(super));
00115             if (super.s_magic == SUPER_MAGIC) {
00116                 offset= (unsigned long) super.s_nzones * _STATIC_BLOCK_SIZE;
00117             } else
00118             if (super.s_magic == SUPER_V2) {
00119                 offset= (unsigned long) super.s_zones * _STATIC_BLOCK_SIZE;
00120             } else if (super.s_magic == SUPER_V3) {
00121                 offset= (unsigned long) super.s_zones * super.s_block_size;
00122             } else {
00123                 first= 1;
00124             }
00125         }
00126         if (size == 0) size= devsize - offset;
00127         if (size == 0 || offset + size > devsize) {
00128             fprintf(stderr, "mkswap: There is no room on %s for ", file);
00129             if (size > 0) fprintf(stderr, "%lu kilobytes of ", size/1024);
00130             fprintf(stderr, "swapspace\n");
00131             if (offset > 0) {
00132                 fprintf(stderr, "(Use the -f flag to wipe the file system)\n");
00133             }
00134             exit(1);
00135         }
00136     } else
00137     if (S_ISREG(st.st_mode)) {
00138         /* Write to the swap file to guarantee space for MM. */
00139         unsigned long n;
00140 
00141         if (size == 0) {
00142             fprintf(stderr, "mkswap: No size specified for %s\n", file);
00143             usage();
00144         }
00145 
00146         memset(block, 0, sizeof(block));
00147         for (n= 0; n < size; n += r) {
00148             r= size > sizeof(block) ? sizeof(block) : size;
00149             if ((r= write(fd, block, r)) <= 0) {
00150                 fprintf(stderr, "mkswap: %s: %s\n",
00151                     file, r == 0 ? "End of file" : strerror(errno));
00152                 exit(1);
00153             }
00154         }
00155         first= 1;
00156     } else {
00157         fprintf(stderr, "mkswap: %s is not a device or a file\n", file);
00158         exit(1);
00159     }
00160 
00161     if (offset < SWAP_OFFSET) {
00162         offset += SWAP_OFFSET;
00163         if (size < SWAP_OFFSET) size= 0; else size -= SWAP_OFFSET;
00164     }
00165     swap_hdr.sh_magic[0]= SWAP_MAGIC0;
00166     swap_hdr.sh_magic[1]= SWAP_MAGIC1;
00167     swap_hdr.sh_magic[2]= SWAP_MAGIC2;
00168     swap_hdr.sh_magic[3]= SWAP_MAGIC3;
00169     swap_hdr.sh_version= SH_VERSION;
00170     swap_hdr.sh_priority= 0;
00171     swap_hdr.sh_offset= offset;
00172     swap_hdr.sh_swapsize= size;
00173 
00174     r= -1;
00175     if (lseek(fd, SWAP_BOOTOFF, SEEK_SET) == -1
00176         || (r= read(fd, block, sizeof(block))) < sizeof(block)
00177     ) {
00178         fprintf(stderr, "mkswap: %s: %s\n", file,
00179             file, r >= 0 ? "End of file" : strerror(errno));
00180         exit(1);
00181     }
00182 
00183     r= (first ? SWAP_BOOTOFF : OPTSWAP_BOOTOFF) - SWAP_BOOTOFF;
00184     memcpy(block + r, &swap_hdr, sizeof(swap_hdr));
00185 
00186     r= -1;
00187     if (lseek(fd, SWAP_BOOTOFF, SEEK_SET) == -1
00188         || (r= write(fd, block, sizeof(block))) < sizeof(block)
00189     ) {
00190         fprintf(stderr, "mkswap: %s: %s\n", file,
00191             file, r >= 0 ? "End of file" : strerror(errno));
00192         exit(1);
00193     }
00194     printf("%s: swapspace at offset %lu, size %lu kilobytes\n",
00195         file, offset / 1024, size / 1024);
00196     return 0;
00197 }

Generated on Fri Apr 14 22:57:09 2006 for minix by  doxygen 1.4.6