]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - historic/mkswap.c
2 * mkswap.c - set up a linux swap device
4 * (C) 1991 Linus Torvalds. This file may be redistributed as per
9 * 20.12.91 - time began. Got VM working yesterday by doing this by hand.
11 * Usuage: mkswap [-c] device [size-in-blocks]
13 * -c for readablility checking (use it unless you are SURE!)
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
18 * Patches from jaggy@purplet.demon.co.uk (Mike Jagdis) to make the
19 * size-in-blocks parameter optional added Wed Feb 8 10:33:43 1995.
32 #error "needs gcc for the bitop-__asm__'s"
39 #define TEST_BUFFER_PAGES 8
41 static char * program_name
= "mkswap";
42 static char * device_name
= NULL
;
44 static long PAGES
= 0;
46 static int badpages
= 0;
48 static char signature_page
[PAGE_SIZE
];
50 #define bitop(name,op) \
51 static inline int name(char * addr,unsigned int nr) \
54 __asm__ __volatile__("bt" op " %1,%2; adcl $0,%0" \
56 :"r" (nr),"m" (*(addr)),"0" (0)); \
65 * Volatile to let gcc know that this doesn't return. When trying
66 * to compile this under minix, volatile gives a warning, as
67 * exit() isn't defined as volatile under minix.
69 volatile void fatal_error(const char * fmt_string
)
71 fprintf(stderr
,fmt_string
,program_name
,device_name
);
75 #define usage() fatal_error("Usage: %s [-c] /dev/name [blocks]\n")
76 #define die(str) fatal_error("%s: " str "\n")
78 void check_blocks(void)
80 unsigned int current_page
;
82 static char buffer
[PAGE_SIZE
];
85 while (current_page
< PAGES
) {
87 setbit(signature_page
,current_page
++);
90 if (do_seek
&& lseek(DEV
,current_page
*PAGE_SIZE
,SEEK_SET
) !=
91 current_page
*PAGE_SIZE
)
92 die("seek failed in check_blocks");
93 if (do_seek
= (PAGE_SIZE
!= read(DEV
, buffer
, PAGE_SIZE
))) {
94 clrbit(signature_page
,current_page
++);
98 setbit(signature_page
,current_page
++);
101 printf("%d bad page%s\n",badpages
,(badpages
>1)?"s":"");
104 static long valid_offset (int fd
, int offset
)
108 if (lseek (fd
, offset
, 0) < 0)
110 if (read (fd
, &ch
, 1) < 1)
115 static int count_blocks (int fd
)
120 for (high
= 1; valid_offset (fd
, high
); high
*= 2)
122 while (low
< high
- 1)
124 const int mid
= (low
+ high
) / 2;
126 if (valid_offset (fd
, mid
))
131 valid_offset (fd
, 0);
135 static int get_size(const char *file
)
140 fd
= open(file
, O_RDWR
);
145 if (ioctl(fd
, BLKGETSIZE
, &size
) >= 0) {
150 size
= count_blocks(fd
);
155 int main(int argc
, char ** argv
)
161 memset(signature_page
,0,PAGE_SIZE
);
163 program_name
= *argv
;
166 if (argv
[0][0] != '-')
168 PAGES
= strtol(argv
[0],&tmp
,0)>>2;
172 device_name
= argv
[0];
173 else while (*++argv
[0])
174 switch (argv
[0][0]) {
175 case 'c': check
=1; break;
179 if (device_name
&& !PAGES
) {
180 PAGES
= get_size(device_name
) / PAGE_SIZE
;
182 if (!device_name
|| PAGES
<10)
184 if (PAGES
> 32688) /* 130752 blocks */
190 DEV
= open(device_name
,O_RDWR
);
191 if (DEV
< 0 || fstat(DEV
, &statbuf
) < 0) {
195 if (!S_ISBLK(statbuf
.st_mode
))
197 else if (statbuf
.st_rdev
== 0x0300 || statbuf
.st_rdev
== 0x0340)
198 die("Will not try to make swapdevice on '%s'");
200 if (!clrbit(signature_page
,0))
201 die("fatal: first page unreadable");
202 goodpages
= PAGES
- badpages
- 1;
204 die("Unable to set up swap-space: unreadable");
205 printf("Setting up swapspace, size = %d bytes\n",goodpages
*PAGE_SIZE
);
206 strncpy(signature_page
+PAGE_SIZE
-10,"SWAP-SPACE",10);
207 if (lseek(DEV
, 0, SEEK_SET
))
208 die("unable to rewind swap-device");
209 if (PAGE_SIZE
!= write(DEV
, signature_page
, PAGE_SIZE
))
210 die("unable to write signature page");