2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
24 #include <sys/types.h>
26 #include <sys/mount.h>
32 #include <libubox/list.h>
33 #include <libubox/vlist.h>
34 #include <libubox/blobmsg_json.h>
35 #include <libubox/avl-cmp.h>
37 #include "libblkid-tiny/libblkid-tiny.h"
45 struct vlist_node node
;
61 static struct vlist_tree mounts
;
62 static struct blob_buf b
;
63 static LIST_HEAD(devices
);
64 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
65 static unsigned int delay_root
;
77 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
78 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
79 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
80 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
81 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
82 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
83 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
96 static const struct uci_blob_param_list config_attr_list
= {
97 .n_params
= __CFG_MAX
,
98 .params
= config_policy
,
101 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
102 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
103 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
104 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
105 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
106 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
107 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
110 static const struct uci_blob_param_list mount_attr_list
= {
111 .n_params
= __MOUNT_MAX
,
112 .params
= mount_policy
,
124 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
125 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
126 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
127 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
128 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
129 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
132 static const struct uci_blob_param_list swap_attr_list
= {
133 .n_params
= __SWAP_MAX
,
134 .params
= swap_policy
,
143 # define MS_DIRSYNC (1 << 7)
147 # define MS_RELATIME (1 << 21)
150 #ifndef MS_STRICTATIME
151 # define MS_STRICTATIME (1 << 24)
154 static const struct mount_flag mount_flags
[] = {
155 { "sync", MS_SYNCHRONOUS
},
156 { "async", ~MS_SYNCHRONOUS
},
157 { "dirsync", MS_DIRSYNC
},
158 { "mand", MS_MANDLOCK
},
159 { "nomand", ~MS_MANDLOCK
},
160 { "atime", ~MS_NOATIME
},
161 { "noatime", MS_NOATIME
},
162 { "dev", ~MS_NODEV
},
163 { "nodev", MS_NODEV
},
164 { "diratime", ~MS_NODIRATIME
},
165 { "nodiratime", MS_NODIRATIME
},
166 { "exec", ~MS_NOEXEC
},
167 { "noexec", MS_NOEXEC
},
168 { "suid", ~MS_NOSUID
},
169 { "nosuid", MS_NOSUID
},
170 { "rw", ~MS_RDONLY
},
172 { "relatime", MS_RELATIME
},
173 { "norelatime", ~MS_RELATIME
},
174 { "strictatime", MS_STRICTATIME
},
177 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
182 return strdup(blobmsg_get_string(attr
));
185 static char *blobmsg_get_basename(struct blob_attr
*attr
)
190 return strdup(basename(blobmsg_get_string(attr
)));
193 static void parse_mount_options(struct mount
*m
, char *optstr
)
197 char *p
, *opts
, *last
;
202 if (!optstr
|| !*optstr
)
205 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
218 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
219 if (!strcmp(last
, mount_flags
[i
].name
)) {
220 if (mount_flags
[i
].flag
< 0)
221 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
223 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
230 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
239 static int mount_add(struct uci_section
*s
)
241 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
244 blob_buf_init(&b
, 0);
245 uci_to_blob(&b
, s
, &mount_attr_list
);
246 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
248 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
251 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
254 m
= malloc(sizeof(struct mount
));
255 m
->type
= TYPE_MOUNT
;
256 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
257 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
258 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
259 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
261 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
263 m
->overlay
= m
->extroot
= 0;
264 if (m
->target
&& !strcmp(m
->target
, "/"))
266 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
267 m
->extroot
= m
->overlay
= 1;
270 vlist_add(&mounts
, &m
->node
, m
->uuid
);
272 vlist_add(&mounts
, &m
->node
, m
->label
);
274 vlist_add(&mounts
, &m
->node
, m
->device
);
279 static int swap_add(struct uci_section
*s
)
281 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
284 blob_buf_init(&b
, 0);
285 uci_to_blob(&b
, s
, &swap_attr_list
);
286 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
288 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
291 m
= malloc(sizeof(struct mount
));
292 memset(m
, 0, sizeof(struct mount
));
294 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
295 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
296 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
298 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
300 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
302 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
303 /* store complete swap path */
305 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
308 vlist_add(&mounts
, &m
->node
, m
->uuid
);
310 vlist_add(&mounts
, &m
->node
, m
->label
);
312 vlist_add(&mounts
, &m
->node
, m
->device
);
318 static int global_add(struct uci_section
*s
)
320 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
322 blob_buf_init(&b
, 0);
323 uci_to_blob(&b
, s
, &config_attr_list
);
324 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
326 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
328 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
331 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
333 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
336 if (tb
[CFG_DELAY_ROOT
])
337 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
339 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
345 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
349 vlist_for_each_element(&mounts
, m
, node
) {
350 if (m
->type
!= TYPE_SWAP
)
352 if (uuid
&& m
->uuid
&& !strcmp(m
->uuid
, uuid
))
354 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
356 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
363 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
368 vlist_for_each_element(&mounts
, m
, node
) {
369 if (m
->type
!= TYPE_MOUNT
)
371 if (m
->uuid
&& uuid
&& !strcmp(m
->uuid
, uuid
))
373 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
375 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
377 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
384 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
385 struct vlist_node
*node_old
)
389 static int config_load(char *cfg
)
391 struct uci_context
*ctx
;
392 struct uci_package
*pkg
;
393 struct uci_element
*e
;
395 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
397 ctx
= uci_alloc_context();
400 snprintf(path
, 32, "%s/etc/config", cfg
);
401 uci_set_confdir(ctx
, path
);
404 if (uci_load(ctx
, "fstab", &pkg
))
407 vlist_update(&mounts
);
408 uci_foreach_element(&pkg
->sections
, e
) {
409 struct uci_section
*s
= uci_to_section(e
);
411 if (!strcmp(s
->type
, "mount"))
413 if (!strcmp(s
->type
, "swap"))
415 if (!strcmp(s
->type
, "global"))
418 vlist_flush(&mounts
);
423 static struct blkid_struct_probe
* _probe_path(char *path
)
425 struct blkid_struct_probe
*pr
;
427 pr
= malloc(sizeof(*pr
));
432 memset(pr
, 0, sizeof(*pr
));
433 probe_block(path
, pr
);
435 if (pr
->err
|| !pr
->id
) {
443 static int _cache_load(const char *path
)
445 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
449 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
452 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
453 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
455 list_add_tail(&pr
->list
, &devices
);
463 static void cache_load(int mtd
)
466 _cache_load("/dev/mtdblock*");
467 _cache_load("/dev/mmcblk*");
468 _cache_load("/dev/sd*");
469 _cache_load("/dev/sdc*");
470 _cache_load("/dev/hd*");
471 _cache_load("/dev/md*");
472 _cache_load("/dev/mapper/*");
475 static int print_block_info(struct blkid_struct_probe
*pr
)
477 printf("%s:", pr
->dev
);
479 printf(" UUID=\"%s\"", pr
->uuid
);
482 printf(" LABEL=\"%s\"", pr
->label
);
485 printf(" NAME=\"%s\"", pr
->name
);
488 printf(" VERSION=\"%s\"", pr
->version
);
490 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
495 static int print_block_uci(struct blkid_struct_probe
*pr
)
497 if (!strcmp(pr
->id
->name
, "swap")) {
498 printf("config 'swap'\n");
500 printf("config 'mount'\n");
501 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
504 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
506 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
507 printf("\toption\tenabled\t'0'\n\n");
512 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
514 struct blkid_struct_probe
*pr
= NULL
;
517 list_for_each_entry(pr
, &devices
, list
)
518 if (!strcmp(pr
->uuid
, uuid
))
522 list_for_each_entry(pr
, &devices
, list
)
523 if (!strcmp(pr
->label
, label
))
527 list_for_each_entry(pr
, &devices
, list
)
528 if (!strcmp(basename(pr
->dev
), basename(path
)))
534 static char* find_mount_point(char *block
)
536 FILE *fp
= fopen("/proc/mounts", "r");
537 static char line
[256];
538 int len
= strlen(block
);
544 while (fgets(line
, sizeof(line
), fp
)) {
545 if (!strncmp(line
, block
, len
)) {
546 char *p
= &line
[len
+ 1];
547 char *t
= strstr(p
, " ");
564 static void mkdir_p(char *dir
)
566 char *l
= strrchr(dir
, '/');
576 static void check_filesystem(struct blkid_struct_probe
*pr
)
580 char *e2fsck
= "/usr/sbin/e2fsck";
582 if (strncmp(pr
->id
->name
, "ext", 3)) {
583 fprintf(stderr
, "check_filesystem: %s is not supported\n", pr
->id
->name
);
587 if (stat(e2fsck
, &statbuf
) < 0) {
588 fprintf(stderr
, "check_filesystem: %s not found\n", e2fsck
);
594 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
596 } else if (pid
> 0) {
599 waitpid(pid
, &status
, 0);
600 if (WEXITSTATUS(status
))
601 fprintf(stderr
, "check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
605 static void handle_swapfiles(bool on
)
609 struct blkid_struct_probe
*pr
;
611 vlist_for_each_element(&mounts
, m
, node
)
613 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
616 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
619 pr
= _probe_path(m
->target
);
624 if (!strcmp(pr
->id
->name
, "swap")) {
626 swapon(pr
->dev
, m
->prio
);
635 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
643 device
= basename(pr
->dev
);
645 if (!strcmp(pr
->id
->name
, "swap")) {
646 if (hotplug
&& !auto_swap
)
648 m
= find_swap(pr
->uuid
, pr
->label
, device
);
650 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
655 if (hotplug
&& !auto_mount
)
658 if (find_mount_point(pr
->dev
)) {
659 fprintf(stderr
, "%s is already mounted\n", pr
->dev
);
663 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
668 char *target
= m
->target
;
673 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
679 check_filesystem(pr
);
681 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
682 (m
->options
) ? (m
->options
) : (""));
684 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
685 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
687 handle_swapfiles(true);
692 char target
[] = "/mnt/mmcblk123";
695 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
699 check_filesystem(pr
);
701 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
703 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
704 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
706 handle_swapfiles(true);
713 static int umount_device(struct blkid_struct_probe
*pr
)
716 char *device
= basename(pr
->dev
);
723 if (!strcmp(pr
->id
->name
, "swap"))
726 mp
= find_mount_point(pr
->dev
);
730 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
734 err
= umount2(mp
, MNT_DETACH
);
736 fprintf(stderr
, "unmounting %s (%s) failed (%d) - %s\n",
737 pr
->dev
, mp
, err
, strerror(err
));
739 fprintf(stderr
, "unmounted %s (%s)\n",
745 static int main_hotplug(int argc
, char **argv
)
748 char *action
, *device
, *mount_point
;
750 action
= getenv("ACTION");
751 device
= getenv("DEVNAME");
753 if (!action
|| !device
)
755 snprintf(path
, sizeof(path
), "/dev/%s", device
);
757 if (!strcmp(action
, "remove")) {
759 mount_point
= find_mount_point(path
);
761 err
= umount2(mount_point
, MNT_DETACH
);
764 fprintf(stderr
, "umount of %s failed (%d) - %s\n",
765 mount_point
, err
, strerror(err
));
768 } else if (strcmp(action
, "add")) {
769 fprintf(stderr
, "Unkown action %s\n", action
);
774 if (config_load(NULL
))
778 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
781 static int find_block_mtd(char *name
, char *part
, int plen
)
783 FILE *fp
= fopen("/proc/mtd", "r");
784 static char line
[256];
790 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
791 if (strstr(line
, name
)) {
792 char *eol
= strstr(line
, ":");
807 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
812 static int check_extroot(char *path
)
814 struct blkid_struct_probe
*pr
= NULL
;
817 if (find_block_mtd("rootfs", fs
, sizeof(fs
)))
820 list_for_each_entry(pr
, &devices
, list
) {
821 if (!strcmp(pr
->dev
, fs
)) {
825 char uuid
[64] = { 0 };
827 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
829 fp
= fopen(tag
, "w+");
831 fprintf(stderr
, "extroot: failed to write uuid tag file\n");
832 /* return 0 to continue boot regardless of error */
840 fp
= fopen(tag
, "r");
842 fprintf(stderr
, "extroot: failed to open uuid tag file\n");
846 fgets(uuid
, sizeof(uuid
), fp
);
848 if (!strcmp(uuid
, pr
->uuid
))
850 fprintf(stderr
, "extroot: uuid tag does not match rom uuid\n");
856 static int mount_extroot(char *cfg
)
858 char overlay
[] = "/tmp/extroot/overlay";
859 char mnt
[] = "/tmp/extroot/mnt";
861 struct blkid_struct_probe
*pr
;
865 if (config_load(cfg
))
868 m
= find_block(NULL
, NULL
, NULL
, "/");
870 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
872 if (!m
|| !m
->extroot
)
875 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
877 if (!pr
&& delay_root
){
878 fprintf(stderr
, "extroot: is not ready yet, retrying in %u seconds\n", delay_root
);
882 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
885 if (strncmp(pr
->id
->name
, "ext", 3)) {
886 fprintf(stderr
, "extroot: %s is not supported, try ext4\n", pr
->id
->name
);
894 check_filesystem(pr
);
896 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
899 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
900 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
901 } else if (m
->overlay
) {
902 err
= check_extroot(path
);
911 static int main_extroot(int argc
, char **argv
)
913 struct blkid_struct_probe
*pr
;
915 char fs_data
[32] = { 0 };
918 if (!getenv("PREINIT"))
922 fprintf(stderr
, "Usage: block extroot mountpoint\n");
929 find_block_mtd("rootfs", fs
, sizeof(fs
));
933 pr
= find_block_info(NULL
, NULL
, fs
);
937 find_block_mtd("rootfs_data", fs_data
, sizeof(fs_data
));
939 pr
= find_block_info(NULL
, NULL
, fs_data
);
940 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
941 char cfg
[] = "/tmp/jffs_cfg";
944 if (!mount(fs_data
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
945 err
= mount_extroot(cfg
);
946 umount2(cfg
, MNT_DETACH
);
949 rmdir("/tmp/overlay");
955 return mount_extroot(NULL
);
958 static int main_mount(int argc
, char **argv
)
960 struct blkid_struct_probe
*pr
;
962 if (config_load(NULL
))
966 list_for_each_entry(pr
, &devices
, list
)
969 handle_swapfiles(true);
974 static int main_umount(int argc
, char **argv
)
976 struct blkid_struct_probe
*pr
;
978 if (config_load(NULL
))
981 handle_swapfiles(false);
984 list_for_each_entry(pr
, &devices
, list
)
990 static int main_detect(int argc
, char **argv
)
992 struct blkid_struct_probe
*pr
;
995 printf("config 'global'\n");
996 printf("\toption\tanon_swap\t'0'\n");
997 printf("\toption\tanon_mount\t'0'\n");
998 printf("\toption\tauto_swap\t'1'\n");
999 printf("\toption\tauto_mount\t'1'\n");
1000 printf("\toption\tdelay_root\t'5'\n");
1001 printf("\toption\tcheck_fs\t'0'\n\n");
1002 list_for_each_entry(pr
, &devices
, list
)
1003 print_block_uci(pr
);
1008 static int main_info(int argc
, char **argv
)
1011 struct blkid_struct_probe
*pr
;
1015 list_for_each_entry(pr
, &devices
, list
)
1016 print_block_info(pr
);
1021 for (i
= 2; i
< argc
; i
++) {
1024 if (stat(argv
[i
], &s
)) {
1025 fprintf(stderr
, "failed to stat %s\n", argv
[i
]);
1028 if (!S_ISBLK(s
.st_mode
)) {
1029 fprintf(stderr
, "%s is not a block device\n", argv
[i
]);
1032 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1034 print_block_info(pr
);
1040 static int swapon_usage(void)
1042 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1043 "\tStart swapping on [DEVICE]\n"
1044 " -a\tStart swapping on all swap devices\n"
1045 " -p pri\tSet priority of swap device\n"
1046 " -s\tShow summary\n");
1050 static int main_swapon(int argc
, char **argv
)
1056 struct blkid_struct_probe
*pr
;
1062 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1065 fp
= fopen("/proc/swaps", "r");
1069 fprintf(stderr
, "failed to open /proc/swaps\n");
1072 while (getline(&lineptr
, &s
, fp
) > 0)
1080 list_for_each_entry(pr
, &devices
, list
) {
1081 if (strcmp(pr
->id
->name
, "swap"))
1083 if (swapon(pr
->dev
, 0))
1084 fprintf(stderr
, "failed to swapon %s\n", pr
->dev
);
1090 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1093 return swapon_usage();
1098 if (optind
!= (argc
- 1))
1099 return swapon_usage();
1101 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1102 fprintf(stderr
, "%s is not a block device or file\n", argv
[optind
]);
1105 err
= swapon(argv
[optind
], flags
);
1107 fprintf(stderr
, "failed to swapon %s (%d)\n", argv
[optind
], err
);
1114 static int main_swapoff(int argc
, char **argv
)
1117 fprintf(stderr
, "Usage: swapoff [-a] [DEVICE]\n\n"
1118 "\tStop swapping on DEVICE\n"
1119 " -a\tStop swapping on all swap devices\n");
1123 if (!strcmp(argv
[1], "-a")) {
1124 FILE *fp
= fopen("/proc/swaps", "r");
1128 fprintf(stderr
, "failed to open /proc/swaps\n");
1131 fgets(line
, sizeof(line
), fp
);
1132 while (fgets(line
, sizeof(line
), fp
)) {
1133 char *end
= strchr(line
, ' ');
1139 err
= swapoff(line
);
1141 fprintf(stderr
, "failed to swapoff %s (%d)\n", line
, err
);
1148 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1149 fprintf(stderr
, "%s is not a block device or file\n", argv
[1]);
1152 err
= swapoff(argv
[1]);
1154 fprintf(stderr
, "fsiled to swapoff %s (%d)\n", argv
[1], err
);
1162 int main(int argc
, char **argv
)
1164 char *base
= basename(*argv
);
1168 if (!strcmp(base
, "swapon"))
1169 return main_swapon(argc
, argv
);
1171 if (!strcmp(base
, "swapoff"))
1172 return main_swapoff(argc
, argv
);
1174 if ((argc
> 1) && !strcmp(base
, "block")) {
1175 if (!strcmp(argv
[1], "info"))
1176 return main_info(argc
, argv
);
1178 if (!strcmp(argv
[1], "detect"))
1179 return main_detect(argc
, argv
);
1181 if (!strcmp(argv
[1], "hotplug"))
1182 return main_hotplug(argc
, argv
);
1184 if (!strcmp(argv
[1], "extroot"))
1185 return main_extroot(argc
, argv
);
1187 if (!strcmp(argv
[1], "mount"))
1188 return main_mount(argc
, argv
);
1190 if (!strcmp(argv
[1], "umount"))
1191 return main_umount(argc
, argv
);
1194 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");