tools/testing/selftests/filesystems/empty_mntns/overmount_chroot_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/filesystems/empty_mntns/overmount_chroot_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/filesystems/empty_mntns/overmount_chroot_test.c
Extension
.c
Size
5538 bytes
Lines
226
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (write(fd, "layer_0", 7) != 7) {
			close(fd);
			_exit(9);
		}
		close(fd);

		/* Step 5: Overmount /newroot multiple times with tmpfs */
		for (i = 0; i < NR_OVERMOUNTS; i++) {
			if (mount("tmpfs", "/newroot", "tmpfs", 0, "size=1M"))
				_exit(10);

			/* Record mount ID for this layer */
			mnt_ids[i + 1] = get_unique_mnt_id("/newroot");
			if (!mnt_ids[i + 1])
				_exit(11);

			/* Create a marker file in each layer */
			snprintf(marker, sizeof(marker), "/newroot/layer_%d", i + 1);
			fd = open(marker, O_CREAT | O_RDWR, 0644);
			if (fd < 0)
				_exit(12);

			if (write(fd, marker, strlen(marker)) != (ssize_t)strlen(marker)) {
				close(fd);
				_exit(13);
			}
			close(fd);
		}

		/* Verify mount count increased */
		nr_mounts = count_mounts();
		if (nr_mounts < NR_OVERMOUNTS + 2)
			_exit(14);

		/* Record root mount ID before chroot */
		root_id_before = get_unique_mnt_id("/newroot");

		/* Verify this is the topmost layer's mount */
		if (root_id_before != mnt_ids[NR_OVERMOUNTS])
			_exit(15);

		/* Step 6: Chroot into /newroot (the topmost overmount) */
		if (chroot("/newroot"))
			_exit(16);

		/* Change to root directory within the chroot */
		if (chdir("/"))
			_exit(17);

		/* Step 7: Verify we're in the topmost layer */
		root_id_after = get_unique_mnt_id("/");

		/* The mount ID should be the same as the topmost layer */
		if (root_id_after != mnt_ids[NR_OVERMOUNTS])
			_exit(18);

		/* Verify the topmost layer's marker file exists */
		snprintf(marker, sizeof(marker), "/layer_%d", NR_OVERMOUNTS);
		if (access(marker, F_OK))
			_exit(19);

		/* Verify we cannot see markers from lower layers (they're hidden) */
		for (i = 0; i < NR_OVERMOUNTS; i++) {
			snprintf(marker, sizeof(marker), "/layer_%d", i);
			if (access(marker, F_OK) == 0)
				_exit(20);
		}

		/* Verify the root mount is tmpfs */
		sm = statmount_alloc(root_id_after, 0,
				     STATMOUNT_MNT_BASIC | STATMOUNT_MNT_ROOT |
				     STATMOUNT_MNT_POINT | STATMOUNT_FS_TYPE, 0);
		if (!sm)
			_exit(21);

		if (sm->mask & STATMOUNT_FS_TYPE) {
			if (strcmp(sm->str + sm->fs_type, "tmpfs") != 0) {
				free(sm);
				_exit(22);
			}
		}

		free(sm);
		_exit(0);
	}

	ASSERT_EQ(wait_for_pid(pid), 0);
}

TEST_HARNESS_MAIN

Annotation

Implementation Notes