120
122
if (setsid() < 0) {
121
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
122
_("setsid failed: %s"), strerror(errno));
123
virReportSystemError(NULL, errno, "%s",
126
128
if (ioctl(ttyfd, TIOCSCTTY, NULL) < 0) {
127
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
128
_("ioctl(TIOCSTTY) failed: %s"), strerror(errno));
129
virReportSystemError(NULL, errno, "%s",
130
_("ioctl(TIOCSTTY) failed"));
139
141
if (dup2(ttyfd, 0) < 0) {
140
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
141
_("dup2(stdin) failed: %s"), strerror(errno));
142
virReportSystemError(NULL, errno, "%s",
143
_("dup2(stdin) failed"));
145
147
if (dup2(ttyfd, 1) < 0) {
146
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
147
_("dup2(stdout) failed: %s"), strerror(errno));
148
virReportSystemError(NULL, errno, "%s",
149
_("dup2(stdout) failed"));
151
153
if (dup2(ttyfd, 2) < 0) {
152
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
153
_("dup2(stderr) failed: %s"), strerror(errno));
154
virReportSystemError(NULL, errno, "%s",
155
_("dup2(stderr) failed"));
178
180
writeCount = safewrite(control, &msg, sizeof(msg));
179
181
if (writeCount != sizeof(msg)) {
180
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
181
_("unable to send container continue message: %s"),
182
virReportSystemError(NULL, errno, "%s",
183
_("unable to send container continue message"));
207
208
readLen = saferead(control, &msg, sizeof(msg));
208
209
if (readLen != sizeof(msg) ||
209
210
msg != LXC_CONTINUE_MSG) {
210
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
211
_("Failed to read the container continue message: %s"),
211
virReportSystemError(NULL, errno, "%s",
212
_("Failed to read the container continue message"));
267
267
static int lxcContainerPivotRoot(virDomainFSDefPtr root)
271
272
/* First step is to ensure the new root itself is
273
274
if (mount(root->src, root->src, NULL, MS_BIND, NULL) < 0) {
274
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
275
_("failed to bind new root %s: %s"),
276
root->src, strerror(errno));
280
if (asprintf(&oldroot, "%s/.oldroot", root->src) < 0) {
282
lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
286
if (virFileMakePath(oldroot) < 0) {
275
virReportSystemError(NULL, errno,
276
_("failed to bind new root %s"),
281
if (virAsprintf(&oldroot, "%s/.oldroot", root->src) < 0) {
282
virReportOOMError(NULL);
286
if ((rc = virFileMakePath(oldroot)) < 0) {
287
287
VIR_FREE(oldroot);
288
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
289
_("failed to create %s: %s"),
290
oldroot, strerror(errno));
288
virReportSystemError(NULL, rc,
289
_("failed to create %s"),
295
295
* this and will soon be unmounted completely */
296
296
if (pivot_root(root->src, oldroot) < 0) {
297
297
VIR_FREE(oldroot);
298
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
299
_("failed to pivot root %s to %s: %s"),
300
oldroot, root->src, strerror(errno));
298
virReportSystemError(NULL, errno,
299
_("failed to pivot root %s to %s"),
303
303
VIR_FREE(oldroot);
327
328
{ LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_URANDOM, 0666, "/dev/urandom" },
330
if (virFileMakePath("/dev") < 0 ||
331
mount("none", "/dev", "tmpfs", 0, NULL) < 0) {
332
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
333
_("failed to mount /dev tmpfs for container: %s"),
331
if ((rc = virFileMakePath("/dev")) < 0) {
332
virReportSystemError(NULL, rc, "%s",
333
_("cannot create /dev/"));
336
if (mount("none", "/dev", "tmpfs", 0, NULL) < 0) {
337
virReportSystemError(NULL, errno, "%s",
338
_("failed to mount /dev tmpfs"));
337
341
/* Move old devpts into container, since we have to
340
344
XXX This sucks, we need to figure out how to get our
341
345
own private devpts for isolation
343
if (virFileMakePath("/dev/pts") < 0 ||
344
mount("/.oldroot/dev/pts", "/dev/pts", NULL,
347
if ((rc = virFileMakePath("/dev/pts") < 0)) {
348
virReportSystemError(NULL, rc, "%s",
349
_("cannot create /dev/pts"));
352
if (mount("/.oldroot/dev/pts", "/dev/pts", NULL,
345
353
MS_MOVE, NULL) < 0) {
346
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
347
_("failed to move /dev/pts into container: %s"),
354
virReportSystemError(NULL, errno, "%s",
355
_("failed to move /dev/pts into container"));
354
361
dev_t dev = makedev(devs[i].maj, devs[i].min);
355
362
if (mknod(devs[i].path, 0, dev) < 0 ||
356
363
chmod(devs[i].path, devs[i].mode)) {
357
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
358
_("failed to make device %s: %s"),
359
devs[i].path, strerror(errno));
364
virReportSystemError(NULL, errno,
365
_("failed to make device %s"),
378
385
if (vmDef->fss[i]->type != VIR_DOMAIN_FS_TYPE_MOUNT)
381
if (asprintf(&src, "/.oldroot/%s", vmDef->fss[i]->src) < 0) {
382
lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
388
if (virAsprintf(&src, "/.oldroot/%s", vmDef->fss[i]->src) < 0) {
389
virReportOOMError(NULL);
386
if (virFileMakePath(vmDef->fss[i]->dst) < 0 ||
387
mount(src, vmDef->fss[i]->dst, NULL, MS_BIND, NULL) < 0) {
389
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
390
_("failed to mount %s at %s for container: %s"),
391
vmDef->fss[i]->src, vmDef->fss[i]->dst, strerror(errno));
393
if (virFileMakePath(vmDef->fss[i]->dst) < 0) {
394
virReportSystemError(NULL, errno,
395
_("failed to create %s"),
400
if (mount(src, vmDef->fss[i]->dst, NULL, MS_BIND, NULL) < 0) {
402
virReportSystemError(NULL, errno,
403
_("failed to mount %s at %s"),
401
415
static int lxcContainerUnmountOldFS(void)
403
struct mntent *mntent;
417
struct mntent mntent;
404
418
char **mounts = NULL;
409
424
if (!(procmnt = setmntent("/proc/mounts", "r"))) {
410
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
411
_("failed to read /proc/mounts: %s"),
425
virReportSystemError(NULL, errno, "%s",
426
_("failed to read /proc/mounts"));
415
while ((mntent = getmntent(procmnt)) != NULL) {
416
if (!STRPREFIX(mntent->mnt_dir, "/.oldroot"))
429
while (getmntent_r(procmnt, &mntent, mntbuf, sizeof(mntbuf)) != NULL) {
430
if (!STRPREFIX(mntent.mnt_dir, "/.oldroot"))
419
433
if (VIR_REALLOC_N(mounts, nmounts+1) < 0) {
420
434
endmntent(procmnt);
421
lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
435
virReportOOMError(NULL);
424
if (!(mounts[nmounts++] = strdup(mntent->mnt_dir))) {
438
if (!(mounts[nmounts++] = strdup(mntent.mnt_dir))) {
425
439
endmntent(procmnt);
426
lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
440
virReportOOMError(NULL);
435
449
for (i = 0 ; i < nmounts ; i++) {
436
450
if (umount(mounts[i]) < 0) {
437
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
438
_("failed to unmount %s: %s"),
439
mounts[i], strerror(errno));
451
virReportSystemError(NULL, errno,
452
_("failed to unmount '%s'"),
442
456
VIR_FREE(mounts[i]);
460
474
if (virFileMakePath("/proc") < 0 ||
461
475
mount("none", "/proc", "proc", 0, NULL) < 0) {
462
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
463
_("failed to mount /proc for container: %s"),
476
virReportSystemError(NULL, errno, "%s",
477
_("failed to mount /proc"));
496
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
497
_("failed to mount %s at %s for container: %s"),
498
vmDef->fss[i]->src, vmDef->fss[i]->dst, strerror(errno));
509
virReportSystemError(NULL, errno,
510
_("failed to mount %s at %s"),
503
517
/* mount /proc */
504
518
if (mount("lxcproc", "/proc", "proc", 0, NULL) < 0) {
505
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
506
_("failed to mount /proc for container: %s"),
519
virReportSystemError(NULL, errno, "%s",
520
_("failed to mount /proc"));
559
572
ttyfd = open(argv->ttyPath, O_RDWR|O_NOCTTY);
561
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
562
_("open(%s) failed: %s"), argv->ttyPath, strerror(errno));
574
virReportSystemError(NULL, errno,
575
_("failed to open %s"),
605
619
/* allocate a stack for the container */
606
620
if (VIR_ALLOC_N(stack, stacksize) < 0) {
607
lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
621
virReportOOMError(NULL);
610
624
stacktop = stack + stacksize;
619
633
DEBUG("clone() returned, %d", pid);
622
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
623
_("clone() failed, %s"), strerror(errno));
636
virReportSystemError(NULL, errno, "%s",
637
_("failed to run clone container"));