| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | package buildah | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-06 17:14:06 +08:00
										 |  |  | import "fmt" | 
					
						
							| 
									
										
										
										
											2018-10-03 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-11 03:45:06 +08:00
										 |  |  | // Mount mounts a container's root filesystem in a location which can be
 | 
					
						
							|  |  |  | // accessed from the host, and returns the location.
 | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | func (b *Builder) Mount(label string) (string, error) { | 
					
						
							|  |  |  | 	mountpoint, err := b.store.Mount(b.ContainerID, label) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-09-18 18:36:08 +08:00
										 |  |  | 		return "", fmt.Errorf("mounting build container %q: %w", b.ContainerID, err) | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	b.MountPoint = mountpoint | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = b.Save() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-09-18 18:36:08 +08:00
										 |  |  | 		return "", fmt.Errorf("saving updated state for build container %q: %w", b.ContainerID, err) | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return mountpoint, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-10-29 21:06:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (b *Builder) setMountPoint(mountPoint string) error { | 
					
						
							|  |  |  | 	b.MountPoint = mountPoint | 
					
						
							|  |  |  | 	if err := b.Save(); err != nil { | 
					
						
							| 
									
										
										
										
											2022-09-18 18:36:08 +08:00
										 |  |  | 		return fmt.Errorf("saving updated state for build container %q: %w", b.ContainerID, err) | 
					
						
							| 
									
										
										
										
											2020-10-29 21:06:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Mounted returns whether the container is mounted or not
 | 
					
						
							|  |  |  | func (b *Builder) Mounted() (bool, error) { | 
					
						
							|  |  |  | 	mountCnt, err := b.store.Mounted(b.ContainerID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-09-18 18:36:08 +08:00
										 |  |  | 		return false, fmt.Errorf("determining if mounting build container %q is mounted: %w", b.ContainerID, err) | 
					
						
							| 
									
										
										
										
											2020-10-29 21:06:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	mounted := mountCnt > 0 | 
					
						
							|  |  |  | 	if mounted && b.MountPoint == "" { | 
					
						
							|  |  |  | 		ctr, err := b.store.Container(b.ContainerID) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-09-18 18:36:08 +08:00
										 |  |  | 			return mountCnt > 0, fmt.Errorf("determining if mounting build container %q is mounted: %w", b.ContainerID, err) | 
					
						
							| 
									
										
										
										
											2020-10-29 21:06:53 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		layer, err := b.store.Layer(ctr.LayerID) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-09-18 18:36:08 +08:00
										 |  |  | 			return mountCnt > 0, fmt.Errorf("determining if mounting build container %q is mounted: %w", b.ContainerID, err) | 
					
						
							| 
									
										
										
										
											2020-10-29 21:06:53 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return mounted, b.setMountPoint(layer.MountPoint) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !mounted && b.MountPoint != "" { | 
					
						
							|  |  |  | 		return mounted, b.setMountPoint("") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return mounted, nil | 
					
						
							|  |  |  | } |