source: mainline/uspace/app/sysinst/rdimg.c@ 4a8d0dd1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4a8d0dd1 was 8fefd8b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Preserve srv directory structure

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup sysinst
30 * @{
31 */
32/** @file RAM disk image manipulation
33 */
34
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <str.h>
39#include <task.h>
40#include <vfs/vfs.h>
41#include <vol.h>
42
43#include "rdimg.h"
44
45#define FILE_BD "/srv/bd/file_bd"
46#define RD_SVC "bd/iird"
47#define RD_LABEL "HelenOS-rd"
48
49/** Find volume by volume label. */
50static errno_t rd_img_part_by_label(vol_t *vol, const char *label,
51 service_id_t *rid)
52{
53 vol_part_info_t vinfo;
54 service_id_t *part_ids = NULL;
55 size_t nparts;
56 size_t i;
57 errno_t rc;
58
59 rc = vol_get_parts(vol, &part_ids, &nparts);
60 if (rc != EOK) {
61 printf("Error getting list of volumes.\n");
62 goto out;
63 }
64
65 for (i = 0; i < nparts; i++) {
66 rc = vol_part_info(vol, part_ids[i], &vinfo);
67 if (rc != EOK) {
68 printf("Error getting volume information.\n");
69 rc = EIO;
70 goto out;
71 }
72
73 if (str_cmp(vinfo.label, label) == 0) {
74 *rid = part_ids[i];
75 rc = EOK;
76 goto out;
77 }
78 }
79
80 rc = ENOENT;
81out:
82 free(part_ids);
83 return rc;
84}
85
86/** Open RAM disk image.
87 *
88 * @param imgpath Image path
89 * @param rpath Place to store pointer to newly allocated string, the path to
90 * the mounted RAM disk.
91 * @param rimg Place to store pointer to newly allocated object.
92 *
93 * @return EOK on success or an error code
94 */
95errno_t rd_img_open(const char *imgpath, char **rpath, rd_img_t **rimg)
96{
97 rd_img_t *img = NULL;
98 char *rdpath = NULL;
99 errno_t rc;
100 task_id_t id;
101 task_wait_t wait;
102 task_exit_t texit;
103 vfs_stat_t stat;
104 int retval;
105 int cnt;
106
107 printf("rd_img_open: begin\n");
108 rdpath = str_dup("/vol/" RD_LABEL);
109 if (rdpath == NULL) {
110 rc = ENOMEM;
111 goto error;
112 }
113
114 img = calloc(1, sizeof(rd_img_t));
115 if (img == NULL) {
116 rc = ENOMEM;
117 goto error;
118 }
119
120 printf("rd_img_open: spawn file_bd\n");
121 rc = task_spawnl(&id, &wait, FILE_BD, FILE_BD, imgpath, RD_SVC, NULL);
122 if (rc != EOK) {
123 rc = EIO;
124 goto error;
125 }
126
127 printf("rd_img_open: wait for file_bd\n");
128 rc = task_wait(&wait, &texit, &retval);
129 if (rc != EOK || texit != TASK_EXIT_NORMAL) {
130 rc = EIO;
131 goto error;
132 }
133
134 /* Wait for the RAM disk to become available */
135 printf("rd_img_open: wait for RAM disk to be mounted\n");
136 cnt = 10;
137 while (cnt > 0) {
138 rc = vfs_stat_path(rdpath, &stat);
139 if (rc == EOK)
140 break;
141
142 fibril_sleep(1);
143 --cnt;
144 }
145
146 if (cnt == 0) {
147 printf("rd_img_open: ran out of time\n");
148 rc = EIO;
149 goto error;
150 }
151
152 img->filebd_tid = id;
153 *rimg = img;
154 *rpath = rdpath;
155 printf("rd_img_open: success\n");
156 return EOK;
157error:
158 if (rdpath != NULL)
159 free(rdpath);
160 if (img != NULL)
161 free(img);
162 return rc;
163}
164
165/** Close RAM disk image.
166 *
167 */
168errno_t rd_img_close(rd_img_t *img)
169{
170 errno_t rc;
171 service_id_t rd_svcid;
172 vol_t *vol = NULL;
173
174 printf("rd_img_close: begin\n");
175
176 rc = vol_create(&vol);
177 if (rc != EOK) {
178 printf("Error opening volume management service.\n");
179 rc = EIO;
180 goto error;
181 }
182
183 printf("rd_img_close: Find RAM disk volume.\n");
184 rc = rd_img_part_by_label(vol, RD_LABEL, &rd_svcid);
185 if (rc != EOK) {
186 printf("Error getting RAM disk service ID.\n");
187 rc = EIO;
188 goto error;
189 }
190
191 printf("rd_img_close: eject RAM disk volume\n");
192 rc = vol_part_eject(vol, rd_svcid);
193 if (rc != EOK) {
194 printf("Error ejecting RAM disk volume.\n");
195 rc = EIO;
196 goto error;
197 }
198
199 vol_destroy(vol);
200
201 rc = task_kill(img->filebd_tid);
202 if (rc != EOK) {
203 printf("Error killing file_bd.\n");
204 rc = EIO;
205 goto error;
206 }
207
208 free(img);
209 printf("rd_img_close: success\n");
210 return EOK;
211error:
212 free(img);
213 if (vol != NULL)
214 vol_destroy(vol);
215 return rc;
216}
217
218/** @}
219 */
Note: See TracBrowser for help on using the repository browser.