1 /* losetup.c - Loopback setup
3 * Copyright 2012 Rob Landley <rob@landley.net>
7 USE_LOSETUP(NEWTOY(losetup, ">2S(sizelimit)#s(show)ro#j:fdca[!afj]", TOYFLAG_SBIN))
13 usage: losetup [-cdrs] [-o OFFSET] [-S SIZE] {-d DEVICE...|-j FILE|-af|{DEVICE FILE}}
15 Associate a loopback device with a file, or show current file (if any)
16 associated with a loop device.
19 -a Iterate through all loopback devices
20 -f Find first unused loop device (may create one)
21 -j Iterate through all loopback devices associated with FILE
24 -c Check capacity (file size changed)
25 -d Detach loopback device
28 -s Show device name (alias --show)
29 -o Start assocation at OFFSET into FILE
31 -S Limit SIZE of loopback association (alias --sizelimit)
36 #include <linux/loop.h>
49 todo: basic /dev file association
53 allocate new loop device:
55 https://lkml.org/lkml/2011/7/26/148
58 // -f: *device is NULL
60 // Perform requested operation on one device. Returns 1 if handled, 0 if error
61 static void loopback_setup(char *device, char *file)
63 struct loop_info64 *loop = (void *)(toybuf+32);
64 int lfd = -1, ffd = ffd;
65 unsigned flags = toys.optflags;
67 // Open file (ffd) and loop device (lfd)
69 if (file) ffd = xopen(file, TT.openflags);
71 int i, cfd = open("/dev/loop-control", O_RDWR);
73 // We assume /dev is devtmpfs so device creation has no lag. Otherwise
74 // just preallocate loop devices and stay within them.
76 // mount -o loop depends on found device being at the start of toybuf.
78 if (0 <= (i = ioctl(cfd, 0x4C82))) // LOOP_CTL_GET_FREE
79 sprintf(device = toybuf, "/dev/loop%d", i);
84 if (device) lfd = open(device, TT.openflags);
86 // Stat the loop device to see if there's a current association.
87 memset(loop, 0, sizeof(struct loop_info64));
88 if (-1 == lfd || ioctl(lfd, LOOP_GET_STATUS64, loop)) {
89 if (errno == ENXIO && (flags & (FLAG_a|FLAG_j))) goto done;
90 if (errno != ENXIO || !file) {
91 perror_msg("%s", device ? device : "-f");
96 // Skip -j filtered devices
97 if (TT.jfile && (loop->lo_device != TT.jdev || loop->lo_inode != TT.jino))
100 // Check size of file or delete existing association
101 if (flags & (FLAG_c|FLAG_d)) {
102 // The constant is LOOP_SET_CAPACITY
103 if (ioctl(lfd, (flags & FLAG_c) ? 0x4C07 : LOOP_CLR_FD, 0)) {
104 perror_msg("%s", device);
107 // Associate file with this device?
109 char *s = xabspath(file, 1);
111 if (!s) perror_exit("file"); // already opened, but if deleted since...
112 if (ioctl(lfd, LOOP_SET_FD, ffd)) perror_exit("%s=%s", device, file);
113 loop->lo_offset = TT.offset;
114 loop->lo_sizelimit = TT.size;
115 strncpy((char *)loop->lo_file_name, s, LO_NAME_SIZE);
116 s[LO_NAME_SIZE-1] = 0;
117 if (ioctl(lfd, LOOP_SET_STATUS64, loop)) perror_exit("%s=%s", device, file);
118 if (flags & FLAG_s) printf("%s", device);
120 } else if (flags & FLAG_f) printf("%s", device);
122 xprintf("%s: [%04llx]:%llu (%s)", device, loop->lo_device, loop->lo_inode,
124 if (loop->lo_offset) xprintf(", offset %llu", loop->lo_offset);
125 if (loop->lo_sizelimit) xprintf(", sizelimit %llu", loop->lo_sizelimit);
130 if (file) close(ffd);
131 if (lfd != -1) close(lfd);
134 // Perform an action on all currently existing loop devices
135 static int dash_a(struct dirtree *node)
137 char *s = node->name;
139 // Initial /dev node needs to recurse down one level, then only loop[0-9]*
140 if (*s == '/') return DIRTREE_RECURSE;
141 if (strncmp(s, "loop", 4) || !isdigit(s[4])) return 0;
143 s = dirtree_path(node, 0);
144 loopback_setup(s, 0);
150 void losetup_main(void)
154 TT.openflags = (toys.optflags & FLAG_r) ? O_RDONLY : O_RDWR;
159 xstat(TT.jfile, &st);
164 // With just device, display current association
165 // -a, -f substitute for device
166 // -j substitute for device
168 // new association: S size o offset rs - need a file
169 // existing association: cd
173 if (toys.optflags & FLAG_f) {
174 if (toys.optc > 1) perror_exit("max 1 arg");
175 loopback_setup(NULL, *toys.optargs);
176 } else if (toys.optflags & (FLAG_a|FLAG_j)) {
177 if (toys.optc) error_exit("bad args");
178 dirtree_read("/dev", dash_a);
179 // Do we need one DEVICE argument?
181 char *file = (toys.optflags & (FLAG_d|FLAG_c)) ? NULL : toys.optargs[1];
183 if (!toys.optc || (file && toys.optc != 2)) {
185 perror_exit("needs %d arg%s", 1+!!file, file ? "s" : "");
187 for (s = toys.optargs; *s; s++) {
188 loopback_setup(*s, file);