Index: head/sbin/mksnap_ffs/Makefile =================================================================== --- head/sbin/mksnap_ffs/Makefile (revision 193050) +++ head/sbin/mksnap_ffs/Makefile (revision 193051) @@ -1,14 +1,19 @@ # $FreeBSD$ +.PATH: ${.CURDIR}/../mount + PROG= mksnap_ffs +SRCS= mksnap_ffs.c getmntopts.c MAN= mksnap_ffs.8 + +CFLAGS+=-I${.CURDIR}/../mount .if defined(NOSUID) BINMODE=550 .else BINMODE=4550 BINOWN= root .endif BINGRP= operator .include Index: head/sbin/mksnap_ffs/mksnap_ffs.8 =================================================================== --- head/sbin/mksnap_ffs/mksnap_ffs.8 (revision 193050) +++ head/sbin/mksnap_ffs/mksnap_ffs.8 (revision 193051) @@ -1,78 +1,81 @@ .\" .\" Copyright (c) 2003 Networks Associates Technology, Inc. .\" All rights reserved. .\" .\" This software was developed for the FreeBSD Project by Marshall .\" Kirk McKusick and Network Associates Laboratories, the Security .\" Research Division of Network Associates, Inc. under DARPA/SPAWAR .\" contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS .\" research program. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. The names of the authors may not be used to endorse or promote .\" products derived from this software without specific prior written .\" permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd January 19, 2003 +.Dd May 29, 2009 .Dt MKSNAP_FFS 8 .Os .Sh NAME .Nm mksnap_ffs .Nd take a file system snapshot .Sh SYNOPSIS .Nm -.Ar mountpoint .Ar snapshot_name .Sh DESCRIPTION The .Nm utility creates a snapshot named -.Ar snapshot_name -on the file system mounted at -.Ar mountpoint . -The -.Ar snapshot_name -argument must be contained within the file system mounted at -.Ar mountpoint . +.Ar snapshot_name . .Pp The group ownership of the file is set to .Dq Li operator ; the owner of the file remains .Dq Li root . The mode of the snapshot is set to be readable by the owner or members of the .Dq Li operator group. +.Sh EXAMPLES +Create a snapshot of +.Pa /usr/home +file system and mount the snapshot elsewhere: +.Bd -literal -offset indent +mksnap_ffs /usr/home/snapshot +mdconfig -a -t vnode -o readonly -f /usr/home/snapshot +mount -o ro /dev/md0 /mnt/ +.Ed .Sh SEE ALSO .Xr chmod 2 , .Xr chown 8 , +.Xr mdconfig 8, .Xr mount 8 .Sh CAVEATS The disk full situation is not handled gracefully and may lead to a system panic when no free blocks are found. .Sh HISTORY The .Nm utility first appeared in .Fx 5.0 . Index: head/sbin/mksnap_ffs/mksnap_ffs.c =================================================================== --- head/sbin/mksnap_ffs/mksnap_ffs.c (revision 193050) +++ head/sbin/mksnap_ffs/mksnap_ffs.c (revision 193051) @@ -1,130 +1,136 @@ /* * Copyright (c) 2003 Networks Associates Technology, Inc. * All rights reserved. * * This software was developed for the FreeBSD Project by Marshall * Kirk McKusick and Network Associates Laboratories, the Security * Research Division of Network Associates, Inc. under DARPA/SPAWAR * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS * research program. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include +#include #include #include #include #include #include -void usage(void); +static void +usage(void) +{ + errx(EX_USAGE, "usage: mksnap_ffs snapshot_name"); +} + int main(int argc, char **argv) { - char *dir, *cp, path[PATH_MAX]; + char errmsg[255], path[PATH_MAX]; + char *cp, *snapname; struct statfs stfsbuf; - struct ufs_args args; struct group *grp; struct stat stbuf; - int fd; + struct iovec *iov; + int fd, iovlen; - if (argc != 3) + if (argc == 2) + snapname = argv[1]; + else if (argc == 3) + snapname = argv[2]; /* Old usage. */ + else usage(); - dir = argv[1]; - memset(&args, 0, sizeof args); - args.fspec = argv[2]; - /* * Check that the user running this program has permission * to create and remove a snapshot file from the directory * in which they have requested to have it made. If the * directory is sticky and not owned by the user, then they * will not be able to remove the snapshot when they are * done with it. */ - if (strlen(args.fspec) >= PATH_MAX) - errx(1, "pathname too long %s", args.fspec); - cp = strrchr(args.fspec, '/'); + if (strlen(snapname) >= PATH_MAX) + errx(1, "pathname too long %s", snapname); + cp = strrchr(snapname, '/'); if (cp == NULL) { strlcpy(path, ".", PATH_MAX); - } else if (cp == args.fspec) { + } else if (cp == snapname) { strlcpy(path, "/", PATH_MAX); } else { - strlcpy(path, args.fspec, cp - args.fspec + 1); + strlcpy(path, snapname, cp - snapname + 1); } if (statfs(path, &stfsbuf) < 0) err(1, "%s", path); if (stat(path, &stbuf) < 0) err(1, "%s", path); if (!S_ISDIR(stbuf.st_mode)) errx(1, "%s: Not a directory", path); if (access(path, W_OK) < 0) err(1, "Lack write permission in %s", path); if ((stbuf.st_mode & S_ISTXT) && stbuf.st_uid != getuid()) errx(1, "Lack write permission in %s: Sticky bit set", path); /* * Having verified access to the directory in which the * snapshot is to be built, proceed with creating it. */ if ((grp = getgrnam("operator")) == NULL) errx(1, "Cannot retrieve operator gid"); - if (mount("ufs", dir, MNT_UPDATE | MNT_SNAPSHOT | stfsbuf.f_flags, - &args) < 0) - err(1, "Cannot create %s", args.fspec); - if ((fd = open(args.fspec, O_RDONLY)) < 0) - err(1, "Cannot open %s", args.fspec); + + build_iovec(&iov, &iovlen, "fstype", "ffs", 4); + build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1); + build_iovec(&iov, &iovlen, "fspath", stfsbuf.f_mntonname, (size_t)-1); + build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); + build_iovec(&iov, &iovlen, "update", NULL, 0); + build_iovec(&iov, &iovlen, "snapshot", NULL, 0); + + if (nmount(iov, iovlen, stfsbuf.f_flags) < 0) + err(1, "Cannot create snapshot %s: %s", snapname, errmsg); + if ((fd = open(snapname, O_RDONLY)) < 0) + err(1, "Cannot open %s", snapname); if (fstat(fd, &stbuf) != 0) - err(1, "Cannot stat %s", args.fspec); + err(1, "Cannot stat %s", snapname); if ((stbuf.st_flags & SF_SNAPSHOT) == 0) - errx(1, "File %s is not a snapshot", args.fspec); + errx(1, "File %s is not a snapshot", snapname); if (fchown(fd, -1, grp->gr_gid) != 0) - err(1, "Cannot chown %s", args.fspec); + err(1, "Cannot chown %s", snapname); if (fchmod(fd, S_IRUSR | S_IRGRP) != 0) - err(1, "Cannot chmod %s", args.fspec); + err(1, "Cannot chmod %s", snapname); exit(EXIT_SUCCESS); -} - -void -usage() -{ - - fprintf(stderr, "usage: mksnap_ffs mountpoint snapshot_name\n"); - exit(EX_USAGE); }