2 * losetup.c - setup and control loop devices
8 const char losetup_usage[] = "losetup\n"
10 "\tlosetup loop_device give info\n"
11 "\tlosetup -d loop_device delete\n"
12 "\tlosetup [ -o offset ] loop_device file setup\n";
17 * losetup.c - setup and control loop devices
27 #include <sys/ioctl.h>
29 /* #include "loop.h" */
32 * include/linux/loop.h
34 * Written by Theodore Ts'o, 3/29/93.
36 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
37 * permitted under the GNU Public License.
40 #define LO_NAME_SIZE 64
41 #define LO_KEY_SIZE 32
44 int lo_number; /* ioctl r/o */
45 dev_t lo_device; /* ioctl r/o */
46 unsigned long lo_inode; /* ioctl r/o */
47 dev_t lo_rdevice; /* ioctl r/o */
50 int lo_encrypt_key_size; /* ioctl w/o */
51 int lo_flags; /* ioctl r/o */
52 char lo_name[LO_NAME_SIZE];
53 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
54 unsigned long lo_init[2];
59 * IOCTL commands --- we will commandeer 0x4C ('L')
62 #define LOOP_SET_FD 0x4C00
63 #define LOOP_CLR_FD 0x4C01
64 #define LOOP_SET_STATUS 0x4C02
65 #define LOOP_GET_STATUS 0x4C03
67 /* #include "lomount.h" */
69 extern int set_loop (const char *, const char *, int, int *);
70 extern int del_loop (const char *);
72 static void show_loop(const char *device)
74 struct loop_info loopinfo;
77 if ((fd = open(device, O_RDWR)) < 0) {
81 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) < 0) {
82 perror("Cannot get loop info");
86 printf("%s: [%04x]:%ld (%s) offset %d\n",
87 device, (unsigned int)loopinfo.lo_device, loopinfo.lo_inode,
88 loopinfo.lo_name, loopinfo.lo_offset);
93 int set_loop(const char *device, const char *file, int offset, int *loopro)
95 struct loop_info loopinfo;
98 mode = *loopro ? O_RDONLY : O_RDWR;
99 if ((ffd = open (file, mode)) < 0 && !*loopro
100 && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) {
104 if ((fd = open (device, mode)) < 0) {
109 *loopro = (mode == O_RDONLY);
111 memset(&loopinfo, 0, sizeof(loopinfo));
112 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
113 loopinfo.lo_name[LO_NAME_SIZE-1] = 0;
115 loopinfo.lo_offset = offset;
117 loopinfo.lo_encrypt_key_size = 0;
118 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
119 perror("ioctl: LOOP_SET_FD");
122 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
123 (void) ioctl(fd, LOOP_CLR_FD, 0);
124 perror("ioctl: LOOP_SET_STATUS");
132 int del_loop(const char *device)
136 if ((fd = open(device, O_RDONLY)) < 0) {
140 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
141 perror("ioctl: LOOP_CLR_FD");
149 static int losetup_usage_fn(void)
151 fprintf(stderr, losetup_usage);
155 int losetup_main(struct FileInfo * i, int argc, char * * argv)
163 while ((c = getopt(argc,argv,"do:")) != EOF) {
175 if (argc == 1) losetup_usage_fn();
176 if ((delete && (argc != optind+1 || offset)) ||
177 (!delete && (argc < optind+1 || argc > optind+2)))
179 if (argc == optind+1)
181 del_loop(argv[optind]);
183 show_loop(argv[optind]);
185 if (offset && sscanf(offset,"%d",&off) != 1)
187 set_loop(argv[optind],argv[optind+1],off,&ro);