]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/rdev.c
d49da823c959e6adfcd4dbc99be1a24b3a2f668f
3 rdev.c - query/set root device.
5 -------------------------------------------------------------------------
7 Date: Sun, 27 Dec 1992 15:55:31 +0000
9 From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
10 To: Rik Faith <faith@cs.unc.edu>
12 There are quite a few versions of rdev:
14 - the original rootdev that only printed the current root device, by
16 - rdev that does what rootdev did and that also allows you to change
17 the root (and swap) device, by me.
18 - rdev got renamed to setroot and I think even to rootdev on various
20 - Peter MacDonald added video mode and RAM disk setting and included
21 this version on SLS, called rdev again. I've attached his rdev.c to
24 -------------------------------------------------------------------------
26 Date: 11 Mar 92 21:37:37 GMT
27 Subject: rdev - query/set root device
28 From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
29 Organization: Swiss Federal Institute of Technology (ETH), Zurich, CH
31 With all that socket, X11, disk driver and FS hacking going on, apparently
32 nobody has found time to address one of the minor nuisances of life: set-
33 ting the root FS device is still somewhat cumbersome. I've written a little
34 utility which can read and set the root device in boot images:
36 rdev accepts an optional offset argument, just in case the address should
37 ever move from 508. If called without arguments, rdev outputs an mtab line
38 for the current root FS, just like /etc/rootdev does.
40 ramsize sets the size of the ramdisk. If size is zero, no ramdisk is used.
42 vidmode sets the default video mode at bootup time. -1 uses default video
45 -------------------------------------------------------------------------
47 Sun Dec 27 10:42:16 1992: Minor usage changes, faith@cs.unc.edu.
48 Tue Mar 30 09:31:52 1993: rdev -Rn to set root readonly flag, sct@dcs.ed.ac.uk
49 Wed Jun 22 21:12:29 1994: Applied patches from Dave
50 (gentzel@nova.enet.dec.com) to prevent dereferencing
51 the NULL pointer, faith@cs.unc.edu
52 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
53 - added Native Language Support
55 -------------------------------------------------------------------------
63 /* rdev.c - query/set root device. */
68 puts(_("usage: rdev [ -rsv ] [ -o OFFSET ] [ IMAGE [ VALUE [ OFFSET ] ] ]"));
69 puts(_(" rdev /dev/fd0 (or rdev /linux, etc.) displays the current ROOT device"));
70 puts(_(" rdev /dev/fd0 /dev/hda2 sets ROOT to /dev/hda2"));
71 puts(_(" rdev -R /dev/fd0 1 set the ROOTFLAGS (readonly status)"));
72 puts(_(" rdev -s /dev/fd0 /dev/hda2 set the SWAP device"));
73 puts(_(" rdev -r /dev/fd0 627 set the RAMDISK size"));
74 puts(_(" rdev -v /dev/fd0 1 set the bootup VIDEOMODE"));
75 puts(_(" rdev -o N ... use the byte offset N"));
76 puts(_(" rootflags ... same as rdev -R"));
77 puts(_(" swapdev ... same as rdev -s"));
78 puts(_(" ramsize ... same as rdev -r"));
79 puts(_(" vidmode ... same as rdev -v"));
80 puts(_("Note: video modes are: -3=Ask, -2=Extended, -1=NormalVga, 1=key1, 2=key2,..."));
81 puts(_(" use -R 1 to mount root readonly, -R 0 for read/write."));
93 #include <sys/types.h>
96 #define DEFAULT_OFFSET 508
105 /* Earlier rdev fails on /dev/ida/c0d0p1 so we allow for
106 (limited) recursion in /dev. -- Paul Clements */
111 find_dev_recursive(char *dirnamebuf
, int number
, int depth
) {
119 if ((dp
= opendir(dirnamebuf
)) == NULL
)
121 dirnamelen
= strlen(dirnamebuf
);
122 while ((dir
= readdir(dp
)) != NULL
) {
123 if (!strcmp(dir
->d_name
, ".") || !strcmp(dir
->d_name
, ".."))
125 if (dirnamelen
+ 1 + strlen(dir
->d_name
) > PATH_MAX
)
127 dirnamebuf
[dirnamelen
] = '/';
128 strcpy(dirnamebuf
+dirnamelen
+1, dir
->d_name
);
129 if (lstat(dirnamebuf
, &s
) < 0)
131 if ((s
.st_mode
& S_IFMT
) == S_IFBLK
&& s
.st_rdev
== number
)
133 if ((s
.st_mode
& S_IFMT
) == S_IFDIR
&&
134 find_dev_recursive(dirnamebuf
, number
, depth
-1))
137 dirnamebuf
[dirnamelen
] = 0;
142 find_dev(int number
) {
143 static char name
[PATH_MAX
+1];
146 return "Boot device";
147 strcpy(name
, "/dev");
148 if (find_dev_recursive(name
, number
, MAX_DEPTH
))
150 sprintf(name
, "0x%04x", number
);
154 /* enum { RDEV, SDEV, RAMSIZE, VIDMODE }; */
155 enum { RDEV
, VIDMODE
, RAMSIZE
, SDEV
, __syssize__
, ROOTFLAGS
};
156 char *cmdnames
[6] = { "rdev", "vidmode", "ramsize", "swapdev",
158 char *desc
[6] = { "Root device", "Video mode", "Ramsize", "Swap device",
160 #define shift(n) argv+=n,argc-=n
163 main(int argc
, char **argv
) {
164 int image
, offset
, dev_nr
, i
, newoffset
=-1;
169 setlocale(LC_ALL
, "");
170 bindtextdomain(PACKAGE
, LOCALEDIR
);
175 /* use the command name to figure out what we have to do - ugly */
177 if ((ptr
= strrchr(argv
[0],'/')) != NULL
)
182 if (!strcmp(ptr
,cmdnames
[i
])) {
188 if (argv
[1][0] != '-')
190 switch (argv
[1][1]) {
209 newoffset
=atoi(argv
[1]+2);
212 } else if (argc
> 2) {
213 newoffset
=atoi(argv
[2]);
217 /* Fall through. . . */
223 offset
= DEFAULT_OFFSET
-cmd
*2;
227 if ((cmd
== RDEV
) && (argc
== 1 || argc
> 4)) {
228 if (stat("/",&s
) < 0) die("/");
229 printf("%s /\n", find_dev(s
.st_dev
));
231 } else if ((cmd
!= RDEV
) && (argc
== 1 || argc
> 4))
234 if ((cmd
== RDEV
) || (cmd
== SDEV
)) {
237 offset
= atoi(argv
[3]);
240 if (isdigit(*argv
[2]))
241 offset
= atoi(argv
[2]);
250 offset
= atoi(argv
[3]);
253 if ((cmd
== SDEV
) || (cmd
== RDEV
)) {
254 if (stat(device
,&s
) < 0)
257 s
.st_rdev
=atoi(device
);
258 if ((image
= open(argv
[1],O_WRONLY
)) < 0) die(argv
[1]);
259 if (lseek(image
,offset
,0) < 0) die("lseek");
260 if (write(image
,(char *)&s
.st_rdev
,2) != 2) die(argv
[1]);
261 if (close(image
) < 0) die("close");
263 if ((image
= open(argv
[1],O_RDONLY
)) < 0) die(argv
[1]);
264 if (lseek(image
,offset
,0) < 0) die("lseek");
266 if (read(image
,(char *)&dev_nr
,2) != 2) die(argv
[1]);
267 if (close(image
) < 0) die("close");
268 fputs(desc
[cmd
], stdout
);
269 if ((cmd
== SDEV
) || (cmd
== RDEV
))
270 printf(" %s\n", find_dev(dev_nr
));
272 printf(" %d\n", dev_nr
);