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.
23 #include <sys/types.h>
25 #include <sys/mount.h>
31 #include <libubox/list.h>
32 #include <libubox/vlist.h>
33 #include <libubox/blobmsg_json.h>
34 #include <libubox/avl-cmp.h>
36 #include "libblkid-tiny/libblkid-tiny.h"
44 struct vlist_node node
;
60 static struct vlist_tree mounts
;
61 static struct blob_buf b
;
62 static LIST_HEAD(devices
);
63 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
64 static unsigned int delay_root
;
76 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
77 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
78 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
79 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
80 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
81 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
82 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
95 static const struct uci_blob_param_list config_attr_list
= {
96 .n_params
= __CFG_MAX
,
97 .params
= config_policy
,
100 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
101 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
102 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
103 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
104 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
105 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
106 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
109 static const struct uci_blob_param_list mount_attr_list
= {
110 .n_params
= __MOUNT_MAX
,
111 .params
= mount_policy
,
122 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
123 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
124 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
125 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
126 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
129 static const struct uci_blob_param_list swap_attr_list
= {
130 .n_params
= __SWAP_MAX
,
131 .params
= swap_policy
,
140 # define MS_DIRSYNC (1 << 7)
144 # define MS_RELATIME (1 << 21)
147 #ifndef MS_STRICTATIME
148 # define MS_STRICTATIME (1 << 24)
151 static const struct mount_flag mount_flags
[] = {
152 { "sync", MS_SYNCHRONOUS
},
153 { "async", ~MS_SYNCHRONOUS
},
154 { "dirsync", MS_DIRSYNC
},
155 { "mand", MS_MANDLOCK
},
156 { "nomand", ~MS_MANDLOCK
},
157 { "atime", ~MS_NOATIME
},
158 { "noatime", MS_NOATIME
},
159 { "dev", ~MS_NODEV
},
160 { "nodev", MS_NODEV
},
161 { "diratime", ~MS_NODIRATIME
},
162 { "nodiratime", MS_NODIRATIME
},
163 { "exec", ~MS_NOEXEC
},
164 { "noexec", MS_NOEXEC
},
165 { "suid", ~MS_NOSUID
},
166 { "nosuid", MS_NOSUID
},
167 { "rw", ~MS_RDONLY
},
169 { "relatime", MS_RELATIME
},
170 { "norelatime", ~MS_RELATIME
},
171 { "strictatime", MS_STRICTATIME
},
174 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
179 return strdup(blobmsg_get_string(attr
));
182 static char *blobmsg_get_basename(struct blob_attr
*attr
)
187 return strdup(basename(blobmsg_get_string(attr
)));
190 static void parse_mount_options(struct mount
*m
, char *optstr
)
194 char *p
, *opts
, *last
;
199 if (!optstr
|| !*optstr
)
202 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
215 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
216 if (!strcmp(last
, mount_flags
[i
].name
)) {
217 if (mount_flags
[i
].flag
< 0)
218 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
220 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
227 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
236 static int mount_add(struct uci_section
*s
)
238 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
241 blob_buf_init(&b
, 0);
242 uci_to_blob(&b
, s
, &mount_attr_list
);
243 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
245 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
248 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
251 m
= malloc(sizeof(struct mount
));
252 m
->type
= TYPE_MOUNT
;
253 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
254 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
255 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
256 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
258 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
260 m
->overlay
= m
->extroot
= 0;
261 if (m
->target
&& !strcmp(m
->target
, "/"))
263 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
264 m
->extroot
= m
->overlay
= 1;
267 vlist_add(&mounts
, &m
->node
, m
->uuid
);
269 vlist_add(&mounts
, &m
->node
, m
->label
);
271 vlist_add(&mounts
, &m
->node
, m
->device
);
276 static int swap_add(struct uci_section
*s
)
278 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
281 blob_buf_init(&b
, 0);
282 uci_to_blob(&b
, s
, &swap_attr_list
);
283 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
285 if (!tb
[SWAP_UUID
] && !tb
[SWAP_DEVICE
])
288 m
= malloc(sizeof(struct mount
));
289 memset(m
, 0, sizeof(struct mount
));
291 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
292 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
294 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
296 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
298 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
299 /* store complete swap path */
301 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
302 vlist_add(&mounts
, &m
->node
, (m
->uuid
) ? (m
->uuid
) : (m
->device
));
308 static int global_add(struct uci_section
*s
)
310 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
312 blob_buf_init(&b
, 0);
313 uci_to_blob(&b
, s
, &config_attr_list
);
314 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
316 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
318 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
321 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
323 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
326 if (tb
[CFG_DELAY_ROOT
])
327 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
329 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
335 static struct mount
* find_swap(const char *uuid
, const char *device
)
339 vlist_for_each_element(&mounts
, m
, node
) {
340 if (m
->type
!= TYPE_SWAP
)
342 if (uuid
&& m
->uuid
&& !strcmp(m
->uuid
, uuid
))
344 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
351 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
356 vlist_for_each_element(&mounts
, m
, node
) {
357 if (m
->type
!= TYPE_MOUNT
)
359 if (m
->uuid
&& uuid
&& !strcmp(m
->uuid
, uuid
))
361 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
363 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
365 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
372 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
373 struct vlist_node
*node_old
)
377 static int config_load(char *cfg
)
379 struct uci_context
*ctx
;
380 struct uci_package
*pkg
;
381 struct uci_element
*e
;
383 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
385 ctx
= uci_alloc_context();
388 snprintf(path
, 32, "%s/etc/config", cfg
);
389 uci_set_confdir(ctx
, path
);
392 if (uci_load(ctx
, "fstab", &pkg
))
395 vlist_update(&mounts
);
396 uci_foreach_element(&pkg
->sections
, e
) {
397 struct uci_section
*s
= uci_to_section(e
);
399 if (!strcmp(s
->type
, "mount"))
401 if (!strcmp(s
->type
, "swap"))
403 if (!strcmp(s
->type
, "global"))
406 vlist_flush(&mounts
);
411 static struct blkid_struct_probe
* _probe_path(char *path
)
413 struct blkid_struct_probe
*pr
;
415 pr
= malloc(sizeof(*pr
));
420 memset(pr
, 0, sizeof(*pr
));
421 probe_block(path
, pr
);
423 if (pr
->err
|| !pr
->id
) {
431 static int _cache_load(const char *path
)
433 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
437 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
440 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
441 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
443 list_add_tail(&pr
->list
, &devices
);
451 static void cache_load(int mtd
)
454 _cache_load("/dev/mtdblock*");
455 _cache_load("/dev/mmcblk*");
456 _cache_load("/dev/sd*");
457 _cache_load("/dev/sdc*");
458 _cache_load("/dev/hd*");
459 _cache_load("/dev/md*");
460 _cache_load("/dev/mapper/*");
463 static int print_block_info(struct blkid_struct_probe
*pr
)
465 printf("%s:", pr
->dev
);
467 printf(" UUID=\"%s\"", pr
->uuid
);
470 printf(" LABEL=\"%s\"", pr
->label
);
473 printf(" NAME=\"%s\"", pr
->name
);
476 printf(" VERSION=\"%s\"", pr
->version
);
478 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
483 static int print_block_uci(struct blkid_struct_probe
*pr
)
485 if (!strcmp(pr
->id
->name
, "swap")) {
486 printf("config 'swap'\n");
488 printf("config 'mount'\n");
489 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
492 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
494 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
495 printf("\toption\tenabled\t'0'\n\n");
500 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
502 struct blkid_struct_probe
*pr
= NULL
;
505 list_for_each_entry(pr
, &devices
, list
)
506 if (!strcmp(pr
->uuid
, uuid
))
510 list_for_each_entry(pr
, &devices
, list
)
511 if (strcmp(pr
->label
, label
))
515 list_for_each_entry(pr
, &devices
, list
)
516 if (!strcmp(pr
->dev
, path
))
522 static char* find_mount_point(char *block
)
524 FILE *fp
= fopen("/proc/mounts", "r");
525 static char line
[256];
526 int len
= strlen(block
);
532 while (fgets(line
, sizeof(line
), fp
)) {
533 if (!strncmp(line
, block
, len
)) {
534 char *p
= &line
[len
+ 1];
535 char *t
= strstr(p
, " ");
550 static void mkdir_p(char *dir
)
552 char *l
= strrchr(dir
, '/');
562 static void check_filesystem(struct blkid_struct_probe
*pr
)
566 char *e2fsck
= "/usr/sbin/e2fsck";
568 if (strncmp(pr
->id
->name
, "ext", 3)) {
569 fprintf(stderr
, "check_filesystem: %s is not supported\n", pr
->id
->name
);
573 if (stat(e2fsck
, &statbuf
) < 0) {
574 fprintf(stderr
, "check_filesystem: %s not found\n", e2fsck
);
580 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
582 } else if (pid
> 0) {
585 waitpid(pid
, &status
, 0);
586 if (WEXITSTATUS(status
))
587 fprintf(stderr
, "check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
591 static void handle_swapfiles(bool on
)
595 struct blkid_struct_probe
*pr
;
597 vlist_for_each_element(&mounts
, m
, node
)
599 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
602 fprintf(stderr
, "swapfile: %s\n", m
->target
);
604 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
607 pr
= _probe_path(m
->target
);
612 fprintf(stderr
, "probe: %p\n", pr
);
614 if (!strcmp(pr
->id
->name
, "swap")) {
616 swapon(pr
->dev
, m
->prio
);
625 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
633 device
= basename(pr
->dev
);
635 if (!strcmp(pr
->id
->name
, "swap")) {
636 if (hotplug
&& !auto_swap
)
638 m
= find_swap(pr
->uuid
, device
);
640 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
645 if (hotplug
&& !auto_mount
)
648 if (find_mount_point(pr
->dev
)) {
649 fprintf(stderr
, "%s is already mounted\n", pr
->dev
);
653 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
658 char *target
= m
->target
;
663 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
669 check_filesystem(pr
);
671 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
672 (m
->options
) ? (m
->options
) : (""));
674 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
675 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
677 handle_swapfiles(true);
682 char target
[] = "/mnt/mmcblk123";
685 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
689 check_filesystem(pr
);
691 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
693 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
694 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
696 handle_swapfiles(true);
703 static int umount_device(struct blkid_struct_probe
*pr
)
706 char *device
= basename(pr
->dev
);
713 if (!strcmp(pr
->id
->name
, "swap"))
716 mp
= find_mount_point(pr
->dev
);
720 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
724 err
= umount2(mp
, MNT_DETACH
);
726 fprintf(stderr
, "unmounting %s (%s) failed (%d) - %s\n",
727 pr
->dev
, mp
, err
, strerror(err
));
729 fprintf(stderr
, "unmounted %s (%s)\n",
735 static int main_hotplug(int argc
, char **argv
)
738 char *action
, *device
, *mount_point
;
740 action
= getenv("ACTION");
741 device
= getenv("DEVNAME");
743 if (!action
|| !device
)
745 snprintf(path
, sizeof(path
), "/dev/%s", device
);
747 if (!strcmp(action
, "remove")) {
749 mount_point
= find_mount_point(path
);
751 err
= umount2(mount_point
, MNT_DETACH
);
754 fprintf(stderr
, "umount of %s failed (%d) - %s\n",
755 mount_point
, err
, strerror(err
));
758 } else if (strcmp(action
, "add")) {
759 fprintf(stderr
, "Unkown action %s\n", action
);
764 if (config_load(NULL
))
768 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
771 static int find_block_mtd(char *name
, char *part
, int plen
)
773 FILE *fp
= fopen("/proc/mtd", "r");
774 static char line
[256];
780 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
781 if (strstr(line
, name
)) {
782 char *eol
= strstr(line
, ":");
797 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
802 static int check_extroot(char *path
)
804 struct blkid_struct_probe
*pr
= NULL
;
807 if (find_block_mtd("rootfs", fs
, sizeof(fs
)))
810 list_for_each_entry(pr
, &devices
, list
) {
811 if (!strcmp(pr
->dev
, fs
)) {
815 char uuid
[64] = { 0 };
817 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
819 fp
= fopen(tag
, "w+");
821 fprintf(stderr
, "extroot: failed to write uuid tag file\n");
822 /* return 0 to continue boot regardless of error */
830 fp
= fopen(tag
, "r");
832 fprintf(stderr
, "extroot: failed to open uuid tag file\n");
836 fgets(uuid
, sizeof(uuid
), fp
);
838 if (!strcmp(uuid
, pr
->uuid
))
840 fprintf(stderr
, "extroot: uuid tag does not match rom uuid\n");
846 static int mount_extroot(char *cfg
)
848 char overlay
[] = "/tmp/extroot/overlay";
849 char mnt
[] = "/tmp/extroot/mnt";
851 struct blkid_struct_probe
*pr
;
855 if (config_load(cfg
))
858 m
= find_block(NULL
, NULL
, NULL
, "/");
860 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
862 if (!m
|| !m
->extroot
)
865 pr
= find_block_info(m
->uuid
, m
->label
, NULL
);
867 if (!pr
&& delay_root
){
868 fprintf(stderr
, "extroot: is not ready yet, retrying in %u seconds\n", delay_root
);
872 pr
= find_block_info(m
->uuid
, m
->label
, NULL
);
875 if (strncmp(pr
->id
->name
, "ext", 3)) {
876 fprintf(stderr
, "extroot: %s is not supported, try ext4\n", pr
->id
->name
);
884 check_filesystem(pr
);
886 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
889 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
890 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
891 } else if (m
->overlay
) {
892 err
= check_extroot(path
);
901 static int main_extroot(int argc
, char **argv
)
903 struct blkid_struct_probe
*pr
;
905 char fs_data
[32] = { 0 };
908 if (!getenv("PREINIT"))
912 fprintf(stderr
, "Usage: block extroot mountpoint\n");
919 find_block_mtd("rootfs", fs
, sizeof(fs
));
923 pr
= find_block_info(NULL
, NULL
, fs
);
927 find_block_mtd("rootfs_data", fs_data
, sizeof(fs_data
));
929 pr
= find_block_info(NULL
, NULL
, fs_data
);
930 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
931 char cfg
[] = "/tmp/jffs_cfg";
934 if (!mount(fs_data
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
935 err
= mount_extroot(cfg
);
936 umount2(cfg
, MNT_DETACH
);
939 rmdir("/tmp/overlay");
945 return mount_extroot(NULL
);
948 static int main_mount(int argc
, char **argv
)
950 struct blkid_struct_probe
*pr
;
952 if (config_load(NULL
))
956 list_for_each_entry(pr
, &devices
, list
)
962 static int main_umount(int argc
, char **argv
)
964 struct blkid_struct_probe
*pr
;
966 if (config_load(NULL
))
969 handle_swapfiles(false);
972 list_for_each_entry(pr
, &devices
, list
)
978 static int main_detect(int argc
, char **argv
)
980 struct blkid_struct_probe
*pr
;
983 printf("config 'global'\n");
984 printf("\toption\tanon_swap\t'0'\n");
985 printf("\toption\tanon_mount\t'0'\n");
986 printf("\toption\tauto_swap\t'1'\n");
987 printf("\toption\tauto_mount\t'1'\n");
988 printf("\toption\tdelay_root\t'5'\n");
989 printf("\toption\tcheck_fs\t'0'\n\n");
990 list_for_each_entry(pr
, &devices
, list
)
996 static int main_info(int argc
, char **argv
)
999 struct blkid_struct_probe
*pr
;
1003 list_for_each_entry(pr
, &devices
, list
)
1004 print_block_info(pr
);
1009 for (i
= 2; i
< argc
; i
++) {
1012 if (stat(argv
[i
], &s
)) {
1013 fprintf(stderr
, "failed to stat %s\n", argv
[i
]);
1016 if (!S_ISBLK(s
.st_mode
)) {
1017 fprintf(stderr
, "%s is not a block device\n", argv
[i
]);
1020 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1022 print_block_info(pr
);
1028 static int main_swapon(int argc
, char **argv
)
1031 fprintf(stderr
, "Usage: swapon <-s> <-a> [DEVICE]\n\n\tStart swapping on [DEVICE]\n -a\tStart swapping on all swap devices\n -s\tShow summary\n");
1035 if (!strcmp(argv
[1], "-s")) {
1036 FILE *fp
= fopen("/proc/swaps", "r");
1037 char *lineptr
= NULL
;
1041 fprintf(stderr
, "failed to open /proc/swaps\n");
1044 while (getline(&lineptr
, &s
, fp
) > 0)
1049 } else if (!strcmp(argv
[1], "-a")) {
1050 struct blkid_struct_probe
*pr
;
1053 list_for_each_entry(pr
, &devices
, list
) {
1054 if (strcmp(pr
->id
->name
, "swap"))
1056 if (swapon(pr
->dev
, 0))
1057 fprintf(stderr
, "failed to swapon %s\n", pr
->dev
);
1063 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1064 fprintf(stderr
, "%s is not a block device or file\n", argv
[1]);
1067 err
= swapon(argv
[1], 0);
1069 fprintf(stderr
, "failed to swapon %s (%d)\n", argv
[1], err
);
1077 static int main_swapoff(int argc
, char **argv
)
1080 fprintf(stderr
, "Usage: swapoff [-a] [DEVICE]\n\n\tStop swapping on DEVICE\n -a\tStop swapping on all swap devices\n");
1084 if (!strcmp(argv
[1], "-a")) {
1085 FILE *fp
= fopen("/proc/swaps", "r");
1089 fprintf(stderr
, "failed to open /proc/swaps\n");
1092 fgets(line
, sizeof(line
), fp
);
1093 while (fgets(line
, sizeof(line
), fp
)) {
1094 char *end
= strchr(line
, ' ');
1100 err
= swapoff(line
);
1102 fprintf(stderr
, "failed to swapoff %s (%d)\n", line
, err
);
1109 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1110 fprintf(stderr
, "%s is not a block device or file\n", argv
[1]);
1113 err
= swapoff(argv
[1]);
1115 fprintf(stderr
, "fsiled to swapoff %s (%d)\n", argv
[1], err
);
1123 int main(int argc
, char **argv
)
1125 char *base
= basename(*argv
);
1129 if (!strcmp(base
, "swapon"))
1130 return main_swapon(argc
, argv
);
1132 if (!strcmp(base
, "swapoff"))
1133 return main_swapoff(argc
, argv
);
1135 if ((argc
> 1) && !strcmp(base
, "block")) {
1136 if (!strcmp(argv
[1], "info"))
1137 return main_info(argc
, argv
);
1139 if (!strcmp(argv
[1], "detect"))
1140 return main_detect(argc
, argv
);
1142 if (!strcmp(argv
[1], "hotplug"))
1143 return main_hotplug(argc
, argv
);
1145 if (!strcmp(argv
[1], "extroot"))
1146 return main_extroot(argc
, argv
);
1148 if (!strcmp(argv
[1], "mount"))
1149 return main_mount(argc
, argv
);
1151 if (!strcmp(argv
[1], "umount"))
1152 return main_umount(argc
, argv
);
1155 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");