// RemoveSnapFiles removes the snap files from the disk after unmounting the snap. func (b Backend) RemoveSnapFiles(s snap.PlaceInfo, typ snap.Type, meter progress.Meter) error { mountDir := s.MountDir() // this also ensures that the mount unit stops if err := removeMountUnit(mountDir, meter); err != nil { return err } if err := os.RemoveAll(mountDir); err != nil { return err } // try to remove parent dir, failure is ok, means some other // revisions are still in there os.Remove(filepath.Dir(mountDir)) // snapPath may either be a file or a (broken) symlink to a dir snapPath := s.MountFile() if _, err := os.Lstat(snapPath); err == nil { // remove the kernel assets (if any) if typ == snap.TypeKernel { if err := boot.RemoveKernelAssets(s); err != nil { return err } } // remove the snap if err := os.RemoveAll(snapPath); err != nil { return err } } return nil }
func (s *kernelOSSuite) TestExtractKernelAssetsAndRemove(c *C) { files := [][]string{ {"kernel.img", "I'm a kernel"}, {"initrd.img", "...and I'm an initrd"}, {"dtbs/foo.dtb", "g'day, I'm foo.dtb"}, {"dtbs/bar.dtb", "hello, I'm bar.dtb"}, // must be last {"meta/kernel.yaml", "version: 4.2"}, } si := &snap.SideInfo{ RealName: "ubuntu-kernel", Revision: snap.R(42), } fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files) snapf, err := snap.Open(fn) c.Assert(err, IsNil) info, err := snap.ReadInfoFromSnapFile(snapf, si) c.Assert(err, IsNil) err = boot.ExtractKernelAssets(info, snapf) c.Assert(err, IsNil) // this is where the kernel/initrd is unpacked bootdir := s.bootloader.Dir() kernelAssetsDir := filepath.Join(bootdir, "ubuntu-kernel_42.snap") for _, def := range files { if def[0] == "meta/kernel.yaml" { break } fullFn := filepath.Join(kernelAssetsDir, def[0]) content, err := ioutil.ReadFile(fullFn) c.Assert(err, IsNil) c.Assert(string(content), Equals, def[1]) } // remove err = boot.RemoveKernelAssets(info) c.Assert(err, IsNil) c.Check(osutil.FileExists(kernelAssetsDir), Equals, false) }