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"
39 #define ERROR(fmt, ...) do { \
40 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
41 fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
50 struct vlist_node node
;
66 static struct vlist_tree mounts
;
67 static struct blob_buf b
;
68 static LIST_HEAD(devices
);
69 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
70 static unsigned int delay_root
;
82 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
83 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
84 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
85 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
86 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
87 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
88 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
101 static const struct uci_blob_param_list config_attr_list
= {
102 .n_params
= __CFG_MAX
,
103 .params
= config_policy
,
106 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
107 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
108 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
109 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
110 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
111 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
112 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
115 static const struct uci_blob_param_list mount_attr_list
= {
116 .n_params
= __MOUNT_MAX
,
117 .params
= mount_policy
,
129 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
130 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
131 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
132 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
133 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
134 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
137 static const struct uci_blob_param_list swap_attr_list
= {
138 .n_params
= __SWAP_MAX
,
139 .params
= swap_policy
,
148 # define MS_DIRSYNC (1 << 7)
152 # define MS_RELATIME (1 << 21)
155 #ifndef MS_STRICTATIME
156 # define MS_STRICTATIME (1 << 24)
159 static const struct mount_flag mount_flags
[] = {
160 { "sync", MS_SYNCHRONOUS
},
161 { "async", ~MS_SYNCHRONOUS
},
162 { "dirsync", MS_DIRSYNC
},
163 { "mand", MS_MANDLOCK
},
164 { "nomand", ~MS_MANDLOCK
},
165 { "atime", ~MS_NOATIME
},
166 { "noatime", MS_NOATIME
},
167 { "dev", ~MS_NODEV
},
168 { "nodev", MS_NODEV
},
169 { "diratime", ~MS_NODIRATIME
},
170 { "nodiratime", MS_NODIRATIME
},
171 { "exec", ~MS_NOEXEC
},
172 { "noexec", MS_NOEXEC
},
173 { "suid", ~MS_NOSUID
},
174 { "nosuid", MS_NOSUID
},
175 { "rw", ~MS_RDONLY
},
177 { "relatime", MS_RELATIME
},
178 { "norelatime", ~MS_RELATIME
},
179 { "strictatime", MS_STRICTATIME
},
182 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
187 return strdup(blobmsg_get_string(attr
));
190 static char *blobmsg_get_basename(struct blob_attr
*attr
)
195 return strdup(basename(blobmsg_get_string(attr
)));
198 static void parse_mount_options(struct mount
*m
, char *optstr
)
202 char *p
, *opts
, *last
;
207 if (!optstr
|| !*optstr
)
210 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
223 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
224 if (!strcmp(last
, mount_flags
[i
].name
)) {
225 if (mount_flags
[i
].flag
< 0)
226 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
228 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
235 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
244 static int mount_add(struct uci_section
*s
)
246 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
249 blob_buf_init(&b
, 0);
250 uci_to_blob(&b
, s
, &mount_attr_list
);
251 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
253 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
256 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
259 m
= malloc(sizeof(struct mount
));
260 m
->type
= TYPE_MOUNT
;
261 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
262 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
263 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
264 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
266 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
268 m
->overlay
= m
->extroot
= 0;
269 if (m
->target
&& !strcmp(m
->target
, "/"))
271 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
272 m
->extroot
= m
->overlay
= 1;
275 vlist_add(&mounts
, &m
->node
, m
->uuid
);
277 vlist_add(&mounts
, &m
->node
, m
->label
);
279 vlist_add(&mounts
, &m
->node
, m
->device
);
284 static int swap_add(struct uci_section
*s
)
286 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
289 blob_buf_init(&b
, 0);
290 uci_to_blob(&b
, s
, &swap_attr_list
);
291 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
293 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
296 m
= malloc(sizeof(struct mount
));
297 memset(m
, 0, sizeof(struct mount
));
299 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
300 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
301 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
303 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
305 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
307 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
308 /* store complete swap path */
310 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
313 vlist_add(&mounts
, &m
->node
, m
->uuid
);
315 vlist_add(&mounts
, &m
->node
, m
->label
);
317 vlist_add(&mounts
, &m
->node
, m
->device
);
323 static int global_add(struct uci_section
*s
)
325 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
327 blob_buf_init(&b
, 0);
328 uci_to_blob(&b
, s
, &config_attr_list
);
329 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
331 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
333 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
336 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
338 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
341 if (tb
[CFG_DELAY_ROOT
])
342 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
344 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
350 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
354 vlist_for_each_element(&mounts
, m
, node
) {
355 if (m
->type
!= TYPE_SWAP
)
357 if (uuid
&& m
->uuid
&& !strcmp(m
->uuid
, uuid
))
359 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
361 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
368 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
373 vlist_for_each_element(&mounts
, m
, node
) {
374 if (m
->type
!= TYPE_MOUNT
)
376 if (m
->uuid
&& uuid
&& !strcmp(m
->uuid
, uuid
))
378 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
380 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
382 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
389 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
390 struct vlist_node
*node_old
)
394 static int config_load(char *cfg
)
396 struct uci_context
*ctx
;
397 struct uci_package
*pkg
;
398 struct uci_element
*e
;
400 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
402 ctx
= uci_alloc_context();
405 snprintf(path
, 32, "%s/etc/config", cfg
);
406 uci_set_confdir(ctx
, path
);
409 if (uci_load(ctx
, "fstab", &pkg
))
412 uci_get_errorstr(ctx
, &err
, "fstab");
413 ERROR("extroot: failed to load %s/etc/config/%s\n",
414 cfg
? cfg
: "", err
);
419 vlist_update(&mounts
);
420 uci_foreach_element(&pkg
->sections
, e
) {
421 struct uci_section
*s
= uci_to_section(e
);
423 if (!strcmp(s
->type
, "mount"))
425 if (!strcmp(s
->type
, "swap"))
427 if (!strcmp(s
->type
, "global"))
430 vlist_flush(&mounts
);
435 static struct blkid_struct_probe
* _probe_path(char *path
)
437 struct blkid_struct_probe
*pr
;
439 pr
= malloc(sizeof(*pr
));
444 memset(pr
, 0, sizeof(*pr
));
445 probe_block(path
, pr
);
447 if (pr
->err
|| !pr
->id
) {
455 static int _cache_load(const char *path
)
457 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
461 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
464 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
465 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
467 list_add_tail(&pr
->list
, &devices
);
475 static void cache_load(int mtd
)
478 _cache_load("/dev/mtdblock*");
479 _cache_load("/dev/ubiblock*");
481 _cache_load("/dev/mmcblk*");
482 _cache_load("/dev/sd*");
483 _cache_load("/dev/hd*");
484 _cache_load("/dev/md*");
485 _cache_load("/dev/vd*");
486 _cache_load("/dev/mapper/*");
489 static int print_block_info(struct blkid_struct_probe
*pr
)
491 printf("%s:", pr
->dev
);
493 printf(" UUID=\"%s\"", pr
->uuid
);
496 printf(" LABEL=\"%s\"", pr
->label
);
499 printf(" NAME=\"%s\"", pr
->name
);
502 printf(" VERSION=\"%s\"", pr
->version
);
504 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
509 static int print_block_uci(struct blkid_struct_probe
*pr
)
511 if (!strcmp(pr
->id
->name
, "swap")) {
512 printf("config 'swap'\n");
514 printf("config 'mount'\n");
515 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
518 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
520 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
521 printf("\toption\tenabled\t'0'\n\n");
526 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
528 struct blkid_struct_probe
*pr
= NULL
;
531 list_for_each_entry(pr
, &devices
, list
)
532 if (!strcmp(pr
->uuid
, uuid
))
536 list_for_each_entry(pr
, &devices
, list
)
537 if (!strcmp(pr
->label
, label
))
541 list_for_each_entry(pr
, &devices
, list
)
542 if (!strcmp(basename(pr
->dev
), basename(path
)))
548 static char* find_mount_point(char *block
)
550 FILE *fp
= fopen("/proc/mounts", "r");
551 static char line
[256];
552 int len
= strlen(block
);
558 while (fgets(line
, sizeof(line
), fp
)) {
559 if (!strncmp(line
, block
, len
)) {
560 char *p
= &line
[len
+ 1];
561 char *t
= strstr(p
, " ");
578 static void mkdir_p(char *dir
)
580 char *l
= strrchr(dir
, '/');
590 static void check_filesystem(struct blkid_struct_probe
*pr
)
594 char *e2fsck
= "/usr/sbin/e2fsck";
596 if (strncmp(pr
->id
->name
, "ext", 3)) {
597 ERROR("check_filesystem: %s is not supported\n", pr
->id
->name
);
601 if (stat(e2fsck
, &statbuf
) < 0) {
602 ERROR("check_filesystem: %s not found\n", e2fsck
);
608 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
610 } else if (pid
> 0) {
613 waitpid(pid
, &status
, 0);
614 if (WEXITSTATUS(status
))
615 ERROR("check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
619 static void handle_swapfiles(bool on
)
623 struct blkid_struct_probe
*pr
;
625 vlist_for_each_element(&mounts
, m
, node
)
627 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
630 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
633 pr
= _probe_path(m
->target
);
638 if (!strcmp(pr
->id
->name
, "swap")) {
640 swapon(pr
->dev
, m
->prio
);
649 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
657 device
= basename(pr
->dev
);
659 if (!strcmp(pr
->id
->name
, "swap")) {
660 if (hotplug
&& !auto_swap
)
662 m
= find_swap(pr
->uuid
, pr
->label
, device
);
664 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
669 if (hotplug
&& !auto_mount
)
672 if (find_mount_point(pr
->dev
)) {
673 ERROR("%s is already mounted\n", pr
->dev
);
677 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
682 char *target
= m
->target
;
687 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
693 check_filesystem(pr
);
695 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
696 (m
->options
) ? (m
->options
) : (""));
698 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
699 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
701 handle_swapfiles(true);
706 char target
[] = "/mnt/mmcblk123";
709 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
713 check_filesystem(pr
);
715 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
717 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
718 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
720 handle_swapfiles(true);
727 static int umount_device(struct blkid_struct_probe
*pr
)
730 char *device
= basename(pr
->dev
);
737 if (!strcmp(pr
->id
->name
, "swap"))
740 mp
= find_mount_point(pr
->dev
);
744 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
748 err
= umount2(mp
, MNT_DETACH
);
750 ERROR("unmounting %s (%s) failed (%d) - %s\n",
751 pr
->dev
, mp
, err
, strerror(err
));
753 ERROR("unmounted %s (%s)\n",
759 static int main_hotplug(int argc
, char **argv
)
762 char *action
, *device
, *mount_point
;
764 action
= getenv("ACTION");
765 device
= getenv("DEVNAME");
767 if (!action
|| !device
)
769 snprintf(path
, sizeof(path
), "/dev/%s", device
);
771 if (!strcmp(action
, "remove")) {
773 mount_point
= find_mount_point(path
);
775 err
= umount2(mount_point
, MNT_DETACH
);
778 ERROR("umount of %s failed (%d) - %s\n",
779 mount_point
, err
, strerror(err
));
782 } else if (strcmp(action
, "add")) {
783 ERROR("Unkown action %s\n", action
);
788 if (config_load(NULL
))
792 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
795 static int find_block_mtd(char *name
, char *part
, int plen
)
797 FILE *fp
= fopen("/proc/mtd", "r");
798 static char line
[256];
804 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
805 if (strstr(line
, name
)) {
806 char *eol
= strstr(line
, ":");
821 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
826 static int check_extroot(char *path
)
828 struct blkid_struct_probe
*pr
= NULL
;
831 if (find_block_mtd("rootfs", fs
, sizeof(fs
)))
834 list_for_each_entry(pr
, &devices
, list
) {
835 if (!strcmp(pr
->dev
, fs
)) {
839 char uuid
[64] = { 0 };
841 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
843 fp
= fopen(tag
, "w+");
845 ERROR("extroot: failed to write uuid tag file\n");
846 /* return 0 to continue boot regardless of error */
854 fp
= fopen(tag
, "r");
856 ERROR("extroot: failed to open uuid tag file\n");
860 fgets(uuid
, sizeof(uuid
), fp
);
862 if (!strcmp(uuid
, pr
->uuid
))
864 ERROR("extroot: uuid tag does not match rom uuid\n");
870 static int mount_extroot(char *cfg
)
872 char overlay
[] = "/tmp/extroot/overlay";
873 char mnt
[] = "/tmp/extroot/mnt";
875 struct blkid_struct_probe
*pr
;
879 if (config_load(cfg
))
882 m
= find_block(NULL
, NULL
, NULL
, "/");
884 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
886 if (!m
|| !m
->extroot
)
888 ERROR("extroot: no root or overlay mount defined\n");
892 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
894 if (!pr
&& delay_root
){
895 ERROR("extroot: is not ready yet, retrying in %u seconds\n", delay_root
);
899 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
902 if (strncmp(pr
->id
->name
, "ext", 3)) {
903 ERROR("extroot: %s is not supported, try ext4\n", pr
->id
->name
);
911 check_filesystem(pr
);
913 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
916 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
917 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
918 } else if (m
->overlay
) {
919 err
= check_extroot(path
);
924 ERROR("extroot: cannot find block device\n");
930 static int main_extroot(int argc
, char **argv
)
932 struct blkid_struct_probe
*pr
;
934 char fs_data
[32] = { 0 };
937 if (!getenv("PREINIT"))
941 fprintf(stderr
, "Usage: block extroot mountpoint\n");
948 find_block_mtd("rootfs", fs
, sizeof(fs
));
950 ERROR("extroot: unable to locate rootfs mtdblock\n");
954 pr
= find_block_info(NULL
, NULL
, fs
);
956 ERROR("extroot: unable to retrieve rootfs information\n");
960 find_block_mtd("rootfs_data", fs_data
, sizeof(fs_data
));
962 pr
= find_block_info(NULL
, NULL
, fs_data
);
963 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
964 char cfg
[] = "/tmp/jffs_cfg";
967 if (!mount(fs_data
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
968 err
= mount_extroot(cfg
);
969 umount2(cfg
, MNT_DETACH
);
972 rmdir("/tmp/overlay");
978 return mount_extroot(NULL
);
981 static int main_mount(int argc
, char **argv
)
983 struct blkid_struct_probe
*pr
;
985 if (config_load(NULL
))
989 list_for_each_entry(pr
, &devices
, list
)
992 handle_swapfiles(true);
997 static int main_umount(int argc
, char **argv
)
999 struct blkid_struct_probe
*pr
;
1001 if (config_load(NULL
))
1004 handle_swapfiles(false);
1007 list_for_each_entry(pr
, &devices
, list
)
1013 static int main_detect(int argc
, char **argv
)
1015 struct blkid_struct_probe
*pr
;
1018 printf("config 'global'\n");
1019 printf("\toption\tanon_swap\t'0'\n");
1020 printf("\toption\tanon_mount\t'0'\n");
1021 printf("\toption\tauto_swap\t'1'\n");
1022 printf("\toption\tauto_mount\t'1'\n");
1023 printf("\toption\tdelay_root\t'5'\n");
1024 printf("\toption\tcheck_fs\t'0'\n\n");
1025 list_for_each_entry(pr
, &devices
, list
)
1026 print_block_uci(pr
);
1031 static int main_info(int argc
, char **argv
)
1034 struct blkid_struct_probe
*pr
;
1038 list_for_each_entry(pr
, &devices
, list
)
1039 print_block_info(pr
);
1044 for (i
= 2; i
< argc
; i
++) {
1047 if (stat(argv
[i
], &s
)) {
1048 ERROR("failed to stat %s\n", argv
[i
]);
1051 if (!S_ISBLK(s
.st_mode
)) {
1052 ERROR("%s is not a block device\n", argv
[i
]);
1055 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1057 print_block_info(pr
);
1063 static int swapon_usage(void)
1065 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1066 "\tStart swapping on [DEVICE]\n"
1067 " -a\tStart swapping on all swap devices\n"
1068 " -p pri\tSet priority of swap device\n"
1069 " -s\tShow summary\n");
1073 static int main_swapon(int argc
, char **argv
)
1079 struct blkid_struct_probe
*pr
;
1085 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1088 fp
= fopen("/proc/swaps", "r");
1092 ERROR("failed to open /proc/swaps\n");
1095 while (getline(&lineptr
, &s
, fp
) > 0)
1103 list_for_each_entry(pr
, &devices
, list
) {
1104 if (strcmp(pr
->id
->name
, "swap"))
1106 if (swapon(pr
->dev
, 0))
1107 ERROR("failed to swapon %s\n", pr
->dev
);
1113 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1116 return swapon_usage();
1121 if (optind
!= (argc
- 1))
1122 return swapon_usage();
1124 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1125 ERROR("%s is not a block device or file\n", argv
[optind
]);
1128 err
= swapon(argv
[optind
], flags
);
1130 ERROR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1137 static int main_swapoff(int argc
, char **argv
)
1140 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1141 "\tStop swapping on DEVICE\n"
1142 " -a\tStop swapping on all swap devices\n");
1146 if (!strcmp(argv
[1], "-a")) {
1147 FILE *fp
= fopen("/proc/swaps", "r");
1151 ERROR("failed to open /proc/swaps\n");
1154 fgets(line
, sizeof(line
), fp
);
1155 while (fgets(line
, sizeof(line
), fp
)) {
1156 char *end
= strchr(line
, ' ');
1162 err
= swapoff(line
);
1164 ERROR("failed to swapoff %s (%d)\n", line
, err
);
1171 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1172 ERROR("%s is not a block device or file\n", argv
[1]);
1175 err
= swapoff(argv
[1]);
1177 ERROR("fsiled to swapoff %s (%d)\n", argv
[1], err
);
1185 int main(int argc
, char **argv
)
1187 char *base
= basename(*argv
);
1191 if (!strcmp(base
, "swapon"))
1192 return main_swapon(argc
, argv
);
1194 if (!strcmp(base
, "swapoff"))
1195 return main_swapoff(argc
, argv
);
1197 if ((argc
> 1) && !strcmp(base
, "block")) {
1198 if (!strcmp(argv
[1], "info"))
1199 return main_info(argc
, argv
);
1201 if (!strcmp(argv
[1], "detect"))
1202 return main_detect(argc
, argv
);
1204 if (!strcmp(argv
[1], "hotplug"))
1205 return main_hotplug(argc
, argv
);
1207 if (!strcmp(argv
[1], "extroot"))
1208 return main_extroot(argc
, argv
);
1210 if (!strcmp(argv
[1], "mount"))
1211 return main_mount(argc
, argv
);
1213 if (!strcmp(argv
[1], "umount"))
1214 return main_umount(argc
, argv
);
1217 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");